public static void main(String[] args) { Scanner in = new Scanner(System.in); St
ID: 3553876 • Letter: P
Question
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String[][] board = {
{" ", " ", " "},
{" ", " ", " "},
{" ", " ", " "}
};
boolean done = false;
int player = 1;
int row = 0;
int col = 0;
while(done != true)
{
System.out.println("Enter the row and column for your next move");
row = in.nextInt();
col = in.nextInt();
if(player ==1)
{
board[row][col] = "X";
player = 2;
}
else
{
board[row][col] = "O";
player = 1;
}
printBoard(board);
}
}
public static void printBoard(String[][] boardValues)
{
for(int row = 0; row<3; row++)
{
for(int col=0;col<3;col++)
{
System.out.print("|"+boardValues[row][col] + "|");
}
System.out.println();
System.out.println("---|---|---");
Explanation / Answer
import java.util.Scanner;
public class ticTacToe{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String[][] board = {
{" ", " ", " "},
{" ", " ", " "},
{" ", " ", " "}
};
boolean done = false;
int player = 1;
int row = 0;
int col = 0;
while(done != true){
System.out.println("Enter the row and column for your next move");
row = in.nextInt();
col = in.nextInt();
if(player ==1){
board[row][col] = " X ";
player = 2;
}
else{
board[row][col] = " O ";
player = 1;
}
printBoard(board);
}
}
public static void printBoard(String[][] boardValues){
for(int row = 0; row<3; row++){
for(int col=0;col<3;col++){
System.out.print("|"+boardValues[row][col] + "|");
}
System.out.println();
}
System.out.println(" ---|---|---");
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.