Question: The program has bugs. Try to choose a position that has been chosen(either by the same player or the other player), you will see that

The program has bugs. Try to choose a position that has been chosen(either by the same player or the other player), you will see that the new value replaces the old value, which is not correct. Also, try to enter a position that is out of the range,e.g.,3 0, or 5 8 and you will see ArrayOutOfBound- Exception on the screen, which is also not desirable.

what I had:

package TICTAC; import java.util.*; public class PlayTicTac { /** board a 3x3 array containing 0, 1, 2, values indicating blanks or player numbers */ private int[][] board; int movesCount = 0; public PlayTicTac() { this.board = new int[3][3];

for (int i = 0; i < 3; i++) {

for (int j = 0; j < 3; j++) {

board[i][j] = -1;

}

} } /** Checks if player has won tic-tac-toe along diagonal lines. @param player the player to check for a winning sequence of marks @return true if player won, false otherwise */ private boolean wonDiagonal( int player) { if ((board[0][0] == board[1][1]) && (board[1][1] == board[2][2]) && (board[0][0] != -1)) {

// if a diagonal is complete print the winning player

System.out.println("Player " + player + " Won!!");

return true;

// secondary diagonal

} else if ((board[0][2] == board[1][1]) && (board[1][1] == board[2][0]) && (board[0][2] != -1)) {

System.out.println("Player " + player + " Won!!");

return true;

} else {

return false;

} }

/** Checks if player has won tic-tac-toe along straight lines. @param player the player to check for a winning sequence of marks @return true if player won, false otherwise */ private boolean wonStraightLines( int player) { if ((board[0][0] == board[0][1]) && (board[0][1] == board[0][2]) && (board[0][0] != -1)) {

System.out.println("Player " + player + " Won!!");

return true;

} else if ((board[1][0] == board[1][1]) && (board[1][1] == board[1][2]) && (board[1][0] != -1)) {

System.out.println("Player " + player + " Won!!");

return true;

} else if ((board[2][0] == board[2][1]) && (board[2][1] == board[2][2]) && (board[2][0] != -1)) {

System.out.println("Player " + player + " Won!!");

return true;

} else {

return false;

} }

/** Checks if player has won. @param player the player to check for a winning sequence of marks @return true if player won, false otherwise */ public boolean win(int player) { return (wonDiagonal(player) || wonStraightLines(player)); }

/** Draws gameboard, player 1 is X, player 2 is O. */ public void drawBoard() { System.out.println("|-----|"); for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (board[i][j] == 1) { System.out.print("|X"); } else if (board[i][j] == 2) { System.out.print("|O"); } else { System.out.print("| "); } } System.out.println("| |-----|"); } } /** Choose a cell for player has won. @param r the row number chose @param c the column number chose @param player the player who choose a position @throws UnavailableCellException is the cell has been occupied (by either player) */ public void choose(int r, int c, int player) { // increment moves count

this.movesCount++;

// update the board

this.board[r][c] = player;

}

// check entry function to check if the row and column is suitable to play

public boolean checkEntry(int row, int column) {

// check if it is between 0-2

if (row >= 0 && row <= 2 && column >= 0 && column <= 2) {

// check if it is an empty space

if (this.board[row][column] == -1) {

return true;

} else {

System.out.println("Please choose an Empty space");

return false;

}

} else {

System.out.println("Please choose co ordinates within (0-2) for both row and column");

return false;

} } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!