SHARE
TWEET

Untitled

a guest Dec 8th, 2019 96 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. import java.util.List;
  3.  
  4. public class AssassinManager {
  5.  
  6.     // YOUR CODE GOES HERE
  7.     AssassinNode front;
  8.     AssassinNode graveFront;
  9.  
  10.     public AssassinManager(List<String> names) {
  11.         if (names == null || names.isEmpty()) {
  12.             throw new IllegalArgumentException();
  13.         }
  14.         graveFront = new AssassinNode(null);
  15.         front = new AssassinNode(names.get(0));
  16.         AssassinNode temp = front;
  17.         for (int i = 1; i < names.size(); i++) {
  18.             temp.next = new AssassinNode(names.get(i));
  19.             temp = temp.next;
  20.         }
  21.     }
  22.  
  23.     public String printKillRing() {
  24.         if (front.name == null) {
  25.             return "";
  26.         } else {
  27.             if (front.next == null) {
  28.                 return front.name + " won the game!";
  29.             } else {
  30.                 String hold = "";
  31.                 AssassinNode temp = front;
  32.                 while (temp.next != null) {
  33.                     hold += "  " + temp.name + " is stalking " + temp.next.name + "\n";
  34.                     temp = temp.next;
  35.                 }
  36.                 hold += "  " + temp.name + " is stalking " + front.name + "\n";
  37.                 System.out.print(hold);
  38.                 return hold;
  39.             }
  40.         }
  41.     }
  42.  
  43.     public String printGraveyard() {
  44.         if (graveFront.name == null) {
  45.             System.out.print("");
  46.             return "";
  47.         } else {
  48.             String hold = "";
  49.             AssassinNode temp = graveFront;
  50.             while (temp.name != null) {
  51.                 hold += "  " + temp.name + " was killed by " + temp.killer + "\n";
  52.                 temp = temp.next;
  53.             }
  54.             System.out.print(hold);
  55.             return hold;
  56.         }
  57.  
  58.     }
  59.  
  60.     public boolean killRingContains(String name) {
  61.         AssassinNode temp = front;
  62.         while (temp != null) {
  63.             if (temp.name.equalsIgnoreCase(name)) {
  64.                 return true;
  65.             }
  66.             temp = temp.next;
  67.         }
  68.         return false;
  69.     }
  70.  
  71.     public boolean graveyardContains(String name) {
  72.         if(graveFront.name == null) {
  73.             return false;
  74.         }
  75.         AssassinNode temp = graveFront;
  76.         while (temp.name != null) {
  77.             if (temp.name.equalsIgnoreCase(name)) {
  78.                 return true;
  79.             }
  80.             temp = temp.next;
  81.         }
  82.         return false;
  83.     }
  84.  
  85.     public boolean isGameOver() {
  86.         return front.next == null;
  87.     }
  88.  
  89.     public String winner() {
  90.         if (isGameOver()) {
  91.             return front.name;
  92.         }
  93.         return null;
  94.     }
  95.  
  96.     public void kill(String name) {
  97.         if (isGameOver()) {
  98.             throw new IllegalStateException();
  99.         }
  100.         if (!killRingContains(name)) {
  101.             throw new IllegalArgumentException();
  102.         }
  103.         if (front.name.equalsIgnoreCase(name)) {
  104.             AssassinNode victim = front;
  105.             AssassinNode killer = front.next;
  106.             front = front.next;
  107.             while (killer.next != null) {
  108.                 killer = killer.next;
  109.             }
  110.             AssassinNode temp = graveFront;
  111.             graveFront = victim;
  112.             graveFront.killer = killer.name;
  113.             graveFront.next = temp;
  114.          
  115.         }
  116.         else {
  117.             AssassinNode killer = front;
  118.             while (killer.next != null) {
  119.                 if(killer.next.name.equalsIgnoreCase(name)) {
  120.                     break;
  121.                 }
  122.                 killer = killer.next;
  123.             }
  124.             AssassinNode victim = killer.next;
  125.             killer.next = victim.next;
  126.             AssassinNode temp = graveFront;
  127.             graveFront = victim;
  128.             graveFront.killer = killer.name;
  129.             graveFront.next = temp;            
  130.         }
  131.     }
  132.  
  133.     //////// DO NOT MODIFY AssassinNode.  You will lose points if you do. ////////
  134.     /**
  135.      * Each AssassinNode object represents a single node in a linked list for a
  136.      * game of Assassin.
  137.      */
  138.     private static class AssassinNode {
  139.  
  140.         public final String name;  // this person's name
  141.         public String killer;      // name of who killed this person (null if alive)
  142.         public AssassinNode next;  // next node in the list (null if none)
  143.  
  144.         /**
  145.          * Constructs a new node to store the given name and no next node.
  146.          */
  147.         public AssassinNode(String name) {
  148.             this(name, null);
  149.         }
  150.  
  151.         /**
  152.          * Constructs a new node to store the given name and a reference to the
  153.          * given next node.
  154.          */
  155.         public AssassinNode(String name, AssassinNode next) {
  156.             this.name = name;
  157.             this.killer = null;
  158.             this.next = next;
  159.         }
  160.     }
  161. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top