I need to make a java based tic tac toe game, that is player vs computer, but i am unsure how to implement the random number by the computer onto the board.
import java.io.*;
import java.util.*;
class TTT
{
public static void main(String args[])
{
int a,b,x,y;
int person,row,col,count=0;
char pl;
char[][] array=
{
{' ',' ',' '},
{' ',' ',' '},
{' ',' ',' '},
};
Random generator = new Random();
int r = generator.nextInt(3);
do
{
for(a=0;a<4;a++)
{
System.out.println("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
for(b=0;b<2;b++)
{
if(b==1)
{
pl='o';
}
else
{
pl='x';
}
if(b==1)
{
person=1;
}
else
{
person=2;
}
System.out.println(" 0 1 2");
for(x=0;x<3;x++)
{
System.out.print(x);
for(y=0;y<3;y++)
{
System.out.print("["+array[x][y] +"]");
}
System.out.println();
}
boolean invalidcoordinates = false;
do{
count=count+1;
System.out.println("Enter Row(0,1,2)");
Scanner scan = new Scanner(System.in);
row=scan.nextInt();
System.out.println("Enter Column(0,1,2)");
col=scan.nextInt();
invalidcoordinates = array[row][col] != ' ';
}while(invalidcoordinates);
System.out.println(row+" "+col);
array[row][col]=pl;
System.out.println(" 0 1 2");
for(x=0;x<3;x++)
{
System.out.print(x);
for(y=0;y<3;y++)
{
System.out.print("["+array[x][y] +"]");
}
System.out.println();
}
if(array[0][0]==array[0][1]&&array[0][1]==array[0][2]&&array[0][1]!=' ')
{
System.out.println("WINNER");
System.out.println("Thank you for playing Tic Tac Toe");
System.exit(1);
}
if(array[1][0]==array[1][1]&&array[1][1]==array[1][2]&&array[1][0]!=' ')
{
System.out.println("WINNER");
System.out.println("Thank you for playing Tic Tac Toe");
System.exit(1);
}
if(array[2][0]==array[2][1]&&array[2][1]==array[2][2]&&array[2][0]!=' ')
{
System.out.println("WINNER");
System.out.println("Thank you for playing Tic Tac Toe");
System.exit(1);
}
if(array[0][0]==array[1][1]&&array[1][1]==array[2][2]&&array[0][0]!=' ')
{
System.out.println("WINNER");
System.out.println("Thank you for playing Tic Tac Toe");
System.exit(1);
}
if(array[0][2]==array[1][1]&&array[1][1]==array[2][0]&&array[0][2]!=' ')
{
System.out.println("WINNER");
System.out.println("Thank you for playing Tic Tac Toe");
System.exit(1);
}
if(array[0][0]==array[1][0]&&array[1][0]==array[2][0]&&array[2][0]!=' ')
{
System.out.println("WINNER");
System.out.println("Thank you for playing Tic Tac Toe");
System.exit(1);
}
if(array[0][1]==array[1][1]&&array[1][1]==array[2][1]&&array[2][1]!=' ')
{
System.out.println("WINNER");
System.out.println("Thank you for playing Tic Tac Toe");
System.exit(1);
}
}
}
}while (count<9);
}
}