Fix the below code to be in working condition. Java, I use eclipse. Please inclu
ID: 3730355 • Letter: F
Question
Fix the below code to be in working condition.
Java, I use eclipse.
Please include screen shots of the working program.
------------------------------------------------------------------------
/Header file section
Import javax.swing.*;
//Main Class
Public class TicTacToe
{
//Declare variable
private final int Boardsize = 3;
private final int WIN = 1, DRAW = 2, CONTINUE = 3;
private int board[][];
private Boolean firstPlayer, gameOver;
//Default constructor
Public TicTacToe()
{
board = new int [BOARDSIZE][BOARDSIZE];
firstPlayer = true;
gameOver = false;
}
//Method makeMove()
//start game
public void makeMove()
{
Int row = 0, column = 0;
while (!gameOver)
{
//first player’s turn
If (firstPlayer)
{
row = Integer.parseInt(
JOptionPane.showInputDialog(
“Player 1: Enter row (0 <=row<3):”));
column = Integer.parseInt(
JOptionPane.showInputDialog(
“Player 1: Enter column (0<=column<3):”));
//validate move
while (!validMove(row,column))
{
row = Integer.parseInt(
JOptionPane.showInputDialog(
“Player 1: Enter row (0<=row<3):”));
column = Integer.parseInt
(JOptionPane.showInputDialog(
“Player 1: Enter column (
0<=column<3):”));
}
firstPlayer = false;
board[row][column] = 1;
printBoard();
printStatus(1);
}
//end first player’s turn
//second player’s turn
else
{
row = Integer.parseInt(
JOptionPane.showInputDialog(
“Player 2: Enter row (0 <=row<3):”));
column = Integer.parseInt(
JOptionPane.showInputDialog(
“Player 2: Enter column (0<=column<3):”));
//validate move
while (!validMove(row,column))
{
row = Integer.parseInt(
JOptionPane.showInputDialog(
“Player 2: Enter row (0<=row<3):”));
column = Integer.parseInt
(JOptionPane.showInputDialog(
“Player 2: Enter column (
0<=column<3):”));
}
firstPlayer = ture;
board[row][column] = 2;
printBoard();
printStatus(2);
} //end second player’s turn
}//end while
}//end method makeMove
//Method PrintStatus
//show game status in status bar
Public void printStatus (int Player)
{
int status = gameStatus();
//check game status
swtich (status)
{
case WIN:
System.out.println(“Player “ + player + “ wins. “);
gameOver = true;
break;
case DRAW:
System.out.println(“Game is a draw. “);
gameOver = true;
break;
case CONTINUE:
if (player == 1)
System.out.println(“Player 2’s turn.”);
else
System.out.println(“Player 1’s turn.”);
break;
} // end switch
} // end method printStatus
//Method gameStatus
//get game status
Public int gameStatus()
{
int a;
//check for a win on diagonals
If (board [0][0]! = 0 &&
board [0][0] == board [1][1] &&
board [0][0] == board [2][2])
return WIN;
else if (board [2][0]! = 0 &&
board [2][0] == board [1][1]
&& board [2][0] == board [0][2])
return WIN;
//check for win in rows
for (int a = 0; a<3;++a)
if (board[a][0]!=0 && board [a][0] ==
board [a][1] && board [a][0] == board [a][2])
return WIN;
//check for win in columns
for (int a = 0; a<3;++a)
if (board[0][a]!=0 && board [0][a] ==
board [1][a] && board [0][a] == board [2][a])
//check for a completed game
for (int r = 0; a<3;++r)
for (int c = 0; c<3; ++c)
if (board[r][c] == 0)
return CONTINUTE; //game is not finished
return DRAW; // game is a draw
} // end method game Status
// Method printBoard
// display board
public void printBoard()
{
Ssytem.out.println( “___________________________”);
for (int row = 0; row < BOARDSIZE; ++row)
{
System.out.println(“| | | |”);
for (int column = 0; column< BOARDSIZE; ++column)
printSymbol(column, board[row][column]);
System.out.println(“|________|_________|_________|”);
}
} // end method printBoard
//Method printSymbol
//print moves
public void printSymbol (int column, int player)
{
String output = “”;
if (column !=2)
{ // first two columns
switch (player)
case 0:
output = “| |”;
break;
case 1:
output = “| 1 | ”;
break;
case 2:
output = “| 2 | ”;
break;
}
}
System.out.print(output);
} // end method printSymbol
// Method validMove
//validate move
public Boolean validMove(int row, int column)
{
return row >= 0 && <3 && column >= 0 && column <3 &&
board [row][column] == 0;
}
// main method
public static void main (String [] args)
{
TicTacToe game = new TicTacToe ();
game.printBoard();
System.out.println(“Player 1’s turn.”);
System.exit(0);
}
} // end class TicTacToe
Explanation / Answer
Hi.. I have removed all the errors in your program. Please check below.
import javax.swing.*;
//Main Class
public class TicTacToe
{
//Declare variable
private final int Boardsize = 3;
private final int WIN = 1, DRAW = 2, CONTINUE = 3;
private int board[][];
private Boolean firstPlayer, gameOver;
//Default constructor
public TicTacToe()
{
board = new int [Boardsize][Boardsize];
firstPlayer = true;
gameOver = false;
}
//Method makeMove()
//start game
public void makeMove()
{
int row = 0, column = 0;
while (!gameOver)
{
//first player’s turn
if (firstPlayer)
{
row = Integer.parseInt(JOptionPane.showInputDialog("Player 1: Enter row"));
//JOptionPane.showInputDialog("");
column = Integer.parseInt(
JOptionPane.showInputDialog("Player 1: Enter column (0<=column<3):"));
//validate move
while (!validMove(row,column))
{
row = Integer.parseInt(
JOptionPane.showInputDialog(
"Player 1: Enter row (0<=row<3):"));
column = Integer.parseInt
(JOptionPane.showInputDialog(
"Player 1: Enter column (0<=column<3):"));
}
firstPlayer = false;
board[row][column] = 1;
printBoard();
printStatus(1);
}
//end first player’s turn
//second player’s turn
else
{
row = Integer.parseInt(
JOptionPane.showInputDialog(
"Player 2: Enter row (0 <=row<3):"));
column = Integer.parseInt(
JOptionPane.showInputDialog(
"Player 2: Enter column (0<=column<3):"));
//validate move
while (!validMove(row,column))
{
row = Integer.parseInt(
JOptionPane.showInputDialog(
"Player 2: Enter row (0<=row<3):"));
column = Integer.parseInt
(JOptionPane.showInputDialog(
"Player 2: Enter column ( 0<=column<3):"));
}
firstPlayer = true;
board[row][column] = 2;
printBoard();
printStatus(2);
} //end second player’s turn
}//end while
}//end method makeMove
//Method PrintStatus
//show game status in status bar
public void printStatus (int Player)
{
int status = gameStatus();
//check game status
switch (status)
{
case WIN:
System.out.println("Player " + Player + " wins. ");
gameOver = true;
break;
case DRAW:
System.out.println("Game is a draw. ");
gameOver = true;
break;
case CONTINUE:
if (Player == 1)
System.out.println("Player 2’s turn.");
else
System.out.println("Player 1’s turn.");
break;
} // end switch
} // end method printStatus
//Method gameStatus
//get game status
public int gameStatus()
{
//check for a win on diagonals
if (board [0][0]!= 0 &&
board [0][0] == board [1][1] &&
board [0][0] == board [2][2])
return WIN;
else if (board [2][0]!= 0 &&
board [2][0] == board [1][1]
&& board [2][0] == board [0][2])
return WIN;
//check for win in rows
for (int a = 0; a<3;++a)
if (board[a][0]!=0 && board [a][0] ==
board [a][1] && board [a][0] == board [a][2])
return WIN;
//check for win in columns
for (int a = 0; a<3;++a)
if (board[0][a]!=0 && board [0][a] ==
board [1][a] && board [0][a] == board [2][a])
//check for a completed game
for (int r = 0; a<3;++r)
for (int c = 0; c<3; ++c)
if (board[r][c] == 0)
continue;//game is not finished
return DRAW; // game is a draw
} // end method game Status
// Method printBoard
// display board
public void printBoard()
{
System.out.println( "___________________________");
for (int row = 0; row < Boardsize; ++row)
{
System.out.println("| | | |");
for (int column = 0; column< Boardsize; ++column)
printSymbol(column, board[row][column]);
System.out.println("|________|_________|_________|");
}
} // end method printBoard
//Method printSymbol
//print moves
public void printSymbol (int column, int player)
{
String output = "";
if (column !=2)
{ // first two columns
switch (player){
case 0:
output = "| |";
break;
case 1:
output = "| 1 | ";
break;
case 2:
output = "| 2 | ";
break;
}
}
System.out.print(output);
} // end method printSymbol
// Method validMove
//validate move
public Boolean validMove(int row, int column)
{
return row >= 0 && row<3 && column >= 0 && column <3 &&
board [row][column] == 0;
}
// main method
public static void main (String [] args)
{
TicTacToe game = new TicTacToe ();
game.printBoard();
System.out.println("Player 1’s turn.");
System.exit(0);
}
} // end class TicTacToe
Please test the code and let me know if any querries. Thank you. All the best.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.