import java.util.Scanner;
public class TicTacToe {
static char[][] tttBoard = new char[3][3];
static boolean done = false;
public static void main (String[] args){
for(int i = 0; itttBoard.length; i++){
for(int j = 0; jtttBoard[i].length; j++){
tttBoard[i][j]=' ';
}
}
System.out.print("The starting board is empty, spaces represent spots that have not yet been taken:\n");
printBoard();
while(!done){
System.out.println("\nPlayer Turn");
playerTurn();
printBoard();
checkWin();
System.out.println("\nComputer Turn");
comTurn();
printBoard();
checkWin();
}
}
public static void printBoard(){
for(int i = 0; itttBoard.length; i++){
for(int j = 0; jtttBoard[i].length; j++){
if(j==0){
System.out.print("\t");
}
if (j2){
System.out.print(" " + tttBoard[i][j] + " |");
}else System.out.print(" " + tttBoard[i][j] + " ");
}
if(i2){
System.out.println();
System.out.println("\t-----------");
}else System.out.println();
}
}
public static void comTurn(){
int row = (int)Math.random()*3;
int col = (int)Math.random()*3;
while(tttBoard[row][col]!=' '){
row = (int)(Math.random()*3);
col = (int)(Math.random()*3);
}
tttBoard[row][col] = 'O';
}
public static void playerTurn(){
Scanner input = new Scanner(System.in);
String x = "";
String y = "";
while(!(x.equals("1") || x.equals("2") || x.equals("3"))
|| !(y.equals("1") || y.equals("2") || y.equals("3"))){
System.out.print("Please enter the row number where you\n "
+ "would like to place an X (1-3):");
x = input.nextLine();
System.out.print("Please enter the column number where you\n "
+ "would like to place an X (1-3):");
y = input.nextLine();
}
while(tttBoard[Integer.parseInt(x) - 1][Integer.parseInt(y) - 1] != ' '){
System.out.print("Please enter the row number where you\n "
+ "would like to place an X that is empty (1-3):");
x = input.nextLine();
System.out.print("Please enter the column number where you\n "
+ "would like to place an X that is empty (1-3):");
y = input.nextLine();
while(!(x.equals("1") || x.equals("2") || x.equals("3"))
|| !(y.equals("1") || y.equals("2") || y.equals("3"))){
System.out.print("Please enter the row number where you\n "
+ "would like to place an X (1-3):");
x = input.nextLine();
System.out.print("Please enter the column number where you\n "
+ "would like to place an X (1-3):");
y = input.nextLine();
}
}
tttBoard[Integer.parseInt(x) - 1][Integer.parseInt(y) - 1] = 'X';
}
public static void checkWin(){
boolean comWin = false, playerWin = false, draw = false;
if( (tttBoard[0][0]=='X'tttBoard[0][1]=='X'tttBoard[0][2]=='X')
||( tttBoard[1][0]=='X'tttBoard[1][1]=='X'tttBoard[1][2]=='X')
||( tttBoard[2][0]=='XtttBoard[2][1]=='X'tttBoard[2][2]=='X')
||( tttBoard[0][1]=='X'tttBoard[1][1]=='X'tttBoard[2][1]=='X')
||( tttBoard[0][0]=='X'tttBoard[1][0]=='X'tttBoard[2][0]=='X')
||( tttBoard[0][2]=='X'tttBoard[1][2]=='X'tttBoard[2][2]=='X')
||( tttBoard[0][0]=='X'tttBoard[1][1]=='X'tttBoard[2][2]=='X')
||( tttBoard[0][2]=='X'tttBoard[1][1]=='X'tttBoard[2][0]=='X')){
playerWin = true;
done = true;
}
if( (tttBoard[0][0]=='O'tttBoard[0][1]=='O'tttBoard[0][2]=='O')
||( tttBoard[1][0]=='O'tttBoard[1][1]=='O'tttBoard[1][2]=='O')
||( tttBoard[2][0]=='O'tttBoard[2][1]=='O'tttBoard[2][2]=='O')
||( tttBoard[0][1]=='O'tttBoard[1][1]=='O'tttBoard[2][1]=='O')
||( tttBoard[0][0]=='O'tttBoard[1][0]=='O'tttBoard[2][0]=='O')
||( tttBoard[0][2]=='O'tttBoard[1][2]=='O'tttBoard[2][2]=='O')
||( tttBoard[0][0]=='O'tttBoard[1][1]=='O'tttBoard[2][2]=='O')
||( tttBoard[0][2]=='O'tttBoard[1][1]=='O'tttBoard[2][0]=='O')){
comWin = true;
done = true;
}
int spaceCharCount = 0;
for(int i = 0; i tttBoard.length; i++){
for(int j = 0; j tttBoard[i].length; j++){
if(tttBoard[i][j] == ' '){
spaceCharCount++;
}
}
}
if(spaceCharCount == 0 playerWin == false comWin == false){
draw = true;
done = true;
}
if(playerWin done){
System.out.println("Player Wins! Congratulations!");
System.exit(0);
}
if(comWin done){
System.out.println("Computer Wins! You Lose!");
System.exit(0);
}
if(draw done){
System.out.println("Draw!");
System.exit(0);
}
}
}