Page 2 of 2 First 12
  • Jump to page:
    #16
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    59
    Rep Power
    2
    Yes I used integer variable not Boolean. I updated my code if you want you can check it out.
  2. #17
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    I updated my code
    I still see these:
    int outcome = -2;
    } while ( outcome == -2 );
    static boolean winOrTie() {

    Where did you post the updated code?
  4. #18
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    59
    Rep Power
    2
    I am working on some logic to check for possible wins in the tic tac toe java below. Thanks for the help I hope this updated version is better.

    Code:
    
    import java.util.Scanner;  
      
    public class Good {  
      
        public static String[][] gameboard;  
      
        // The whole game should be wrapped in a game object and for each play  
        // create an instance. This variable belong in the instance.  
        private static boolean gameComplete = false;  
        private static Player winner = null;  
      
        static boolean set(String val, int row, int col) throws  
                IllegalArgumentException {  
            if (gameboard[row - 1][col - 1] == null) {  
                gameboard[row - 1][col - 1] = val;  
                return true;  
            }  
            return false;  
        }  
      
        static void displayBoard() {  
            System.out.println("\n-------\n");  
            for (int r = 0; r < gameboard.length; r++) {  
                System.out.print("|");  
                for (int c = 0; c < gameboard[r].length; c++) {  
                    if (gameboard[r][c] == null) {  
                        System.out.print(" ");  
                    } else {  
                        System.out.print(gameboard[r][c]);  
                    }  
                    System.out.print("|");  
                }  
                System.out.println("\n-------\n");  
            }  
        }  
      
        static void createBoard(int row, int col) {  
            gameboard = new String[row][col];  
        }  
      
        static void checkGameStatus() {  
            /* 
             * TODO this method needs to check game status and update two params, 
             * gameComplete and winner. 
             */  
    
      
        }  
      
       
        public static void main(String[] args) {  
            Scanner scan = new Scanner(System.in);  
      
            createBoard(3, 3);  
            int turn = 0;  
      
            boolean b = true;  
      
            Player[] players = { Player.ONE, Player.TWO };  
            int currentPlayerIndex = 0;  
      
            do {  
                displayBoard();  
                currentPlayerIndex = (turn % 2 == 0) ? 0 : 1;  
                System.out.println(String.format("\n-%s's turn-",  
                        players[currentPlayerIndex].getPieceValue()));  
      
                System.out.print("Enter row and column:");  
      
                boolean validMove = false;  
      
                do {  
                    System.out.print("Enter row and column:");  
                    validMove = set(players[currentPlayerIndex].getPieceValue(),  
                            scan.nextInt(), scan.nextInt());  
                    if (!validMove) {  
                        System.out  
                                .println("Sorry, you've entered an invalid move.");  
                    }  
                } while (validMove == false);  
      
                checkGameStatus();  
                turn++;  
            } while (!gameComplete);  
      
            if (winner != null) {  
                System.out  
                        .println(String.format("%s wins!", winner.getPieceValue()));  
            } else {  
                System.out.println("Tie");  
            }  
        }  
      
        private enum Player {  
            ONE("X"),  
            TWO("O");  
      
            private String piece;  
      
            private Player(String piece) {  
                this.piece = piece;  
            }  
      
            public String getPieceValue() {  
                return piece;  
                
    
                
            }  
        }  
      
    }
    Last edited by Bykenhaal; December 18th, 2013 at 06:39 PM.
  6. #19
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    59
    Rep Power
    2
    I am having trouble with the checkGameStatus I can not get the game to give me a winner. This might be because I do not have a do statement with true. What do you think?
  8. #20
  9. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    I am having trouble with the checkGameStatus I can not get the game to give me a winner.
    That method has no code in it.

    Hint for faster testing: Preload the Scanner class constructor with some moves. Here is an example from the earlier version of the program:
    Code:
            Scanner scan = new Scanner("0 0  1 1  2 2  0 1  0 2  1 0  1 1  1 2  2 0  2 1"); // System.in);
    When the program is executed the input will come from that String of digits instead of the user having to input them.
Page 2 of 2 First 12
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo