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

Use Java language to create this program Write a program that allows two players

ID: 3882389 • Letter: U

Question

Use Java language to create this program

Write a program that allows two players to play a game of tic-tac-toe. Using a two-dimensional array with three rows and three columns as the game board. Each element of the array should be initialized with a number from 1 - 9 (like below):

1 2 3

4 5 6

7 8 9

The program should run a loop that

Displays the contents of the board array

allows player 1 to select the location on the board for an X

allows player 2 to select the location on the board for an O

determine 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.

A player wins when they have three in a row on a game board. They can appear in a row, column or diagonally across the board. A tie occurs when all the locations are filed but there is no winner.

Explanation / Answer

import java.util.Scanner;

public class TicTacToe {

public static void main(String[] args)
{

char[][] board = {{'1','2','3'}, {'4','5','6'}, {'7', '8', '9'}};

int play;

char player = 'X';

char cpu = 'O';

int rowNumber;

int columnNumber;

int playerORow;
int playerOcolumn;

System.out.println("Welcome to tic, tac, toe! ");

System.out.println("Do you wish to play? 1 for yes, 2 for no ");

Scanner input = new Scanner(System.in);

play = input.nextInt();

if(play != 1) {
System.out.print("Invalid input, game will now EXIT thanks for playing!");
System.exit(0);
}

displayBoard(board);
System.out.println("You are limited to X's only, good luck!");
System.out.println("Please enter row (0-3) of your move: ");
rowNumber = input.nextInt();
System.out.println("Please enter column (1-3); of your move: ");
columnNumber = input.nextInt();

System.out.println("Please enter row (0-3) for player O: ");
playerORow = input.nextInt();
System.out.println("Please enter column (1-3); of your move: ");
playerOcolumn = input.nextInt();


if(board[rowNumber][columnNumber] != 'X' && board[rowNumber][columnNumber] != 'O')
{
board[rowNumber][columnNumber] = player;
}
else {

}
makeAMove(board, player);
hasWon(board, player);
boardFull(board);
}
public static void displayBoard(char[][] board)
{
  
System.out.println(board[0][0] + " | " + board[0][1] + " | " + board[0][2] + " ---------");
System.out.println(board[1][0] + " | " + board[1][1] + " | " + board[1][2] + " ---------");
System.out.println(board[2][0] + " | " + board[2][1] + " | " + board[2][2] + " ");

}


public static void makeAMove(char[][] board, char player)
{
displayBoard(board);


}
public static boolean hasWon(char[][] board, char player)
{

Check if the player has won by checking winning conditions.
if (board[0][0] == player && board[0][1] == player && board[0][2] == player ||
board[1][0] == player && board[1][1] == player && board[1][2] == player ||
board[2][0] == player && board[2][1] == player && board[2][2] == player ||
board[0][0] == player && board[1][0] == player && board[2][0] == player ||
board[0][1] == player && board[1][1] == player && board[2][1] == player ||
board[0][2] == player && board[1][2] == player && board[2][2] == player ||
board[0][0] == player && board[1][1] == player && board[2][2] == player ||
board[2][0] == player && board[1][1] == player && board[0][2] == player)

return true;

else {

return false;
}

}

public static boolean boardFull(char [][] board)
{

if (board[0][0] != '1' && board[0][1] != '2' && board[0][2] != '3' &&
board[1][0] != '4' && board[1][1] != '5' && board[1][2] != '6' &&
board[2][0] != '7' && board[2][1] != '8' && board[2][2] != '9')

return true;

else {

return false;
}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote