Advertisement
Guest User

Practice (game of 'Pig')

a guest
Jul 30th, 2013
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.62 KB | None | 0 0
  1. import java.util.Scanner;
  2. public class chapterThreeProblemTen
  3. {
  4.     /*This program simulates a game of 'Pig', in which two players role a
  5.      *2d6 die taking turns till they reach 100 points. On a players turn
  6.      *they role the die until they get a '1', or choose to 'hold'. If they
  7.      *get a '1', their turn is over and they don't get any points. If they
  8.      *choose to 'hold', that player keeps all the points they have earned
  9.      *up to that point and add them to their score. (Since one of the
  10.      *players here is a computer, they limit themselves to <= 20 points in
  11.      *a turn)*/
  12.     public static void main(String[] args)
  13.     {
  14.         Scanner keyboard = new Scanner(System.in);
  15.        
  16.         //obvious variables
  17.         int humanScore = 0;
  18.         int computerScore = 0;
  19.         int diceRoll = 0;
  20.        
  21.         boolean playerTurn = true; //If the number is true, it is the players turn.
  22.        
  23.         int turnTotal = 0; //This variable is the amount of points one has
  24.                             //rolled in their turn. This is temporary,
  25.                             //because rolling a '1' will make this '0'
  26.        
  27.         String playerInput = "r"; //This variable controls the if statement that
  28.                                 //controls whether players continue to roll
  29.                                 //or hold with whatever point they may have
  30.                                 //'r' is roll; 'h' is hold;
  31.        
  32.         System.out.println("Score to win is 100.");
  33.        
  34.         if ((humanScore < 100) || (computerScore < 100))
  35.         {
  36.             do
  37.             {
  38.                 System.out.println("Your score is: " + humanScore);
  39.                 System.out.println("Computers score is: "+ computerScore);
  40.                 System.out.println("Points this turn: " + turnTotal);
  41.                 System.out.println("type 'r' to roll or 'h' to hold.");
  42.                 playerInput = keyboard.nextLine();
  43.                
  44.                 if (playerInput.equalsIgnoreCase("r"))
  45.                 {
  46.                     diceRoll = ( int )(Math.random() * 6) + 1;
  47.                     System.out.println("You rolled a " + diceRoll);
  48.                    
  49.                     if (diceRoll != 1)
  50.                     {
  51.                         turnTotal = (turnTotal + diceRoll);
  52.                     }
  53.                     else // terminal condition dice =1, break from turn
  54.                     {
  55.                         turnTotal = 0;
  56.                         playerTurn = false;
  57.                     }
  58.                 }
  59.                 else
  60.                 {
  61.                     humanScore = (humanScore + turnTotal);
  62.                     turnTotal = 0;
  63.                     playerTurn = false;
  64.                 }
  65.             }
  66.             while (playerTurn = true);
  67.  
  68.             do
  69.             {
  70.                 if (turnTotal < 20)
  71.                 {
  72.                     diceRoll = ( int )(Math.random() * 6) + 1;
  73.                    
  74.                     if (diceRoll != 1)
  75.                     {
  76.                         turnTotal = (turnTotal + diceRoll);
  77.                     }
  78.                     else // terminal condition dice =1, break from turn
  79.                     {
  80.                         turnTotal = 0;
  81.                         playerTurn = true;
  82.                     }
  83.                 }
  84.                 else
  85.                 {
  86.                     computerScore = (computerScore + turnTotal);
  87.                     turnTotal = 0;
  88.                     playerTurn = true;
  89.                 }
  90.             }
  91.             while (playerTurn != true);
  92.         }
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement