Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void playGame(int players, int passes) {
- /*
- * Use the helper methods addPlayers and passPotatoe to play the game
- * Continue passing the potato until only 1 player remains
- * Print the winning players number
- *
- * For players = 5 and passes = 3, the winner should be 1. Players should be removed in this order:
- * - 4, 3, 5, 2
- */
- addPlayers(players);
- for(int i = 0; i < passes; i++) {
- passPotato(passes);
- }
- System.out.println(start.value);
- }
- /*
- * Grading:
- * Correctly creates circular linked list of size amount - 1pt
- */
- private void addPlayers(int amount) {
- /*
- * Set up this method to create a Node for each player
- * The value of each Node, should be the player number, starting at 1
- * For example, if the amount is 5, there should be Nodes 1-5
- * Node 1 should always be set as the start
- * Make list circular by connecting the last player Node to the first
- */
- for(int i = 0; i < amount; i++) {
- Node current = new Node(i+1);
- if(start == null) {
- current.next = current;
- start = current;
- }
- else {
- current.next = start.next;
- start.next = current;
- }
- count++;
- }
- }
- /*
- * Grading:
- * Correctly removes the player the number of passes away from the start - 1pt
- * Correctly changes the start to the player after the one being removed - 0.5pt
- */
- private void passPotato(int passes) {
- /*
- * Set up this method to play a single round of the game
- * Move through the list the number of passes from the start
- * Remove the player/Node at this position
- * Set the start equal to the player/Node after this position
- * Do not play a round if there is one 1 player remaining
- * Print the player number that was removed and the player with the potato
- */
- if(count > 1) {
- Node current = start;
- Node prev = start;
- for(int i = 0; i < passes; i++) {
- prev = current;
- current = current.next;
- }
- System.out.println("Removed: " + current.value + ", Number " + current.next.value + " with potato");
- prev.next = current.next;
- start = current.next;
- count--;
- }
- else {
- System.out.println("No more players to pass to");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement