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.
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 |
Ans:-
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;
}
}NOTE: If u have any doubts please comment below and i am happily help to u brother
Write a Java program to play the game Tic-Tac-Toe. Start off with a human playing a...
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...
Write a c 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. (assue they know how to play the game). Prompt first player(X) to enter their first move. .Then draw the gameboard showing the move. .Prompt the second player(O) to enter their first move. . Then draw the gameboard showing both moves. And so on...through 9 moves. You will need to:...
You will create a PYTHON program for Tic-Tac-Toe game. Tic-Tac-Toe is normally played with two people. One player is X and the other player is O. Players take turns placing their X or O. If a player gets three of his/her marks on the board in a row, column, or diagonal, he/she wins. When the board fills up with neither player winning, the game ends in a draw 1) Player 1 VS Player 2: Two players are playing the game...
JAVA TIC TAC TOE - please help me correct my code Write a program that will allow two players to play a game of TIC TAC TOE. When the program starts, it will display a tic tac toe board as below | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 The program will assign X to Player 1, and O to Player The program will ask Player 1, to...
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...
Assignment C++: Rock-Scissor-Paper & Tic-Tac-Toe i need you to write two different program for RSP and TTT: R-S-P Requirement: - write one program that mimics the Rock-Scissor-Paper game. This program will ask user to enter an input (out of R-S-P), and the computer will randomly pick one and print out the result (user wins / computer wins). - User's input along with computer's random pick will be encoded in the following format: -- user's rock: 10 -- user's scissor: 20...
(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...
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) ...
In a game of Tic Tac Toe, two players take turns making 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 stalemate occurs when all the cells on the grid have been filled with tokens and neither player has achieved a win. Write a program...
In a game of Tic Tac Toe, two players take turns making 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 stalemate occurs when all the cells on the grid have been filled with tokens and neither player has achieved a win. Write a program...