Results 1 to 9 of 9
Thread: Auto Game
- 03-07-2015, 11:15 PM #1
Member
- Join Date
- Jan 2015
- Posts
- 31
- Rep Power
- 0
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; } } }
- 03-08-2015, 01:30 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 19
Re: Auto Game
The method is supposed to stop once the payer or the computer wins
Java Code:playerWon() != true || computerWon() != true
- 03-09-2015, 04:06 AM #3
Member
- Join Date
- Jan 2015
- Posts
- 31
- Rep Power
- 0
Re: Auto Game
That still does not work if you use && instead of ||. It still goes until both have won.
- 03-09-2015, 04:18 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 19
Re: Auto Game
It still goes until both have won
- 03-09-2015, 04:44 AM #5
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: Auto Game
Why don't you try this out on paper.
Java Code:do{ playerTurn(); if(pTurn == false){ computerTurn(); } }while(condition);
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 03-09-2015, 07:43 AM #6
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){
If you're forever cleaning cobwebs, it's time to get rid of the spiders.
- 03-09-2015, 10:47 AM #7
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 13
Re: Auto Game
Last edited by gimbal2; 03-09-2015 at 11:46 AM.
"Syntactic sugar causes cancer of the semicolon." -- Alan Perlis
- 03-09-2015, 11:21 AM #8
- 03-09-2015, 11:46 AM #9
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 13
Similar Threads
-
Executing my game from within an auto expander.
By Doctor_N in forum Advanced JavaReplies: 0Last Post: 06-26-2013, 08:40 AM -
Auto contrast and auto brightness
By oxxxis in forum Java 2DReplies: 0Last Post: 01-21-2010, 08:32 PM -
auto log off
By simontkk2005 in forum AWT / SwingReplies: 1Last Post: 11-18-2009, 03:55 PM -
JSP Auto Log -In
By Eku in forum JavaServer Pages (JSP) and JSTLReplies: 3Last Post: 09-18-2008, 03:58 AM -
Auto-complete/Auto-fix for custom statement
By dark_cybernetics in forum EclipseReplies: 0Last Post: 08-19-2008, 11:19 AM
Bookmarks