Question

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

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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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;
}

}

Add a comment
Know the answer?
Add Answer to:
Use Java language to create this program Write a program that allows two players to play...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 18. Tic-Tac-Toe Game rite a program that allows two players to play a game of tic-tac-toe. Use di...

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

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

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

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

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

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

  • ***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 comme...

    ***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 in c#

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

  • Java project In a game of tic-tac-toe, two players take turns marking an available cell in...

    Java project In a game of tic-tac-toe, two players take turns marking an available cell in a 3 x 3 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 on the grid have been filled with tokens and neither player has achieved a win. Create...

  • (Tic-Tac-Toe) Create a class Tic-Tac-Toe that will enable you to write a program to play Tic-Tac-Toe....

    (Tic-Tac-Toe) Create a class Tic-Tac-Toe that will enable you to write a program to play Tic-Tac-Toe. The class contains a private 3-by-3 two-dimensional array. Use an enumeration to represent the value in each cell of the array. The enumeration’s constants should be named X, O and EMPTY (for a position that does not contain an X or an O). The constructor should initialize the board elements to EMPTY. Allow two human players. Wherever the first player moves, place an X...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT