Post by Josh on Feb 10, 2010 16:02:30 GMT -8
Hi all! I am racking my brain trying to figure out why my code is not working for this TicTacToe board assignment. I keep getting the following error:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at TicTacToe.makeMove(TicTacToe.java:33)
at TicTacToeTest.main(TicTacToeTest.java:72)
The assignment calls for a 3-by-3 2-dimensional array of integers. The constructor should initialize the empty board to all zeros (which I've done). Allow 2 human players. Wherever the first player moves, place a 1 in the specified square, and place a 2 wherever the second player moves. Each move must be to an empty square. After each move, determine whether the game has been won and whether it is a draw.
I am beginner with java and this has frustrated me for the last couple days that I can't figure this out. I do notice that when it asks to pick which row and column, and if I pick let's say row 3, col 3, it gives me the above error. It shouldn't even allow me to input a 3 in the 1st place as the main method error traps for that input. Plus I can't ever get the input to go in to the 1st row, and 1st col. I think there's issues in the makeMove and displayBoard methods. Please help. I'll put my code that I have, plus the main method:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at TicTacToe.makeMove(TicTacToe.java:33)
at TicTacToeTest.main(TicTacToeTest.java:72)
The assignment calls for a 3-by-3 2-dimensional array of integers. The constructor should initialize the empty board to all zeros (which I've done). Allow 2 human players. Wherever the first player moves, place a 1 in the specified square, and place a 2 wherever the second player moves. Each move must be to an empty square. After each move, determine whether the game has been won and whether it is a draw.
I am beginner with java and this has frustrated me for the last couple days that I can't figure this out. I do notice that when it asks to pick which row and column, and if I pick let's say row 3, col 3, it gives me the above error. It shouldn't even allow me to input a 3 in the 1st place as the main method error traps for that input. Plus I can't ever get the input to go in to the 1st row, and 1st col. I think there's issues in the makeMove and displayBoard methods. Please help. I'll put my code that I have, plus the main method:
public class TicTacToe
{
// Declare variables
private int player;
private int board[][];
public TicTacToe()
{
// Initializes private data
player = 1; // player1 always goes first, but could randomize it
// Constructs 3 X 3 array of int for the board
board = new int[3][3];
// Initializes all elements to the array of 0; nested for loop
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
board[i][j] = 0;
}
}
}
public boolean makeMove(int row, int col)
{
// Initialize boolean move
boolean move;
// Checks to see if board location is occupied and a move can be made
if (board[row][col] == 0)
{
board[row][col] = player;
return true;
}
else
{
// The specified cell is already occupied.
move = false;
}
return move;
}
public boolean isBoardComplete()
{
boolean complete = true;
for (int i = 0; i < 3; i++)
{
for(int j =0; j < 3; j++)
{
if (board[i][j] == 0)
{
complete = false;
}
}
}
return complete;
}
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 1st 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()
{
// nested for loop to display the TTT board after each players input
for ( int i = 0; i < 3; i++)
{
System.out.println(" ");
for( int j = 0; j < 3; j++)
{
System.out.print(board[i][j] + " ");
}
System.out.println("\n");
}
}
public int getCurrentPlayer()
{
return player;
}
}
import java.util.Scanner;
public class TicTacToeTest
{
public static void main(String[] args)
{
// declare variables included in our TTT board
TicTacToe myGame = new TicTacToe();
Scanner input = new Scanner(System.in);
int row;
int col;
// As long as there is no winner and there are still moves that can
// be made, then play on. If these conditions happen game is over or we
// have a draw
while(myGame.determineWinner() == 0 && !myGame.isBoardComplete())
{
// dispaly the current board state
myGame.displayBoard();
// get the current move from the current player
System.out.println("Player " + myGame.getCurrentPlayer());
System.out.println("Make your move.");
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();
}
// 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
{
// the last player to move won but the board has already advanced
// the turn, therefore the winner is opposite of whose turn it is
System.out.print("The Winner is Player ");
if (myGame.getCurrentPlayer() == 1)
{
System.out.println("2");
}
else
{
System.out.println("1");
}
}
}
}