Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class chapterThreeProblemTen
- {
- /*This program simulates a game of 'Pig', in which two players role a
- *2d6 die taking turns till they reach 100 points. On a players turn
- *they role the die until they get a '1', or choose to 'hold'. If they
- *get a '1', their turn is over and they don't get any points. If they
- *choose to 'hold', that player keeps all the points they have earned
- *up to that point and add them to their score. (Since one of the
- *players here is a computer, they limit themselves to <= 20 points in
- *a turn)*/
- public static void main(String[] args)
- {
- Scanner keyboard = new Scanner(System.in);
- //obvious variables
- int humanScore = 0;
- int computerScore = 0;
- int diceRoll = 0;
- boolean playerTurn = true; //If the number is true, it is the players turn.
- int turnTotal = 0; //This variable is the amount of points one has
- //rolled in their turn. This is temporary,
- //because rolling a '1' will make this '0'
- String playerInput = "r"; //This variable controls the if statement that
- //controls whether players continue to roll
- //or hold with whatever point they may have
- //'r' is roll; 'h' is hold;
- System.out.println("Score to win is 100.");
- if ((humanScore < 100) || (computerScore < 100))
- {
- do
- {
- System.out.println("Your score is: " + humanScore);
- System.out.println("Computers score is: "+ computerScore);
- System.out.println("Points this turn: " + turnTotal);
- System.out.println("type 'r' to roll or 'h' to hold.");
- playerInput = keyboard.nextLine();
- if (playerInput.equalsIgnoreCase("r"))
- {
- diceRoll = ( int )(Math.random() * 6) + 1;
- System.out.println("You rolled a " + diceRoll);
- if (diceRoll != 1)
- {
- turnTotal = (turnTotal + diceRoll);
- }
- else // terminal condition dice =1, break from turn
- {
- turnTotal = 0;
- playerTurn = false;
- }
- }
- else
- {
- humanScore = (humanScore + turnTotal);
- turnTotal = 0;
- playerTurn = false;
- }
- }
- while (playerTurn = true);
- do
- {
- if (turnTotal < 20)
- {
- diceRoll = ( int )(Math.random() * 6) + 1;
- if (diceRoll != 1)
- {
- turnTotal = (turnTotal + diceRoll);
- }
- else // terminal condition dice =1, break from turn
- {
- turnTotal = 0;
- playerTurn = true;
- }
- }
- else
- {
- computerScore = (computerScore + turnTotal);
- turnTotal = 0;
- playerTurn = true;
- }
- }
- while (playerTurn != true);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement