Question

In: Computer Science

Write a class (and a client class to test it) that encapsulates a tic-tac-toe board. A...

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.

//Below is the Java project I have so far that needs changes based on the instructions above.

import java.util.Scanner;
import java.util.Random;

public class LeavinesTicTacToe
{
public static Scanner sc = new Scanner(System.in);

public static void main(String[] args)
{
  
final int SIZE = 3;
char[][] board = new char[SIZE][SIZE]; // game board

resetBoard(board); // initialize the board (with ' ' for all cells)

// First, welcome message and display the board.
System.out.println("===== WELCOME TO THE TIC-TAC-TOE GAME!! =====\n");
showBoard(board);

// Then ask the user which symbol (x or o) he/she wants to play.
System.out.print(" Which symbol do you want to play, \"x\" or \"o\"? ");
char userSymbol = sc.next().toLowerCase().charAt(0);
char compSymbol = (userSymbol == 'x') ? 'o' : 'x';

// Also ask whether or not the user wants to go first.
System.out.println();
System.out.print(" Do you want to go first (y/n);? ");
char ans = sc.next().toLowerCase().charAt(0);
         
int turn; // 0 -- the user, 1 -- the computer
int remainCount = SIZE * SIZE; // empty cell count

// THE VERY FIRST MOVE.
if (ans == 'y') {
turn = 0;
userPlay(board, userSymbol); // user puts his/her first tic
}
else {
turn = 1;
compPlay(board, compSymbol); // computer puts its first tic
}
// Show the board, and decrement the count of remaining cells.
showBoard(board);
remainCount--;

// Play the game until either one wins.
boolean done = false;
int winner = -1; // 0 -- the user, 1 -- the computer, -1 -- draw

while (!done && remainCount > 0) {
// If there is a winner at this time, set the winner and the done flag to true.
done = isGameWon(board, turn, userSymbol, compSymbol); // Did the turn won?

if (done)
winner = turn; // the one who made the last move won the game
else {
// No winner yet. Find the next turn and play.
turn = (turn + 1 ) % 2;

if (turn == 0)
userPlay(board, userSymbol);
else
compPlay(board, compSymbol);

// Show the board after one tic, and decrement the rem count.
showBoard(board);
remainCount--;
}
}

// Winner is found. Declare the winner.
if (winner == 0)
System.out.println("\n** YOU WON. CONGRATULATIONS!! **");
else if (winner == 1)
System.out.println("\n** YOU LOST.. Maybe next time :) **");
else
System.out.println("\n** DRAW... **");

}

public static void resetBoard(char[][] brd)
{
for (int i = 0; i < brd.length; i++)
for (int j = 0; j < brd[0].length; j++)
brd[i][j] = ' ';
}

public static void showBoard(char[][] brd)
{
int numRow = brd.length;
int numCol = brd[0].length;

System.out.println();

// First write the column header
System.out.print(" ");
for (int i = 0; i < numCol; i++)
System.out.print(i + " ");
System.out.print('\n');

System.out.println(); // blank line after the header

// The write the table
for (int i = 0; i < numRow; i++) {
System.out.print(i + " ");
for (int j = 0; j < numCol; j++) {
if (j != 0)
System.out.print("|");
System.out.print(" " + brd[i][j] + " ");
}

System.out.println();

if (i != (numRow - 1)) {
// separator line
System.out.print(" ");
for (int j = 0; j < numCol; j++) {
if (j != 0)
System.out.print("+");
System.out.print("---");
}
System.out.println();
}
}
System.out.println();
}

public static void userPlay(char[][] brd, char usym)
{
System.out.print("\nEnter the row and column indices: ");
int rowIndex = sc.nextInt();
int colIndex = sc.nextInt();

while (brd[rowIndex][colIndex] != ' ') {
System.out.print("\n!! The cell is already taken.\nEnter the row and column indices: ");
rowIndex = sc.nextInt();
colIndex = sc.nextInt();
}

brd[rowIndex][colIndex] = usym;
}

public static void compPlay(char[][] brd, char csym)
{
// Find the first empty cell and put a tic there.
for (int i = 0; i < brd.length; i++) {
for (int j = 0; j < brd[0].length; j++) {
if (brd[i][j] == ' ') { // empty cell
brd[i][j] = csym;
return;
}
}
}
}

public static boolean isGameWon(char[][] brd, int turn, char usym, char csym)
{
char sym;
if (turn == 0)
sym = usym;
else
sym = csym;

int i, j;
boolean win = false;

// Check win by a row
for (i = 0; i < brd.length && !win; i++) {
for (j = 0; j < brd[0].length; j++) {
if (brd[i][j] != sym)
break;
}
if (j == brd[0].length)
win = true;
}

// Check win by a column
for (j = 0; j < brd[0].length && !win; j++) {
for (i = 0; i < brd.length; i++) {
if (brd[i][j] != sym)
break;
}
if (i == brd.length)
win = true;
}

// Check win by a diagonal (1)
if (!win) {
for (i = 0; i < brd.length; i++) {
if (brd[i][i] != sym)
break;
}
if (i == brd.length)
win = true;
}

// Check win by a diagonal (2)
if (!win) {
for (i = 0; i < brd.length; i++) {
if (brd[i][brd.length - 1 - i] != sym)
break;
}
if (i == brd.length)
win = true;
}

// Finally return win
return win;
}
}

Solutions

Expert Solution

import java.util.Scanner;

public class TTTConsoleNonOO2P {

   public static final int EMPTY = 0;

   public static final int CROSS = 1;

   public static final int NOUGHT = 2;

   public static final int PLAYING = 0;

   public static final int DRAW = 1;

   public static final int CROSS_WON = 2;

   public static final int NOUGHT_WON = 3;

   public static final int ROWS = 3, COLS = 3;

   public static int[][] board = new int[ROWS][COLS];

   public static int currentState;

                                   

   public static int currentPlayer;

   public static int currntRow, currentCol;

   public static Scanner in = new Scanner(System.in);

   public static void main(String[] args) {

      initGame();

      do {

         playerMove(currentPlayer);

         updateGame(currentPlayer, currntRow, currentCol);

         printBoard();

         if (currentState == CROSS_WON) {

            System.out.println("'X' won! Bye!");

         } else if (currentState == NOUGHT_WON) {

            System.out.println("'O' won! Bye!");

         } else if (currentState == DRAW) {

            System.out.println("It's a Draw! Bye!");

         }

         currentPlayer = (currentPlayer == CROSS) ? NOUGHT : CROSS;

      } while (currentState == PLAYING);

   }

   public static void initGame() {

    for (int row = 0; row < ROWS; ++row) {

         for (int col = 0; col < COLS; ++col) {

            board[row][col] = EMPTY;

         }

      }

      currentState = PLAYING;

      currentPlayer = CROSS;

   }

   public static void playerMove(int theSeed) {

      boolean validInput = false;

      do {

         if (theSeed == CROSS) {

            System.out.print("Player 'X', enter your move (row[1-3] column[1-3]): ");

         } else {

            System.out.print("Player 'O', enter your move (row[1-3] column[1-3]): ");

         }

         int row = in.nextInt() - 1;

         int col = in.nextInt() - 1;

         if (row >= 0 && row < ROWS && col >= 0 && col < COLS && board[row][col] == EMPTY) {

            currntRow = row;

            currentCol = col;

            board[currntRow][currentCol] = theSeed;

            validInput = true;

         } else {

            System.out.println("This move at (" + (row + 1) + "," + (col + 1)

                  + ") is not valid. Try again...");

         }

      } while (!validInput);

   }

   public static void updateGame(int theSeed, int currentRow, int currentCol) {

      if (hasWon(theSeed, currentRow, currentCol)) {

         currentState = (theSeed == CROSS) ? CROSS_WON : NOUGHT_WON;

      } else if (isDraw()) {

         currentState = DRAW;

      }

   }

   public static boolean isDraw() {

      for (int row = 0; row < ROWS; ++row) {

         for (int col = 0; col < COLS; ++col) {

            if (board[row][col] == EMPTY) {

               return false;

            }

         }

      }

      return true;

   }

   public static boolean hasWon(int theSeed, int currentRow, int currentCol) {

      return (board[currentRow][0] == theSeed        

                   && board[currentRow][1] == theSeed

                   && board[currentRow][2] == theSeed

              || board[0][currentCol] == theSeed     

                   && board[1][currentCol] == theSeed

                   && board[2][currentCol] == theSeed

              || currentRow == currentCol           

                   && board[0][0] == theSeed

                   && board[1][1] == theSeed

                   && board[2][2] == theSeed

              || currentRow + currentCol == 2                    && board[0][2] == theSeed

                   && board[1][1] == theSeed

                   && board[2][0] == theSeed);

   }

   public static void printBoard() {

      for (int row = 0; row < ROWS; ++row) {

         for (int col = 0; col < COLS; ++col) {

            printCell(board[row][col]);

            if (col != COLS - 1) {

               System.out.print("|");  

            }

         }

         System.out.println();

         if (row != ROWS - 1) {

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

         }

      }

      System.out.println();

   }

   public static void printCell(int content) {

      switch (content) {

         case EMPTY: System.out.print("   "); break;

         case NOUGHT: System.out.print(" O "); break;

         case CROSS: System.out.print(" X "); break;

      }

   }

}


Related Solutions

Write a class (and a client class to test it) that encapsulates a tic-tac-toe board. A...
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....
Write a program that plays tic-tac-toe. The tic-tac-toe game is played on a 3 × 3...
Write a program that plays tic-tac-toe. The tic-tac-toe game is played on a 3 × 3 grid as shown below: The game is played by two players, who take turns. The first player marks moves with a circle, the second with a cross. The player who has formed a horizontal, vertical, or diagonal sequence of three marks wins. Your program should draw the game board, ask the user for the coordinates of the next mark (their move), change the players...
C# (Tic-Tac-Toe) Create class TicTacToe that will enable you to write a complete app to play...
C# (Tic-Tac-Toe) Create class TicTacToe that will enable you to write a complete app to play the game of Tic-Tac-Toe. The class contains a private 3-by-3 rectangular array of integers. The constructor should initialize the empty board to all 0s. Allow two human players. Wherever the first player moves, place a 1 in the specified square, and place a 2 wherever the second player moves. Each move must be to an empty square. After each move, determine whether the game...
Python Code Needed Two-Player, Two-Dimensional Tic-Tac-Toe Write a script to play two-dimensional Tic-Tac-Toe between two human...
Python Code Needed Two-Player, Two-Dimensional Tic-Tac-Toe Write a script to play two-dimensional Tic-Tac-Toe between two human players who alternate entering their moves on the same computer. Create a 3-by-3 two-dimensional array. Each player indicates their moves by entering a pair of numbers representing the row and column indices of the square in which they want to place their mark, either an 'X' or an 'O'. When the first player moves, place an 'X' in the specified square. When the second...
Stewart invented “Max” a board game similar to “Tic Tac Toe.” In May 2017, Stewart began...
Stewart invented “Max” a board game similar to “Tic Tac Toe.” In May 2017, Stewart began negotiating with Big Board, Inc., to license “Max” for distribution outside the United States. On June 11, 2017, the parties met and orally discussed terms. As compensation, Big Board promised to pay Stewart the amount due from another board game he had developed for Big Board two years ago. On June 26, 2017, Lisa, a Big Board employee, sent Stewart an email titled “Max...
If you were to write a game of tic-tac-toe, you may store the representation of the...
If you were to write a game of tic-tac-toe, you may store the representation of the game board as a two dimensional list such as   [['X', '', 'X'], ['O', 'X', ''], ['', 'O', 'X']] where each sublist is a row in the board.   Empty strings ('') denote a space that does not yet have a value. Assuming this representation, write functions (contained in the same file) that do the following: a) Create a new empty tic-tac-toe board. This function should...
For this assignment, you will write a tic-tac-toe application in HTML and JavaScript, using an HTML...
For this assignment, you will write a tic-tac-toe application in HTML and JavaScript, using an HTML <canvas> tag. The game will be played "hot seat" where players take turns using the same device. Requirements: The canvas should be 600px tall and wide, with the gameplay area occupying most of the canvas. The X's and O's may be drawn using polygons or large-font text The grid should be drawn using polygons, specifically long, thin rectangles Before & between games, the canvas...
If anyone can please write a code for a 5x5 tic tac toe game in matlab...
If anyone can please write a code for a 5x5 tic tac toe game in matlab I would greatly appreciate it. Its extra credit. PLEASE HELP ME :(
QUESTION: This is a tic-tac-toe game in react.js. Covert the following class-based components in React.js to...
QUESTION: This is a tic-tac-toe game in react.js. Covert the following class-based components in React.js to Functional Based component in React. //CODE import React from 'react'; import ReactDOM from 'react-dom'; import './index.css'; function calculateWinner(squares) { const lines = [ [0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 3, 6], [1, 4, 7], [2, 5, 8], [0, 4, 8], [2, 4, 6] ]; for (let i = 0; i < lines.length; i++) { const [a, b, c] = lines[i];...
How to make tic tac toe game in javascript with vue.js
How to make tic tac toe game in javascript with vue.js
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT