Results 1 to 3 of 3
Thread: Help with Tic Tac Program
- 07-03-2007, 06:33 PM #1
Senior Member
- Join Date
- Jun 2007
- Posts
- 111
- Rep Power
- 0
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.
Java 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
- 07-03-2007, 06:37 PM #2
Member
- Join Date
- Jun 2007
- Posts
- 91
- Rep Power
- 0
Sure, check out the tutorials at The Java™ TutorialsThere are a ton of them.
Good luck.
Greetings.
Daniel:o
- 07-03-2007, 06:45 PM #3
Senior Member
- Join Date
- Jun 2007
- Posts
- 114
- Rep Power
- 0
First of all, you'll need a board.
I would recommend making final chars to represent what is in each sqaure.Java Code:private char[][] board = new char[3][3];
you need to initialize the board.Java Code:private final char EMPTY = ' '; private final char PLAYER1 = 'X'; private final char PLAYER2 = 'O';
to check the winner, this gets a bit tedious.Java Code:public void init() { for (int x = 0; x < 3; x++) { for (int y = 0; y < 3; y++) { board[x][y] = EMPTY; } } }
GreetingsJava 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; }
Albert:rolleyes:
Similar Threads
-
Executing a program within a program
By gibsonrocker800 in forum New To JavaReplies: 5Last Post: 05-12-2008, 08:24 AM -
How to execute an External Program through Java program
By Java Tip in forum java.ioReplies: 0Last Post: 04-04-2008, 02:40 PM -
cannot run the program
By amiey in forum New To JavaReplies: 1Last Post: 11-20-2007, 04:13 AM -
How to execute an External Program through Java program
By JavaBean in forum Java TipReplies: 0Last Post: 10-04-2007, 09:33 PM -
Why does this program not end?
By trill in forum New To JavaReplies: 1Last Post: 08-07-2007, 07:22 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks