Results 1 to 2 of 2

Thread: 2D Array Error

  1. #1
    Englewingz is offline Member
    Join Date
    May 2015
    Posts
    1
    Rep Power
    0

    Default 2D Array Error

    I've been programming a TicTacToe game and whenever I run the program, I get the error:

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
    at PlayerX.playerWin(PlayerX.java:123)
    at TicTacToeTester.main(TicTacToeTester.java:30)

    I didn't call any arrays out of bounds though as far as I can tell. Here is the class, PlayerX. I marked line 123.

    Java Code:
    public class PlayerX 
    {
    	private int cpuMove, playerMove;
    	private String[][] board;
    		
    	public PlayerX()
    	{
    		board = new String[2][2];
    		
    		for (int i = 0; i < board.length; i++)
    		{
    			for (int j = 0; j < board[i].length; j++)
    			{
    				board[i][j] = " ";
    			}
    		}
    	}
    	//Precondition:
    	//Postcondition:
    	public int getMoveCPU()
    	{
    		cpuMove = (int)(Math.random() * 9);
    		return cpuMove;
    	}
    	//Precondition:
    	//Postcondition
    	public void askPlayerMove()
    	{
    		playerMove = Cs111.readInt("Enter your move (1-9): ", 1, 9);
    	}
    	//Precondition:
    	//Postcondition:
    	public int getMovePlayer()
    	{
    		return playerMove;
    	}
    	//Precondition:
    	//Postcondition:
    	public void setMoveCPU(int cpuMove)												
    	{
    		if (cpuMove == 1)
    		{
    			board[0][0] = "O";
    		}
    		else if (cpuMove == 2)
    		{
    			board[0][1] = "O";
    		}
    		else if (cpuMove == 3)
    		{
    			board[0][2] = "O";
    		}
    		else if (cpuMove == 4)
    		{
    			board[1][0] = "O";
    		}
    		else if (cpuMove == 5)
    		{
    			board[1][1] = "O";
    		}
    		else if (cpuMove == 6)
    		{
    			board[1][2] = "O";
    		}
    		else if (cpuMove == 7)
    		{
    			board[2][0] = "O";
    		}
    		else if (cpuMove == 8)
    		{
    			board[2][1] = "O";
    		}
    		else
    		{
    			board[2][2] = "O";
    		}
    	}
    	//Precondition:
    	//Postcondition:
    	public void setMovePlayer(int playerMove)												
    	{
    		if (playerMove == 1)
    		{
    			board[0][0] = "X";
    		}
    		else if (playerMove == 2)
    		{
    			board[0][1]	= "X";
    		}
    		else if (playerMove == 3)
    		{
    			board[0][2] = "X";
    		}
    		else if (playerMove == 4)
    		{
    			board[1][0] = "X";
    		}
    		else if (playerMove == 5)
    		{
    			board[1][1] = "X";
    		}
    		else if (playerMove == 6)
    		{
    			board[1][2] = "X";
    		}
    		else if (playerMove == 7)
    		{
    			board[2][0] = "X";
    		}
    		else if (playerMove == 8)
    		{
    			board[2][1] = "X";
    		}
    		else 
    		{
    			board[2][2] = "X";
    		}
    	}
    	//Precondition:
    	//Postcondition:
    	public boolean playerWin()					
    	{
    		if ((board[0][0].equals("X")) && (board[0][1].equals("X")) && (board[0][2].equals("X"))             //LINE 123
             || (board[1][0].equals("X")) && (board[1][1].equals("X")) && (board[1][2].equals("X"))
             || (board[2][0].equals("X")) && (board[2][1].equals("X")) && (board[2][2].equals("X"))
             || (board[0][0].equals("X")) && (board[1][0].equals("X")) && (board[2][0].equals("X"))
             || (board[0][1].equals("X")) && (board[1][1].equals("X")) && (board[2][1].equals("X"))
             || (board[0][2].equals("X")) && (board[1][2].equals("X")) && (board[2][2].equals("X"))
             || (board[0][0].equals("X")) && (board[1][1].equals("X")) && (board[2][2].equals("X"))
             || (board[0][2].equals("X")) && (board[1][1].equals("X")) && (board[2][0].equals("X")))
    		{
    			return true;
    		}
            else
            {
            	return false;
            }
    	}
    	//Precondition:
    	//Postcondition:
    	public boolean cpuWin()					
    	{
    		if ((board[0][0].equals("O")) && (board[0][1].equals("O")) && (board[0][2].equals("O"))
             || (board[1][0].equals("O")) && (board[1][1].equals("O")) && (board[1][2].equals("O"))
             || (board[2][0].equals("O")) && (board[2][1].equals("O")) && (board[2][2].equals("O"))
             || (board[0][0].equals("O")) && (board[1][0].equals("O")) && (board[2][0].equals("O"))
             || (board[0][1].equals("O")) && (board[1][1].equals("O")) && (board[2][1].equals("O"))
             || (board[0][2].equals("O")) && (board[1][2].equals("O")) && (board[2][2].equals("O"))
             || (board[0][0].equals("O")) && (board[1][1].equals("O")) && (board[2][2].equals("O"))
             || (board[0][2].equals("O")) && (board[1][1].equals("O")) && (board[2][0].equals("O")))
    		{
    			return true;
    		}
            else
            {
            	return false;
            }
    	}
    	//Precondition:
    	//Postcondition:
    	public boolean isBoardFull()				
    	{
    		if ((board[0][0].equals("X")) || (board[0][0].equals("O")) &&
    			(board[0][1].equals("X")) || (board[0][1].equals("O")) &&
    			(board[0][2].equals("X")) || (board[0][2].equals("O")) &&
    			(board[1][0].equals("X")) || (board[1][0].equals("O")) &&
    			(board[1][1].equals("X")) || (board[1][1].equals("O")) &&
    			(board[1][2].equals("X")) || (board[1][2].equals("O")) &&
    			(board[2][0].equals("X")) || (board[2][0].equals("O")) &&
    			(board[2][1].equals("X")) || (board[2][1].equals("O")) &&
    			(board[2][2].equals("X")) || (board[2][2].equals("O")))
    		{
    			return true;
    		}
    		else
    		{
    			return false;
    		}
    	}
    	//Precondition:
    	//Postcondition:
    	public void printBoard()
    	{
    		System.out.println("   " + board[0][0] + " | " + board[0][1] + " | " + board[0][2]);
    		System.out.println("-------------");
    		System.out.println("   " + board[1][0] + " | " + board[1][1] + " | " + board[1][2]);
    		System.out.println("-------------");
    		System.out.println("   " + board[2][0] + " | " + board[2][1] + " | " + board[2][2]); 
    	}
    	//Precondition:
    	//Postcondition:
    	public void resetBoard()
    	{
    		for (int i = 0; i < board.length; i++)
    		{
    			for (int j = 0; j < board[i].length; j++)
    			{
    				board[i][j] = " ";
    			}
    		}
    	}
    }
    Here is my TicTacToe Tester. I marked Line 30

    Java Code:
    public class TicTacToeTester
    {
    	public static void main(String[] args)
    	{	
    		/***DECLARATIONS SECTION***/
    		
    		TicTacToe ttt;
    		PlayerX pX;
    		PlayerO pO;
    		int menuChoice;
    		double percentWins;
    		
    		/***INITIALIZATION SECTION***/
    		
    		ttt = new TicTacToe();
    		pX = new PlayerX();
    		pO = new PlayerO();
    		
    		/***PROCESSING SECTION***/
    		
    		ttt.printHeader();
    		
    		do
    		{
    			menuChoice = Cs111.readInt("Enter menu choice: ", 1, 3);
    			System.out.println();
    			
    			if (menuChoice == 1)											
    			{
    				while((pX.playerWin() == false) && (pX.cpuWin() == false) && (pX.isBoardFull() == false))     //LINE 30
    				{
    					pX.printBoard();
    					pX.askPlayerMove();
    					pX.setMovePlayer(pX.getMovePlayer());
    					pX.printBoard();
    					pX.setMoveCPU(pX.getMoveCPU());
    					pX.printBoard();
    					ttt.incrementRounds();
    					ttt.printStats();
    					pX.resetBoard();
    			
    					percentWins = (ttt.getWins()) / (ttt.getRounds());
    					System.out.printf("Win Percentage: %d.2", percentWins);					
    				}
    				if(pX.playerWin() == true)
    				{
    					System.out.println("You won!");
    					ttt.incrementWins();
    					ttt.incrementRounds();
    					ttt.printStats();
    					pX.resetBoard();
    			
    					percentWins = (ttt.getWins()) / (ttt.getRounds());
    					System.out.printf("Win Percentage: %d.2", percentWins);					
    				}
    				else if(pX.cpuWin() == true)
    				{
    					System.out.println("You lost!");
    					ttt.incrementLosses();
    					ttt.incrementRounds();
    					ttt.printStats();
    					pX.resetBoard();
    			
    					percentWins = (ttt.getWins()) / (ttt.getRounds());
    					System.out.printf("Win Percentage: %d.2", percentWins);					
    				}
    				else
    				{
    					System.out.println("Draw.");
    					ttt.incrementDraws();
    					ttt.incrementRounds();
    					ttt.printStats();
    					pX.resetBoard();
    			
    					percentWins = (ttt.getWins()) / (ttt.getRounds());
    					System.out.printf("Win Percentage: %d.2", percentWins);					
    				}
    			}
    			else if (menuChoice == 2)
    			{
    				while((pO.playerWin() == false) && (pO.cpuWin() == false) && (pO.isBoardFull() == false));
    				{
    					pO.printBoard();
    					pO.setMoveCPU(pO.getMoveCPU());
    					pO.printBoard();				
    					pO.askPlayerMove();				
    					pO.setMovePlayer(pO.getMovePlayer());
    					pO.printBoard();
    				}
    				if(pO.playerWin() == true)
    				{
    					System.out.println("You won!");
    					ttt.incrementWins();
    					ttt.incrementRounds();
    					ttt.printStats();
    					pO.resetBoard();
    			
    					percentWins = (ttt.getWins()) / (ttt.getRounds());
    					System.out.printf("Win Percentage: %d.2", percentWins);					
    				}
    				else if(pO.cpuWin() == true)
    				{
    					System.out.println("You lost!");
    					ttt.incrementLosses();
    					ttt.incrementRounds();
    					ttt.printStats();
    					pO.resetBoard();
    			
    					percentWins = (ttt.getWins()) / (ttt.getRounds());
    					System.out.printf("Win Percentage: %d.2", percentWins);					
    				}
    				else
    				{
    					System.out.println("Draw.");
    					ttt.incrementDraws();
    					ttt.incrementRounds();
    					ttt.printStats();
    					pO.resetBoard();
    			
    					percentWins = (ttt.getWins()) / (ttt.getRounds());
    					System.out.printf("Win Percentage: %d.2", percentWins);					
    				}
    			}
    
    		} while (menuChoice != 3);
    		
    		System.exit(0);
    	}
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: 2D Array Error

    Look at line #8: you define a 2x2 board there, shouldn't it be 3x3 board?

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. Array Spreadsheet error
    By dokunbam in forum New To Java
    Replies: 2
    Last Post: 10-09-2013, 04:19 PM
  2. Array Error
    By crueoj in forum New To Java
    Replies: 3
    Last Post: 07-09-2012, 09:46 PM
  3. Array Size Error
    By pondwire in forum New To Java
    Replies: 5
    Last Post: 07-21-2011, 08:07 AM
  4. array error
    By blu_fox88 in forum New To Java
    Replies: 4
    Last Post: 04-25-2010, 12:18 PM
  5. Can't Solve Array Error
    By Gregadeaux in forum New To Java
    Replies: 2
    Last Post: 12-14-2009, 03:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •