By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,233 Members | 1,538 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,233 IT Pros & Developers. It's quick & easy.

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

P: 1
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.ArrayIndexOutOfBoundsException: 2 and have no clue how to fix this.

Here are my three classes:
Expand|Select|Wrap|Line Numbers
  1. import javax.swing.JOptionPane;
  2.  
  3.  
  4. public class TicTacToeGame {
  5.  
  6.         static char PLAYER1 = 'X', PLAYER2 = 'O', EMPTY = '?';
  7.  
  8.         public static void main(String[] args)
  9.         {
  10.             GameBoard.clearBoard();
  11.  
  12.             do
  13.             {
  14.                 Player.playerMove(GameBoard.board, PLAYER1);
  15.                 GameBoard.drawBoard();
  16.                 if(GameBoard.checkDraw() || GameBoard.checkWinner(PLAYER1) || GameBoard.checkWinner(PLAYER2))
  17.                     break;
  18.                 Player.playerMove(GameBoard.board, PLAYER2);
  19.                 GameBoard.drawBoard();
  20.             }while(!GameBoard.checkDraw() && !GameBoard.checkWinner(PLAYER1) && !GameBoard.checkWinner(PLAYER2));
  21.  
  22.             if(GameBoard.checkWinner(PLAYER1) == true){
  23.  
  24.                 JOptionPane.showMessageDialog(null, "Player 1 was one the game!");
  25.             }
  26.  
  27.             else if (GameBoard.checkWinner(PLAYER2) == true){
  28.  
  29.                 JOptionPane.showMessageDialog(null, "Player 2 was one the game!");
  30.  
  31.             }
  32.         }
  33. }
  34.  
  35.  
  36.  
  37. import javax.swing.JOptionPane;
  38.  
  39.  
  40. public class GameBoard {
  41.  
  42.  
  43.     static int X, Y;
  44.     static char PLAYER1 = 'X', PLAYER2 = 'O', EMPTY = '?';
  45.  
  46.     static char[][] board = new char[X][Y];
  47.  
  48.     public static void drawBoard()
  49.         {
  50.             for (int i = 0; i < board.length ; i++)
  51.             {
  52.                 for (int j = 0; j < board.length; j++)
  53.                   System.out.print("  " + board[i][j] + "  ");
  54.                   System.out.print("\n");
  55.             }
  56.             System.out.print("\n");
  57.         }
  58.  
  59.     public static void clearBoard()
  60.         {
  61.           String input;
  62.  
  63.             input = JOptionPane.showInputDialog("Enter an value for the height of the board?");
  64.             X = Integer.parseInt(input);
  65.  
  66.             input = JOptionPane.showInputDialog("Enter an value for the width of the board?");
  67.             Y = Integer.parseInt(input);
  68.  
  69.             for (int i = 0; i < board.length ; i++){
  70.               for (int j = 0; j < board.length; j++){
  71.                 board[i][j] = EMPTY;
  72.               }
  73.         }
  74.      }
  75.  
  76.      public static boolean checkWinner(char player)
  77.         {
  78.             for (int i = 0, j = 0; i < board.length; i++)  
  79.         {
  80.            if (board[i][j] == player && board[i][j + 1] == player && board[i][j + 2] == player)
  81.             return true;
  82.         }
  83.             for (int i = 0, j = 0; j < board.length; j++)  
  84.         {
  85.            if (board[i][j] == player && board[i + 1][j] == player && board[i + 2][j] == player)
  86.             return true;
  87.         }
  88.         if (board[0][0] == player && board[1][1] == player && board[2][2] == player)
  89.             return true;
  90.         else if (board[0][2] == player && board[1][1] == player && board[2][0] == player)
  91.             return true;
  92.             return false;
  93.         }
  94.  
  95.         public static boolean checkDraw()
  96.         {
  97.             int count = 0;
  98.             for (int i = 0; i < board.length ; i++)
  99.             {
  100.                 for (int j = 0; j < board.length; j++)
  101.                 {
  102.                     if(board[i][j] == PLAYER1 || board[i][j] == PLAYER2)
  103.                         count++;
  104.                 }
  105.             }
  106.             if(count == X*Y)
  107.                 return true;
  108.             else 
  109.                 return false;
  110.  
  111.         }
  112.  
  113.  
  114. }
  115.  
  116.  
  117. import javax.swing.JOptionPane;
  118.  
  119.  
  120. public class Player {
  121.  
  122.     public static void playerMove(char board[][], char player)
  123.     {
  124.         int x, y;
  125.         String input;
  126.  
  127.         input = JOptionPane.showInputDialog("Enter an X value");
  128.         x = Integer.parseInt(input);
  129.  
  130.         input = JOptionPane.showInputDialog("Enter an Y value");
  131.         y = Integer.parseInt(input);
  132.  
  133.         board[x][y] = player;
  134.     }
  135.  
  136. }
Thanks!!!
Oct 20 '12 #1
Share this Question
Share on Google+
1 Reply


Rabbit
Expert Mod 10K+
P: 12,421
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.
Oct 20 '12 #2

Post your reply

Sign in to post your reply or Sign up for a free account.