Results 1 to 6 of 6
Thread: Tic Tac Toe Problem!
- 10-20-2012, 06:52 AM #1
Member
- Join Date
- Oct 2012
- Posts
- 3
- Rep Power
- 0
Tic Tac Toe Problem!
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 this error:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
at Player.playerMove(Player.java:17)
at TicTacToeGame.main(TicTacToeGame.java:14)
I do not know how to fix it or what to do. I have tried to debug it but I can't seem to find the problem.
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 = 0, Y = 0;
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 = 0, y = 0;
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!!!
-
Re: Tic Tac Toe Problem!
Based on this code snippet above, how big is your 2-dimensional array, board going to be?Java Code:static int X = 0, Y = 0; static char PLAYER1 = 'X', PLAYER2 = 'O', EMPTY = '?'; static char[][] board = new char[X][Y];
Note that once you've created your board array, changing X or Y will have no effect on board -- it's dimensions will remain what you've coded them to be above.
- 10-20-2012, 07:16 AM #3
Member
- Join Date
- Oct 2012
- Posts
- 3
- Rep Power
- 0
Re: Tic Tac Toe Problem!
Well if you look in the clearboard function I ask the user to input the height and width of the board and set their answers to X and Y.
-
Re: Tic Tac Toe Problem!
- 10-20-2012, 07:29 AM #5
Member
- Join Date
- Oct 2012
- Posts
- 3
- Rep Power
- 0
Re: Tic Tac Toe Problem!
Even if I don't set X & Y to anything I still get an error?
-
Re: Tic Tac Toe Problem!
Similar Threads
-
Small problem with problem with Java, C++ parse program.
By dragstang86 in forum New To JavaReplies: 4Last Post: 10-30-2011, 03:43 AM -
Can anyone see the problem with my code? problem understanding switch (newbish)
By keith in forum New To JavaReplies: 9Last Post: 09-21-2010, 04:15 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks