Advertisement
Guest User

Untitled

a guest
Oct 21st, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.30 KB | None | 0 0
  1.  
  2. /**
  3. * @author Shawn
  4. *
  5. */
  6. public class Game {
  7.  
  8.  
  9.  
  10. /**
  11. * Instance Variables
  12. */
  13.  
  14. static private Player[] players;
  15.  
  16.  
  17. /**
  18. * @param args Unused
  19. */
  20. public static void main(String[] args) {
  21.  
  22. // users();
  23. // play();
  24. Game g = new Game();
  25. g.play();
  26.  
  27. }
  28.  
  29.  
  30. public Game() {
  31. players = users();
  32. }
  33.  
  34.  
  35.  
  36. /**
  37. * Users, Asks how many players there will be, and creates an array of object
  38. */
  39. public static Player[] users() {
  40. System.out.println("How many Players?");
  41. int players = TextIO.getlnInt();
  42.  
  43. Player[] pigGame = new Player[players];
  44.  
  45. for (int i = 0; i < pigGame.length; i++) {
  46. System.out.println("Player " + (i + 1) + "'s name is: ");
  47. String name = TextIO.getln();
  48. pigGame[i] = new Player(name, 0);
  49. }
  50.  
  51. return pigGame;
  52. }
  53.  
  54. public static boolean playerTurn(Player p) {
  55. boolean hold = false;
  56. PairOfDice rolls = new PairOfDice();
  57. int total = p.getScore();
  58. int turnTotal = total;
  59. System.out.println("Player: " + p.getName());
  60.  
  61. while(!hold) {
  62. rolls.roll();
  63. int one = rolls.getDie1();
  64. int two = rolls.getDie2();
  65. if (one == 1 && two == 1) {
  66. System.out.println(one +", " + two);
  67. System.out.println("You rolled double 1s, score set to 0 and turn is ended");
  68. p.setScore(0);
  69. hold = true;
  70. }
  71. else if (one == 1 || two == 1) {
  72. System.out.println(one +", " + two);
  73. System.out.println("You rolled a 1, turn is ended");
  74. hold = true;
  75. }
  76. else if (one > 1 && two > 1 && one == two) {
  77. turnTotal = turnTotal + one + two;
  78. System.out.println(one +", " + two);
  79. System.out.println("Your Score for this turn is: " + turnTotal);
  80. System.out.println("You rolled doubles, you must roll again");
  81. }
  82. else if (one > 1 && two > 1 && one != two) {
  83. turnTotal = turnTotal + one + two;
  84. System.out.println(one +", " + two);
  85. System.out.println("Your Score for this turn is: " + turnTotal);
  86. System.out.println("Would you like to hold? (y/n)");
  87. char input = TextIO.getlnChar();
  88.  
  89. if(input == 'y')
  90. hold = true;
  91.  
  92. p.setScore(turnTotal);
  93. System.out.println("Your score is: " + p.getScore());
  94. }
  95.  
  96. }
  97.  
  98. if (p.getScore() >= 100) {
  99. return true;
  100. }
  101. else {
  102. return false;
  103. }
  104. }
  105.  
  106. /**
  107. * Runs the pig game until a winner is found
  108. * @param pigGame The array of Player objects
  109. */
  110. public static void play() {
  111.  
  112. boolean gameOver = false;
  113. int currentPlayer = 0;
  114. while(!gameOver) {
  115. gameOver = playerTurn(players[currentPlayer]);
  116. currentPlayer++;
  117. if(currentPlayer == players.length)
  118. currentPlayer = 0;
  119.  
  120. // Maybe more here, not sure
  121.  
  122. }
  123.  
  124.  
  125. }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement