The findWinner method just returns a space (indicating no winner yet). This meth
ID: 3619770 • Letter: T
Question
The findWinner method just returns a space (indicating no winner yet). Thismethod needs to be revised to return the winner, or a ‘T’ if the game has
ended in a tie.
public static char findWinner(final char[][] theBoard){
// this method should return 'X' if X is the winner
// 'O' if O is the winner, a space (' ') if there is no winner,
// or 'T' if there is a tie. For now it just returns a space no matter what.
return ' ';
This si the code:
public static char findWinner(final char[][] theBoard)
{
int i;
for(i=0; i<3; i++) /* check rows */
if(theBoard[i][0]==theBoard[i][1] &&
theBoard[i][0]==theBoard[i][2]) return theBoard[i][0];
for(i=0; i<3; i++) /* check columns */
if(theBoard[0][i]==theBoard[1][i] &&
theBoard[0][i]==theBoard[2][i]) return theBoard[0][i];
/* test diagonals */
if(theBoard[0][0]==theBoard[1][1] &&
theBoard[1][1]==theBoard[2][2])
return theBoard[0][0];
if(theBoard[0][2]==theBoard[1][1] && theBoard[1][1]==theBoard[2][0])
return theBoard[0][2];
return ' ';
}
This is not returning a tie it just returns winner. What we put to return a tie?
Explanation / Answer
please rate - thanks changes highlighted in red} ------------------------------------------------------------------------------------
in addition the main does not check for a tie change this code also public static void main(String[] args)
{
char[][] matrix=new char[3][3]; /* the tic tac toe matrix */
char done;
System.out.println("This is the game of Tic Tac Toe.");
System.out.println("You will be playing against the computer.");
done = ' ';
init_matrix(matrix);
do {
displayBoard(matrix);
getMove(matrix,'X');
done = findWinner(matrix); /* see if winner */
if(done!= ' ') break; /* winner!*/
displayBoard(matrix);
getMove(matrix,'O');
done =findWinner(matrix); /* see if winner */
} while(done== ' ');
if(done=='X') System.out.println("X wins!");
else if(done=='O')
System.out.println("O wins!!!!");
else
System.out.println("Tie game!!!!");
displayBoard(matrix); /* show final positions */
}
public static void main(String[] args)
{
char[][] matrix=new char[3][3]; /* the tic tac toe matrix */
char done;
System.out.println("This is the game of Tic Tac Toe.");
System.out.println("You will be playing against the computer.");
done = ' ';
init_matrix(matrix);
do {
displayBoard(matrix);
getMove(matrix,'X');
done = findWinner(matrix); /* see if winner */
if(done!= ' ') break; /* winner!*/
displayBoard(matrix);
getMove(matrix,'O');
done =findWinner(matrix); /* see if winner */
} while(done== ' ');
if(done=='X') System.out.println("X wins!");
else if(done=='O')
System.out.println("O wins!!!!");
else
System.out.println("Tie game!!!!");
displayBoard(matrix); /* show final positions */
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.