Question: Using Java arrays and loops, create a Tic Tac Toe simulator `TicTacToe` default constructor * call `this(Player X, Player O, true)` * `TicTacToe`: String xPlayer,

Using Java arrays and loops, create a Tic Tac Toe simulator

`TicTacToe` default constructor

* call `this("Player X", "Player O", true)`

* `TicTacToe`: String xPlayer, String oPlayer, boolean xturn

* Set the xPlayer name.

* Set the oPlayer name.

* Set xturn value.

* Set plays to 0.

* Initialize board.

* Set each element in board to the character `-`.

* `getXturn` method

* Return the value of xturn.

* `play` method

* Receives an x and y int representing the row/col in the board array.

* Calls the `makeMove` method, passing the x and y coordinates.

* Increases plays by 1 if makeMove returned true.

* Sets xturn to the opposite of its current value if makeMove returned true.

* Calls the `isGameOver` method and prints the name of the winner if someone has won, prints tie game if the game is tied.

* Returns true when game is finished (win or tie), false otherwise.

* `makeMove` method

* Receives an x and y int representing the row/col in the board array.

* If the number of plays is 9, then state that `The board is already completely filled.` and return false.

* If x or y value is out of the range of 0-2, print that the `The coordinate (x, y) is out of range.` and return false.

* If the board at x,y has already been played, print `The coordinate (x, y) is already taken.` and return false.

* Otherwise, set the board at (x, y) to an 'X' if xturn is true and 'O' if xturn is false and return true.

* `isGameOver` method

* If any of the rows have three X's or O's in a row, return the winning character.

* If any of the columns have three X's or O's in a row, return the winning character.

* If any of the two diagnals have three X's or O's in a a row, return the winning character.

* If the number of plays is 9, return T for tie.

* Otherwise return - for no winner yet/tie.

* `toString` method

* Print the array so it matches the sample output.

Class for testing program

```java

public class TicTacToeClient {

public static void main(String[] args) {

TicTacToe ttt = new TicTacToe();

System.out.println("Below should be an empty board: ");

System.out.println(ttt);

System.out.println("Testing diagonal win (X should win): ");

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

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

boolean retVal = ttt.play(i, j);

if (retVal)

break;

}

}

System.out.println(" " + ttt);

System.out.println("Testing other diagonal win (X should win): ");

ttt = new TicTacToe();

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

for (int j = 2; j >= 0; j--) {

boolean retVal = ttt.play(i, j);

if (retVal)

break;

}

}

System.out.println(" " + ttt);

System.out.println("Testing row 1 win (X should win): ");

ttt = new TicTacToe();

ttt.play(1, 0);

ttt.play(0, 1);

ttt.play(1, 1);

ttt.play(0, 2);

ttt.play(1, 2);

System.out.println(ttt);

System.out.println("Testing row 1 win (O should win): ");

ttt = new TicTacToe("Player X", "Player O", false);

ttt.play(1, 0);

ttt.play(0, 1);

ttt.play(1, 1);

ttt.play(0, 2);

ttt.play(0, 0);

ttt.play(2, 2);

ttt.play(1, 2);

System.out.println(ttt);

System.out.println("Tie game: ");

ttt = new TicTacToe("Arthur", "Ford", false);

ttt.play(1, 1);

ttt.play(2, 2);

ttt.play(1, 2);

ttt.play(1, 0);

ttt.play(2, 0);

ttt.play(0, 2);

ttt.play(0, 1);

ttt.play(2, 1);

ttt.play(0, 0);

System.out.println(ttt);

System.out.println("Testing making a move after 9 plays: ");

ttt.play(0, 1);

System.out.println();

System.out.println("Testing col 1 win (Arthur should win): ");

ttt = new TicTacToe("Arthur", "Ford", true);

ttt.play(1, 1);

ttt.play(2, 2);

ttt.play(0, 1);

ttt.play(2, 0);

ttt.play(2, 1);

System.out.println(ttt);

System.out.println("Testing playing the same position (it should be O's turn again): ");

ttt = new TicTacToe();

ttt.play(0, 0);

ttt.play(0, 0);

if (ttt.isXturn()) {

System.out.println("Incorrect, it should be O's turn.");

} else {

System.out.println("It is O's turn!");

}

System.out.println();

System.out.println("Testing the out of range error (it should be O's turn again): ");

ttt = new TicTacToe();

ttt.play(0, 0);

ttt.play(-1, 0);

ttt.play(0, -1);

ttt.play(3, 0);

ttt.play(0, 3);

if (ttt.isXturn()) {

System.out.println("Incorrect, it should be O's turn.");

} else {

System.out.println("It is O's turn!");

}

}

}

```

Expected Output

```

Below should be an empty board:

| - | - | - |

| - | - | - |

| - | - | - |

Testing diagonal win (X should win):

Player X wins!!!

| X | O | X |

| O | X | O |

| X | - | - |

Testing other diagonal win (X should win):

Player X wins!!!

| X | O | X |

| O | X | O |

| - | - | X |

Testing row 1 win (X should win):

Player X wins!!!

| - | O | O |

| X | X | X |

| - | - | - |

Testing row 1 win (O should win):

Player O wins!!!

| O | X | X |

| O | O | O |

| - | - | X |

Tie game:

Tie game...

| O | O | X |

| X | O | O |

| O | X | X |

Testing making a move after 9 plays:

The board is already completely filled.

Testing col 1 win (Arthur should win):

Arthur wins!!!

| - | X | - |

| - | X | - |

| O | X | O |

Testing playing the same position (it should be O's turn again):

The coordinate (0, 0) is already taken.

It is O's turn!

Testing the out of range error (it should be O's turn again):

The coordinate (-1, 0) is out of the range.

The coordinate (0, -1) is out of the range.

The coordinate (3, 0) is out of the range.

The coordinate (0, 3) is out of the range.

It is O's turn!

```

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!