-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssassinManager.java
More file actions
158 lines (131 loc) · 4.99 KB
/
AssassinManager.java
File metadata and controls
158 lines (131 loc) · 4.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package AssassinManager;
import java.util.*;
public class AssassinManager {
private AssassinNode killring;
private AssassinNode graveyard;
/**
* Constructor
* @param names the list of string names
*/
public AssassinManager (List<String> names) {
if (names.isEmpty()) throw new IllegalArgumentException();
for (int i=0;i<names.size(); i++) {
AssassinNode newNode = new AssassinNode (names.get(i));
// 1. attach the node to the last
AssassinNode current = killring;
if (current == null) killring = newNode; // if the list is empty, update killring
else {
while (current.next != null) { // traverse down to the last node
current = current.next;
}
current.next = newNode; // attach the new node
}
}
}
/**
* printKillRing()
* print the names of the people in the kill ring, one per line,
* indented four spaces, with output of the form “<name> is stalking <name>”.
*/
public void printKillRing() {
AssassinNode current = this.killring;
while (current.next != null){
System.out.println(" " + current.name + " is stalking " + current.next.name);
current = current.next;
}
System.out.println(" " + current.name + " is stalking " + killring.name);
}
/**
* printGraveyard()
* print the names of the people in the graveyard, one per line,
* indented four spaces, with output of the form “ was killed by ”.
* print the names in reverse kill order (most recently killed first)
*/
public void printGraveyard() {
AssassinNode current = this.graveyard;
while (current != null){
System.out.println(" " + current.name + " was killed by " + current.killer);
current = current.next;
}
}
/**
* killRingContains(name)
* ignore case in comparing names
* @param name String value of player
* @return true if the given name is in the current kill ring
*/
public boolean killRingContains (String name) {
AssassinNode current = this.killring;
if (gameOver()) return name.equalsIgnoreCase((current.name));
while (current != null) {
if (name.equalsIgnoreCase(current.name)) return true;
else {
current = current.next;
}
}
return false;
}
/**graveyardContains(name)
* ignore case in comparing names.
* @param name String value of player
* @return true if the given name is in the current graveyard
*/
public boolean graveyardContains (String name) {
if (this.graveyard == null) return false;
AssassinNode current = this.graveyard;
if (name.equalsIgnoreCase(current.name)) return true;
while (current.next != null){
if (name.equalsIgnoreCase(current.name)) return true;
else current = current.next;
}
return false;
}
/**
* gameOver()
* @return true if the game is over
*/
public boolean gameOver() {
return (this.killring.next == null);
}
/**
* winner()
* @return the name of last person in killRing, return null if game is not over
*/
public String winner () {
if (gameOver()) return this.killring.name;
return null;
}
/**
* kill(name)
* records the killing of the person with the given name
* transferring the person from the killRing to tard
* throw an IllegalArgumentException if the given name is not part of the current kill ring
* throw an IllegalStateException if the game is over
* ignore case in comparing names.
* @param name will be killed person of name
*/
public void kill (String name) {
if (gameOver()) throw new IllegalStateException("Game is over");
if (!killRingContains(name)) throw new IllegalArgumentException();
AssassinNode current = this.killring;
AssassinNode killed = this.killring; //will update to graveyard
while (current.next != null) {
if (name.equalsIgnoreCase(current.next.name)) {
killed = current.next;
killed.killer = current.name;
current.next = current.next.next;
killed.next = this.graveyard;
this.graveyard = killed;
} else {
current = current.next;
}
} //the case when the first person got killed
if (this.killring.name.equalsIgnoreCase(name)){
killed = this.killring;
this.killring = this.killring.next;
killed.next = this.graveyard;
this.graveyard = killed;
this.graveyard.killer = current.name;
}
}
}