Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Complete the playGame(int players, int passes) method
- * Complete the addPlayers(int players) method
- * Complete the passPotatoe(int passes) method
- * No other methods/variables should be added/modified
- */
- public class A3CircleLL {
- /*
- * Grading:
- * Correctly uses helpers to play game - 1pt
- * Prints correct winner when game is complete - 0.5pt
- */
- 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);
- System.out.println(toString());
- passPotato(passes);
- }
- /*
- * 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 = 1; i <= amount; i++) {
- Node newPlayer = new Node(i);
- if(i == 1) {
- start = newPlayer;
- last = newPlayer;
- start.next = last;
- last.next = newPlayer;
- }
- else if(i <= amount) {
- Node temp = new Node(i);
- temp.next = last.next;
- last.next = temp;
- last = temp;
- last.next = start;
- }
- else {
- return;
- }
- }
- }
- /*
- * 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
- */
- int count = 0;
- Node runner = start;
- Node bomb = start;
- while(runner.next != start) {
- if(runner.equals(start)) count++;
- count++;
- runner = runner.next;
- }
- while(count > 1) {
- int ticks = passes;
- while(ticks!= 1) {
- bomb.value = bomb.next.value;
- bomb = bomb.next;
- ticks--;
- if(ticks == 1) {
- boolean removed = false;
- runner = start;
- while(!removed) {
- if(runner.next.equals(bomb)) {
- bomb = runner.next.next;
- runner.next = runner.next.next;
- removed = true;
- count--;
- System.out.println("Player:" + bomb.value + " deleted");
- }
- runner = runner.next;
- }
- }
- }
- }
- System.out.println("Player " + runner.value + " wins");
- }
- private Node start;
- private Node last;
- private int count;
- public A3CircleLL() {
- start = null;
- count = 0;
- last = null;
- }
- public String printList() {
- String output = "";
- if(start != null) {
- Node current = start;
- do {
- output += current.value + ",";
- current = current.next;
- }while(current != start);
- }
- return output;
- }
- public String toString() {
- return this.printList();
- }
- private class Node {
- Integer value;
- Node next;
- public Node(Integer v) {
- value = v;
- next = null;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement