-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList.java
More file actions
405 lines (346 loc) · 11.9 KB
/
LinkedList.java
File metadata and controls
405 lines (346 loc) · 11.9 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/*
*Partner 1 Name: Evelyn Bailey
*Partner 1 PennKey: ebail
*Partner 1 Recitation #: 215
*
*Partner 2 Name: Stephen Eyerly
*Partner 2 PennKey: seyerly
*Partner 2 Recitation #: 216
*
* Description: The LinkedList class constructs an empty linked list and holds
* several functions to insert nodes, remove nodes, and move objects within the
* linked list.
*
*/
public class LinkedList{
private Node head;
private Node lastNode;
private Missile m;
private int size;
private int explosionDuration = 10;
private int score = 0;
/*
* Name: LinkedList
* Description: creates an empty linked list
* Inputs: -
* Outputs: -
*/
public LinkedList() {
head = null;
lastNode = null;
size = 0;
}
/*
* Name: isEmpty
* Description: checks if the linked list is empty
* Inputs: -
* Outputs: True if empty, false otherwise
*/
private boolean isEmpty() {
if (head == null && lastNode == null) {
return true;
}
else {
return false;
}
}
/*
* Name: size
* Description: keeps track of the size of the linked list for debugging
* purposes
* Inputs: -
* Outputs: int size
*/
public int size() {
if (isEmpty()) {
return 0;
}
else {
return size;
}
}
// This method adds to the score the argument
public void addToScore(int x) {
score += x;
}
// This method returns the score
public int score() {
return score;
}
/*
* Name: head
* Description: returns the head of the linked list for input into
* isDestroying method
* Inputs: -
* Outputs: Node head
*/
public Node head() {
return head;
}
/*
* Name: insert
* Description: inserts a missile into the linked list
* Inputs: Missile m
* Outputs: -
*/
public void insert(Missile m) {
//if Missile is null, do nothing
if (m == null) {
return;
}
//special case for inserting a missile for the first time
else if (isEmpty()) {
head = new Node(m);
lastNode = new Node(m);
head.previous = null;
head.next = lastNode;
lastNode.previous = head;
lastNode.next = null;
size = 1;
}
else {
//create a new node containing the missile
Node insert = new Node(m);
//insert the node into the end of the list
insert.next = lastNode.next;
insert.previous = lastNode;
lastNode.next = insert;
lastNode = insert;
//increment the size of the linked list
size++;
}
}
/*
* Name: insert
* Description: inserts a picture into the linked list
* Inputs: Picture p
* Outputs: -
*/
public void insert(Picture p) {
//error checking
if (p == null) {
return;
}
//special case for inserting a picture for the first time
else if (isEmpty()) {
Node insert = new Node (p);
head = insert;
lastNode = insert;
size = 1;
}
else {
//create a new node containing the picture
Node insert = new Node(p);
//insert the node into the end of the list
insert.next = lastNode.next;
insert.previous = lastNode;
lastNode.next = insert;
lastNode = insert;
//increment the size of the linked list
size++;
}
}
/*
* Name: delete
* Description: removes a node from the linked list
* Inputs: Node m
* Outputs: -
*/
public void delete (Node m) {
//throw an error if the node is invalid
if (m == null) {
throw new RuntimeException("Error: invalid node to remove");
}
//if the missile to be deleted is a defensive missile, play
//explosion animation
// if m is the only node in the linked list, make the list empty
if (m == head && m == lastNode) {
head = null;
lastNode = null;
}
//if m is at the end of the linked list, set lastNode equal to the
//previous node
else if (m.next == null) {
lastNode = lastNode.previous;
lastNode.next = null;
}
//if m is the head, set head to the next node
else if (m.previous == null) {
this.head = m.next;
this.head.previous = null;
}
//if m is in the middle of the list, remove the node as normal
else {
m.previous.next = m.next;
m.next.previous = m.previous;
}
//decrement the size
size--;
}
/*
* Name: moveMissiles
* Description: advances the animation of each missile within the linked list
* Inputs: LinkedList background
* Outputs: -
*/
public void moveMissiles(LinkedList background) {
// does nothing if there are no missiles in the list
if (isEmpty()) {
return;
}
Node current = head;
while (current != null){
//iterate through the linked list and move the missiles
current.missile.move();
//if the iterations after a missile has exploded exceeds the
//explosion duration (defined above), remove node
if(current.missile.iterationsExploded() > explosionDuration) {
//if the missile to be deleted is an enemy missile,
//check to see if it should destroy a city
if (current.missile.isEnemy()){
double xEnd = current.missile.xEnd();
//removes a picture from the background linked list
background.removePicture(xEnd);
}
//remove the missile from the list
delete(current);
}
current = current.next;
}
}
/*
* Name: defeat
* Description: returns true if all the cities have been destroyed
* Inputs: -
* Outputs: True if all cities have been destroyed, and false otherwise
*/
public boolean defeat(){
if ((head.picture.isDeleted() == true) &&
(head.next.picture.isDeleted() == true)
&& (head.next.next.picture.isDeleted() == true)){
//System.out.println("True");
return true;
}
//System.out.println("False");
return false;
}
/*
* Name: removePicture
* Description: simulates the removal of an image by setting a city's
* deleted attribute to true if a missile has landed on the city
* Inputs: double x coordinate of missile
* Outputs: -
*/
public void removePicture(double x){
//if a missile lands within the width of the image, the city
//is marked destroyed
if ((1.0/5.0) <= x && x <= (2.0/5.0)){
head.picture.setDeletion(true);
//System.out.println("Huntsman");
}
else if ((2.0/5.0) < x && x <= (3.0/5.0)){
head.next.picture.setDeletion(true);
//System.out.println("PennMedicine");
}
else if ((3.0/5.0) < x && x <= (4.0/5.0)){
head.next.next.picture.setDeletion(true);
//System.out.println("Singh");
}
}
/*
* Name: draw
* Description: draws the missiles in a linked list
* Inputs: -
* Outputs: -
*/
public void draw() {
// does nothing if linked list is empty
if (isEmpty()) {
return;
}
Node current = head;
while (current != null){
//if the missile is an enemy, draw the enemy missile image
if(current.missile.isEnemy()){
PennDraw.picture(current.missile.xCurrent(),
current.missile.yCurrent(),
"missiles.png", 100, 100);
}
//if the missile is defensive, draw the user missile image
if(!current.missile.isEnemy()){
PennDraw.picture(current.missile.xCurrent(),
current.missile.yCurrent(),
"defensivemissiles.png", 100, 100);
}
current = current.next;
}
}
/*
* Name: drawTo
* Description: draws the pictures in a linked list
* Inputs: -
* Outputs: -
*/
public void drawTo() {
//does nothing if the list is empty
if (isEmpty()) {
return;
}
Node current = head;
while (current != null){
//only draw the image if the city has not been destroyed
if (current.picture.isDeleted() == false) {
PennDraw.picture(current.picture.x(),
current.picture.y(),
current.picture.filename(),
current.picture.width(),
current.picture.height());
}
current = current.next;
}
}
/*
* Name: isDestroying
* Description: Goes through the linked list and eliminates all missiles being
* destroyed recursively.
* Inputs: Node n. Takes in head of linked list
* Outputs: -
*/
public void isDestroying(Node n) {
//do nothing if the list is empty
if (isEmpty()) {
return;
}
// only check through the list if a defensive missile is exploding
if (!n.missile.isEnemy()) {
// check if the missile is exploding
if (n.missile.isExploding()) {
Node current = head;
// if so, iterate through the list to see if the other missiles
//are within the explosion radius
while (current != null) {
// The user missile cannot destroy itself or other
// user missiles.
if (current != n && current.missile.isEnemy()) {
// If the enemy missile is within the blast, delete
// it by eliminating it from the linked list
if (current.missile.distanceTo(n.missile) <
current.missile.explosionRadius()) {
//delete the missile
delete(current);
size--;
//increment the player's score for every missile
//destroyed
score+=100;
}
}
current = current.next;
}
}
}
// only continue checking through the list if current is not lastNode
if (n.next != null) {
isDestroying(n.next);
}
}
}