Data Structures and Algorithms in Java
Data Structures and Algorithms in Java
6th Edition
ISBN: 9781119278023
Author: Michael T. Goodrich; Roberto Tamassia; Michael H. Goldwasser
Publisher: Wiley Global Education US
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
Need help writing code to answer this question in Python! (image attached)
Need help with python code! How do I simplify my code for a beginner to understand, simple fixed format and centering? Such as:  print(f"As an int variable: {age_int:^7}") print(f"In numeric binary: {age_int:^7b}") My Code:name = input("Enter your name: ")print(f"In text name is: {' '.join(name)}")decimal_values = []binary_values = []for letter in name:   ascii_val = ord(letter)   binary_val = format(ascii_val, '08b')   decimal_values.append(str(ascii_val))   binary_values.append(binary_val)# Loop through each letter:print(f"In ASCII decimal: {' '.join(decimal_values)}")print(f"In ASCII binary: {' '.join(binary_values)}")# Ageage_str = input("Enter your age: ")age_int = int(age_str)print(f"As a string \"{age_str}\": {' '.join(age_str)}")age_decimal_values = []age_binary_values = []for digit in age_str:   ascii_val = ord(digit)   binary_val = format(ascii_val, '07b')   age_decimal_values.append(str(ascii_val))   age_binary_values.append(binary_val)print(f"In ASCII decimal: {'…
Don't use chatgpt or any other AI

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