Help with this java method.....
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 27

Thread: Help with this java method.....

  1. #1
    Join Date
    Apr 2017
    Posts
    13

    Help with this java method.....

    I am trying to complete a tic tac toe game in Java, using Netbeans. My issue is in the win or tie method. Here is what I have so far with the method....

    Code:
    static int winOrTie() {
        if (gameboard[0][0] + gameboard[0][1] + gameboard[0][2] == 3*NOUGHT) 
        {return NOUGHT;}
        if (gameboard[1][0] + gameboard[1][1] + gameboard[1][2] == 3*NOUGHT)
        {return NOUGHT;}
        if (gameboard[2][0] + gameboard[2][1] + gameboard[2][2] == 3*NOUGHT)
        {return NOUGHT;}
     
        if (gameboard[0][0] + gameboard[1][0] + gameboard[2][0] == 3*NOUGHT)
        {return NOUGHT;}
        if (gameboard[0][1] + gameboard[1][1] + gameboard[2][1] == 3*NOUGHT)
        {return NOUGHT;}
        if (gameboard[0][2] + gameboard[1][2] + gameboard[2][2] == 3*NOUGHT)
        {return NOUGHT;}
        if (gameboard[0][0] + gameboard[1][1] + gameboard[2][2] == 3*NOUGHT)
        {return NOUGHT;}
        if (gameboard[0][2] + gameboard[1][1] + gameboard[2][0] == 3*NOUGHT)
        {return NOUGHT;}
    
        if (gameboard[0][0] + gameboard[0][1] + gameboard[0][2] == 3*CROSS)
        {return CROSS;}    
        if (gameboard[1][0] + gameboard[1][1] + gameboard[1][2] == 3*CROSS)
        {return CROSS;}
        if (gameboard[2][0] + gameboard[2][1] + gameboard[2][2] == 3*CROSS)
        {return CROSS;}
    
        if (gameboard[0][0] + gameboard[1][0] + gameboard[2][0] == 3*CROSS)
        {return CROSS;}    
        if (gameboard[0][1] + gameboard[1][1] + gameboard[2][1] == 3*CROSS)
        {return CROSS;}
        if (gameboard[0][2] + gameboard[1][2] + gameboard[2][2] == 3*CROSS)
        {return CROSS;}
        
        if (gameboard[0][0] + gameboard[1][1] + gameboard[2][2] == 3*CROSS)
        {return CROSS;} 
        if (gameboard[0][2] + gameboard[1][1] + gameboard[2][0] == 3*CROSS)
        {return CROSS;}
    
        
        if(){
            return ;
        else if ()    
            return ;
        else if ()
            return ;
        else
            return -2;
            
        }
        }
    The last section of code the "if else if" is where my issue is. I can't seem to figure out what code goes in to get this working right.

    Any help would be appreciated.

  2. #2
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,733

    Re: Help with this java method.....

    what code goes in to get this working right.
    Please explain what the code is supposed to do in some detail so that the code can be designed.
    Norm

  3. #3
    Join Date
    Apr 2017
    Posts
    13

    Re: Help with this java method.....

    Hi,

    I am getting this in the output section when I run the project...

    Code:
    run: 
    | | | | 
    ------ 
    
    | | | | 
    ------ 
    
    | | | | 
    ------

    It should look like this in the output section..

    Code:
    |||| 
    ------- 
    |||| 
    ------- 
    |||| 
    ------- 
    --Os turn-- 
    |O| | | 
    ------- 
    |||| 
    ------- 
    |||| 
    ------- 
    --Xs turn-- 
    |O|X| | 
    ------- |||| ------- |||| ------- 
    
    --Os turn-- 
    |O|X| | 
    ------- 
    | |O| | 
    ------- 
    |||| 
    ------- 
    --Xs turn-- 
    |O|X| | 
    ------- 
    | |O| | 
    ------- 
    |X| | | 
    ______ 
    Os turn 
    |O|X| | 
    ------- 
    | |O| | 
    ------- 
    |X| |O| 
    O wins!

    Here is the complete code I have, including the winortie method..



    Code:
    package tictactoegame;
    
    /**
     *
     * @author doughenderson
     */
    public class tictactoegame {
        
        static int [] [] gameboard;
        static final int EMPTY = 0;
        static final int NOUGHT = -1;
        static final int CROSS = 1;
        
        static void set (int val, int row)
                throws IllegalArgumentException {
            int col = 0;
            if (gameboard[row] [col]  == EMPTY)
                    gameboard[row] [col] = val;
            else throw new
                IllegalArgumentException("Player already there!");
        }
        
        static void displayBoard () {
            for (int[] gameboard1 : gameboard) {
                System.out.print("|");
                for (int c = 0; c < gameboard1.length; c++) {
                    switch (gameboard1[c]) {
                        case NOUGHT:
                            System.out.print("0");
                            break;
                        case CROSS:
                            System.out.print("X");
                            break;
                        default:           //Empty
                            System.out.print(" ");
                    }
                    System.out.print("|");
                }
                System.out.println("\n------\n");
            }
        }
        static void createBoard(int rows, int cols) {
            gameboard = new int [rows] [cols];
        }
        
        static int winOrTie() {
        if (gameboard[0][0] + gameboard[0][1] + gameboard[0][2] == 3*NOUGHT) 
        {return NOUGHT;}
        if (gameboard[1][0] + gameboard[1][1] + gameboard[1][2] == 3*NOUGHT)
        {return NOUGHT;}
        if (gameboard[2][0] + gameboard[2][1] + gameboard[2][2] == 3*NOUGHT)
        {return NOUGHT;}
     
        if (gameboard[0][0] + gameboard[1][0] + gameboard[2][0] == 3*NOUGHT)
        {return NOUGHT;}
        if (gameboard[0][1] + gameboard[1][1] + gameboard[2][1] == 3*NOUGHT)
        {return NOUGHT;}
        if (gameboard[0][2] + gameboard[1][2] + gameboard[2][2] == 3*NOUGHT)
        {return NOUGHT;}
        if (gameboard[0][0] + gameboard[1][1] + gameboard[2][2] == 3*NOUGHT)
        {return NOUGHT;}
        if (gameboard[0][2] + gameboard[1][1] + gameboard[2][0] == 3*NOUGHT)
        {return NOUGHT;}
    
        if (gameboard[0][0] + gameboard[0][1] + gameboard[0][2] == 3*CROSS)
        {return CROSS;}    
        if (gameboard[1][0] + gameboard[1][1] + gameboard[1][2] == 3*CROSS)
        {return CROSS;}
        if (gameboard[2][0] + gameboard[2][1] + gameboard[2][2] == 3*CROSS)
        {return CROSS;}
    
        if (gameboard[0][0] + gameboard[1][0] + gameboard[2][0] == 3*CROSS)
        {return CROSS;}    
        if (gameboard[0][1] + gameboard[1][1] + gameboard[2][1] == 3*CROSS)
        {return CROSS;}
        if (gameboard[0][2] + gameboard[1][2] + gameboard[2][2] == 3*CROSS)
        {return CROSS;}
        
        if (gameboard[0][0] + gameboard[1][1] + gameboard[2][2] == 3*CROSS)
        {return CROSS;} 
        if (gameboard[0][2] + gameboard[1][1] + gameboard[2][0] == 3*CROSS)
        {return CROSS;}
    
        
        if(){
            return ;
        else if ()    
            return ;
        else if ()
            return ;
        else
            return -2;
            
        }
        }  
               
         
         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 ("\n-0's turn-");
                } else {
                    System.out.println("\n-X's turn-");
                    }
                System.out.print("Enter row and Column:");
                try {
                    set(playerVal, scan.nextInt());
                } catch (IllegalArgumentException ex)
                {System.err.println(ex);}
                turn ++;
                outcome = winOrTie();
            } while ( outcome == -2 );
            displayBoard();
            switch (outcome) {
                case NOUGHT:
                    System.out.println("0 wins!");
                    break;
                case CROSS:
                    System.out.println("X wins!");
                    break;
                case 0:
                    System.out.println("Tie.");
                    break;
                }
         }
         
    }
    If I can get the if/else if statement working right, with the right code I should have the out put section (when running the project) looking like what I have above.

  4. #4
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,733

    Re: Help with this java method.....

    get the if/else if statement working right, with the right code
    Can you explain what the "right code" should be doing? Given a description of what it is supposed to do, we can help you write the code to do it.
    Norm

  5. #5
    Join Date
    Apr 2017
    Posts
    13

    Re: Help with this java method.....

    I need to define the winOrTie method. Check first for a win with rows and columns and then diagonally. I then need to check to see if there are any empty cells without a CROSS or NOUGHT.

    My issue seems to be in the if/else if statement. CROSS, NOUGHT, Tie should be in the code. I just can't seem to put the correct code in so it behaves as it should in the output section.

    Code:
    static int winOrTie() {
        if (CROSS has won)
            return ...
        else if (NOUGHT has won)
            return ...
        else if (The game ends in a Tie)
            return ...
        else
            return -2;
    }


    When I click to 'run' the program this appears in the out put section.

    Code:
    run: 
    | | | | 
    ------ 
    
    | | | | 
    ------ 
    
    | | | | 
    ------

    The above is incorrect, as it should behave and show the following in the output section...



    Code:
    |||| 
    ------- 
    |||| 
    ------- 
    |||| 
    ------- 
    --Os turn-- 
    |O| | | 
    ------- 
    |||| 
    ------- 
    |||| 
    ------- 
    --Xs turn-- 
    |O|X| | 
    ------- |||| ------- |||| ------- 
    
    --Os turn-- 
    |O|X| | 
    ------- 
    | |O| | 
    ------- 
    |||| 
    ------- 
    --Xs turn-- 
    |O|X| | 
    ------- 
    | |O| | 
    ------- 
    |X| | | 
    ______ 
    Os turn 
    |O|X| | 
    ------- 
    | |O| | 
    ------- 
    |X| |O| 
    O wins!

  6. #6
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,733

    Re: Help with this java method.....

    Code:
       if (CROSS has won)
            return ...
        else if (NOUGHT has won)
            return ...
        else if (The game ends in a Tie)
            return ...
        else
            return -2;
    Ok that is a start. Now define what CROSS has won and NOUGHT has won means. Are there variables with values that the program can look at to make the decision?

    What is the purpose of all the if statements that are before the above statements in the winOrTie mehtod?
    Don't they already return a value when there is a winner? What is left to do if no winner was found?
    Last edited by Norm; April 5th, 2017 at 05:37 AM.
    Norm

  7. #7
    Join Date
    Apr 2017
    Posts
    13

    Re: Help with this java method.....

    Okay...

    Looking through the code I see this..

    playerVal = (turn % 2 == 0)? NOUGHT : CROSS;

    The purpose of all those if statements are to execute the statement. If no winner is found it is a Tie.

  8. #8
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,733

    Re: Help with this java method.....

    What is wrong with the existing if statements in winOrTie?
    Why are you trying to add those 4 if/else if/else statements after the big block of if statements?
    Norm

  9. #9
    Join Date
    Apr 2017
    Posts
    13

    Re: Help with this java method.....

    It can be condensed via for loop I believe. I am adding those if/else statement to 'activate' the code.

  10. #10
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,733

    Re: Help with this java method.....

    Does the program work when you remove those last uncompleted if/else if/else statements?
    If not, please explain what happens?
    Norm

  11. #11
    Join Date
    Apr 2017
    Posts
    13

    Re: Help with this java method.....

    When I delete the last uncompleted if/else/if/else statements and click the run project button it says...

    "One or more projects are compiled with errors."

    So I click the "run anyway" button. It then continuously runs unless I click the stop button. This is what shows up in the output section...


    Code:
    | | | |
    ------
    
    | | | |
    ------
    
    | | | |
    ------
    
    
    -0's turn-
    Enter row and Column:

  12. #12
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,733

    Re: Help with this java method.....

    compiled with errors.
    You need to copy the full text of the compiler's errors and paste them here if you need help fixing them.
    Norm

  13. #13
    Join Date
    Apr 2017
    Posts
    13

    Re: Help with this java method.....

    I removed the error(s) which was just one "Missing return statement" on line 89. Still when I click the green arrow to run the project in continuously runs unless I hit the stop button. No errors now but it still shows the following in the out put.


    Code:
    run:
    | | | |
    ------
    
    | | | |
    ------
    
    | | | |
    ------
    
    
    -0's turn-
    Enter row and Column
    :

  14. #14
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,733

    Re: Help with this java method.....

    Missing return statement
    There should be a return statement at the end that returns the value for when no 3 in a row was found.

    the project in continuously runs unless I hit the stop button
    Please post the full code for the program that will compile, execute and show the problem.
    Also post the contents of the console showing what was printed and what you entered as input.
    Last edited by Norm; April 11th, 2017 at 06:59 AM.
    Norm

  15. #15
    Join Date
    Apr 2017
    Posts
    13

    Re: Help with this java method.....

    Here is the complete code....


    Code:
    package tictactoegame;
    
    /**
     *
     * @author Doughenderson
     */
    public class tictactoegame {
        
        static int [] [] gameboard;
        static final int EMPTY = 0;
        static final int NOUGHT = -1;
        static final int CROSS = 1;
        
        static void set (int val, int row)
                throws IllegalArgumentException {
            int col = 0;
            if (gameboard[row] [col]  == EMPTY)
                    gameboard[row] [col] = val;
            else throw new
                IllegalArgumentException("Player already there!");
        }
        
        static void displayBoard () {
            for (int[] gameboard1 : gameboard) {
                System.out.print("|");
                for (int c = 0; c < gameboard1.length; c++) {
                    switch (gameboard1[c]) {
                        case NOUGHT:
                            System.out.print("0");
                            break;
                        case CROSS:
                            System.out.print("X");
                            break;
                        default:           //Empty
                            System.out.print(" ");
                    }
                    System.out.print("|");
                }
                System.out.println("\n------\n");
            }
        }
        static void createBoard(int rows, int cols) {
            gameboard = new int [rows] [cols];
        }
        
        static int winOrTie() {
        if (gameboard[0][0] + gameboard[0][1] + gameboard[0][2] == 3*NOUGHT) 
        {return NOUGHT;}
        if (gameboard[1][0] + gameboard[1][1] + gameboard[1][2] == 3*NOUGHT)
        {return NOUGHT;}
        if (gameboard[2][0] + gameboard[2][1] + gameboard[2][2] == 3*NOUGHT)
        {return NOUGHT;}
     
        if (gameboard[0][0] + gameboard[1][0] + gameboard[2][0] == 3*NOUGHT)
        {return NOUGHT;}
        if (gameboard[0][1] + gameboard[1][1] + gameboard[2][1] == 3*NOUGHT)
        {return NOUGHT;}
        if (gameboard[0][2] + gameboard[1][2] + gameboard[2][2] == 3*NOUGHT)
        {return NOUGHT;}
        if (gameboard[0][0] + gameboard[1][1] + gameboard[2][2] == 3*NOUGHT)
        {return NOUGHT;}
        if (gameboard[0][2] + gameboard[1][1] + gameboard[2][0] == 3*NOUGHT)
        {return NOUGHT;}
    
        if (gameboard[0][0] + gameboard[0][1] + gameboard[0][2] == 3*CROSS)
        {return CROSS;}    
        if (gameboard[1][0] + gameboard[1][1] + gameboard[1][2] == 3*CROSS)
        {return CROSS;}
        if (gameboard[2][0] + gameboard[2][1] + gameboard[2][2] == 3*CROSS)
        {return CROSS;}
    
        if (gameboard[0][0] + gameboard[1][0] + gameboard[2][0] == 3*CROSS)
        {return CROSS;}    
        if (gameboard[0][1] + gameboard[1][1] + gameboard[2][1] == 3*CROSS)
        {return CROSS;}
        if (gameboard[0][2] + gameboard[1][2] + gameboard[2][2] == 3*CROSS)
        {return CROSS;}
        
        if (gameboard[0][0] + gameboard[1][1] + gameboard[2][2] == 3*CROSS)
        {return CROSS;} 
        if (gameboard[0][2] + gameboard[1][1] + gameboard[2][0] == 3*CROSS)
        {return CROSS;}
            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 ("\n-0's turn-");
                } else {
                    System.out.println("\n-X's turn-");
                    }
                System.out.print("Enter row and Column:");
                try {
                    set(playerVal, scan.nextInt());
                } catch (IllegalArgumentException ex)
                {System.err.println(ex);}
                turn ++;
                outcome = winOrTie();
            } while ( outcome == -2 );
            displayBoard();
            switch (outcome) {
                case NOUGHT:
                    System.out.println("0 wins!");
                    break;
                case CROSS:
                    System.out.println("X wins!");
                    break;
                case 0:
                    System.out.println("Tie.");
                    break;
                }
         }
         
    }

Page 1 of 2 12 LastLast

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


On-Demand Webinars (sponsored)