LANGUAGE JAVA Write a class and a client class to test it th
LANGUAGE: JAVA
Write a class (and a client class to test it) that encapsulates a tic-tac-toe board. A tic-tac-toe board looks like a table of three rows and three columns partially or completely filled with the characters X and O. At any point, a cell of that table could be empty or could contain an X or an O. You should have one instance variable, a two-dimensional array of values representing the tic-tac-toe board. This game should involve one human player vs. the computer. At the start of each game, randomly select if the computer will play X or O and who (i.e. human or computer) will make the first move. Your default constructor should instantiate the array so that it represents an empty board.
You should include the following methods:
- a method that generates a valid play by the computer and displays the board after each play.
- a method that requests a valid play from the human and displays the board after each play.
- a method to display the tic-tac-toe board.
- a method checking if a player has won based on the contents of the board; this method takes no parameter. It returns X if the \"X player\" has won, O if the \"O player\" has won, T if the game was a tie. A player wins if he or she has placed an X (or an O) in all cells in a row, all cells in a column, or all cells in one of the diagonals.
NOTE: Be sure to display the board after each move. You must provide clear prompts for the human player to select a space on the tic-tac-toe board.
Input Validation: Verify that all moves by the human player are to a valid space on the tic-tac-toe board. An incorrect choice should not halt or terminate the game.
| Tic Tac Toe Project Rubric | ||
| Criteria | Ratings | Points |
| Correct Project Name | 2.0 | |
| Board Displayed Correctly | 3.0 | |
| Game Played Correctly | 10.0 | |
| Input Validation Performed | 3.0 | |
| Clear Input Prompts Provided | 2.0 | |
| TOTAL POINTS POSSIBLE | 20.0 |
Solution
import java.util.*;
public class TicTacToe
{
private String[][] board;
static String X = \"X\";
static String O = \"O\";
public TicTacToe()
{
board = new String[3][3];
}
public void printBoard()
{
System.out.println();
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
if (board[i][j] == null) {
System.out.print(\"_\");
} else {
System.out.print(board[i][j]);
}
if (j < 2) {
System.out.print(\"|\");
} else {
System.out.println();
}
}
}
System.out.println();
}
public Boolean checkWinner(String play) {
int playInRow = 0;
int playD1 = 0;
int playD2 = 0;
int[] playInColumn = new int[board[0].length];
for (int i = 0; i < board.length; i++) {
playInRow = 0;
for (int j = 0; j < board[i].length; j++) {
if (null == board[i][j]) {
continue;
}
if (board[i][j].equals(play)) {
playInRow++;
playInColumn[j]++;
if (i == j) {
playD1++;
} else if (2 == i + j) {
playD2++;
}
}
}
if (playInRow == 3) {
return true;
}
}
if (3 == playD1 || 3 == playD2) {
return true;
}
for (int i = 0; i < playInColumn.length; i++) {
if (playInColumn[i] == 3) {
return true;
}
}
return false;
}
public void makeMove(Scanner stdin, String play) {
int r;
int c;
Boolean goodInput = false;
while(!goodInput) {
r = -1;
c = -1;
System.out.println (\"Enter coordinates to play your \" + play);
if (stdin.hasNextInt()) {
r = stdin.nextInt();
}
if (stdin.hasNextInt()) {
c = stdin.nextInt();
}
else {
stdin.nextLine();
System.out.println(\"Both inputs must be integers between 0 and 2.\");
continue;
}
if ((r < 0) || (r > 2) || (c < 0) || (c > 2)) {
System.out.println(\"Both inputs must be integers between 0 and 2.\");
continue;
}
else if (board[r][c] != null ){
System.out.println(\"That location is occupied\");
continue;
}
else {
board[r][c] = play;
return;
}
}
return;
}
public static void main(String[] args) {
TicTacToe ttt = new TicTacToe();
Scanner stdin = new Scanner(System.in);
int moves = 0;
System.out.println(\"Let\'s play TicTacToe -- X goes first\");
ttt.printBoard();
while (moves < 9) {
ttt.makeMove(stdin, ttt.X);
moves++;
if (moves > 4) {
if (ttt.checkWinner(X)) {
System.out.println(X + \" You Win!!!\");
break;
}
}
ttt.printBoard();
ttt.makeMove(stdin, ttt.O);
moves++;
if (moves > 4) {
if (ttt.checkWinner(O)) {
System.out.println(O + \" You Win!!!\");
break;
}
}
ttt.printBoard();
}
}
}




