In: Computer Science
Game of Tic Tac Toe with the following conditions
A point system where a move that leads to a winning game is given 1 point, a move that leads to a tie is given 0 point, and a lead to a losing game will get -1 point.
Grid size of 5x5
A timer that can be set for how long a game can be played
5 symbols in a row to get a point
Connected lines cannot be crossed (No diagonal lines)
The game ends when there is a tie or the timer runs out
The winner is the one with the most
import java.util.Scanner;
public class TicTakToe {
static char presentChoice='-';
static char
game[][]={{'-','-','-','-','-'},{'-','-','-','-','-'},{'-','-','-','-','-'},{'-','-','-','-','-'},{'-','-','-','-','-'}};
static int count=0,m,n,m1,n1,m2,n2;
static Scanner sc;
//driver method to run the game
public static void main(String[] args)
{
System.out.println("*******The Time Remaining to
finish the game is 5 min*******\n");
System.out.println("User1 - '0' & User2 -
'X'");
sc=new Scanner(System.in);
display(game);
while(true)
{
while(true) if(input(1,'0')) break;
display(game);
gameFinishCheck();
while(true) if(input(2,'X')) break;
display(game);
gameFinishCheck();
if(count==10) break;
}
while(true){
while(true) if(move(1))
break;
display(game);
presentChoice='0';
gameFinishCheck();
while(true) if(move(2))
break;
display(game);
presentChoice='X';
gameFinishCheck();
}
}
//to check game has finished or not
private static void gameFinishCheck() {
if(validate(game,presentChoice))
{
if(presentChoice=='0')
System.out.println("User1 won the game");
else
System.out.println("User2 won the game");
System.exit(0);
}
}
//to move the coin from one place to another place
private static boolean move(int i){
try{
char oldplace,newplace;
System.out.println("\n\tUser"+i+" Turn");
while(true){
System.out.print("Select a position to move (row &
column)\t:");
m1=Integer.parseInt(sc.next());
n1=Integer.parseInt(sc.next());
if(movevalidate(m1,n1,game) &&
crctChoiceValidataion(i,game[m1][n1]))
{
oldplace=game[m1][n1];
break;
}
else
{
System.out.println("\nEnter correct
position\n");
}
}
while(true)
{
System.out.print("Enter new
position (row & column)\t\t:");
m2=Integer.parseInt(sc.next());
n2=Integer.parseInt(sc.next());
if(validate(m2,n2,game))
{
newplace=game[m2][n2];
break;
}
else
{
System.out.println("\nEnter correct position\n");
}
}
game[m1][n1]=newplace;
game[m2][n2]=oldplace;
return true;
}catch(Exception e){
System.out.println("\nEnter correct
input\n");
return false;
}
}
//to check weather user is picking correct choice for moving or
not
private static boolean crctChoiceValidataion(int i, char c) {
if(i==1 && c=='0') return true;
else if(i==2 && c=='X') return true;
else return false;
}
//to place the coin
private static boolean input(int i,char c){
try{
System.out.println("\n\tUser"+i+" Turn");
System.out.print("Enter position (row &
column)\t:");
m=Integer.parseInt(sc.next());
n=Integer.parseInt(sc.next());
if(validate(m,n,game)){
game[m][n]=c;
presentChoice=c;
count++;
return true;
}
else{
System.out.println("\nEnter correct
position\n");
return false;
}
}catch(Exception e){
System.out.println("\nEnter correct
input\n");
return false;
}
}
//this is to check game finished or not
private static boolean validate(char[][] game,char c) {
boolean result=false;
if((game[0][0]==c&&game[0][1]==c&&game[0][2]==c&&game[0][3]==c&&game[0][4]==c))
result= true;
if((game[1][0]==c&&game[1][1]==c&&game[1][2]==c&&game[1][3]==c&&game[1][4]==c))
result= true;
if((game[2][0]==c&&game[2][1]==c&&game[2][2]==c&&game[2][3]==c&&game[2][4]==c))
result= true;
if((game[0][0]==c&&game[1][0]==c&&game[2][0]==c&&game[3][0]==c&&game[4][0]==c))
result= true;
if((game[0][1]==c&&game[1][1]==c&&game[2][1]==c&&game[3][1]==c&&game[4][1]==c))
result= true;
if((game[0][2]==c&&game[1][2]==c&&game[2][2]==c&&game[3][2]==c&&game[4][2]==c))
result= true;
return result;
}
//to validate user input
private static boolean validate(int m, int n, char[][] game)
{
if(m<0|| n<0||m>4
||n>4||game[m][n]=='X'||game[m][n]=='0') return false;
else return true;
}
//to validate user move
private static boolean movevalidate(int m, int n, char[][] game)
{
if(m<0|| n<0||m>4 ||n>4) return
false;
else return true;
}
//to print the game board
private static void display(char[][] game) {
System.out.println("\n------------------------------------\n");
for(int i=0;i<5;i++){
for(int j=0;j<5;j++){
System.out.print(game[i][j]);
if(j!=5) System.out.print("\t");
}
System.out.println("\n");
}
System.out.println("------------------------------------");
}
}