Tic Tac Toe
Tic Tac Toe
import java.io.* ;
import java.util.* ;
public class TicTacToe
{
String name1 ;
String name2 ;
String sym1 ;
String sym2 ;
int[][] game = new int[3][3] ;
void show()
{
for(int i = 0 ; i < 3 ; i++)
{
System.out.print("|") ;
for(int j = 0 ; j < 3 ; j++)
{
switch(game[i][j])
{
case 0 :
System.out.print(" |") ;
break ;
case 1 :
System.out.print(" " + sym1 + " |") ;
break ;
case 2 :
System.out.print(" " + sym2 + " |") ;
break ;
}
}
System.out.println() ;
}
}
boolean legal(int m)
{
boolean ret = true ;
if(m < 0 || m > 8)
ret = false ;
if(m < 3)
if(game[0][m] > 0)
ret = false ;
else if(m >= 3 && m < 6)
if(game[0][m - 3] > 0)
ret = false ;
else if(m >= 6 && m < 9)
if(game[0][m - 6] > 0)
ret = false ;
return ret ;
}
int status()
{
int turn = 0;
boolean empty = false ;
for(int i = 0 ; i < 3 ; i++)
{
for(int j = 0 ; j < 3 ; j++)
{
if(game[i][j] == 0)
empty = true ;
}
}
if(!empty)
turn = 3 ;
if((game[0][0] == 1 && game[0][1] == 1 && game[0][2] == 1) ||
(game[1][0] == 1 && game[1][1] == 1 && game[1][2] == 1) ||
(game[2][0] == 1 && game[2][1] == 1 && game[2][2] == 1) ||
(game[0][0] == 1 && game[1][0] == 1 && game[2][0] == 1) ||
(game[0][1] == 1 && game[1][1] == 1 && game[2][1] == 1) ||
(game[0][2] == 1 && game[1][2] == 1 && game[2][2] == 1) ||
(game[0][0] == 1 && game[1][1] == 1 && game[2][2] == 1) ||
(game[0][2] == 1 && game[1][1] == 1 && game[2][0] == 1))
turn = 1 ;
if((game[0][0] == 2 && game[0][1] == 2 && game[0][2] == 2) ||
(game[1][0] == 2 && game[1][1] == 2 && game[1][2] == 2) ||
(game[2][0] == 2 && game[2][1] == 2 && game[2][2] == 2) ||
(game[0][0] == 2 && game[1][0] == 2 && game[2][0] == 2) ||
(game[0][1] == 2 && game[1][1] == 2 && game[2][1] == 2) ||
(game[0][2] == 2 && game[1][2] == 2 && game[2][2] == 2) ||
(game[0][0] == 2 && game[1][1] == 2 && game[2][2] == 2) ||
(game[0][2] == 2 && game[1][1] == 2 && game[2][0] == 2))
turn = 2 ;
return turn ;
}
void end()
{
show() ;
switch(status())
{
case 1 :
System.out.println("The winner is " + name1 +".") ;
break ;
case 2 :
System.out.println("The winner is " + name2 +".") ;
break ;
case 3 :
System.out.println("The game is a draw.") ;
break ;
}
System.exit(0) ;
}
public void play() throws Exception
{
InputStreamReader isr = new InputStreamReader(System.in) ;
BufferedReader br = new BufferedReader(isr) ;
Random rnd = new Random() ;
System.out.println("Welcome to Tic-Tac-Toe.") ;
System.out.println("This classic game is made for two players to play.") ;
System.out.println("The boxes are numbered as follows.") ;
System.out.println(" | 0 | 1 | 2 |") ;
System.out.println(" | 3 | 4 | 5 |") ;
System.out.println(" | 6 | 7 | 8 |") ;
System.out.println("To enter your symbol in a box, just type in its number.")
;
System.out.println("You play for a specified number of rounds.") ;
System.out.println("The player with more victories under his/her belt
wins.") ;
System.out.println("What's your name, Player 1?") ;
name1 = br.readLine() ;
System.out.print(name1 + ", enter your symbol (a char) : ") ;
sym1 = br.readLine() ;
boolean flag1 = true ;
while(flag1)
{
System.out.println("What's your name, Player 2?") ;
name2 = br.readLine() ;
System.out.print(name2 + ", enter your symbol (a char) : ") ;
sym2 = br.readLine() ;
if((name1.equals(name2)) || (sym1 == sym2))
System.out.println("Either the names or the symbols are same. Try again.") ;
else
flag1 = false ;
}
int stats = 0 ;
boolean first = rnd.nextBoolean() ;
boolean frstRun = true ;
while(status() == 0)
{
if(first)
{
if(frstRun)
{
System.out.println(name1 + " will start the game.") ;
frstRun = false ;
}
show() ;
boolean flag2 = true ;
while(flag2)
{
System.out.print(name1 + ", enter your move : ") ;
int move1 = Integer.parseInt(br.readLine()) ;
if(legal(move1))
{
flag2 = false ;
if(move1 < 3)
game[0][move1] = 1 ;
else if(move1 >= 3 && move1 < 6)
game[1][move1 - 3] = 1 ;
else
game[2][move1 - 6] = 1 ;
}
else
System.out.println("That move is not allowed.") ;
}
System.out.println(name1 + " has moved.") ;
if(status() > 0)
end() ;
show() ;
boolean flag3 = true ;
while(flag3)
{
System.out.print(name2 + ", enter your move : ") ;
int move2 = Integer.parseInt(br.readLine()) ;
if(legal(move2))
{
flag3 = false ;
if(move2 < 3)
game[0][move2] = 2 ;
else if(move2 >= 3 && move2 < 6)
game[1][move2 - 3] = 2 ;
else
game[2][move2 - 6] = 2 ;
}
else
System.out.println("That move is not allowed.") ;
}
System.out.println(name2 + " has moved.") ;
if(status() > 0)
end() ;
}
else
{
if(frstRun)
{
System.out.println(name2 + " will start the game.") ;
frstRun = false ;
}
show() ;
boolean flag2 = true ;
while(flag2)
{
System.out.print(name2 + ", enter your move : ") ;
int move2 = Integer.parseInt(br.readLine()) ;
if(legal(move2))
{
flag2 = false ;
if(move2 < 3)
game[0][move2] = 2 ;
else if(move2 >= 3 && move2 < 6)
game[1][move2 - 3] = 2 ;
else
game[2][move2 - 6] = 2 ;
}
else
System.out.println("That move is not allowed.") ;
}
System.out.println(name2 + " has moved.") ;
if(status() > 0)
end() ;
show() ;
boolean flag3 = true ;
while(flag3)
{
System.out.print(name1 + ", enter your move : ") ;
int move1 = Integer.parseInt(br.readLine()) ;
if(legal(move1))
{
flag3 = false ;
if(move1 < 3)
game[0][move1] = 1 ;
else if(move1 >= 3 && move1 < 6)
game[1][move1 - 3] = 1 ;
else
game[2][move1 - 6] = 1 ;
}
else
System.out.println("That move is not allowed.") ;
}
System.out.println(name1 + " has moved.") ;
if(status() > 0)
end() ;
}
}
}
public static void main(String[] args) throws Exception
{
TicTacToe ttt = new TicTacToe() ;
ttt.play() ;
}
}
SAMPLE OUTPUT:
Welcome to Tic-Tac-Toe.
This classic game is made for two players to play.
The boxes are numbered as follows.
|0|1|2|
|3|4|5|
|6|7|8|
To enter your symbol in a box, just type in its number.
You play for a specified number of rounds.
The player with more victories under his/her belt wins.
What's your name, Player 1?
Souvik
Souvik, enter your symbol (a char) : *
What's your name, Player 2?
Soumodip
Soumodip, enter your symbol (a char) : #
Souvik will start the game.
||||
||||
||||
Souvik, enter your move : 0
Souvik has moved.
|*|||
||||
||||
Soumodip, enter your move : 5
Soumodip has moved.
|*|||
|||#|
||||
Souvik, enter your move : 4
Souvik has moved.
|*|||
||*|#|
||||
Soumodip, enter your move : 7
Soumodip has moved.
|*|||
||*|#|
||#||
Souvik, enter your move : 9
That move is not allowed.
Souvik, enter your move : 8
Souvik has moved.
|*|||
||*|#|
||#|*|
The winner is Souvik.