Write a program for two players to play Tic-Tac-Toe. The board should be display
ID: 3588134 • Letter: W
Question
Write a program for two players to play Tic-Tac-Toe. The board should be displayed in the console, with empty board spaces represented by and underscore character. As each player takes their turn, they should be asked which row and column to place their marker, and the board should be updated and printed with the appropriate change. The game should end when either player gets three symbols in a row, column, or diagonal; or, it should end when all spaces are filled but neither player has won yet. 2 Specifications The program should start by printing an empty Tic-Tac-Toe board: Next, the "X" player should go first. The program should ask for the row and column in which to place the marker (start counting rows and columns from 1). For example, to place the "X in the center, the output and input would look like this: Player X's turn! Enter the row: 2 Enter the column:2 The state of the board should be stored in a 2-dimensional array. After every turn, the program should check to see if one of the players has won, or to report a draw. Your project should use only one class file, called TicTactoe, which contains all the subroutines used by the program. Be sure to clearly document your code! Any and all user input should be validated! Do not let the users try to overwrite the other player's pieces, or place a piece in an invalid board cell.Explanation / Answer
package chegg;
import java.util.Scanner;
public class TicTacToe {
int col,row; //declared two variable for row and column input from user
int count = 1; //counter for nine places of tic-tac-toe
char a[][] = new char[3][3]; //2D array for nine places
//main function
public static void main(String[] args) {
//create an object 't' of class TicTacToe
TicTacToe t = new TicTacToe();
t.BlankDisplay(); //call the method to show a blank tic-tac-toe with underscore
t.InsertMarker(); //call the method to insert markers
}
//displays the blank tic-tac-toe with underscore
public void BlankDisplay()
{
for(int i = 0;i<3;i++){
for(int j=0;j<3;j++)
{
a[i][j]='_';
System.out.print(" "+a[i][j]);
}
System.out.println();
}
}
//method to store value X or 0, given by the user one by one
public void InsertMarker()
{
while(count<=9){ //counter for nine values
if(count%2!=0){
Scanner sc = new Scanner(System.in);
System.out.println("Player X's turn!"); //input for X
System.out.println("Enter the row");
row = sc.nextInt();
row--;
System.out.println("Enter the column");
col = sc.nextInt();
col--;
for(int i = 0;i<3;i++){
for(int j=0;j<3;j++)
{//insertion of X at given location of user
if((row==i)&&(col==j)){
if(a[i][j]=='_'){
a[row][col]='X';
System.out.print(" "+a[row][col]);
}
else{//check for already filled position
System.out.println("Already filled, Choose another position");
count--;
}
}
else
System.out.print(" "+a[i][j]);
}
System.out.println();
}
count++;
}
else{
Scanner sc = new Scanner(System.in); //input for 0
System.out.println("Player 0's turn!");
System.out.println("Enter the row");
row = sc.nextInt();
row--;
System.out.println("Enter the column");
col = sc.nextInt();
col--;
for(int i = 0;i<3;i++){
for(int j=0;j<3;j++)
{//insertion of 0 at given location of user
if((row==i)&&(col==j)){
if(a[i][j]=='_'){
a[row][col]='0';
System.out.print(" "+a[row][col]);
}
else{//check for already filled position
System.out.println("Already filled, Choose another position");
count--;
}
}
else
System.out.print(" "+a[i][j]);
}
System.out.println();
}
count++;
}
if(count>5)
checkStatus();//call method to check who wins or draw
}
}
//method to check status of win or draw
public void checkStatus()
{
if((a[0][0]==a[0][1])&&(a[0][1]==a[0][2])){
System.out.println("First Row Complete. Player "+a[0][0]+" wins");
System.exit(0);
}
else if((a[1][0]==a[1][1])&&(a[1][1]==a[1][2])){
System.out.println("Second Row Complete. Player "+a[1][0]+" wins");
System.exit(0);
}
else if((a[2][0]==a[2][1])&&(a[2][1]==a[2][2])){
System.out.println("Third Row Complete. Player "+a[2][0]+" wins");
System.exit(0);
}
else if((a[0][0]==a[1][0])&&(a[1][0]==a[2][0])){
System.out.println("First Column Complete. Player "+a[0][0]+" wins");
System.exit(0);
}
else if((a[0][1]==a[1][1])&&(a[1][1]==a[2][1])){
System.out.println("Second Column Complete. Player "+a[0][1]+" wins");
System.exit(0);
}
else if((a[0][2]==a[1][2])&&(a[1][2]==a[2][2])){
System.out.println("Third Column Complete. Player "+a[0][2]+" wins");
System.exit(0);
}
else if((a[0][0]==a[1][1])&&(a[1][1]==a[2][2])){
System.out.println("First diagonal Complete. Player "+a[0][0]+" wins");
System.exit(0);
}
else if((a[0][2]==a[1][1])&&(a[1][1]==a[2][0])){
System.out.println("Second diagonal Complete. Player "+a[0][1]+" wins");
System.exit(0);
}
else if(count==9){
System.out.println("Its a draw");
System.exit(0);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.