Results 1 to 9 of 9

Thread: Auto Game

  1. #1
    glockner is offline Member
    Join Date
    Jan 2015
    Posts
    31
    Rep Power
    0

    Default Auto Game

    I am working on a dice game and I am having trouble with the autoGame method. The method is supposed to stop once the payer or the computer wins but it continues until both the player and computer have reached 100+. Not sure why this is happening. autoGame method is line 182.

    Java Code:
    import javax.swing.JOptionPane;
    /**************************************
     * Game of dice pig
     * 
     * @author 
     * @version March 9, 2015
     **************************************/
    public class PigGame
    {
        /** Dice */
        private GVdie d1,d2;
    
        /** Player Score */
        private int pScore;
    
        /** Computer Score */
        private int cScore;
    
        /** Round Score */
        private int rScore;
    
        /** Winning Score */
        private final int WSCORE = 100;
    
        /** Number of rounds */
        private int rNum;
    
        /** Is Player Turn */
        private boolean pTurn;
    
        /*********************************
        Constructor
         *********************************/
        public PigGame(){
            //Set initial values
            System.out.println("Welcome to George's Game of Pig! \n");
            pScore = 0;
            cScore = 0;
            rScore = 0;
            rNum = 0;
            d1 = new GVdie();
            d2 = new GVdie();
        }
    
        private void rollDice(){
            //Declare variables for values of both dice
            int val1;
            int val2;
    
            //Roll both dice
            d1.roll();
            d2.roll();
    
            //Assign the value rolled by each die to given value
            val1 = d1.getValue();
            val2 = d2.getValue();
    
            //Set values of Round Score and Player Score depending on roll of dice
            if (val1 == 1 || val2 == 1){
                rScore = 0;
            }
            else{
                rScore += val1 + val2;
            }
    
            System.out.println("" + val1 + " " + val2 + " Round Score: " + rScore);
        }
    
        public void playerRolls(){
            //Invoke roll dice method
            rollDice();
    
            //Outputs for different situations
            if(d1.getValue() + d2.getValue() == 2){
                pScore = 0;
                rScore = 0;
                rNum ++;
                System.out.println("---- Your Score: " + pScore);
                System.out.println("");
    
            }
            else if(rScore == 0){
                rNum ++;
                System.out.println("---- Your Score: " + pScore);
                System.out.println("");
    
            }
            else if (pScore + rScore >= WSCORE){
                rNum ++; 
                System.out.println("---- Your Score: " + (pScore + rScore));
                System.out.println("You won! \n"); 
                System.out.println("Number of rounds: " + rNum);
    
            }
    
        }
    
        public void playerHolds(){
            //Calcluate players score, set round score back to 0, print players score
            pScore += rScore;
            rScore = 0;
            rNum ++;
            System.out.println("---- Your Score: " + pScore);
            System.out.println("");
            pTurn = false;
    
        }
    
        public void computerTurn(){  
            //Set rScore to 0;
            rScore = 0; 
    
            //Continues to roll dice until condition met
            do{
                rollDice();
            }while(d1.getValue() != 1 && d2.getValue() != 1 && rScore <= 19 && (cScore + rScore)<= WSCORE);
    
            //Updates computer score
            cScore += rScore;
    
            //Conditions for computer and round score
            if (d1.getValue() + d2.getValue() == 2){
                cScore = 0;
                rScore = 0;
                pTurn = true;
            }
            else if (d1.getValue() == 1 || d2.getValue() == 1){
                rScore = 0;
                pTurn = true;
            }
            else if (cScore >= WSCORE){
                System.out.println("Computer Wins! \n");
                System.out.println("Number of rounds: " + (rNum + 1));
    
            }
    
            //Print computer score, reset round score to 0, increases round # by 1
            System.out.println("---- Computer Score: " + cScore);
            System.out.println("");
            rScore = 0;
            rNum ++;
            pTurn = true;
    
        }
        
        private void playerTurn(){  
            //Set rScore to 0;
            rScore = 0; 
    
            //Continues to roll dice until condition met
            do{
                rollDice();
            }while(d1.getValue() != 1 && d2.getValue() != 1 && rScore <= 19 && (pScore + rScore) <= WSCORE);
    
            //Updates computer score
            pScore += rScore;
    
            //Conditions for computer and round score
            if (d1.getValue() + d2.getValue() == 2){
                pScore = 0;
                rScore = 0;
                pTurn = false;
            }
            else if (d1.getValue() == 1 || d2.getValue() == 1){
                rScore = 0;
                pTurn = false;
            }
            else if (pScore >= WSCORE){
                System.out.println("You win! \n");
                System.out.println("Number of rounds: " + (rNum + 1));
    
            }
            //Print computer score, reset round score to 0, increases round # by 1
            System.out.println("---- Your Score: " + pScore);
            System.out.println("");
            rScore = 0;
            rNum ++;
            pTurn = false;
    
        }
    
        public void autoGame(){  
           
            do{
                playerTurn();
                if(pTurn == false){
                    computerTurn();
                }
            }while(playerWon() != true || computerWon() != true);
            
            reset();
        }
    
        /***************************************************************************
        Reset Game
         ****************************************************************************/
        public void reset(){
            //Resets register totals to 0
            String message = "Reset Game?";
            int answer = JOptionPane.showConfirmDialog(null, message); 
            if (answer == JOptionPane.YES_OPTION) {
                pScore = 0;
                cScore = 0;
                rScore = 0;
                rNum = 0;
    
            }
        }
    
        /***************************************************************************
        Determine if it is player's turn
    
        @return player turn
         ****************************************************************************/     
        public boolean isPlayerTurn(){
            return pTurn;
        }
    
        /***************************************************************************
        Get round score
    
        @return round score
         ****************************************************************************/
        public int getRoundScore(){
            return rScore;
        }
    
        /***************************************************************************
        Get player score
    
        @return player score
         ****************************************************************************/
        public int getPlayerScore(){
            return pScore;
        }
    
        /***************************************************************************
        Get computer score
    
        @return computer score
         ****************************************************************************/
        public int getComputerScore(){
            return cScore;
        }
    
        /***************************************************************************
        Get die value
    
        @return die value
        @param num
         ****************************************************************************/
        public GVdie getDie (int num){
            if(num == 1){
                return d1;
            }
            else if(num == 2){
                return d2;
            }
            return d1;
        }
    
        /***************************************************************************
        Did player win
    
        @return player won
         ****************************************************************************/
        public boolean playerWon(){
            if (pScore >= WSCORE){
                return true;
            }else{
                return false;
            }     
        }
    
        /***************************************************************************
        Did computer win
    
        @return computer win
         ****************************************************************************/
        public boolean computerWon(){
            if (cScore >= WSCORE){
                return true;
            }else{
                return false;
            }
        }
    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,716
    Rep Power
    19

    Default Re: Auto Game

    The method is supposed to stop once the payer or the computer wins
    But that is not what the condition you have written is saying

    Java Code:
    playerWon() != true || computerWon() != true
    That condition is saying the loop should coninue while either have not won. You mean to say that the loop should continue while both have not won.

  3. #3
    glockner is offline Member
    Join Date
    Jan 2015
    Posts
    31
    Rep Power
    0

    Default Re: Auto Game

    That still does not work if you use && instead of ||. It still goes until both have won.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,716
    Rep Power
    19

    Default Re: Auto Game

    It still goes until both have won
    What still goes on until both have won? Post code.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    15

    Default Re: Auto Game

    Why don't you try this out on paper.

    Java Code:
    do{ 
       playerTurn();
       if(pTurn == false){
           computerTurn();
       }
    }while(condition);
    Now, you want to exit the loop when the condition is false, so draw a truth table with player and computer and decide under what situation you want to exit the loop. You can use || and && for two truth tables. This will ultimately save you time over guessing.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    27

    Default Re: Auto Game

    A note: never compare boolean variables (or expressions) with the boolean literals true and false. It's not only unnecessarily verbose, it's also error-prone.
    Java Code:
    // if(pTurn == false) {
    if(!pTurn){
    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    13

    Default Re: Auto Game

    Quote Originally Posted by DarrylBurke View Post
    it's also error-prone.
    Because among other things this is possible:

    Java Code:
    if(pTurn = true){  // whoops, forgot an =
       System.out.println("This compiles and runs but won't ever print anything");
    }
    This compiles and runs but might misbehave when you least expect it.
    Last edited by gimbal2; 03-09-2015 at 11:46 AM.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    27

    Default Re: Auto Game

    Quote Originally Posted by gimbal2 View Post
    Because among other things this is possible:

    Java Code:
    if(pTurn = false){  // whoops, forgot an =
       System.out.println("This prints even when pTurn was true!");
    }
    Um, no, it doesn't print even when pTurn was false.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  9. #9
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    13

    Default Re: Auto Game

    Yep; if you don't test it, its broken. Well your quote proves that I goofed, let me just fix up the post :)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Executing my game from within an auto expander.
    By Doctor_N in forum Advanced Java
    Replies: 0
    Last Post: 06-26-2013, 08:40 AM
  2. Auto contrast and auto brightness
    By oxxxis in forum Java 2D
    Replies: 0
    Last Post: 01-21-2010, 08:32 PM
  3. auto log off
    By simontkk2005 in forum AWT / Swing
    Replies: 1
    Last Post: 11-18-2009, 03:55 PM
  4. JSP Auto Log -In
    By Eku in forum JavaServer Pages (JSP) and JSTL
    Replies: 3
    Last Post: 09-18-2008, 03:58 AM
  5. Auto-complete/Auto-fix for custom statement
    By dark_cybernetics in forum Eclipse
    Replies: 0
    Last Post: 08-19-2008, 11:19 AM

Posting Permissions

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