Advertisement
Guest User

Untitled

a guest
Feb 27th, 2020
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.53 KB | None | 0 0
  1. package dicegames;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Pig {
  6. private int score1, score2;
  7. private Die die;
  8. private Scanner scan;
  9. private int rounds = 1;
  10. private int maxPoints;
  11. private boolean finished = false;
  12.  
  13. // Default constructor
  14. public Pig() {
  15. this(100);
  16. }
  17.  
  18. // Constructor with parameter
  19. public Pig(int maxPoints) {
  20. this.maxPoints = maxPoints;
  21. score1 = score2 = 0;
  22. die = new Die();
  23. scan = new Scanner(System.in);
  24. }
  25.  
  26. /**
  27. * Welcome message of the game that goes over the rules, which will change
  28. * depending on what the field variable, maxPoints, is set to.
  29. */
  30. public void welcomeToTheGame() {
  31. System.out.println("Hver spiller skiftes til at kaste med en terning "
  32. + "indtil han enten kaster 1, eller beslutter sig for at\r\n"
  33. + "stoppe. Hvis han slår en 1’er, får han ingen point i denne "
  34. + "omgang. Hvis han beslutter sig for at\r\n"
  35. + "stoppe inden har slår en 1’er, lægges summen af alle hans " + "kast i denne tur sammen med hans\r\n"
  36. + "samlede antal point, og turen går videre til næste " + "spiller. Den første spiller der samlet når "
  37. + maxPoints + "\r\n" + "point har vundet.\r\n" + "");
  38. }
  39.  
  40. /**
  41. * Method that is called when a game is over. This method is only called when a
  42. * game is finished.
  43. */
  44. public void gameOver() {
  45. if (rounds % 2 == 1) {
  46. System.out.println("Spiller 1 vandt med " + score1 + " points");
  47. } else {
  48. System.out.println("Spiller 2 vandt med " + score2 + " points");
  49. }
  50. finished = true;
  51. scan.close();
  52. }
  53.  
  54. /**
  55. * The method for resolving an actor's turn if it was decided that the game is
  56. * versus a computer.
  57. */
  58. private void takeTurnVsPC() {
  59. boolean turnOver = false;
  60. int points = 0;
  61.  
  62. while (!turnOver) {
  63. die.roll();
  64. int roll = die.getFaceValue();
  65. if (rounds % 2 == 1) {
  66. System.out.println("Du har kastet: " + roll);
  67. } else {
  68. System.out.println("Computeren har kastet: " + roll);
  69. }
  70.  
  71. if (roll == 1 && rounds % 2 == 1) {
  72. System.out.println("Du har kastet en 1'er. Din tur er slut. Øv.");
  73. System.out.println();
  74. turnOver = true;
  75. } else if (roll == 1 && rounds % 2 == 0) {
  76. System.out.println("Computeren har kastet en 1'er. Dens tur er slut. Oof.");
  77. System.out.println();
  78. turnOver = true;
  79. } else {
  80. points += roll;
  81.  
  82. if (rounds % 2 == 1) {
  83. System.out.println("Du har i denne runde opnået " + points + " points");
  84. } else {
  85. System.out.println("Computeren har i denne runde opnået " + points + " points");
  86. }
  87.  
  88. if (rounds % 2 == 1 && score1 + points >= maxPoints) {
  89. score1 += points;
  90. System.out.print(", hvilket sammen med dine points giver " + score1 + "!");
  91. gameOver();
  92. turnOver = true;
  93. } else if (rounds % 2 == 0 && score2 + points >= maxPoints) {
  94. score2 += points;
  95. gameOver();
  96. turnOver = true;
  97. } else if (rounds % 2 == 1) {
  98. System.out.println("Vil du kaste terningen igen? Angiv Ja eller Nej: ");
  99. String goOn = scan.nextLine();
  100. if (goOn.equalsIgnoreCase("Nej")) {
  101. turnOver = true;
  102. score1 += points;
  103. System.out.println("Nu har du en score på: " + score1);
  104. }
  105.  
  106. /**
  107. * Here is the computer opponent's algorithm: 20 is chosen as the computer's
  108. * border value for determining the end of a turn, since the average amount of
  109. * wins equals the average amount of loses --> 1/6 of the time all points are
  110. * lost, and the rest of the 5/6 of the time there is an average win of
  111. * (2+3+4+5+6)/5 = 4 points. Given that 5/6 * 4 = 20/6 there will a equilibrium
  112. * between potential loss and gain when 1/6 * x = 20/6 --> x = 20. The computer
  113. * plays it safe, so it decides to secure it's points if they're exactly 20.
  114. */
  115. } else {
  116. if (points >= 20) {
  117. turnOver = true;
  118. score2 += points;
  119. System.out.println("Computeren stopper sin tur her. Nu har computeren en score på: " + score2);
  120. }
  121.  
  122. }
  123.  
  124. }
  125. }
  126. rounds++;
  127. }
  128.  
  129. // Method for starting the game
  130.  
  131. public void startGame() {
  132. welcomeToTheGame();
  133. while (!finished) {
  134. System.out.println("############################################################################");
  135. System.out.println("Det er nu runde " + rounds);
  136. if (rounds % 2 == 1) {
  137. System.out.println("Det er spiller 1 tur, med " + score1 + " points indtil videre.");
  138. } else {
  139. System.out.println("Det er spiller 2 tur, med " + score2 + " points indtil videre.");
  140. }
  141. takeTurnVsPC();
  142. }
  143. }
  144.  
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement