Jump to content


Check out our Community Blogs

amullen98

Member Since 19 Oct 2012
Offline Last Active Oct 23 2012 10:40 AM
-----

Topics I've Started

Tic Tac Toe Problem!

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

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