import java.util.*;
public class TicTacToe
{
private char[][] board;
/**
* Creates a board where each
* square holds the underline '_' character.
*/
public TicTacToe()
{
board = new char[3][3];
for (int row = 0; row < 3; row ++)
{
for (int col = 0; col < 3; col++)
{
board[row][col] = '_';
} // end of inner loop
} // end of outer loop
}
// Methods
public boolean set(int row, int col, char c)
{
if (row >= 3 || row < 0)
return false;
if (col >= 3 || col < 0)
return false;
if (board[row][col] != '_')
return false;
if ( !(c == 'X' || c == 'O'))
return false;
// assertion: row, col, c are valid
board[row][col] = c;
return true;
}
public char get(int row, int col)
{
return board[row][col];
}
/**
* Returns whether or not the board is full.
* @return False if any space in the board is '_', otherwise True
*/
public boolean isFull()
{
TicTacToe t = new TicTacToe();
t.isFull();
for (int row = 0; row < 3; row++)
{
for(int col = 0; col < 3; col++)
{
t.set(row, col, 'X');
}
}
t.isFull();
return true;
}
public char winnerVertical()
{
TicTacToe t = new TicTacToe();
t.winnerVertical();
t.set(0,0,'X');
t.set(1,0,'O');
t.set(2,0,'O');
t.winnerVertical();
t.set(0,1,'O');
t.set(1,1,'O');
t.set(2,1,'O');
t.print();
t.winnerVertical();
return '_';
}
public char winnerHorizontal()
{
TicTacToe t = new TicTacToe();
t.winnerHorizontal();
t.set(0,0,'X');
t.set(0,1,'O');
t.set(0,2,'O');
t.winnerHorizontal();
t.set(1,0,'O');
t.set(1,1,'O');
t.set(1,2,'O');
t.print();
t.winnerHorizontal();
return '_';
}
public char winnerDiagonal()
{
TicTacToe t = new TicTacToe();
t.winnerDiagonal();
t.set(0,0,'X');
t.set(1,1,'O');
t.set(2,2,'O');
t.winnerDiagonal();
t.set(0,2,'O');
t.set(1,1,'O');
t.set(2,0,'O');
t.print();
t.winnerDiagonal();
return '_';
}
public char winner()
{
return '_';
}
/**
* Plays a full game of TicTacToe.
* Assumes board is empty (variables are initialized
* but no moves have been made).
*/
public void play()
{
Scanner sc = new Scanner(System.in);
int row, col;
char currPlayer = 'X';
System.out.println("Enter -1 to quit at any time.");
while(!isFull() && winner() != 'X' && winner() != 'O')
{
try{
print();
System.out.println("Player "+currPlayer+"'s Turn");
System.out.print("Enter Row:");
System.out.println();
row = sc.nextInt();
if (row == -1){break;}
System.out.print("Enter Col:");
System.out.println();
col = sc.nextInt();
if (col == -1){break;}
if (!set(row,col,currPlayer))
{
throw new Exception("Error Setting.");
}
}catch(Exception e){
if(e instanceof InputMismatchException)
{
sc.nextLine();
}
System.out.println("Bad input");
if(currPlayer == 'X'){
currPlayer = 'O';
}
else{
currPlayer = 'X';
}
}
if(currPlayer == 'X'){
currPlayer = 'O';
}
else{
currPlayer = 'X';
}
}
sc.close();
print();
if (winner() == 'X')
{
System.out.println("Player X wins!!");
}
else if (winner() == 'O')
{
System.out.println("Player O wins!!");
}
else{
System.out.println("Everybody wins!! (Depending on how you look at it)");
}
}
/**
* Prints the state of the board.
*/
public void print()
{
int col;
System.out.println(" col");
System.out.print(" ");
for (col = 0; col < 3; col++)
{
System.out.print(col+" ");
}
System.out.println();
for (int row = 0; row < 3; row ++)
{
if(row == 0)
{
System.out.print("row "+row+" ");
}
else
{
System.out.print(" "+row+" ");
}
for (col = 0; col < 3; col++)
{
System.out.print(board[row][col] + " ");
} // end of inner loop
System.out.println();
} // end of outer loop
}
}