Write a Java program that is a Tic Tac Toe game between two players. The code is
ID: 3566780 • Letter: W
Question
Write a Java program that is a Tic Tac Toe game between two players. The code is started below. import java.util.*; import java.util.*; public class Proj5 { public static void main(String[] args) { Scanner s = new Scanner(System.in); char[][] board = new char[3][3]; //YOU DO THIS //use a nested loop to fill board with '_' (blank) at each position for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { board[i][j] = '_'; } } //count how many moves have been made int moves = 0; //keep track of whose turn it is char turn = 'X'; System.out.println("Current board: "); //YOU DO THIS //Use a nested loop to print the board //keep playing while less than 9 moves while (moves < 9) { //YOU DO THIS //Ask the user for the row and the column //check to see that it is a valid move (row/col should be from 0-2, //and that value in the array should be '_' for empty) //If it is valid: //place the user's piece (turn) at that spot in the array //print the board (copy and paste the loops above that printed the initial board) //check for a winner boolean win = false; for (int i = 0; i < 3; i++) { //checks for three in a row if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] == turn) win = true; //checks for three in a column if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] == turn) win = true; } //YOU DO THIS //check for three on a diagonal (note that there are two diagonals) //Prints who won if (win) { System.out.println(turn + " wins!"); //end the game break; } //YOU DO THIS //switch whose turn it is //if turn is 'X' right now, make it 'O' //otherwise, make it 'X' //One more move has gone by moves++; } //if moves made it to 9, must be a tie if (moves == 9) System.out.println("Tie game."); } }
Explanation / Answer
// This program handles invalid input and asks user to input again without terminating the program. Play around with the code and do let me know if you have any issues.
import java.util.Scanner;
public class Proj5 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
char[][] board = new char[3][3];
//YOU DO THIS
//use a nested loop to fill board with '_' (blank) at each position
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
board[i][j] = '_';
}
}
//count how many moves have been made
int moves = 0;
//keep track of whose turn it is
char turn = 'X';
System.out.println("Current board: ");
//YOU DO THIS
//Use a nested loop to print the board
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(board[i][j]);
System.out.print(" ");
}
System.out.println();
}
System.out.println(); // enter a blank line
//keep playing while less than 9 moves
while (moves < 9) {
//YOU DO THIS
//Ask the user for the row and the column
//check to see that it is a valid move (row/col should be from 0-2,
//and that value in the array should be '_' for empty)
boolean validInput = false;
//If it is valid:
//place the user's piece (turn) at that spot in the array
while(!validInput){
System.out.print("Enter row (0-2): ");
int row = s.nextInt();
System.out.print("Enter column (0-2): ");
int column = s.nextInt();
if(row>=0 && row<=2 && column>=0 && column<=2){
if(board[row][column]=='_'){
board[row][column] = turn;
validInput = true;
}
else{
System.out.println("Position already filled, enter again");
}
}
else{
System.out.println("Invalid Position, enter again");
}
}
//print the board (copy and paste the loops above that printed the initial board)
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(board[i][j]);
System.out.print(" ");
}
System.out.println();
}
System.out.println(); // enter a blank line
//check for a winner
boolean win = false;
for (int i = 0; i < 3; i++) {
//checks for three in a row
if (board[i][0] == board[i][1] &&
board[i][1] == board[i][2] &&
board[i][0] == turn) win = true;
//checks for three in a column
if (board[0][i] == board[1][i] &&
board[1][i] == board[2][i] &&
board[0][i] == turn) win = true;
}
//YOU DO THIS
//check for three on a diagonal (note that there are two diagonals)
if(board[0][0]==board[1][1] &&
board[1][1]==board[2][2] &&
board[2][2]== turn) win = true;
if(board[0][2]==board[1][1] &&
board[1][1]==board[2][0] &&
board[2][0]== turn) win = true;
//Prints who won
if (win) {
System.out.println(turn + " wins!");
//end the game
break;
}
//YOU DO THIS
//switch whose turn it is
//if turn is 'X' right now, make it 'O'
//otherwise, make it 'X'
if(turn=='X')
turn = 'O';
else if(turn=='O')
turn = 'X';
//One more move has gone by
moves++;
}
//if moves made it to 9, must be a tie
if (moves == 9) System.out.println("Tie game.");
}
}
=====================Output========================
Current board:
_ _ _
_ _ _
_ _ _
Enter row (0-2): 0
Enter column (0-2): 0
X _ _
_ _ _
_ _ _
Enter row (0-2): 0
Enter column (0-2): 5
Invalid Position, enter again
Enter row (0-2): 0
Enter column (0-2): 0
Position already filled, enter again
Enter row (0-2): 0
Enter column (0-2): 1
X O _
_ _ _
_ _ _
Enter row (0-2): 1
Enter column (0-2): 1
X O _
_ X _
_ _ _
Enter row (0-2): 2
Enter column (0-2): 1
X O _
_ X _
_ O _
Enter row (0-2): 1
Enter column (0-2): 1
Position already filled, enter again
Enter row (0-2): 2
Enter column (0-2): 2
X O _
_ X _
_ O X
X wins!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.