I cant figure out how to fix this!!! java.lang.ArrayIndexOutOfBoundsException: 2

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • amullen98
    New Member
    • Oct 2012
    • 1

    I cant figure out how to fix this!!! java.lang.ArrayIndexOutOfBoundsException: 2

    Hello,

    I am writing a program to for a tic tac toe game using multiple classes. And I need to be able to have the user enter in the size of the board and then have the game run no matter how big or small the board is. But when I try to implement this I keep getting an error involving java.lang.Array IndexOutOfBound sException: 2 and have no clue how to fix this.

    Here are my three classes:
    Code:
    import javax.swing.JOptionPane;
    
    
    public class TicTacToeGame {
    
    	    static char PLAYER1 = 'X', PLAYER2 = 'O', EMPTY = '?';
    	    
    	    public static void main(String[] args)
    	    {
    	        GameBoard.clearBoard();
    	        
    	        do
    	        {
    	            Player.playerMove(GameBoard.board, PLAYER1);
    	            GameBoard.drawBoard();
    	            if(GameBoard.checkDraw() || GameBoard.checkWinner(PLAYER1) || GameBoard.checkWinner(PLAYER2))
    	                break;
    	            Player.playerMove(GameBoard.board, PLAYER2);
    	            GameBoard.drawBoard();
    	        }while(!GameBoard.checkDraw() && !GameBoard.checkWinner(PLAYER1) && !GameBoard.checkWinner(PLAYER2));
    	        
    	        if(GameBoard.checkWinner(PLAYER1) == true){
    	        	
    	        	JOptionPane.showMessageDialog(null, "Player 1 was one the game!");
    	        }
    	        
    	        else if (GameBoard.checkWinner(PLAYER2) == true){
    	        	
    	        	JOptionPane.showMessageDialog(null, "Player 2 was one the game!");
    	        	
    	        }
    	    }
    }
    
    
    
    import javax.swing.JOptionPane;
    
     
    public class GameBoard {
    	
    	
    	static int X, Y;
        static char PLAYER1 = 'X', PLAYER2 = 'O', EMPTY = '?';
        
        static char[][] board = new char[X][Y];
        
        public static void drawBoard()
    	    {
    	        for (int i = 0; i < board.length ; i++)
    	        {
    	        	for (int j = 0; j < board.length; j++)
    	              System.out.print("  " + board[i][j] + "  ");
    	              System.out.print("\n");
    	        }
    	        System.out.print("\n");
    	    }
        
        public static void clearBoard()
    	    {
        	  String input;
        	  
        		input = JOptionPane.showInputDialog("Enter an value for the height of the board?");
        		X = Integer.parseInt(input);
        		
        		input = JOptionPane.showInputDialog("Enter an value for the width of the board?");
        		Y = Integer.parseInt(input);
    	        
        		for (int i = 0; i < board.length ; i++){
    	    	  for (int j = 0; j < board.length; j++){
    	            board[i][j] = EMPTY;
    	    	  }
    	    }
    	 }
        
    	 public static boolean checkWinner(char player)
    	    {
    	        for (int i = 0, j = 0; i < board.length; i++)  
    		{
    		   if (board[i][j] == player && board[i][j + 1] == player && board[i][j + 2] == player)
    			return true;
    		}
    	        for (int i = 0, j = 0; j < board.length; j++)  
    		{
    		   if (board[i][j] == player && board[i + 1][j] == player && board[i + 2][j] == player)
    			return true;
    		}
    		if (board[0][0] == player && board[1][1] == player && board[2][2] == player)
    			return true;
    		else if (board[0][2] == player && board[1][1] == player && board[2][0] == player)
    			return true;
    	        return false;
    	    }
    	    
    	    public static boolean checkDraw()
    	    {
    	        int count = 0;
    	        for (int i = 0; i < board.length ; i++)
    	        {
    	            for (int j = 0; j < board.length; j++)
    	            {
    	                if(board[i][j] == PLAYER1 || board[i][j] == PLAYER2)
    	                    count++;
    	            }
    	        }
    	        if(count == X*Y)
    	            return true;
    	        else 
    	            return false;
    	        
    	    }
    	    
    
    }
    
    
    import javax.swing.JOptionPane;
    
    
    public class Player {
    	
        public static void playerMove(char board[][], char player)
        {
            int x, y;
            String input;
            
            input = JOptionPane.showInputDialog("Enter an X value");
            x = Integer.parseInt(input);
            
            input = JOptionPane.showInputDialog("Enter an Y value");
            y = Integer.parseInt(input);
            
            board[x][y] = player;
        }
    
    }
    Thanks!!!
    Last edited by Rabbit; Oct 20 '12, 04:09 AM. Reason: Please use code tags when posting code.
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    Please use code tags when posting code.

    That error means that you're trying to access an item in an array that doesn't exist, ie you have an array with 2 items and you try to access item 3.

    Find out which line is causing the error and you'll know which array is going out of bounds.

    Comment

    Working...