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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!