Source Code Sample
/************************************************************************/
/* NAME: Ricky Shah */
/* STUDENT ID: 38104916 */
/* EMAIL: shahrr@uci.edu */
/* TA NAME: Akira Hatanaka */
/* Program: Tic Tac Toe */
/************************************************************************/
//This is the Tic Tac Toe Game class
public class TicTacToe
{
private static char[][] plays= new char[3][3];
private static char turn; //private variables
private static int numPlays=0;
//Precondition: None
//Postcondition: all variables are cleared along with the board
public TicTacToe()
{
clearBoard(); //the default constructor clears teh board, setting everything to zero
}
//Precondition: None
//Postcondition: all variables are cleared along with the board
public void clearBoard()
{
for(int row=0; row<3; row++)
for(int col=0; col<3; col++)
{
plays[row][col]=' '; //goes through all nine spaces in array
} //and replaces data with a ' '
turn='X'; //sets the intial player to X
numPlays=0; //set the total number of plays on the board to zero
}
//Precondition: None
//Postcondition: displays the board on screen
public void printBoard()
{
System.out.println("(R/C) 1 2 3");
System.out.println(" -------------------------");
System.out.println(" | | | |");
System.out.println(" 1 | "+plays[0][0]+" | "+plays[0][1]+" | "+plays[0][2]+" |");
System.out.println(" |_______|_______|______ |");
System.out.println(" | | | |");
System.out.println(" 2 | "+plays[1][0]+" | "+plays[1][1]+" | "+plays[1][2]+" |");
System.out.println(" |_______|_______|______ |");
System.out.println(" | | | |");
System.out.println(" 3 | "+plays[2][0]+" | "+plays[2][1]+" | "+plays[2][2]+" |");
System.out.println(" | | | |");
System.out.println(" -------------------------");
}
//Precondition: None
//Postcondition: returns true if there is a winner, or else false
private boolean isThereWinner()
{
int row, col;
//checks to see if one player has whole row completed
for(row=0; row<3; row++)
{
if ((plays[row][0]==plays[row][1] && plays[row][0]==plays[row][2]) && plays[row][0]!=' ')
return true;
}
//cheacks to see it one player has whole column completed
for(col=0; col<3; col++)
{
if ((plays[0][col]==plays[1][col] && plays[0][col]==plays[2][col]) && plays[2][col]!=' ')
return true;
}
//checks is one player completed either diagonal
if ((plays[0][0]==plays[1][1] && plays[0][0]==plays[2][2]) && plays[0][0]!=' ')
return true;
if ((plays[0][2]==plays[1][1] &&plays[0][2]==plays[2][0]) && plays[0][2]!=' ')
return true;
return false;
}
//Precondition: None
//Postcondition: Controls entire game
public void takeUserInput()
{
char choice;
int moveRow, moveCol;
if (numPlays == 9) //if the board is full, end the game
{
System.out.println("Board Full. No Winner.");
return;
}
else
{
System.out.println("It's "+ whoseTurn()+"'s turn. ");
System.out.println("Where would you like to make your move? ");
do{
System.out.print("Row: ");
//precondition for readInt is that only integers are entered
moveRow=SavitchIn. readInt( );
System.out.print("Col: ");
moveCol=SavitchIn. readInt( );
if (moveRow>3 || moveRow<1 || moveCol>3 || moveCol<1) //checks to make sure point is on board
System.out.println("Out of bounds. Enter 1, 2 or 3 only");
}
while(moveRow>3 || moveRow<1 || moveCol>3 || moveCol<1);
if (plays[moveRow-1][moveCol-1]!=' ') //makes sure that move is only placed on empty space
{
do{
System.out.println("Error: Space is taken. Try another space");
do{
System.out.print("Row: ");
moveRow=SavitchIn. readInt( );
System.out.print("Col: ");
moveCol=SavitchIn. readInt( );
if (moveRow>3 || moveRow<1 || moveCol>3 || moveCol<1)
System.out.println("Out of bounds. Enter 1, 2 or 3 only");
}while(moveRow>3 || moveRow<1 || moveCol>3 || moveCol<1);
}while (plays[moveRow-1][moveCol-1]!=' ');
}
System.out.println("You chose Row: "+ moveRow);
System.out.println("You chose Col: "+ moveCol); //confirms input of user
plays[moveRow-1][moveCol-1]=whoseTurn(); //sets data in array
numPlays++; //updates the number of plays
printBoard();
if (isThereWinner()) //if there is a winner, the winner is printed then exits
{
System.out.println();
System.out.println("********"+whoseTurn()+ " is the winner. *********");
System.out.println();
return;
}
else
nextTurn(); //or else is switched players
}
}
//Precondition: None
//Postcondition: return whose turn it is
public char whoseTurn()
{
return turn; //returns the private varialbe turn
}
//Precondition: None
//Postcondition: changes whose turn is it and then calls the takeuser input function
private void nextTurn()
{
if (turn=='X')
turn='O';
else
turn='X';
takeUserInput();
}
}