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 9 of 9

Thread: Need help please.

  1. #1
    Junior Member
    Join Date
    Sep 2017
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need help please.

    So here's what I've got:

     
    public class TicTacToeGame {
     
        static int [][] gameboard;
        static final int EMPTY = 0;
        static final int NOUGHT = -1;
        static final int CROSS = 1;
        static void createBoard(int row, int col){
              int gameboard[][]= new int [row][col];
     
        }
     
    static int winOrTie(){
            for (int r=0; r<gameboard.length; r++) {
                int x1=gameboard[r][0];
                int x2=gameboard[r][1];
                int x3=gameboard[r][2];
     
                if (x1 == x2 && x2 == x3 && x1 != EMPTY){
                    return x1;
            }
            for (int c=0; c<gameboard.length; c++){
                int y1=gameboard[0][c];
                int y2=gameboard[1][c];
                int y3=gameboard[2][c];
     
                if (y1 == y2 && y2 == y3 && y1 != EMPTY){
                    return y1;
                }
            }
     
            int z1 = gameboard[0][0];
            int z2 = gameboard[1][1];
            int z3 = gameboard[2][2];
            if (z1 == z2 && z2 == z3 && z1 != EMPTY){
                return z1;
            }
            int d1 = gameboard[0][0];
            int d2 = gameboard[1][1];
            int d3 = gameboard[2][2];
     
            if (d1 == d2 && d2 == d3 && d1 != EMPTY){
                return d1;
            }
            for(int row=0; row<gameboard.length; row++){
                for (int col=0; col<gameboard.length; col++){
                    if(gameboard[row][col] == EMPTY)
                        return -2;
                    }
            }
    } return 0;
    }
        public static void main(String[] args) {
            createBoard(3,3);
            int turn = 0;
            int playerVal;
            int outcome;
     
            java.util.Scanner scan = new java.util.Scanner(System.in);
     
            do {
                displayBoard();
                playerVal = (turn % 2 == 0)? NOUGHT : CROSS;
                if (playerVal == NOUGHT)
                    System.out.println("O's turn");
                else{
                    System.out.println("X's turn");
                }
                System.out.print("Enter row and column:");
     
                try {
                    set(playerVal, scan.nextInt(), scan.nextInt());
                } catch (IllegalArgumentException ex)
                {System.err.println(ex);
                }
     
                turn ++;
                outcome=winOrTie();
            } while (-2 == outcome);
     
            displayBoard();
            switch(outcome) {
                case NOUGHT:
                    System.out.println("O wins!");
                    break;
                case CROSS:
                    System.out.println("X wins!");
                    break;
                case 0:
                    System.out.println("Tie.");
                    break;
     
            }
        }
     
    static void set (int val, int row, int col) throws IllegalArgumentException {
        if (gameboard[row][col] == EMPTY) 
            gameboard[row][col] = val;
        else throw new 
            IllegalArgumentException("Player already there!");
    }
     
    static void displayBoard() {
            for (int row=0; row<gameboard.length; row++) {
                System.out.print("|");
                    for (int col = 0; col<gameboard[row].length; col++) {                                         
                        switch (gameboard[row][col]) {
                            case NOUGHT:
                                System.out.print("O");
                                break;
                            case CROSS:
                                System.out.print("X");
                                break;
                            default:
                                System.out.print(" ");
                        }
                        System.out.print("|");
                        {
                            System.out.println("\n-------\n");
                        }
                    }
                }
            }
     
       }

    It's supposed to be a Tic-Tac-Toe game that displays a 3x3 gameboard. I'm using NetBeans to run it, and it's displaying each space on a separate line. Apparently there's a problem with the createBoard() method on line 19. It won't let me use the variable gameboard, but it won't let me use any other name either. Can someone give me some idea as to what I need to do to make this work?

  2. #2
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    277
    My Mood
    Amused
    Thanks
    8
    Thanked 19 Times in 19 Posts

    Default Re: Need help please.

    I run your code but get error
    Exception in thread "main" java.lang.NullPointerException
    	at test.Test.displayBoard(TicTacToeGame.java:105)
    	at test.Test.main(TicTacToeGame.java:63)
    /home/seng/.cache/netbeans/8.2/executor-snippets/run.xml:53: Java returned: 1
    BUILD FAILED (total time: 0 seconds)
    Whatever you are, be a good one

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need help please.

    It won't let me use the variable gameboard,
    Please copy the full text of the error message and paste it here. It has important info about the error.

    Which line is line 19? Please add a comment to show which line it is.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Junior Member
    Join Date
    Sep 2017
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help please.

    Line 19 is the one that reads

    int gameboard[][]= new int[row][col];

    The error message I'm getting is
     
    Exception in thread "main" java.lang.NullPointerException
    	at tictactoegame.TicTacToeGame.displayBoard(TicTacToeGame.java:114)
    	at tictactoegame.TicTacToeGame.main(TicTacToeGame.java:72)
    C:\Users\owner\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1

    I tried rewriting the line to read
    gameboard= new int[row][col]
    and that's when it runs but it displays nine individual spaces on nine individual lines instead of displaying a 3x3 square. Here's the complete code so you can see what I'm talking about.
    public class TicTacToeGame {
     
        static int [][] gameboard;
        static final int EMPTY = 0;
        static final int NOUGHT = -1;
        static final int CROSS = 1;
        static void createBoard(int row, int col){
             gameboard= new int [row][col];
     
        }
     
    static int winOrTie(){
            for (int r=0; r<gameboard.length; r++) {
                int x1=gameboard[r][0];
                int x2=gameboard[r][1];
                int x3=gameboard[r][2];
     
                if (x1 == x2 && x2 == x3 && x1 != EMPTY){
                    return x1;
            }
            for (int c=0; c<gameboard.length; c++){
                int y1=gameboard[0][c];
                int y2=gameboard[1][c];
                int y3=gameboard[2][c];
     
                if (y1 == y2 && y2 == y3 && y1 != EMPTY){
                    return y1;
                }
            }
     
            int z1 = gameboard[0][0];
            int z2 = gameboard[1][1];
            int z3 = gameboard[2][2];
            if (z1 == z2 && z2 == z3 && z1 != EMPTY){
                return z1;
            }
            int d1 = gameboard[0][0];
            int d2 = gameboard[1][1];
            int d3 = gameboard[2][2];
     
            if (d1 == d2 && d2 == d3 && d1 != EMPTY){
                return d1;
            }
            for(int row=0; row<gameboard.length; row++){
                for (int col=0; col<gameboard.length; col++){
                    if(gameboard[row][col] == EMPTY)
                        return -2;
                    }
            }
    } return 0;
    }
        public static void main(String[] args) {
            createBoard(3,3);
            int turn = 0;
            int playerVal;
            int outcome;
     
            java.util.Scanner scan = new java.util.Scanner(System.in);
     
            do {
                displayBoard();
                playerVal = (turn % 2 == 0)? NOUGHT : CROSS;
                if (playerVal == NOUGHT)
                    System.out.println("O's turn");
                else{
                    System.out.println("X's turn");
                }
                System.out.print("Enter row and column:");
     
                try {
                    set(playerVal, scan.nextInt(), scan.nextInt());
                } catch (IllegalArgumentException ex)
                {System.err.println(ex);
                }
     
                turn ++;
                outcome=winOrTie();
            } while (-2 == outcome);
     
            displayBoard();
            switch(outcome) {
                case NOUGHT:
                    System.out.println("O wins!");
                    break;
                case CROSS:
                    System.out.println("X wins!");
                    break;
                case 0:
                    System.out.println("Tie.");
                    break;
     
            }
        }
     
    static void set (int val, int row, int col) throws IllegalArgumentException {
        if (gameboard[row][col] == EMPTY) 
            gameboard[row][col] = val;
        else throw new 
            IllegalArgumentException("Player already there!");
    }
     
    static void displayBoard() {
            for (int row=0; row<gameboard.length; row++) {
                System.out.print("|");
                    for (int col = 0; col<gameboard[row].length; col++) {                                         
                        switch (gameboard[row][col]) {
                            case NOUGHT:
                                System.out.print("O");
                                break;
                            case CROSS:
                                System.out.print("X");
                                break;
                            default:
                                System.out.print(" ");
                        }
                        System.out.print("|");
                        {
                            System.out.println("\n-------\n");
                        }
                    }
                }
            }

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need help please.

    Exception in thread "main" java.lang.NullPointerException
    at tictactoegame.TicTacToeGame.displayBoard(TicTacToe Game.java:114)
    Look at line 114 and find the variable with the null value. Then back track in the code to see why that variable does not have a value assigned to it.

    I tried rewriting the line to read
    That removed the second definition for the gameboard variable and assigned a value to the class field named gameboard.

    displays nine individual spaces on nine individual lines instead of displaying a 3x3 square.
    Please copy the output and paste it here so we can see what it happening.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Sep 2017
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help please.

    Okay, this is what my output looks like:
    run:
    | |
    -------
     
     |
    -------
     
     |
    -------
     
    | |
    -------
     
     |
    -------
     
     |
    -------
     
    | |
    -------
     
     |
    -------
     
     |
    -------
     
    O's turn
    Enter row and column:

    I also noticed that there's a message that says that in gameboard.length, the field "length" references a null object. This is kind of what I thought the problem might be, but I have no idea how to correct it.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need help please.

    a message that says that in gameboard.length, the field "length" references a null object.
    Please copy the full text of the error message and paste it here. It has important info about the error.

    displaying a 3x3 square.
    Can you give an example of what the output should look like?
    Try to print the contents of one line using the print method for each of the characters on the line and with one println method to move to the next line.
    Say there are 5 characters on a line, then there would be 5 print statements and one println
    print
    print
    print
    print
    print
    println
    etc

    The code in the displayBoard method is a little messy and should be cleaned up to make it easier to read.
    For example there is an extra pair of {}s that are not needed and are confusing.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member
    Join Date
    Sep 2017
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help please.

    Thanks for your help, Norm. I finally got it.
    public class TicTacToeGame {
     
        static int [][] gameboard;
        static final int EMPTY = 0;
        static final int NOUGHT = -1;
        static final int CROSS = 1;
        static void createBoard(int row, int col){
            gameboard = new int [row][col];
     
        } 
     
    static int winOrTie(){
            for (int r=0; r<gameboard.length; r++) {
                int x1=gameboard[r][0];
                int x2=gameboard[r][1];
                int x3=gameboard[r][2];
     
                if (x1 == x2 && x2 == x3 && x1 != EMPTY){
                    return x1;
            }
            for (int c=0; c<gameboard.length; c++){
                int y1=gameboard[0][c];
                int y2=gameboard[1][c];
                int y3=gameboard[2][c];
     
                if (y1 == y2 && y2 == y3 && y1 != EMPTY){
                    return y1;
                }
            }
     
            int z1 = gameboard[0][0];
            int z2 = gameboard[1][1];
            int z3 = gameboard[2][2];
            if (z1 == z2 && z2 == z3 && z1 != EMPTY){
                return z1;
            }
            int d1 = gameboard[0][0];
            int d2 = gameboard[1][1];
            int d3 = gameboard[2][2];
     
            if (d1 == d2 && d2 == d3 && d1 != EMPTY){
                return d1;
            }
            for(int row=0; row<gameboard.length; row++){
                for (int col=0; col<gameboard.length; col++){
                    if(gameboard[row][col] == EMPTY)
                        return -2;
                    }
            }
    } return 0;
    }
        public static void main(String[] args) {
            createBoard(3,3);
            int turn = 0;
            int playerVal;
            int outcome;
     
            java.util.Scanner scan = new java.util.Scanner(System.in);
     
            do {
                displayBoard();
                playerVal = (turn % 2 == 0)? NOUGHT : CROSS;
                if (playerVal == NOUGHT)
                    System.out.println("O's turn");
                else{
                    System.out.println("X's turn");
                }
                System.out.print("Enter row and column:");
     
                try {
                    set(playerVal, scan.nextInt(), scan.nextInt());
                } catch (IllegalArgumentException ex)
                {System.err.println(ex);
                }
     
                turn ++;
                outcome=winOrTie();
            } while (-2 == outcome);
     
            displayBoard();
            switch(outcome) {
                case NOUGHT:
                    System.out.println("O wins!");
                    break;
                case CROSS:
                    System.out.println("X wins!");
                    break;
                case 0:
                    System.out.println("Tie.");
                    break;
     
            }
        }
     
    static void set (int val, int row, int col) throws IllegalArgumentException {
        if (gameboard[row][col] == EMPTY) 
            gameboard[row][col] = val;
        else throw new 
            IllegalArgumentException("Player already there!");
    }
     
    static void displayBoard() {
            for (int row=0; row<gameboard.length; row++) {
                System.out.print("|");
                    for (int col = 0; col<gameboard[row].length; col++) {                    
                        switch (gameboard[row][col]) {
                            case NOUGHT:
                                System.out.print("O");
                                break;
                            case CROSS:
                                System.out.print("X");
                                break;
                            default:
                                System.out.print(" ");
                        }
                        System.out.print("|");
                    }
     
     
                            System.out.println("\n-------\n");
     
                    }
                    }
            }

    The output looks like this.
    run:
    | | | |
    -------
     
    | | | |
    -------
     
    | | | |
    -------
     
    O's turn
    Enter row and column:

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need help please.

    Glad it's working.
    If you don't understand my answer, don't ignore it, ask a question.