Advertisement
Guest User

Untitled

a guest
Feb 20th, 2020
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. /*
  2. * Complete the playGame(int players, int passes) method
  3. * Complete the addPlayers(int players) method
  4. * Complete the passPotatoe(int passes) method
  5. * No other methods/variables should be added/modified
  6. */
  7. public class A3CircleLL {
  8. /*
  9. * Grading:
  10. * Correctly uses helpers to play game - 1pt
  11. * Prints correct winner when game is complete - 0.5pt
  12. */
  13. public void playGame(int players, int passes) {
  14. /*
  15. * Use the helper methods addPlayers and passPotatoe to play the game
  16. * Continue passing the potato until only 1 player remains
  17. * Print the winning players number
  18. *
  19. * For players = 5 and passes = 3, the winner should be 1. Players should be removed in this order:
  20. * - 4, 3, 5, 2
  21. */
  22. addPlayers(players);
  23. System.out.println(toString());
  24. passPotato(passes);
  25. }
  26. /*
  27. * Grading:
  28. * Correctly creates circular linked list of size amount - 1pt
  29. */
  30. private void addPlayers(int amount) {
  31. /*
  32. * Set up this method to create a Node for each player
  33. * The value of each Node, should be the player number, starting at 1
  34. * For example, if the amount is 5, there should be Nodes 1-5
  35. * Node 1 should always be set as the start
  36. * Make list circular by connecting the last player Node to the first
  37. */
  38. for(int i = 1; i <= amount; i++) {
  39. Node newPlayer = new Node(i);
  40. if(i == 1) {
  41. start = newPlayer;
  42. last = newPlayer;
  43. start.next = last;
  44. last.next = newPlayer;
  45. }
  46. else if(i <= amount) {
  47. Node temp = new Node(i);
  48. temp.next = last.next;
  49. last.next = temp;
  50. last = temp;
  51. last.next = start;
  52. }
  53. else {
  54. return;
  55. }
  56. }
  57. }
  58. /*
  59. * Grading:
  60. * Correctly removes the player the number of passes away from the start - 1pt
  61. * Correctly changes the start to the player after the one being removed - 0.5pt
  62. */
  63. private void passPotato(int passes) {
  64. /*
  65. * Set up this method to play a single round of the game
  66. * Move through the list the number of passes from the start
  67. * Remove the player/Node at this position
  68. * Set the start equal to the player/Node after this position
  69. * Do not play a round if there is one 1 player remaining
  70. * Print the player number that was removed and the player with the potato
  71. */
  72. int count = 0;
  73. Node runner = start;
  74. Node bomb = start;
  75. while(runner.next != start) {
  76. if(runner.equals(start)) count++;
  77. count++;
  78. runner = runner.next;
  79. }
  80. while(count > 1) {
  81. int ticks = passes;
  82. while(ticks!= 1) {
  83. bomb.value = bomb.next.value;
  84. bomb = bomb.next;
  85.  
  86. ticks--;
  87. if(ticks == 1) {
  88. boolean removed = false;
  89. runner = start;
  90. while(!removed) {
  91. if(runner.next.equals(bomb)) {
  92. bomb = runner.next.next;
  93. runner.next = runner.next.next;
  94. removed = true;
  95. count--;
  96. System.out.println("Player:" + bomb.value + " deleted");
  97. }
  98. runner = runner.next;
  99. }
  100. }
  101. }
  102. }
  103. System.out.println("Player " + runner.value + " wins");
  104. }
  105.  
  106. private Node start;
  107. private Node last;
  108. private int count;
  109. public A3CircleLL() {
  110. start = null;
  111. count = 0;
  112. last = null;
  113. }
  114. public String printList() {
  115. String output = "";
  116. if(start != null) {
  117. Node current = start;
  118. do {
  119. output += current.value + ",";
  120. current = current.next;
  121. }while(current != start);
  122. }
  123. return output;
  124. }
  125. public String toString() {
  126. return this.printList();
  127. }
  128. private class Node {
  129. Integer value;
  130. Node next;
  131. public Node(Integer v) {
  132. value = v;
  133. next = null;
  134. }
  135. }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement