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

There are a few errors that I can\'t seem to fix. Please help me so that the pro

ID: 3638746 • Letter: T

Question

There are a few errors that I can't seem to fix.
Please help me so that the program compiles and executes correctly without any errors.
Thank you.


public class TicTacToe {
private final int BOARDSIZE = 3;
private final int WIN = 1, DRAW = 2, CONTINUE = 3;
private int board[][];
private boolean firstPlayer, gameOver;

public TicTacToe()
{
board = new int[ BOARDSIZE ][ BOARDSIZE ];
firstPlayer = true;
gameOver = false;
}

// 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 = true;
board[ row ][ column ] = 2;
printBoard();

printStatus( 2 );

} // end seond player's turn

} // end while

} // end method makeMove

// 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

// 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 ( 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 ( a = 0; a < 3; ++a )
if ( board[ 0 ][ a ] != 0 && board[ 0 ][ a ] ==
board[ 1 ][ a ] && board[ 0 ][ a ] == board[ 2 ][ a ] )
return WIN;

// check for a completed game
for ( int r = 0; r < 3; ++r )
for ( int c = 0; c < 3; ++c )
if ( board[ r ][ c ] == 0 )
return CONTINUE; // game is not finished

return DRAW; // game is a draw

} // end method gameStatus

// 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

// 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;
}
}
else { // last column

switch ( player ) {
case 0:
output = "| | ";
break;
case 1:
output = "| 1 | ";
break;
case 2:
output = "| 2 | ";
break;
}
}

System.out.print( output );

} // end method printSymbol


// 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." );
game.makeMove();
System.exit( 0 );
}

} // end class TicTacToe

Explanation / Answer

please rate - thanks

all I really found wrong was the columns for the output

if this is no good message me with any specifics


import javax.swing.*;
public class TicTacToe {
private final int BOARDSIZE = 3;
private final int WIN = 1, DRAW = 2, CONTINUE = 3;
private int board[][];
private boolean firstPlayer, gameOver;

public TicTacToe()
{
board = new int[ BOARDSIZE ][ BOARDSIZE ];
firstPlayer = true;
gameOver = false;
}

// 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 = true;
board[ row ][ column ] = 2;
printBoard();

printStatus( 2 );

} // end seond player's turn

} // end while

} // end method makeMove

// 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

// 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 ( 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 ( a = 0; a < 3; ++a )
if ( board[ 0 ][ a ] != 0 && board[ 0 ][ a ] ==
board[ 1 ][ a ] && board[ 0 ][ a ] == board[ 2 ][ a ] )
return WIN;

// check for a completed game
for ( int r = 0; r < 3; ++r )
for ( int c = 0; c < 3; ++c )
if ( board[ r ][ c ] == 0 )
return CONTINUE; // game is not finished

return DRAW; // game is a draw

} // end method gameStatus

// 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

// 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;
}
}
else { // last column

switch ( player ) {
case 0:
output = "|       | ";
break;
case 1:
output = "| 1     | ";
break;
case 2:
output = "| 2     | ";
break;
}
}

System.out.print( output );

} // end method printSymbol


// 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." );
game.makeMove();
System.exit( 0 );
}

} // end class TicTacToe