The first method you will work on will be the resetBoard method. This method wil
ID: 3721788 • Letter: T
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 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. You’ll 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!"); } }
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. You’ll 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!"); } }
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. You’ll 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!"); } } 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!"); } }
Explanation / Answer
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){
for(int i=0;i<board.length;i++) {
for(int j=0;j<board[0].length;j++) {
board[i][j]=' ';
}
}
System.out.println("Board :");
for(int i=0;i<board.length;i++) {
for(int j=0;j<board[0].length;j++) {
System.out.print(" "+board[i][j]);
}
System.out.println();
}
}
public static boolean checkWinner(char[][]board){
boolean ret=false;
boolean win=false;
if(ret==false) {
//Check all row
for(int i=0;i<board.length;i++) {
char player=board[i][0];
if(player==' ')
continue;
win=true;
for(int j=0;j<board[0].length;j++) {
if(board[i][j]!=player) {
win=false;
break;
}
}
if(win==true)
break;
}
if(win==true)
ret=true;
}
if(ret==false) {
//Check all column
for(int j=0;j<board[0].length;j++) {
char player=board[0][j];
if(player==' ')
continue;
win=true;
for(int i=0;i<board.length;i++) {
if(board[i][j]!=player){
win=false;
break;
}
}
if(win==true)
break;
}
if(win==true)
ret=true;
}
if(ret==false) {
//Check Diagonal1
win=true;
char player=board[0][0];
if(player!=' ') {
for(int i=0;i<board.length;i++) {
if(player!=board[i][i]){
win=false;
break;
}
}
if(win==true)
ret=true;
}
}
if(ret==false) {
//Check Diagonal2
win=true;
int j=board[0].length-1;
char player=board[0][j];
if(player!=' ') {
for(int i=0;i<board.length;i++,j--) {
if(player!=board[i][j]){
win=false;
break;
}
}
if(win==true)
ret=true;
}
}
return ret;
}
public static void checkFullBoard(char[][]board){
boolean full=true;
for(int i=0;i<board.length;i++) {
for(int j=0;j<board[0].length;j++) {
if(board[i][j]!='O' || board[i][j]!='X')
full=false;
}
}
if(full==true) {
resetBoard(board);
drawBoard(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!");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.