Results 1 to 2 of 2
Thread: infamous tictactoe program...
- 09-12-2011, 04:17 AM #1
Member
- Join Date
- Jan 2011
- Posts
- 13
- Rep Power
- 0
infamous tictactoe program...
I'm making a tic-tac-toe program, but I have two issues. First, the code compiles and runs, but the while loop in main never stops. I suppose it's not checking for a winner correctly but I'm unsure how to fix it. Also, how do I get the display board to display the 'X' or 'O' after each input?
Main
Tictactoe classJava Code:import java.util.Scanner; public class Assignment2 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("Welcome to Tic-Tac-Toe"); TicTacToe game = new TicTacToe(); int row; int col; while(game.findWinner() == ' ' || game.boardFull()){ game.boardDisplay(); System.out.print(game.currentPlayer() + " choose a row, 0-2: "); row = in.nextInt(); if(row < 0 || row > 2){ System.out.print("Invalid. Try again."); row = in.nextInt();} System.out.print("Choose a column, 0-2: "); col = in.nextInt(); if(col < 0 || col > 2){ System.out.print("Invalid. Try again."); col = in.nextInt();} while(game.setMove(row, col)){ System.out.print("Space taken, try again, choose row 0-2: "); row = in.nextInt(); System.out.print("Choose column: "); col = in.nextInt(); } System.out.print(game.switchPlayer() + " choose a row, 0-2: "); row = in.nextInt(); if(row < 0 || row > 2){ System.out.print("Invalid. Try again."); row = in.nextInt();} System.out.print("Choose a column, 0-2: "); col = in.nextInt(); if(col < 0 || col > 2){ System.out.print("Invalid. Try again."); row = in.nextInt();} while(game.setMove(row, col)){ System.out.print("Space taken, try again, choose row 0-2: "); row = in.nextInt(); System.out.print("Choose column: "); col = in.nextInt(); } } if(game.boardFull() == true){ System.out.print("It's a draw"); } else{ System.out.print(game.findWinner() + "WINS!"); } } }
Java Code:import java.util.*; public class TicTacToe { Scanner in = new Scanner(System.in); char[][] board = new char[3][3]; final int ROWS = 3; final int COLUMNS = 3; final char EMPTY = ' '; char player1 = 'X'; char player2 = 'O'; char currentPlayer; public void init() { for (int r = 0; r < 3; r++) { for (int c = 0; c < 3; c++) { board[r][c] = EMPTY; } } } public boolean setMove(int r, int c){ boolean move; if(board[r][c] == EMPTY){ board[r][c] = currentPlayer; return true; } else{ move = false; } return move; } public boolean boardFull(){ boolean full = true; for(int r = 0; r < 3; r++){ for(int c = 0; c < 3; c++){ if(board[r][c] == EMPTY){ full = false; } } } return full; } public void boardDisplay(){ System.out.println(" "+board[0][0]+" | "+board[0][1]+" | "+board[0][2]); System.out.println(" ---------"); System.out.println(" "+board[1][0]+" | "+board[1][1]+" | "+board[1][2]); System.out.println(" ---------"); System.out.println(" "+board[2][0]+" | "+board[2][1]+" | "+board[2][2]); } public char findWinner(){ char winner = ' '; //check rows while(winner == ' '){ if(board[0][0] == board[0][1] && board[0][0] == board[0][2]){ if(board[0][0] != ' '){ winner = board[0][0]; break; } } if(board[1][0] == board[1][1] && board[1][0] == board[1][2]){ if(board[1][0] != ' '){ winner = board[1][0]; break; } } if(board[2][0] == board[2][1] && board[2][0] == board[2][2]){ if(board[2][0] != ' '){ winner = board[2][0]; break; } } } //check columns while(winner == ' '){ if(board[0][0] == board[1][0] && board[0][0] == board[2][0]){ if(board[0][0] != ' '){ winner = board[0][0]; break; } } if(board[0][1] == board[1][1] && board[0][1] == board[2][1]){ if(board[0][1] != ' '){ winner = board[0][1]; break; } } if(board[0][2] == board[1][2] && board[0][2] == board[2][2]){ if(board[0][2] != ' '){ winner = board[0][2]; break; } } } //check diagonally if(board[0][0] == board[1][1] && board[0][0] == board[2][2]){ if(board[0][0] != ' '){ winner = board[0][0]; } } if(board[0][2] == board[1][1] && board[0][2] == board[2][0]){ if(board[0][2] != ' '){ winner = board[0][0]; } } return winner; } public char currentPlayer(){ currentPlayer = player1; return player1; } public char switchPlayer(){ if(currentPlayer == player1){ currentPlayer = player2; } return currentPlayer; } }Last edited by _inase; 09-12-2011 at 05:11 AM.
- 09-12-2011, 01:53 PM #2
Re: infamous tictactoe program...
Sounds like time for some debugging. Add some printlns to your code to show the values of the variables that are being used to determine a winner. print them every time they are changed. If you know when there is a winner,then the print outs should show you when your logic is missing a winner so you can fix it.I suppose it's not checking for a winner correctly
Copy and paste there what your program displays for the board.how do I get the display board to display the 'X' or 'O' after each input?
Similar Threads
-
TicTacToe Help
By Ryan10 in forum New To JavaReplies: 16Last Post: 03-31-2011, 07:01 PM -
Help with TicTacToe
By jokerboy123 in forum New To JavaReplies: 1Last Post: 01-22-2011, 01:29 AM -
Help with tictactoe
By ximenaacucr in forum New To JavaReplies: 3Last Post: 09-16-2009, 08:30 PM -
TicTacToe
By Thomas Covington in forum New To JavaReplies: 1Last Post: 04-10-2008, 04:51 AM -
TicTacToe
By Joe3161 in forum New To JavaReplies: 4Last Post: 11-25-2007, 06:47 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks