Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA PROGRAMMING. 5. TicTacToeGame Write a program that allows two players to pl

ID: 3866102 • Letter: J

Question

JAVA PROGRAMMING.

5. TicTacToeGame Write a program that allows two players to play a game of tic-tac-toe. Use a two• dimensional char array with three rows and three column s as the game board. Each element of the array should be initialized with an asterisk (*). The program should run a loop that • Displays the contents of the board array • Allows player 1 to select a location on the board for an X. The program should ask the user to enter the row and column number. • Allows player 2 to select a location on the board for an O. The program should ask the user to enter the row and column number. • Determines whether a player has won, or a tie has occurred. If a player has won, the program should declare that player the winner and end. If a tie has occurred, the program should say so and end. Player 1 wins when there are three Xs in a row on the game board. The Xs can appear in a row, in a column, or diagonally across the board. A tie occurs when all of the locations on the board are full, but there is no winner.

Explanation / Answer

import java.util.Scanner;

public class Main {

public static void main(String[] args) {
char player='o';
int row,column;
Scanner k = new Scanner (System.in);
Game g = new Game ();

g.initializeGame();
System.out.println("Game ready ! ");

while(true)
{   
player=g.changePlayer(player);
System.out.print(" "+player +" ,choose your location (row, column):");
row=k.nextInt();
column=k.nextInt();

while (g.checkIfLegal(row,column))
{
System.out.println("That slot is already taken or out of bounds, please try again (row, column).");
g.displayBoard();
row=k.nextInt();
column=k.nextInt();
}
g.changeBoard(player,row,column );
g.displayBoard();

if(g.checkIfWinner())
{
System.out.println(" The winner is "+ player+" !");
break;

}
if(g.checkIfTie())
{
System.out.println(" The game is a tie !");
break;
}
}


}

}

public class Game {

char [][] table = new char[3][3];

public void initializeGame()

{
for (int i = 0; i < 3; i++)
for (int p=0; p < 3; p++)
table [i][p]= ' ';
}

public boolean checkIfLegal(int row, int column)

{
if( (row>2 || column>2) || (row<0 || column <0) )
return true;

else if(table[row][column]=='x' || table[row][column]=='o')
return true;

return false;
}

public void changeBoard(char player, int row, int column)

{
table[row][column]=player;

}


public void displayBoard()
{

System.out.println(" 0 " + table[0][0] + "|" + table[0][1] + "|" + table[0][2]);
System.out.println(" --+-+--");
System.out.println(" 1 " + table[1][0] + "|" + table[1][1] + "|" + table[1][2]);
System.out.println(" --+-+--");
System.out.println(" 2 " + table[2][0] + "|" + table[2][1] + "|" + table[2][2]);
System.out.println(" 0 1 2 ");
}


public char changePlayer(char player) {
char newTurn='e';
if (player == 'o')
newTurn='x';
if (player == 'x')
newTurn='o';
return newTurn;
}


public boolean checkIfWinner() {
if( table [0][0]==table[1][0] && table[1][0] == table[2][0] && (table [0][0]=='x' || table [0][0]=='o'))
return true;
else if( table [0][1]==table[1][1] && table[1][1] == table[2][1] && (table [0][1]=='x' || table [0][1]=='o'))
return true;
else if( table [0][2]==table[1][2] && table[1][2] == table[2][2] && (table [0][2]=='x' || table [0][2]=='o'))
return true;
else if( table [0][0]==table[0][1] && table[0][1] == table[0][2] && (table [0][0]=='x' || table [0][0]=='o'))
return true;
else if( table [1][0]==table[1][1] && table[1][1] == table[1][2] && (table [1][0]=='x' || table [1][0]=='o'))
return true;
else if( table [2][0]==table[2][1] && table[2][1] == table[2][2] && (table [2][0]=='x' || table [2][0]=='o'))
return true;
else if( table [0][0]==table[1][1] && table[1][1] == table[2][2] && (table [0][0]=='x' || table [0][0]=='o'))
return true;
else if( table [2][0]==table[1][1] && table[1][1] == table[0][2] && (table [2][0]=='x' || table [2][0]=='o'))
return true;
else
return false;
}


public boolean checkIfTie() {
for (int i = 0; i < 3; i++)
for (int p=0; p < 3; p++)
if(table [i][p]==' ')
return false;

return true;
}

}