In: Computer Science
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 |
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