Results 1 to 6 of 6
  1. #1
    amullen98 is offline Member
    Join Date
    Oct 2012
    Posts
    3
    Rep Power
    0

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

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Tic Tac Toe Problem!

    Java Code:
    static int X = 0, Y = 0;
    static char PLAYER1 = 'X', PLAYER2 = 'O', EMPTY = '?';
    
    static char[][] board = new char[X][Y];
    Based on this code snippet above, how big is your 2-dimensional array, board going to be?

    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.

  3. #3
    amullen98 is offline Member
    Join Date
    Oct 2012
    Posts
    3
    Rep Power
    0

    Default 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.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Tic Tac Toe Problem!

    Quote Originally Posted by amullen98 View Post
    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.
    As I've stated, changing X and Y after board has been created will have *no* effect on board. This would be equivalent to drawing a picture with a red crayon, and expecting it to turn blue when you pick up a blue one.

  5. #5
    amullen98 is offline Member
    Join Date
    Oct 2012
    Posts
    3
    Rep Power
    0

    Default Re: Tic Tac Toe Problem!

    Even if I don't set X & Y to anything I still get an error?

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Tic Tac Toe Problem!

    Quote Originally Posted by amullen98 View Post
    Even if I don't set X & Y to anything I still get an error?
    Yep. If you don't set X and Y to anything then they are set to their default values of 0.

    Perhaps in your clearBoard() method you want to initialize board to a new array that uses the user input values. Just a thought.

Similar Threads

  1. Small problem with problem with Java, C++ parse program.
    By dragstang86 in forum New To Java
    Replies: 4
    Last Post: 10-30-2011, 03:43 AM
  2. Replies: 9
    Last Post: 09-21-2010, 04:15 PM

Posting Permissions

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