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.
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!\n");
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] + "\n---------");
System.out.println(board[1][0] + " | " + board[1][1] + " | " +
board[1][2] + "\n---------");
System.out.println(board[2][0] + " | " + board[2][1] + " | " +
board[2][2] + "\n");
}
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;
}
}
Use Java language to create this program Write a program that allows two players to play...
18. Tic-Tac-Toe Game rite a program that allows two players to play a game of tic-tac-toe. Use dimensional char array with three rows and three columns as the game board. Each element of the array should be initialized with an asterisk (*). The program should run a loop that does the following: Write . 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...
Using C Programming: (use printf, scanf)
18. Tic-Tac-Toc Game Write a program that allows two players to play a game of tic-tac-toc. Use a two- dimensional char array with three rows and three columns 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...
I need screenshots for this solution done in Flowgorithm. Thank you. Tic-Tac-Toe Game Design a program that allows two players to play a game of tic-tac-toe. Use a two- dimensional String array with three rows and three columns as the game board. Each element of the array should be initialized with an asterisk (*). The program should run a loop that does the following: a. Displays the contents of the board array. b. Allows player 1 to select a location...
Please help me write a Pseudocode (please do NOT answer in JAVA or Python - I will not be able to use those). Please ALSO create a flowchart using Flowgarithm program. Thank you so much, if answer is provided in Pseudocode and Flowchart, I will provide good feedback and thumbs up to the resonder. Thank you! Tic-Tac-Toe Game Write a program that allows two players to play a game of tic-tac-toe. Use a two- dimensional char array with three rows...
Tic-Tac-Toe Game 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 columns as the game board. Each element of the array should be initialized with an asterisk (*). The program should run a loop that does the following: 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...
(Game: play a tic-tac-toe game) In a game of tic-tac-toe, two players take turns marking an available cell in a grid with their respective tokens (either X or O). When one player has placed three tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won. A draw (no winner) occurs when all the cells in the grid have been filled with tokens and neither player has achieved a win. Create...
Tic-Tac-Toe (arrays and multidimensional arrays) C++ Write an interactive program that plays tic-tac-toe. Represent the board as a 3 X 3 character array. Initialize the array to blanks and ask each player in turn to input a position. The first player's position is marked on the board with a O and the second player's position is marked with an X. Continue the process until a player wins or the game is a draw. To win, a player must have 3...
***Java Project***
Please upload the entire code and attach the screenshots of the
code. The screenshots help me to write the code, so please attach
that. Thank you so much. If you could use the comment to explain
the code, it would be perfect! Thank you so much~
Design and code a Swing GUl for a two-player tic-tac-toe (noughts and crosses) game on a 3 x 3 game board. The JFrame should use a BorderLayout with a JLabel in the...
Write a program to Simulate a game of tic tac toe. A game of tic tac toe has two players. A Player class is required to store /represent information about each player. The UML diagram is given below.Player-name: string-symbol :charPlayer (name:string,symbol:char)getName():stringgetSymbol():chargetInfo():string The tic tac toe board will be represented by a two dimensional array of size 3 by 3 characters. At the start of the game each cell is empty (must be set to the underscore character ‘_’). Program flow:1) ...
Objective: To write a program to allow a game of Tic Tac Toe to be played, and to determine when the game is over Complete the TicTacToe program below. It will allow for a full game of Tic Tac Toe between two players, and it will tell you who won or if the game is over with no winner. Details: The TicTacToe board is stored in a 3x3 multidimensional list of characters containing spaces at the start of the game...