Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-03-2007, 07:33 PM
Senior Member
 
Join Date: Jun 2007
Posts: 111
Eric is on a distinguished road
Help with Tic Tac Program
I have a project to create tic tac toe without a gui.
I need Assistance with writing a program.
Can anyone point me in the right direction of where to get some leads.
This is my potential Tic Tac Program without GUI; written in pseudo.

Code:
package proyect; import java.util.*; public class TicTacToeMain { private char [][] board; private boolean xturn; private int numMoves; public static void main() { Scanner keyb = new Scanner(System.in); String response; do { TicTacToeMain game = new TicTacToeMain(); play(); System.out.println("Would you like to play again?"); response = keyb.nextLine(); }while(response.equalsIgnoreCase("yes")); } public TicTacToeMain() { board = new char [5][5]; xturn = true; numMoves = 0; } public void play() { do { //show board //get valid move //update board //determine winner if possible }while((no one has won)&&(moves < 9)); } public boolean won(int row, int col)//computer indices (inside) { if(allSquaresInRowAreSame) or board[0][0] ==board[2][2]&&board[0][2]==board[0][4] return true; if(allSquaresInColAreSame) return true; return wonOnDiagonal(); } public boolean wonOnDiagonal() { if(board[0][0]==board[2][2]&&board[2][2] ==board[4][4]&&board[0][0]!=space or " ") return true; return //same on positive and not space } }

Thanks!

Eric
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-03-2007, 07:37 PM
Member
 
Join Date: Jun 2007
Posts: 92
Daniel is on a distinguished road
Sure, check out the tutorials at The Java™ TutorialsThere are a ton of them.
Good luck.

Greetings.

Daniel
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-03-2007, 07:45 PM
Senior Member
 
Join Date: Jun 2007
Posts: 114
Albert is on a distinguished road
First of all, you'll need a board.
Code:
private char[][] board = new char[3][3];
I would recommend making final chars to represent what is in each sqaure.
Code:
private final char EMPTY = ' '; private final char PLAYER1 = 'X'; private final char PLAYER2 = 'O';
you need to initialize the board.
Code:
public void init() { for (int x = 0; x < 3; x++) { for (int y = 0; y < 3; y++) { board[x][y] = EMPTY; } } }
to check the winner, this gets a bit tedious.
Code:
public char winner() { for (int i = 0; i < board.length; i++) { /* check horizontally */ if (board[i][0] == board[i][1] && board[i][0] == board[i][2]) { if (board[i][0] != EMPTY) { return board[i][0]; } } /* check vertically */ if (board[0][i] == board[1][i] && board[0][i] == board[2][i]) { if (board[0][i] != EMPTY) { return board[0][i]; } } } /* check diagonally */ if (board[0][0] == board[1][1] && board[0][0] == board[2][2]) { if (board[0][0] != EMPTY) { return board[0][0]; } } if (board[0][2] == board[1][1] && board[0][2] == board[2][0]) { if (board[0][2] != EMPTY)) { return board[0][2]; } } return EMPTY; }
Greetings

Albert
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Executing a program within a program gibsonrocker800 New To Java 5 05-12-2008 09:24 AM
How to execute an External Program through Java program Java Tip java.io 0 04-04-2008 03:40 PM
cannot run the program amiey New To Java 1 11-20-2007 05:13 AM
How to execute an External Program through Java program JavaBean Java Tips 0 10-04-2007 10:33 PM
Why does this program not end? trill New To Java 1 08-07-2007 08:22 AM


All times are GMT +3. The time now is 07:52 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org