Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Tic Tac Toe Problem!

tic tac toe error expandable board java.lang.arrayindexoutofboun

  • Please log in to reply
5 replies to this topic

#1 amullen98

amullen98

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts
  • Programming Language:C++, PL/SQL
  • Learning:Java

Posted 19 October 2012 - 06:18 PM

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:
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!!!

Edited by BenW, 20 October 2012 - 02:40 AM.
Added code tags

  • 0

#2 Chall

Chall

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 349 posts
  • Location:Cedar Rapids, IA
  • Programming Language:Java
  • Learning:C, Java, C++, C#, Python, JavaScript, Assembly

Posted 19 October 2012 - 07:01 PM

static char PLAYER1 = 'X', PLAYER2 = 'O', EMPTY = '?';

static char[][] board = new char[X][O];

Not sure if that is even valid. Usually, arrays only take integers. Try changing those to integers.

Oh, and wrap your code with code (<>) tags.
  • 0
Speaks fluent Java

#3 amullen98

amullen98

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts
  • Programming Language:C++, PL/SQL
  • Learning:Java

Posted 19 October 2012 - 08:42 PM

That would not be work. X and O are used to marked the spaces X Y are the values used to get to the space that the player wants to make move in.
  • 0

#4 Chall

Chall

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 349 posts
  • Location:Cedar Rapids, IA
  • Programming Language:Java
  • Learning:C, Java, C++, C#, Python, JavaScript, Assembly

Posted 20 October 2012 - 06:04 AM

Yes, but X and O are just characters, they are not numbers. I'm 99% sure that you cannot make an array using characters for the length.
  • 0
Speaks fluent Java

#5 Unkish

Unkish

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 50 posts

Posted 20 October 2012 - 10:37 PM

The problem is in:
GameBoard.clearBoard()
After asking boards heigt and width you need to reinitialize board with new sizes aquired:
board = new char[X][Y];
Otherwise it'll stay with [0,0] bounds.
  • 1

#6 Chall

Chall

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 349 posts
  • Location:Cedar Rapids, IA
  • Programming Language:Java
  • Learning:C, Java, C++, C#, Python, JavaScript, Assembly

Posted 21 October 2012 - 10:10 AM

I don't get what you're saying, but this is what I got when I problem solved.

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;
	 }
Pay close attention to board[x][y] = player. You need to implement some way to make sure that the x and y are valid indexes on the array, otherwise if I had a 3x3 board, I could enter 4 on accident, and the JRE would throw an ArrayIndexOutOfBoundsException.

public class GameBoard {

static int X, Y;
	 static char PLAYER1 = 'X', PLAYER2 = 'O', EMPTY = '?';
	 static char[][] board = new char[X][Y];
X and Y are both 0 by default. Because of this, when you instantiate board (which always happens while X and Y are 0), it's lengths are always [0][0].

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;
		 }
}
}
This is where the majority of your current problem is. X and Y are never used, even after grabbing them, so board is still [0][0]. You need to re-instantiate board right before iterating through them.
board = new char[X][Y];
That way, your board's lengths are now [X][Y], and your program can function properly. You also should check out that nested loop. It seems that j has to be less than board.length, and i also has to be less than board.length? It shoud be j < board[i].length, so that you don't skip indexes, or go out of bounds. Hope this helps,
~Chall
  • 0
Speaks fluent Java





Also tagged with one or more of these keywords: tic tac toe, error, expandable board, java.lang.arrayindexoutofboun

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download