Question

In: Computer Science

Write a Java program to play the game Tic-Tac-Toe. Start off with a human playing a...

Write a Java program to play the game Tic-Tac-Toe. Start off with a human playing a human, so each player makes their own moves.

Follow the design below, creating the methods indicated and invoking them in the main program.

Use a char array of size 9 as the board; initialize with the characters 0 to 8 so that it starts out looking something like the board on the left.

0|1|2

3|4|5

6|7|8

and then as moves are entered the board looks like this

0|O|2

3|X|5

6|X|O

Make sure the board lines up properly so that the entries and borders all line up properly. DO NOT print a board that looks like or is similar to the output below where columns are misaligned.

0| O|2

3|X | 5

6 | X|O

You will need a variable to keep track of whose turn it is. Use a char variable named turn and initialize to X when the game starts. After a move, if there is no winner and no draw, switch to the O and continue to take turns as the game progresses. Declare additional variables as you build your program.

  1. After the variable declarations, begin a while loop which will keep the game going the user indicates to stop the game. (see a. below)
    1. When a new games starts, allow the user to terminate the program by entering an S or s (remember String has toLowerCase and toUpperCase methods). Any other entry will start a new game.
  2. If you are starting a new game, invoke a method to initialize the board and turn and another method to print the board
  3. Start an inner while loop that runs as long as the game isn’t over (a win or draw will terminate the game but not the program).
  4. Invoke a method to allow the user to make a move
    1. If there is no empty spot, (the game is a draw), print a message and ask the user whether to start a new game or terminate the program
    2. If the value entered is invalid (not 0 to 8), print a message and allow the user to re-enter a move
    3. If the user enters a value but it’s already taken, print a message and allow the user to retry
    4. If the user enters a value and it’s available, set the spot to X or O (depending upon the value of turn) and print the board
  5. After a valid move is made, invoke a method to check if the last one to make a move won the game
    • Winner - print the winner and start a new game
    • No winner - switch turn and ask for the next position

SAMPLE OUTPUT – NOTE OUTPUT BOLDED TO SHOW HANDLING OF BAD ENTRIES

Enter S to stop game, any other letter to play

x

Starting new game

0|1|2

3|4|5

6|7|8

Enter move, a number between 0 and 8

5

0|1|2

3|4|X

6|7|8

Enter move, a number between 0 and 8

5

Spot is taken, choose another

Enter move, a number between 0 and 8

0

O|1|2

3|4|X

6|7|8

Enter move, a number between 0 and 8

4

O|1|2

3|X|X

6|7|8

Enter move, a number between 0 and 8

2

O|1|O

3|X|X

6|7|8

Enter move, a number between 0 and 8

3

O|1|O

X|X|X

6|7|8

X is the winner

Enter S to stop game, any other letter to play

x

Starting new game

0|1|2

3|4|5

6|7|8

Enter move, a number between 0 and 8

0

X|1|2

3|4|5

6|7|8

Enter move, a number between 0 and 8

3

X|1|2

O|4|5

6|7|8

Enter move, a number between 0 and 8

9

9 is not a valid choice

Enter move, a number between 0 and 8

z

z is not a valid choice

Enter move, a number between 0 and 8

6

X|1|2

O|4|5

X|7|8

Enter move, a number between 0 and 8

1

X|O|2

O|4|5

X|7|8

Enter move, a number between 0 and 8

5

X|O|2

O|4|X

X|7|8

Enter move, a number between 0 and 8

8

X|O|2

O|4|X

X|7|O

Enter move, a number between 0 and 8

4

X|O|2

O|X|X

X|7|O

Enter move, a number between 0 and 8

2

X|O|O

O|X|X

X|7|O

Enter move, a number between 0 and 8

7

X|O|O

O|X|X

X|X|O

Game is a draw

Enter S to stop game, any other letter to play

Solutions

Expert Solution

Code -

Main.java-

import java.util.*;


public class Main
{
//initialize class variable
private int counter;
private char position[]=new char[10];
private char player;
Random r = new Random();
//main function
public static void main(String args[])
{
char ch;
Scanner in =new Scanner(System.in);
System.out.println ("Enter S to stop game, any other letter to play ");//after game complete ask player if he want to play again or not
ch=in.next().charAt(0);
if(ch == 's' || ch == 'S' )
System.exit(0);
//create object for toe class
Main Toe=new Main();
do{
//get new board
System.out.println("Starting new game");
Toe.new_board();
Toe.get_move();//get the move from player
System.out.println ("Enter S to stop game, any other letter to play ");//after game complete ask player if he want to play again or not

ch=in.next().charAt(0);
}while (ch!='s'&&ch!='S');
  
  
}
public void new_board()
{
//initialize new board with all values '1'
char posndef[] = {'0','1', '2', '3', '4', '5', '6', '7', '8'};
int i;
counter = 0;
player = 'X';
for (i=0; i<9; i++) position[i]=posndef[i];
print_board();
  
  
}
//method to print the board
public String print_board()
{
System.out.println(position [0]+"|"+position [1]+ "|" +position [2]);
System.out.println(position [3]+"|"+position [4]+"|" +position [5]);
System.out.println(position [6]+"|"+position [7]+"|" +position [8]);
return " print_board";
}
//method to ask user to get the mover the player O or X
public void get_move()
{
int spot;
char blank = ' ';
  
do {
// display current board-
  
System.out.println("Enter move, a number between 0 and 8 ");
print_board();
boolean posTaken = true;
while (posTaken) {
Scanner in =new Scanner (System.in);
spot=in.nextInt();
while(spot>8 || spot<0){
System.out.println(spot+" is not a valid choice");
System.out.println("Enter move, a number between 0 and 8 ");
spot=in.nextInt();
}
posTaken = check_position(spot);
if(posTaken==false)
position[spot]=get_player();
}
  
// display current board
if(player=='X')
print_board();   
  
if (player == 'O')
player = 'X';
else player = 'O';
}while ( check_win () == blank );
  
}
//fucntion- to calculate win
public char check_win ()
{
char Winner = ' ';
  
// condition to check if O wins
if (position[0] == 'O' && position[1] == 'O' && position[2] == 'O') Winner = 'O';
if (position[3] == 'O' && position[4] == 'O' && position[5] == 'O') Winner = 'O';
if (position[6] == 'O' && position[7] == 'O' && position[8] == 'O') Winner = 'O';
if (position[0] == 'O' && position[3] == 'O' && position[6] == 'O') Winner = 'O';
if (position[1] == 'O' && position[4] == 'O' && position[7] == 'O') Winner = 'O';
if (position[2] == 'O' && position[5] == 'O' && position[8] == 'O') Winner = 'O';
if (position[0] == 'O' && position[4] == 'O' && position[8] == 'O') Winner = 'O';
if (position[2] == 'O' && position[4] == 'O' && position[6] == 'O') Winner = 'O';
if (Winner == 'O' )
{System.out.println("Player O wins the game." );
return Winner;
}
  
// condtion to check if X wins
if (position[0] == 'X' && position[1] == 'X' && position[2] == 'X') Winner = 'X';
if (position[3] == 'X' && position[4] == 'X' && position[5] == 'X') Winner = 'X';
if (position[6] == 'X' && position[7] == 'X' && position[8] == 'X') Winner = 'X';
if (position[0] == 'X' && position[3] == 'X' && position[6] == 'X') Winner = 'X';
if (position[1] == 'X' && position[4] == 'X' && position[7] == 'X') Winner = 'X';
if (position[2] == 'X' && position[5] == 'X' && position[8] == 'X') Winner = 'X';
if (position[0] == 'X' && position[4] == 'X' && position[8] == 'X') Winner = 'X';
if (position[2] == 'X' && position[4] == 'X' && position[6] == 'X') Winner = 'X';
if (Winner == 'X' )
{
System.out.println( "Player X wins the game." );
return Winner; }
  
// check for Draw
for(int i=0;i<9;i++)
{
if(position[i]=='O' || position[i]=='X')
{
if(i==8)
{
char Draw='D';
System.out.println(" Game is draw ");
return Draw;
}
continue;
}
else
break;
  
}
  
return Winner;
}
//check for position that the position is already taken or not
public boolean check_position(int spot)
{
  
  
if ( position[spot] == 'O'||position[spot] == 'X')
{
if(player=='X')
System.out.println("Spot is taken, choose another");
return true;
}
else {
return false;
}
  
}
//method to get the current player
public char get_player()
{
return player;
}
  
}

pls do give a like , thank you


Related Solutions

Write a program that plays tic-tac-toe. The tic-tac-toe game is played on a 3 × 3...
Write a program that plays tic-tac-toe. The tic-tac-toe game is played on a 3 × 3 grid as shown below: The game is played by two players, who take turns. The first player marks moves with a circle, the second with a cross. The player who has formed a horizontal, vertical, or diagonal sequence of three marks wins. Your program should draw the game board, ask the user for the coordinates of the next mark (their move), change the players...
PYTHON (Game: Tic-tac-toe): Write a program that plays the tic-tac-toe game. Two players take turns clicking...
PYTHON (Game: Tic-tac-toe): Write a program that plays the tic-tac-toe game. Two players take turns clicking 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 in the grid have been filled with tokens and neither player has...
Write a LISP program to play the game Tic-Tac-Toe on a size 4x4 game board. Your...
Write a LISP program to play the game Tic-Tac-Toe on a size 4x4 game board. Your program must use min-max search and should be invoked by the function call: > (Tic-Tac-Toe) The game is single player, human vs the computer AI.
Write a program that allows two players to play a game of tic-tac-toe. Use a two-dimensional...
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 the row...
FileWrite a program that will allow two users to play a tic-tac-toe game. You should write...
FileWrite a program that will allow two users to play a tic-tac-toe game. You should write the program such that two people can play the game without any special instructions. (assume they know how to play the game). You should code the program to do the five items below only. You do not have to write the entire program, just the five items below. • Use a 2D array(3 rows, 3 columns) of datatype char. Initialize the 2D array with...
Python Code Needed Two-Player, Two-Dimensional Tic-Tac-Toe Write a script to play two-dimensional Tic-Tac-Toe between two human...
Python Code Needed Two-Player, Two-Dimensional Tic-Tac-Toe Write a script to play two-dimensional Tic-Tac-Toe between two human players who alternate entering their moves on the same computer. Create a 3-by-3 two-dimensional array. Each player indicates their moves by entering a pair of numbers representing the row and column indices of the square in which they want to place their mark, either an 'X' or an 'O'. When the first player moves, place an 'X' in the specified square. When the second...
The objective of this assignment is to implement the tic-tac-toe game with a C program. The...
The objective of this assignment is to implement the tic-tac-toe game with a C program. The game is played by two players on a board defined as a 5x5 grid (array). Each board position can contain one of two possible markers, either ‘X’ or ‘O’. The first player plays with ‘X’ while the second player plays with ‘O’. Players place their markers in an empty position of the board in turns. The objective is to place 5 consecutive markers of...
In the C++ programming language write a program capable of playing Tic-Tac-Toe against the user. Your...
In the C++ programming language write a program capable of playing Tic-Tac-Toe against the user. Your program should use OOP concepts in its design. You can use ASCII art to generate and display the 3x3 playing board. The program should randomly decide who goes first computer or user. Your program should know and inform the user if an illegal move was made (cell already occupied). The program should also announce if one of the players wins or if a draw...
In C# A Tic Tac Toe program that uses functions and arrays. The program should start...
In C# A Tic Tac Toe program that uses functions and arrays. The program should start by asking the player one for their name and then player two for their name. Then should print the board and ask the user what column and then what row they would like to put their x (or o, depending on the player) . Use the clear function to make it look as though the board is never repeating.. Thanks!
Programming assignment (100 pts): In the C++ programming language write a program capable of playing Tic-Tac-Toe...
Programming assignment (100 pts): In the C++ programming language write a program capable of playing Tic-Tac-Toe against the user. Your program should use OOP concepts in its design. You can use ASCII art to generate and display the 3x3 playing board. The program should randomly decide who goes first computer or user. Your program should know and inform the user if an illegal move was made (cell already occupied). The program should also announce if one of the players wins...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT