Hey here is my code, i have the main class (TicTacToeApplication) and the TTTboard class
import java.util.Scanner;
public class TicTacToeApplication {
public static void main(String[] args)
{
// declare variables including our TTT board
TTTBoard myGame = new TTTBoard();
Scanner input = new Scanner(System.in);
int row;
int col;
while(myGame.determineWinner() == 0 && !myGame.isBoardComplete())
{
myGame.displayBoard();
System.out.println("Player " + myGame.getCurrentPlayer());
System.out.println("Make your move.");
System.out.print("Row please (1-3):");
row = input.nextInt();
while(row < 1 || row > 3)
{
System.out.println("Invalid Row.");
System.out.print("Try again (1-3):");
row = input.nextInt();
}
System.out.print("Col please (1-3):");
col = input.nextInt();
while(col < 1 || col > 3)
{
System.out.println("Invalid Col.");
System.out.print("Try again (1-3):");
col = input.nextInt();
}
// while the move is invalid make them make another move
while(!myGame.makeMove(row, col))
{
System.out.println("Invalid Move... Try Again.");
System.out.print("Row please (1-3):");
row = input.nextInt();
// error trap for valid row
while(row < 1 || row > 3)
{
System.out.println("Invalid Row.");
System.out.print("Try again (1-3):");
row = input.nextInt();
}
System.out.print("Col please (1-3):");
col = input.nextInt();
// error trap for valid col
while(col < 1 || col > 3)
{
System.out.println("Invalid Col.");
System.out.print("Try again (1-3):");
col = input.nextInt();
}
}
}
// if we left the loop because the boards full and there's no winner
// it must be a cats game
if (myGame.determineWinner() == 0)
{
System.out.println("Sorry - Cat's Game");
}
else
{
System.out.print("The Winner is Player ");
if (myGame.getCurrentPlayer() == 1)
{
System.out.println("2");
}
else
{
System.out.println("1");
}
}
}
}
public class TTTBoard
{
private int [][] _board;
private int _player;
public TTTBoard ()
{
_player = 0;
_board = new int [3][3];
for (int row = 0; row < 3; row++)
{
for(int column = 0; column < 3; column++)
{
_board[row][column] = 0;
}
}
}
public boolean makeMove( int row, int col)
{
row = row - 1;
col = col - 1;
// Checks to see if board location is occupied and a move can be made
if (_board[row][col] == 0)
{
_board[row][col] = _player;
return false;
}
else
{
return true;
}
}
public boolean isBoardComplete ()
{
for (int row = 0; row < 3; row++)
{
for (int column = 0; column <3; column++)
{
if (_board [row][column] == 0)
{
return false;
}
}
}
return true;
}
public int determineWinner ()
{
// First check rows and columns
int winner = 0;
// Check for winner in row 1
if (_board[0][0] == _board[0][1] && _board[0][0] == _board[0][2] &&
_board[0][0] != 0)
{
winner = _board[0][0];
}
// Check for winner in row 2
if (_board[1][0] == _board[1][1] && _board[1][0] == _board[1][2] &&
_board[1][0] != 0)
{
winner = _board[1][0];
}
// Check for winner in row 3
if (_board[2][0] == _board[2][1] && _board[2][0] == _board[2][2] &&
_board[2][0] != 0)
{
winner = _board[2][0];
}
// Check for winner in col 1
if (_board[0][0] == _board[1][0] && _board[0][0] == _board[2][0] &&
_board[0][0] != 0)
{
winner = _board[0][0];
}
// Check for winner in col 2
if (_board[0][1] == _board[1][1] && _board[0][1] == _board[2][1] &&
_board[0][1] != 0)
{
winner = _board[0][1];
}
// Check for winner in col 3
if (_board[0][2] == _board[1][2] && _board[0][2] == _board[2][2] &&
_board[0][2] != 0)
{
winner = _board[0][2];
}
// Check for winner in first diagonal
if (_board[0][0] == _board[1][1] && _board[0][0] == _board[2][2] &&
_board[0][0] != 0)
{
winner = _board[0][0];
}
// Check for winner in 2nd diagonal
if (_board[2][0] == _board[1][1] && _board[2][0] == _board[0][2] &&
_board[2][0] != 0)
{
winner = _board[2][0];
}
return winner;
}
public void displayBoard()
{
System.out.println();
for (int r=0; r<_board.length; r++)
{
for (int c=0; c<_board[r].length; c++)
{
System.out.print(" " + _board[r][c]);
}
System.out.println("");
}
}
public int getCurrentPlayer ()
{
if (_player == 0)
{
return _player = 1;
}
if (_player == 1)
{
return _player = 2;
}
else
{
return _player = 1;
}
}
}