Question
Java: Complete the TicTacToeWinner method, which should look at a tic-tac-toe board & determine if there is a winner. (https://en.wikipedia.org/wiki/Tic-tac-toe) public class TicTacToe { //
Java:
Complete the TicTacToeWinner method, which should look at a tic-tac-toe board & determine if there is a winner. (https://en.wikipedia.org/wiki/Tic-tac-toe)
public class TicTacToe {
// Takes in a tic tac toe board
// returns 1 if 'X' wins
// returns 2 if 'O' wins
// returns 3 if it is a tie
// returns -1 if there is not yet a winner
public static int ticTacToeWinner(char[][] board) {
return -1;
}
public static void printBoard(char[][] board) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++){
System.out.print(board[i][j] + "\t");
}
System.out.println(" ");
}
}
public static void main(String[] args) {
// note: a - indicates that a spot is currently empty
char[][] board = {{'O','-','X'},{'-','-','O'},{'-','X','-'}};
printBoard(board);
int winner = ticTacToeWinner(board);
if (winner == 1) {
System.out.println("X wins");
}
if (winner == 2) {
System.out.println("O wins");
}
if (winner == 3) {
System.out.println("Tie game");
}
if (winner == -1) {
System.out.println("Keep Playing");
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started