Advertisement
Guest User

Untitled

a guest
Feb 11th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.00 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Pigv2 {
  4.  
  5. /**
  6. * @ version date 2016-02-05 @ author Monica Hinga
  7. *
  8. * A small dice game for two players called Pig. Players win when they reach
  9. * 100 total points. If a player rolls a 1, the turn is over and the player
  10. * gets no points. Otherwise, points are added to player's score.
  11. *
  12. */
  13. public static void main(String[] args) {
  14. System.out.println("Welcome to PIG!"); //Welcome and explain rules.
  15. System.out.println("OINK!");
  16. System.out.println("");
  17. System.out.println("Here are the rules:");
  18. System.out.println("1. On your turn, roll the die.");
  19. System.out.println("2. Your score for the turn is the sum of " +
  20. "your rolls.");
  21. System.out.println("BUT, if you roll a 1 your turn is over" +
  22. " and you don't get any points added to your total score.");
  23. System.out.println("3. The first player to have a total " +
  24. "score of 100 wins!");
  25. System.out.println("");
  26. System.out.println("Will you be brave enough " +
  27. "to win? Let's find out!");
  28.  
  29. Scanner scanIn = new Scanner (System.in); //Ask players for their names
  30. System.out.println("Player One, enter your name.");
  31. String nameOne = scanIn.nextLine();
  32. System.out.println("");
  33. System.out.println("Player Two, enter your name.");
  34. String nameTwo = scanIn.nextLine();
  35.  
  36. int totalScore1 = 0;
  37. int totalScore2 = 0;
  38. int turnNumber = 0;
  39. //int rollScore = 0;
  40. int totalRoll = 0;
  41. int roll = 0;
  42.  
  43.  
  44. // Keeps game going as long as neither player has 100 pts.
  45. while(totalScore1 < 100 || totalScore2 < 100){
  46. //Player1 plays on even turns.
  47. while(turnNumber%2==0){
  48. roll = (int)(Math.random()* 6) + 1;
  49. System.out.println(nameOne + ", you're up!");
  50. System.out.println("You rolled a " + roll + ".");
  51. //If player1 rolls a 1, turn ends
  52. if(roll == 1){
  53. System.out.println("::Cue The Price is Right losing" +
  54. " trombone.::");
  55. System.out.println("Your turn is over.");
  56. turnNumber = turnNumber+1;
  57. totalRoll = 0;
  58. //If player1 rolls anything else
  59. }else{
  60. //System.out.println("Good job, " + nameOne + "! You " +
  61. //"rolled a " + roll + "!");
  62. totalRoll = totalRoll + roll;
  63. //totalRoll = roll + rollScore;
  64. System.out.println("If you roll a 1, you will" +
  65. " lose " + totalRoll + " points.");
  66. System.out.println("Roll again? y/n");
  67. String answer = scanIn.nextLine();
  68.  
  69. if(answer == "n"){
  70. totalScore1 = totalRoll + totalScore1;
  71. turnNumber = turnNumber + 1;
  72. }
  73. }
  74. }
  75. while(turnNumber%2!=0){
  76. roll = (int)Math.random()*6+1;
  77. System.out.println(nameTwo + ", you're up!");
  78. System.out.println("You rolled a " + roll +".");
  79.  
  80. if(roll == 1){
  81. System.out.println("::Cue The Price is Right losing" +
  82. " trombone.::");
  83. System.out.println("Your turn is over.");
  84. turnNumber = turnNumber + 1;
  85. totalRoll=0;
  86.  
  87. }else{
  88. //System.out.println("Woo! You rolled" +
  89. //" a " + roll + "!");
  90. totalRoll = roll + totalRoll;
  91. System.out.println("If you roll a 1, you will" +
  92. " lose " + totalRoll + "points.");
  93. System.out.println("Roll again? y/n");
  94. String answer = scanIn.nextLine();
  95.  
  96. if(answer == "n"){
  97. totalScore2 = totalRoll + totalScore2;
  98. turnNumber = turnNumber + 1;
  99. }
  100. }
  101. }
  102. }
  103. if(totalScore1 >= 100){
  104. System.out.println(nameOne + ", you're awesome!");
  105. System.out.println("(Well, at least awesomer " +
  106. "than " + nameTwo + "!)");
  107. System.out.println("You win!");
  108.  
  109. }else{
  110. System.out.println(nameTwo + ", you won!");
  111. System.out.println("That may make you a better human being" +
  112. " than " + nameOne + ".");
  113. System.out.println("But what do I know? I am just a computer.");
  114. System.out.println("And that makes me only as smart as my" +
  115. " programmer. Bad news for me.");
  116. }
  117. }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement