Вы находитесь на странице: 1из 2

import java.util.

*;
public class AssassinManager {
//AssassinNode that points to the front of killRing
private AssassinNode killRing;
//points to the front of graveyard
private AssassinNode graveyard;
//pre: There must be names of players in the list that is passed in
//otherwise a IllegalArgumentException will be thrown
//post: Sets the initial kill ring with the names in list order
public AssassinManager(List<String> names){
if(names.size() == 0){
throw new IllegalArgumentException();
}
for(int i = names.size() - 1; i >= 0; i--){
killRing = new AssassinNode(names.get(i), killRing);
}
}
//post: prints the names in the kill ring, stating who is stalking who
public void printKillRing(){
AssassinNode first = killRing;
while(first.next != null){
AssassinNode second = first.next;
System.out.println("
" + first.name + " is stalking " + second.name)
;
first = first.next;
}
System.out.println("
" + first.name + " is stalking " + killRing.name);
}
//post: prints the names in the graveyard
public void printGraveyard(){
AssassinNode curr = graveyard;
while(curr != null){
System.out.println("
" + curr.name + " was killed by " + curr.killer
);
curr = curr.next;
}
}
//post: returns whether or not given assassin node has the given string
private boolean contains(AssassinNode temp, String name){
while(temp != null){
if(temp.name.equalsIgnoreCase(name)){
return true;
}
temp = temp.next;
}
return false;
}
//post: returns true if the name is in the kill ring, false if not
public boolean killRingContains(String name){
AssassinNode current = killRing;
return contains(current, name);
}

//post: returns true if the name is in the graveyard, false if not


public boolean graveyardContains(String name){
AssassinNode current = graveyard;
return contains(current, name);
}
//post: returns true if there is only one name left in kill ring
public boolean gameOver(){
return killRing.next == null;
}
//post: returns the name of the winner of the game if the game is over,
//returns null if the game is not over
public String winner(){
if(gameOver()){
return killRing.name;
}
return null;
}
//pre: the name must be in the kill ring (not already in the graveyard or did
//not ever play, otherwise IllegalArgumentException will be thrown. if the ga
me
//is over, am IllegalStateException will be thrown
//post: the kill ring will be adjusted to skip over the player just killed,
//and give the assassin a new target
public void kill(String name){
if(!killRingContains(name)){
throw new IllegalArgumentException();
}
if(gameOver()){
throw new IllegalStateException();
}
AssassinNode previous = killRing;
AssassinNode current = previous.next;
if(previous.name.equalsIgnoreCase(name)){
killRing = killRing.next;
while(current.next != null){
current = current.next;
}
previous.killer = current.name;
previous.next = graveyard;
graveyard = previous;
} else{
while(previous.next != null && !current.name.equalsIgnoreCase(name)){
previous = previous.next;
current = current.next;
}
if(previous.next != null){
current.killer = previous.name;
previous.next = previous.next.next;
current.next = graveyard;
graveyard = current;
}
}
}
}

Вам также может понравиться