Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Tic Tac Toe Program, Can someone help me out????

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Angry Tic Tac Toe Program, Can someone help me out????

    /* I KEEP GETTING THIS ERROR CAN SOMEONE HELP ME OUT?
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
    at TicTacToe.playerMove(TicTacToe.java:50)
    at TicTacToeGame.main(TicTacToeGame.java:86)

    */


    public class TicTacToe
    {

    private int player;
    private static int[][] board;

    public TicTacToe()
    {
    board = new int [3][3];

    for(int row=0; row < 3; row++)
    {
    for(int col=0; col < 3; col++)
    {
    board[row][col] = 0;
    }
    }
    displayBoard();
    showBoard();
    }

    public int setPlayer(int player)
    {
    return this.player = player;
    }


    public boolean playerMove(int row, int col )
    {
    boolean move;

    if (board[row][col] == 0)
    {
    board[row][col] = player;

    return true;
    } else {
    move = false;
    }
    return move;
    }



    public boolean isGameOver()
    {

    boolean gameOver = true;

    for (int row = 0; row < 3; row++)
    {
    for (int col = 0; col < 3; col++)
    {
    if (board[row][col] == 0)
    {
    gameOver = false;
    }
    }
    }
    return gameOver;
    }

    public static void displayBoard ()
    {
    System.out.println("Tic Tac Toe Game\n" + "-----------");

    for(int row = 0; row < 3; row++)
    {

    for (int col = 0; col < 3; col++)
    {


    System.out.print(board[row][col] + " | ");
    }
    System.out.println("\n-----------");

    }

    }
    public int determineWinner()
    {
    int winner =0;

    if (board[0][0] == board[0][1] && board[0][0] == board[0][2] && board[0][0] != 0 )
    {

    winner = board[0][0];
    }

    if (board[1][0] == board[1][1] && board[1][0] == board[1][2] && board[1][0] != 0 )
    {

    winner = board[1][0];
    }

    if (board[2][0] == board[2][1] && board[2][0] == board[2][2] && board[2][0] != 0 )
    {

    winner = board[2][0];
    }

    if (board[0][0] == board[1][0] && board[0][0] == board[2][0] && board[0][0] != 0 )
    {

    winner = board[0][0];
    }

    if (board[0][1] == board[1][1] && board[0][1] == board[2][1] && board[0][1] != 0 )
    {

    winner = board[0][1];
    }

    if (board[0][2] == board[1][2] && board[0][2] == board[2][2] && board[0][2] != 0 )
    {

    winner = board[0][2];
    }


    if (board[0][0] == board[1][1] && board[0][0] == board[2][2] && board[0][0] != 0 )
    {

    winner = board[0][0];
    }

    if (board[0][2] == board[1][1] && board[0][2] == board[2][0] && board[0][2] != 0 )
    {

    winner = board[0][2];

    }
    return winner;
    }

    public static void showBoard()
    {

    System.out.println("Tic Tac Toe Game\n" + "-----------");
    for(int row = 0; row < 3; row++)
    {

    for (int col = 0; col < 3; col++)
    {

    System.out.print(board[col] + " | ");
    }
    System.out.println("\n-----------");

    }
    }
    }





    import java.util.Scanner;

    public class TicTacToeGame
    {
    private static int player ;

    public void switchPlayer() {
    if (player ==1)
    {
    player = 2;
    } else {
    player =1;
    }
    }

    public static void main(String[] args)
    {
    TicTacToe myGame = new TicTacToe();
    Scanner input = new Scanner(System.in);
    int row;
    int col = 1;

    while(myGame.determineWinner() == 0 && !myGame.isGameOver())
    {
    TicTacToe.displayBoard();

    System.out.println("Player " + myGame.setPlayer(player));
    System.out.println("Make your move.");
    System.out.print("Row please (1-3):");
    row = input.nextInt();

    while(row < 1 || row > 3)
    {
    System.out.println("Invalid Row.");
    System.out.print("Try again (1-3):");
    row = input.nextInt();
    }

    System.out.print("Col please (1-3):");
    col = input.nextInt();

    while(col < 1 || col > 3)
    {
    System.out.println("Invalid Col.");
    System.out.print("Try again (1-3):");
    col = input.nextInt();
    }


    while(!myGame.playerMove(row, col))
    {
    System.out.println("Invalid Move... Try Again.");
    System.out.print("Row please (1-3):");
    row = input.nextInt();

    while(row < 1 || row > 3)
    {
    System.out.println("Invalid Row.");
    System.out.print("Try again (1-3):");
    row = input.nextInt();
    }

    System.out.print("Col please (1-3):");
    col = input.nextInt();

    while(col < 1 || col > 3)
    {
    System.out.println("Invalid Col.");
    System.out.print("Try again (1-3):");
    col = input.nextInt();
    }
    row -= 1;
    col -= 1;
    }

    }

    if (myGame.determineWinner() == 0)
    {
    System.out.println("Sorry - No Winner");
    }
    else
    {

    System.out.print("The Winner is Player ");
    if (myGame.setPlayer(player) == 1)
    {
    System.out.println("2");
    }
    else
    {
    System.out.println("1");
    }
    }
    }
    }


  2. #2

    Default Re: Tic Tac Toe Program, Can someone help me out????

    You need to use debug and step through your code, line-by-line. It isn't complicated and should give you a good idea of where you are going wrong. After a cursory look at the code, I can't see any index violations as they are all hard coded.
    Kenneth Walter
    Software Developer
    http://kennywalter.com

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Tic Tac Toe Program, Can someone help me out????

    java.lang.ArrayIndexOutOfBoundsException: 3
    The java program says your index is past the end of the array. Does the array have 4 elements in it?
    Check your code on line 50 to see how the index gets to be 3.
    Remember array indexes go from 0 to the length of the array-1

    Please Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting

Similar Threads

  1. Help with class program!!! STUCK! Program not doing what I Want!!!
    By sketch_flygirl in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 4th, 2011, 07:29 AM