Data Structures and Algorithms in Java
Data Structures and Algorithms in Java
6th Edition
ISBN: 9781118771334
Author: Michael T. Goodrich
Publisher: WILEY
bartleby

Videos

Expert Solution & Answer
Book Icon
Chapter 3, Problem 4R

Explanation of Solution

Modified Program:

//Define TicTacToe class

public class TicTacToe

{

  //Declare and initialize the required variables

public static final int X = 1, O = -1;

public static final int EMPTY = 0;

//Declare the two dimensional game board variable

private int board[][] = new int[3][3];

//Declare current player variable

private int player;

//Define default constructor

public TicTacToe( )

{

/*Call clearBorad() method to clear the game board. */

  clearBoard( );

}

  //Define clearBoard() method

public void clearBoard( )

{

  //Loop executes until 3 rows

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

  //Loop executes until 3 columns

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

  //Clear each and every cell

board[i][j] = EMPTY;

//Assign first player is "X"

player = X;

}

  //Modified method

  //Define modified putMark() method

public void putMark(int i, int j) throws IllegalStateException

  {

  //Check whether the rows and column missing

  if ((i < 0) || (i > 2) || (j < 0) || (j > 2))

  //Throw an IllegalStateException

throw new IllegalStateException("Invalid  board position");

  //Check whether the game board is not empty

  if (board[i][j] != EMPTY)

  //Throw an IllegalStateException

throw new IllegalStateException("Board position occupied");

  //Assign current player into board

board[i][j] = player;

  //Change the players

player = -player; // switch players (uses fact that O = - X)

  }

/*Define the isWin() method to checks whether the board configuration is a win for given player.*/

  public boolean isWin(int mark)

  {

/*Return the board configuration for rows and columns. */

return ((board[0][0] + board[0][1] + board[0][2] == mark*3) || (board[1][0] + board[1][1] + board[1][2] == mark*3) || (board[2][0] + board[2][1] + board[2][2] == mark*3) || (board[0][0] + board[1][0] + board[2][0] == mark*3) || (board[0][1] + board[1][1] + board[2][1] == mark*3) || (board[0][2] + board[1][2] + board[2][2] == mark*3) || (board[0][0] + board[1][1] + board[2][2] == mark*3) || (board[2][0] + board[1][1] + board[0][2] == mark*3));

  }

/* Define winner() method to returns the code for winning players or indicates "0" for tie the game or unfinished game.*/

public int winner( )

{

//Call the isWin() method to check the winning player is "X". */

if (isWin(X))

  //Returns the code "X" for winning players

  return(X);

//Otherwise, call the isWin() method to check the winning player is "O". */

else if (isWin(O))

  //Returns the code "O" for winning players

  return(O);

//Otherwise, exit the game

else

  return(0);

}

/*Define toString() method to returns the current board for showing the simple character string. */

public String toString()

{

  //Create an object for StringBuilder

StringBuilder sb = new StringBuilder();

//Loop executes until 3 rows

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

{

  //Loop executes until 3 columns

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

  {

  //Match the character in board

  switch (board[i][j])

  {

/*If the row and column contains "X"...

Blurred answer
Students have asked these similar questions
You have learned in class the major steps that occur when a laptop requests a webpage after connecting to a network. In this assignment, you will apply that knowledge to another scenario: opening and playing a YouTube video that resides in Google's data-center infrastructure. Explain, in as much detail as you can, all the steps involved from your device's initial connection to the home/university network, to DNS resolution, routing across multiple networks, reaching Google's servers, and finally receiving the video data. To support your explanation, use tools such as ipconfig, nslookup, and tracert on your own computer, as well as any online IP-lookup tools of your choice. For each stage, include relevant information such as IP addresses, MAC addresses, router hops, and any other details you can gather. You are not expected to find every piece of information, but be as comprehensive as possible based on what you have learned in class, and justify your reasoning with screenshots from…
I need help with this question, please don,t use AI or chatgpt.
NO USE OF AI PLEASE

Additional Engineering Textbook Solutions

Find more solutions based on key concepts
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781305480537
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Text book image
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage
Text book image
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,
Text book image
CMPTR
Computer Science
ISBN:9781337681872
Author:PINARD
Publisher:Cengage
Text book image
Systems Architecture
Computer Science
ISBN:9781305080195
Author:Stephen D. Burd
Publisher:Cengage Learning
6 Stages of UI Design; Author: DesignerUp;https://www.youtube.com/watch?v=_6Tl2_eM0DE;License: Standard Youtube License