Question: The first method you will work on will be the resetBoard method. This method will change all the items in the 2 dimensional Array to
The first method you will work on will be the resetBoard method. This method will change all the items in the 2 dimensional Array to a blank character, in other words . Afterwards, it will have a print statement to let the user know that the board was reset.
The second method you will work on will be the checkFullBoard method. This method will check all of our board to see if all the spaces are taken up by X or O. If that is the case, it will call the resetBoard method, and then the drawBoard method afterwards to show the blank board.
Finally the last method you will work on will be the checkWinner method. The goal of this is to find out if someone won the game, in other words 3 in a row. Youll have to check horizontally, vertically and both diagonals. If someone wins, the function will return true, otherwise it will return false.
I already have the code below:
import java.util.Scanner;
public class TicTacToe {
public static void drawBoard(char[][] board) {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (i % 2 == 1) {
System.out.print("-");
}
else {
if (j % 2 == 1) {
System.out.print("|");
}
else {
System.out.print(board[i / 2][j / 2]);
}
}
}
System.out.println();
}
}
public static void resetBoard(char[][]board){
}
public static boolean checkWinner(char[][]board){
}
public static void checkFullBoard(char[][]board){
}
public static void main(String[] args) {
// TODO code application logic here
Scanner scan = new Scanner(System.in);
char [][] board = {{' ',' ',' '},{' ',' ',' '},{' ',' ',' '}};
drawBoard(board);
int row, col;
String winner = "Nobody";
while(winner.equals("Nobody")){
System.out.print("Player X choose your spot: ");
row = scan.nextInt();
col = scan.nextInt();
while(board[row][col]!= ' '){
System.out.print("Spot taken, please choose again:");
row = scan.nextInt();
col = scan.nextInt();
}
board[row][col] = 'X';
drawBoard(board);
checkFullBoard(board);
if(checkWinner(board)){
winner = "X";
break;
}
System.out.print("Player O choose your spot: ");
row = scan.nextInt();
col = scan.nextInt();
while(board[row][col]!= ' '){
System.out.print("Spot taken, please choose again:");
row = scan.nextInt();
col = scan.nextInt();
}
board[row][col] = 'O';
drawBoard(board);
checkFullBoard(board);
if(checkWinner(board)){
winner = "O";
break;
}
}
System.out.print(winner + " has won the game!");
}
}
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
