Note that there are some explanatory texts on larger screens.

plurals
  1. POTic Tac Toe in Java printing error
    primarykey
    data
    text
    <p>I've almost got this homework assignment finished, but I'm having some printing errors with the output. The game is functional, but there's a problem with methods <code>printOBoard</code> and <code>printXBoard</code> where it messes up the game board when a move is made. The problem becomes worse the longer the game goes on. Thanks for your help. </p> <pre><code>import java.util.Scanner; public class TicTacToe { private enum Tiles { X, O, EMPTY; } public static void main(String[] args) { int i; int j; //initialize 2d array of enum types called "board" Tiles[][] board = new Tiles[3][3]; for (i=0; i&lt;board.length; i++) for (j=0; j&lt;board.length; j++) board[i][j] = Tiles.EMPTY; //print out an empty board as a 2d array, with each tile set as "EMPTY" printBoard(board); int row, col; int countEmpty=0; //initial countEmpty count, if it's less than 1, the board is full and the game is over. for (i=0; i&lt;board.length; i++) for (j=0; j&lt;board.length; j++) if (board[i][j] == Tiles.EMPTY) countEmpty++; while (countEmpty &gt; 0) { //Player O enters the row coordinate System.out.println("Player O's turn."); System.out.println("Player O: Enter row (0, 1, or 2):"); Scanner stdin1 = new Scanner(System.in); row = stdin1.nextInt(); //Player O enters the column coordinate System.out.println("Player O: Enter column (0, 1, or 2):"); Scanner stdin2 = new Scanner(System.in); col = stdin2.nextInt(); //If the tile is empty, it was a valid move, and an 'O' is placed on the spot. if (board[row][col] == Tiles.EMPTY) { board[row][col] = Tiles.O; //MOVE FOR O ******************** printOBoard(board, row, col); checkWin(board); if (checkWin(board) == 2) ; else break; } //If the tile is not empty, it was not a valid move, and Player O is prompted to try again. else { System.out.println("Space already occupied. Please choose another."); System.out.println("Player O's turn."); //Player 0 enters the row coordinate System.out.println("Player O: Enter row (0, 1, or 2):"); stdin1 = new Scanner(System.in); row = stdin1.nextInt(); //Player O enters the column coordinate System.out.println("Player O: Enter column (0, 1, or 2):"); stdin2 = new Scanner(System.in); col = stdin2.nextInt(); //ERROR MOVE FOR O******************** board[row][col] = Tiles.O; printOBoard(board, row, col); checkWin(board); if (checkWin(board) == 2) ; else break; } //Player X enters the row coordinate System.out.println("Player X's turn."); System.out.println("Player X: Enter row (0, 1, or 2):"); Scanner stdin3 = new Scanner(System.in); row = stdin3.nextInt(); //Player X enters the column coordinate System.out.println("Player X: Enter column (0, 1, or 2):"); Scanner stdin4 = new Scanner(System.in); col = stdin4.nextInt(); if (board[row][col] == Tiles.EMPTY) { board[row][col] = Tiles.X; printXBoard(board, row, col); //MOVE FOR X ************************************************* checkWin(board); if (checkWin(board) == 2) ; else break; } else { System.out.println("Space already occupied. Please choose another."); System.out.println("Player O's turn."); System.out.println("Player O: Enter row (0, 1, or 2):"); stdin3 = new Scanner(System.in); row = stdin3.nextInt(); //Player O enters the column coordinate System.out.println("Player O: Enter column (0, 1, or 2):"); stdin4 = new Scanner(System.in); col = stdin4.nextInt(); board[row][col] = Tiles.O; //ERROR MOVE FOR X **************************************** printXBoard(board, row, col); checkWin(board); if (checkWin(board) == 2) ; else break; } //After both players move, we check to see if the board is full. countEmpty = 0; for (i=0; i&lt;board.length; i++) for (j=0; j&lt;board.length; j++) if (board[i][j] == Tiles.EMPTY) countEmpty++; } } //method printBoard prints out a grid of EMPTY's and returns nothing private static void printBoard(Tiles board[][]) { int i, j; System.out.println(" -----------------------------"); System.out.println("| | | |"); for (i=0; i&lt;board.length; i++){ for (j=0; j&lt;board.length; j++){ System.out.printf("| " + board[i][j] + " "); } System.out.println("|"); System.out.println("| | | |"); System.out.println(" -----------------------------"); if (i&lt;2) System.out.println("| | | |"); } return; } //method printXBoard prints out the grid modified with the addition of an X after Player X's turn private static void printXBoard(Tiles board[][], int curRow, int curCol) { int i, j; System.out.println(" -----------------------------"); System.out.println("| | | |"); for (i=0; i&lt;board.length; i++){ for (j=0; j&lt;board.length; j++){ if (i == curRow &amp;&amp; j == curCol) board[i][j] = Tiles.X; else ; System.out.printf("| " + board[i][j] + " "); } System.out.println("|"); System.out.println("| | | |"); System.out.println(" -----------------------------"); if (i&lt;2) System.out.println("| | | |"); } return; } //method printOBoard prints out the grid modified with the addition of an X after Player X's turn private static void printOBoard(Tiles board[][], int curRow, int curCol) { int i, j; System.out.println(" -----------------------------"); System.out.println("| | | |"); for (i=0; i&lt;board.length; i++){ for (j=0; j&lt;board.length; j++){ if (i == curRow &amp;&amp; j == curCol) board[i][j] = Tiles.O; else ; System.out.printf("| " + board[i][j] + " "); } System.out.println("|"); System.out.println("| | | |"); System.out.println(" -----------------------------"); if (i&lt;2) System.out.println("| | | |"); } return; } //method checkWin checks all possible winning combinations for both players. private static int checkWin(Tiles board[][]) { if (board[0][0] == Tiles.X &amp;&amp; board[0][1] == Tiles.X &amp;&amp; board[0][2] == Tiles.X){ System.out.println("Player X wins!"); return 1; } else if (board[0][0] == Tiles.X &amp;&amp; board[1][1] == Tiles.X &amp;&amp; board[2][2] == Tiles.X){ System.out.println("Player X wins!"); return 1; } else if (board[0][0] == Tiles.X &amp;&amp; board[1][0] == Tiles.X &amp;&amp; board[2][0] == Tiles.X){ System.out.println("Player X wins!"); return 1; } else if (board[1][0] == Tiles.X &amp;&amp; board[1][1] == Tiles.X &amp;&amp; board[1][2] == Tiles.X){ System.out.println("Player X wins!"); return 1; } else if (board[2][0] == Tiles.X &amp;&amp; board[2][1] == Tiles.X &amp;&amp; board[2][2] == Tiles.X){ System.out.println("Player X wins!"); return 1; } else if (board[0][1] == Tiles.X &amp;&amp; board[1][1] == Tiles.X &amp;&amp; board[2][1] == Tiles.X){ System.out.println("Player X wins!"); return 1; } else if (board[0][2] == Tiles.X &amp;&amp; board[1][2] == Tiles.X &amp;&amp; board[2][2] == Tiles.X){ System.out.println("Player X wins!"); return 1; } else if (board[2][0] == Tiles.X &amp;&amp; board[1][1] == Tiles.X &amp;&amp; board[0][2] == Tiles.X){ System.out.println("Player X wins!"); return 1; } //check if player O wins else if (board[0][0] == Tiles.O &amp;&amp; board[0][1] == Tiles.O &amp;&amp; board[0][2] == Tiles.O){ System.out.println("Player O wins!"); return 0; } else if (board[0][0] == Tiles.O &amp;&amp; board[1][1] == Tiles.O &amp;&amp; board[2][2] == Tiles.O){ System.out.println("Player O wins!"); return 0; } else if (board[0][0] == Tiles.O &amp;&amp; board[1][0] == Tiles.O &amp;&amp; board[2][0] == Tiles.O){ System.out.println("Player O wins!"); return 0; } else if (board[1][0] == Tiles.O &amp;&amp; board[1][1] == Tiles.O &amp;&amp; board[1][2] == Tiles.O){ System.out.println("Player O wins!"); return 0; } else if (board[2][0] == Tiles.O &amp;&amp; board[2][1] == Tiles.O &amp;&amp; board[2][2] == Tiles.O) { System.out.println("Player O wins!"); return 0; } else if (board[0][1] == Tiles.O &amp;&amp; board[1][1] == Tiles.O &amp;&amp; board[2][1] == Tiles.O) { System.out.println("Player O wins!"); return 0; } else if (board[0][2] == Tiles.O &amp;&amp; board[1][2] == Tiles.O &amp;&amp; board[2][2] == Tiles.O){ System.out.println("Player O wins!"); return 0; } else if (board[2][0] == Tiles.O &amp;&amp; board[1][1] == Tiles.O &amp;&amp; board[0][2] == Tiles.O) { System.out.println("Player O wins!"); return 0; } else return 2; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload