Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Define a class called TicTacToe . An object of type TicTacToe is a single game o

ID: 675210 • Letter: D

Question

Define a class called TicTacToe. An object of type TicTacToe is a single game of tic-tac-toe. Store the game board as a single two-dimensional array of base type char that has three rows and three columns. Include methods to add a move, to display the board, to tell whose turn it is (X or O), to tell whether there is a winner, to say who the winner is, and to reinitialize the game to the beginning. Write a main method for the class that will allow two players to enter their moves in turn at the same keyboard.

All this is in Java.

Explanation / Answer

import java.util.Scanner;

public class TicTacToe {

private char[][] board;

public char currentPlayerMark;

private static Scanner sc;

          

public TicTacToe() {

board = new char[3][3];

currentPlayerMark = 'x';

initializeBoard();

}

public void initializeBoard() {

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

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

board[i][j] = '-';

}

}

}

  

public void printBoard() {

System.out.println("-------------");

      

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

System.out.print("| ");

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

System.out.print(board[i][j] + " | ");

}

System.out.println();

System.out.println("-------------");

}

}

  

public boolean isBoardFull() {

boolean isFull = true;

      

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

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

if (board[i][j] == '-') {

isFull = false;

}

}

}

      

return isFull;

}

  

public boolean checkForWin() {

return (checkRowsForWin() || checkColumnsForWin() || checkDiagonalsForWin());

}

private boolean checkRowsForWin() {

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

if (checkRowCol(board[i][0], board[i][1], board[i][2]) == true) {

return true;

}

}

return false;

}

private boolean checkColumnsForWin() {

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

if (checkRowCol(board[0][i], board[1][i], board[2][i]) == true) {

return true;

}

}

return false;

}

private boolean checkDiagonalsForWin() {

return ((checkRowCol(board[0][0], board[1][1], board[2][2]) == true) || (checkRowCol(board[0][2], board[1][1], board[2][0]) == true));

}

private boolean checkRowCol(char c1, char c2, char c3) {

return ((c1 != '-') && (c1 == c2) && (c2 == c3));

}

  

  

// Change player marks back and forth.

public void playerChange() {

if (currentPlayerMark == 'x') {

currentPlayerMark = 'o';

}

else {

currentPlayerMark = 'x';

}

}

public boolean play(int row, int col) {

if ((row >= 0) && (row < 3)) {

if ((col >= 0) && (col < 3)) {

if (board[row][col] == '-') {

board[row][col] = currentPlayerMark;

return true;

}

}

}

return false;

}

  

public static void main(String[] args) {

       TicTacToe game = new TicTacToe();

       sc = new Scanner(System.in);

       while (true) {

           System.out.println("Turn for "+game.currentPlayerMark+" player Enter location (row column): ");

           int x = sc.nextInt();

           int y = sc.nextInt();

           game.play(x, y);

           game.printBoard();

          

           if (game.checkForWin()) {

               System.out.println("Congrats! "+game.currentPlayerMark+" wins!!");

               return;

           } else if (game.isBoardFull()) {

               System.out.println("Appears we have a draw!");

               return;

           }

           game.playerChange();

       }

   }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote