Advertisement
Guest User

Untitled

a guest
Feb 21st, 2020
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. public void playGame(int players, int passes) {
  2. /*
  3. * Use the helper methods addPlayers and passPotatoe to play the game
  4. * Continue passing the potato until only 1 player remains
  5. * Print the winning players number
  6. *
  7. * For players = 5 and passes = 3, the winner should be 1. Players should be removed in this order:
  8. * - 4, 3, 5, 2
  9. */
  10.  
  11. addPlayers(players);
  12. for(int i = 0; i < passes; i++) {
  13. passPotato(passes);
  14. }
  15. System.out.println(start.value);
  16. }
  17. /*
  18. * Grading:
  19. * Correctly creates circular linked list of size amount - 1pt
  20. */
  21. private void addPlayers(int amount) {
  22. /*
  23. * Set up this method to create a Node for each player
  24. * The value of each Node, should be the player number, starting at 1
  25. * For example, if the amount is 5, there should be Nodes 1-5
  26. * Node 1 should always be set as the start
  27. * Make list circular by connecting the last player Node to the first
  28. */
  29.  
  30. for(int i = 0; i < amount; i++) {
  31. Node current = new Node(i+1);
  32. if(start == null) {
  33. current.next = current;
  34. start = current;
  35. }
  36. else {
  37. current.next = start.next;
  38. start.next = current;
  39. }
  40. count++;
  41. }
  42.  
  43. }
  44. /*
  45. * Grading:
  46. * Correctly removes the player the number of passes away from the start - 1pt
  47. * Correctly changes the start to the player after the one being removed - 0.5pt
  48. */
  49. private void passPotato(int passes) {
  50. /*
  51. * Set up this method to play a single round of the game
  52. * Move through the list the number of passes from the start
  53. * Remove the player/Node at this position
  54. * Set the start equal to the player/Node after this position
  55. * Do not play a round if there is one 1 player remaining
  56. * Print the player number that was removed and the player with the potato
  57. */
  58.  
  59. if(count > 1) {
  60. Node current = start;
  61. Node prev = start;
  62. for(int i = 0; i < passes; i++) {
  63. prev = current;
  64. current = current.next;
  65. }
  66. System.out.println("Removed: " + current.value + ", Number " + current.next.value + " with potato");
  67.  
  68. prev.next = current.next;
  69. start = current.next;
  70.  
  71. count--;
  72. }
  73. else {
  74. System.out.println("No more players to pass to");
  75. }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement