rjp2525

Java Pig Game

Jan 16th, 2014
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.96 KB | None | 0 0
  1. // Pig Game
  2. // Driver class using PairOfDice that plays a game of pig.  On each turn, the player rolls a pair of dice and adds up his or her points.
  3. // Whoever reaches 100 points first, wins. If a player rolls a 1, he or she loses all points for that round and the dice go to the other player.
  4. // If a player rolls two, 1s in one turn, the player loses all points earned so far in the game and loses control of the dice.
  5. // The player may voluntarily turn over the dice after each roll. So the player must decide to either roll again (be a pig) and risk losing points,
  6. // or give up the dice, possibly letting the other player win. Comp gives up dice after winning 20 points in one round.
  7.  
  8. import cs1.Keyboard;
  9. public class Pig
  10. {
  11.     public static void main(String[] args)
  12.     {
  13.         // create variables, call dice object
  14.         String input = "";
  15.         PairOfDice dice = new PairOfDice();
  16.         int userCount = 0, compCount = 0;
  17.        
  18.        
  19.         System.out.println("Welcome to Pig!  You and the computer are competing to roll dice up to a total of 100 points.\nYou keep rolling until you roll a 1, in which case you lose all points you've acquired in that round and relinquish control to the computer.\nIf you roll two 1s, you lose all points in the game.  You can also choose to give up the dice to the computer and end your round.  \nReady to play?");
  20.         // continues the game by allowing switch between user and computer
  21.         while(userCount < 100 && compCount < 100)
  22.         {
  23.            
  24.             // user turn
  25.            
  26.             // total for this round
  27.             int userRound = 0;
  28.             do
  29.             {
  30.                
  31.                 System.out.println("  Roll the dice (r), or give up the dice (g)?");
  32.                 input = Keyboard.readString();
  33.                 // if user rolls the dice
  34.                 if(input.equals("r"))
  35.                 {
  36.                     int thisRoll = dice.sumOf();
  37.                     if (dice.hasOne() == false && dice.hasTwoOnes() == false)
  38.                     {
  39.                         userRound += thisRoll;
  40.                         System.out.println("You rolled a total of " + thisRoll + ". Your total for the round is now " + userRound + " and your game total is " + (userCount + userRound));
  41.                     }
  42.                     else
  43.                     {
  44.                         if(dice.hasOne() == true)
  45.                         {
  46.                             userRound = 0;
  47.                             System.out.println("You rolled a 1, you lost all points for the round.");
  48.                             if(dice.hasTwoOnes() == true)
  49.                             {
  50.                                 System.out.print("  You also rolled another 1, so you lost all points.");
  51.                                 userCount = 0;
  52.                             }
  53.                             System.out.println("Your game total is " + userCount + ". Dice now switching hands to the computer.\n");
  54.                         }
  55.                        
  56.                     }
  57.                 }
  58.                 // if user gives up the dice
  59.                 if(input.equals("g"))
  60.                 {
  61.                     userCount += userRound;
  62.                     System.out.println("  Your game total is " + userCount + ". Dice now switching hands to the computer.\n");
  63.                 }
  64.                
  65.             }
  66.             while(input.equals("r") && dice.hasOne() == false && userCount < 100);
  67.        
  68.             if(userCount >= 100)
  69.                 break;
  70.            
  71.             //computer turn
  72.            
  73.             int compRound = 0;
  74.             do
  75.             {
  76.                
  77.                 int thisRoll = dice.sumOf();
  78.                     if (dice.hasOne() == false && dice.hasTwoOnes() == false)
  79.                     {
  80.                         compRound += thisRoll;
  81.                         System.out.println("computer rolled a " + thisRoll + ".  Its total for the round is now " + compRound + " and its game total is " + (compCount + compRound));
  82.                     }
  83.                     else
  84.                     {
  85.                         if(dice.hasOne() == true)
  86.                         {
  87.                             compRound = 0;
  88.                             System.out.println("Computer rolled a 1.");
  89.                             if(dice.hasTwoOnes() == true)
  90.                             {
  91.                                 System.out.print("  Computer also rolled another 1, so it lost all points.");
  92.                                 compCount = 0;
  93.                             }
  94.                             System.out.println("  Computer game total is " + compCount + ". Dice now switching hands to the player.\n");
  95.                         }
  96.                        
  97.                     }
  98.             }
  99.                    
  100.             while(compRound < 20 && dice.hasOne() == false && compCount < 100);
  101.             compCount += compRound;
  102.        
  103.             if(compCount >= 100)
  104.                 break;
  105.        
  106.         }
  107.         if(userCount >= 100)
  108.             System.out.println("-----Congratulations, you win!-----");
  109.         if(compCount >= 100)
  110.             System.out.println("-----Too bad, the computer wins.-----");
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment