Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Pigv2 {
- /**
- * @ version date 2016-02-05 @ author Monica Hinga
- *
- * A small dice game for two players called Pig. Players win when they reach
- * 100 total points. If a player rolls a 1, the turn is over and the player
- * gets no points. Otherwise, points are added to player's score.
- *
- */
- public static void main(String[] args) {
- System.out.println("Welcome to PIG!"); //Welcome and explain rules.
- System.out.println("OINK!");
- System.out.println("");
- System.out.println("Here are the rules:");
- System.out.println("1. On your turn, roll the die.");
- System.out.println("2. Your score for the turn is the sum of " +
- "your rolls.");
- System.out.println("BUT, if you roll a 1 your turn is over" +
- " and you don't get any points added to your total score.");
- System.out.println("3. The first player to have a total " +
- "score of 100 wins!");
- System.out.println("");
- System.out.println("Will you be brave enough " +
- "to win? Let's find out!");
- Scanner scanIn = new Scanner (System.in); //Ask players for their names
- System.out.println("Player One, enter your name.");
- String nameOne = scanIn.nextLine();
- System.out.println("");
- System.out.println("Player Two, enter your name.");
- String nameTwo = scanIn.nextLine();
- int totalScore1 = 0;
- int totalScore2 = 0;
- int turnNumber = 0;
- //int rollScore = 0;
- int totalRoll = 0;
- int roll = 0;
- // Keeps game going as long as neither player has 100 pts.
- while(totalScore1 < 100 || totalScore2 < 100){
- //Player1 plays on even turns.
- while(turnNumber%2==0){
- roll = (int)(Math.random()* 6) + 1;
- System.out.println(nameOne + ", you're up!");
- System.out.println("You rolled a " + roll + ".");
- //If player1 rolls a 1, turn ends
- if(roll == 1){
- System.out.println("::Cue The Price is Right losing" +
- " trombone.::");
- System.out.println("Your turn is over.");
- turnNumber = turnNumber+1;
- totalRoll = 0;
- //If player1 rolls anything else
- }else{
- //System.out.println("Good job, " + nameOne + "! You " +
- //"rolled a " + roll + "!");
- totalRoll = totalRoll + roll;
- //totalRoll = roll + rollScore;
- System.out.println("If you roll a 1, you will" +
- " lose " + totalRoll + " points.");
- System.out.println("Roll again? y/n");
- String answer = scanIn.nextLine();
- if(answer == "n"){
- totalScore1 = totalRoll + totalScore1;
- turnNumber = turnNumber + 1;
- }
- }
- }
- while(turnNumber%2!=0){
- roll = (int)Math.random()*6+1;
- System.out.println(nameTwo + ", you're up!");
- System.out.println("You rolled a " + roll +".");
- if(roll == 1){
- System.out.println("::Cue The Price is Right losing" +
- " trombone.::");
- System.out.println("Your turn is over.");
- turnNumber = turnNumber + 1;
- totalRoll=0;
- }else{
- //System.out.println("Woo! You rolled" +
- //" a " + roll + "!");
- totalRoll = roll + totalRoll;
- System.out.println("If you roll a 1, you will" +
- " lose " + totalRoll + "points.");
- System.out.println("Roll again? y/n");
- String answer = scanIn.nextLine();
- if(answer == "n"){
- totalScore2 = totalRoll + totalScore2;
- turnNumber = turnNumber + 1;
- }
- }
- }
- }
- if(totalScore1 >= 100){
- System.out.println(nameOne + ", you're awesome!");
- System.out.println("(Well, at least awesomer " +
- "than " + nameTwo + "!)");
- System.out.println("You win!");
- }else{
- System.out.println(nameTwo + ", you won!");
- System.out.println("That may make you a better human being" +
- " than " + nameOne + ".");
- System.out.println("But what do I know? I am just a computer.");
- System.out.println("And that makes me only as smart as my" +
- " programmer. Bad news for me.");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement