-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZtype.java
More file actions
642 lines (530 loc) · 19.1 KB
/
Ztype.java
File metadata and controls
642 lines (530 loc) · 19.1 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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
import tester.*;
import tester.Tester;
import java.awt.Color;
import javalib.funworld.World;
import javalib.funworld.WorldScene;
import javalib.worldimages.FontStyle;
import javalib.worldimages.RectangleImage;
import javalib.worldimages.TextImage;
import javalib.worldcanvas.*;
import java.util.Random;
import javalib.worldimages.*;
import javalib.funworld.*;
import java.awt.Color;
import java.util.Random;
//implements the class ZTypeWorld
class ZTypeWorld extends World {
Random rand;
ILoWord listOfWords;
IWord currentWord;
int timer;
int lives;
int score;
// Constructor
ZTypeWorld(Random rand, ILoWord listOfWords, IWord currentWord, int timer, int lives, int score) {
this.rand = rand;
this.listOfWords = listOfWords;
this.currentWord = currentWord;
this.timer = timer;
this.lives = lives;
this.score = score;
}
// Constructor
ZTypeWorld(Random rand) {
this.rand = rand;
this.listOfWords = new Utils().randomWord(10, rand);
this.currentWord = new InactiveWord("", 0, 0);
this.timer = 10;
this.lives = 1;
this.score = 0;
}
// Constructor
ZTypeWorld() {
this.rand = new Random();
this.listOfWords = new Utils().randomWord(10, rand);
this.currentWord = new InactiveWord("", 0, 0);
this.timer = 10;
}
// draws the WorldScene
public WorldScene makeScene() {
return this.listOfWords.draw(new WorldScene(500, 500));
}
public WorldScene lastScene(String msg) {
return new WorldScene(300, 300)
.placeImageXY(new RectangleImage(300, 300, "solid", Color.BLACK), 150, 150)
.placeImageXY(new TextImage("Game Over", Color.RED), 100, 100);
}
// every tick it moves words down and every tenth tick adds new word
public World onTick() {
if (this.listOfWords.bottomOfScreen()) {
return this.endOfWorld("Game Over!!");
}
if (this.timer % 10 == 0) {
return new ZTypeWorld(this.rand,
this.listOfWords.moveWordsDown()
.addToEnd(new InactiveWord(new Utils().randomSix(rand),
rand.nextInt(IUtils.BACKGROUND_WIDTH), 0)),
this.currentWord, timer + 1, this.lives, this.score);
}
return new ZTypeWorld(this.rand, this.listOfWords.moveWordsDown(), this.currentWord, timer + 1,
this.lives, this.score);
}
// when a key is pressed makes new world and if active check and reduces
public World onKeyEvent(String key) {
if (this.listOfWords.isActive()) {
return new ZTypeWorld(this.rand, this.listOfWords.checkAndReduce(key).remove(),
this.currentWord, this.timer, this.lives, this.score);
}
else {
return new ZTypeWorld(this.rand, this.listOfWords.changeWord(key).remove(), this.currentWord,
this.timer, this.lives, this.score);
}
}
}
// implements the IUtils interface
interface IUtils {
// constants
int BACKGROUND_HEIGHT = 600;
int BACKGROUND_WIDTH = 600;
String ALPHABET = "abcdefghijklmnopqrstuvwxyz";
}
//implements the Utils class
class Utils implements IUtils {
// produces a six letter random word
String randomSix(Random rand) {
return randomSixAcc(rand, "", 6);
}
// accumulator that produces a string of random letters
String randomSixAcc(Random rand, String acc, int count) {
if (count == 0) {
return acc;
}
else {
int index = rand.nextInt(26);
return randomSixAcc(rand, acc + ALPHABET.substring(index, index + 1), count - 1);
}
}
// generates random word with place on screen
ILoWord randomWord(int length, Random rand) {
if (length == 0) {
return new MtLoWord();
}
else {
return new ConsLoWord(
new InactiveWord(this.randomSix(rand), rand.nextInt(IUtils.BACKGROUND_WIDTH), 0),
this.randomWord(length - 1, rand));
}
}
}
//represents a list of words
interface ILoWord {
//draws all the words onto the WorldScene
WorldScene draw(WorldScene w);
// removes an empty words from list
ILoWord remove();
// moves words down the screen
ILoWord moveWordsDown();
// adds IWord to end of a list of words (ILoWord
ILoWord addToEnd(IWord w);
//reduces active words in a ILoWord by removing the first letter
// if the given string matches the first letter
ILoWord checkAndReduce(String letter);
// checks if word is active or inactive
boolean isActive();
// if inactive word make a word active
// if matches letter & if active reduce
ILoWord changeWord(String letter);
// checks if words made it to bottom of screen
boolean bottomOfScreen();
}
//represents an empty list of words
class MtLoWord implements ILoWord {
//reduces active words in a ILoWord by removing the first letter
// if the given string matches the first letter
public ILoWord checkAndReduce(String letter) {
return this;
}
// draws all of the words in this ILoWord onto the given WorldScene
public WorldScene draw(WorldScene w) {
return w;
}
// produces an ILoWord with the given IWord added at the end
public ILoWord addToEnd(IWord w) {
return new ConsLoWord(w, this);
}
// removes an empty words from list
public ILoWord remove() {
return this;
}
// moves words down the screen
public ILoWord moveWordsDown() {
return this;
}
// checks if word is active or inactive
public boolean isActive() {
return false;
}
// if inactive word make a word active if matches letter and if active reduce
public ILoWord changeWord(String letter) {
return this;
}
// checks if words made it to bottom of screen
public boolean bottomOfScreen() {
return false;
}
}
//implements the class ConsLoWord
class ConsLoWord implements ILoWord {
IWord first;
ILoWord rest;
ConsLoWord(IWord first, ILoWord rest) {
this.first = first;
this.rest = rest;
}
// draws all the words onto the WorldScene
public WorldScene draw(WorldScene w) {
return this.rest.draw(this.first.drawWord(w));
}
// produces an ILoWord with the given IWord added at the end
public ILoWord addToEnd(IWord w) {
return new ConsLoWord(this.first, this.rest.addToEnd(w));
}
// if inactive word make a word active
// if matches letter and if active reduce
public ILoWord changeWord(String letter) {
if (this.first.helperCheckAndReduce(letter).beActive()) {
return new ConsLoWord(this.first.helperCheckAndReduce(letter), this.rest);
}
else {
return new ConsLoWord(this.first, this.rest.changeWord(letter));
}
}
// checks if word is active or inactive
public boolean isActive() {
if (this.first.beActive()) {
return true;
}
else {
return this.rest.isActive();
}
}
// removes empty words from list
public ILoWord remove() {
if (this.first.isEmpty()) {
return this.rest;
}
else {
return new ConsLoWord(this.first, this.rest.remove());
}
}
// moves words down the screen
public ILoWord moveWordsDown() {
return new ConsLoWord(this.first.moveDown(), this.rest.moveWordsDown());
}
//reduces active words in a ILoWord by removing the first letter
// if the given string matches the first letter
public ILoWord checkAndReduce(String letter) {
if (this.first.beActive()) {
return new ConsLoWord(this.first.helperCheckAndReduce(letter),
this.rest.checkAndReduce(letter));
}
else {
return new ConsLoWord(this.first, this.rest.checkAndReduce(letter));
}
}
// checks if the word have reached the bottom of screen
public boolean bottomOfScreen() {
if (this.first.reachedEnd()) {
return true;
}
else {
return this.rest.bottomOfScreen();
}
}
}
//represents a word in the ZType game
interface IWord {
// checks if words y coordinate reached bottom of screen
boolean reachedEnd();
// draws word on word string
WorldScene drawWord(WorldScene w);
// moves the word down a coordinate
IWord moveDown();
// checks if word is non empty and
// if first letter equals letter and
// if so makes new word without first letter
IWord helperCheckAndReduce(String letter);
// checks if the word is active
boolean beActive();
// checks if a word is empty
boolean isEmpty();
}
//represents an active word in the ZType game
class ActiveWord implements IWord {
String word;
int x;
int y;
ActiveWord(String word, int x, int y) {
this.word = word;
this.x = x;
this.y = y;
}
// checks if y coordinate reached bottom of screen
public boolean reachedEnd() {
return this.y >= Utils.BACKGROUND_HEIGHT;
}
// checks if the word is active
public boolean beActive() {
return true;
}
// checks if a word is empty
public boolean isEmpty() {
return this.word.equals("");
}
// draws a word on the WorldScene
public WorldScene drawWord(WorldScene w) {
return w.placeImageXY(new TextImage(this.word, Color.RED), this.x, this.y);
}
// moves the word down a coordinate
public IWord moveDown() {
return new ActiveWord(this.word, this.x, this.y + 1);
}
//checks if word is non empty and
// if first letter equals letter and
// if so makes new word without first letter
public IWord helperCheckAndReduce(String s) {
if (this.word.length() > 0 &&
this.word.substring(0, 1).equals(s)) {
return new ActiveWord(this.word.substring(1, this.word.length()),
this.x, this.y);
}
return this;
}
}
//represents an inactive word in the ZType game
class InactiveWord implements IWord {
String word;
int x;
int y;
InactiveWord(String word, int x, int y) {
this.word = word;
this.x = x;
this.y = y;
}
// checks if words y coordinate reached bottom of screen
public boolean reachedEnd() {
return this.y >= Utils.BACKGROUND_HEIGHT;
}
//checks if word is non empty and
// if first letter equals letter and
// if so makes new word without first letter
public IWord helperCheckAndReduce(String s) {
if (this.word.length() > 0 && this.word.substring(0, 1).equals(s)) {
return new ActiveWord(this.word.substring(1, this.word.length()), this.x, this.y);
}
return this;
}
// checks if a word is empty
public boolean isEmpty() {
return this.word.equals("");
}
// checks if the word is active
public boolean beActive() {
return false;
}
// draws a word on the WorldScene
public WorldScene drawWord(WorldScene w) {
return w.placeImageXY(new TextImage(this.word, Color.BLUE), this.x, this.y);
}
// moves the word down a coordinate
public IWord moveDown() {
return new InactiveWord(this.word, this.x, this.y + 1);
}
}
// All examples and tests for ILoWord
class ExamplesWordLists {
// empty list of words
ILoWord mtWord = new MtLoWord();
// Inactive words
IWord word1 = new InactiveWord("apple", 100, 100);
IWord word2 = new InactiveWord("banana", 200, 30);
IWord word3 = new InactiveWord("orange", 300, 400);
IWord word4 = new InactiveWord("grape", 70, 400);
IWord word5 = new InactiveWord("strawberry", 50, 400);
IWord word6 = new InactiveWord("pineapple", 50, 40);
// Active words
IWord word7 = new ActiveWord("peach", 150, 200);
IWord word8 = new ActiveWord("watermelon", 250, 300);
IWord word9 = new ActiveWord("kiwi", 350, 50);
IWord word10 = new ActiveWord("blueberry", 70, 150);
IWord word11 = new ActiveWord("raspberry", 200, 250);
IWord word12 = new ActiveWord("blackberry", 400, 400);
// Empty lists
ILoWord empty = new MtLoWord();
// Lists with multiple words
// Examples of scenes
WorldScene blankScene = new WorldScene(500, 500)
.placeImageXY(new RectangleImage(250, 250, "solid", Color.GRAY), 250, 250);
WorldScene expectedSceneInactiveWords = blankScene
.placeImageXY(new TextImage("apple", Color.BLUE), 100, 100)
.placeImageXY(new TextImage("banana", Color.BLUE), 200, 30)
.placeImageXY(new TextImage("orange", Color.BLUE), 300, 400)
.placeImageXY(new TextImage("grape", Color.BLUE), 70, 400)
.placeImageXY(new TextImage("strawberry", Color.BLUE), 50, 400)
.placeImageXY(new TextImage("pineapple", Color.BLUE), 50, 40);
WorldScene expectedSceneActiveWords = blankScene
.placeImageXY(new TextImage("peach", Color.RED), 150, 200)
.placeImageXY(new TextImage("watermelon", Color.RED), 250, 300)
.placeImageXY(new TextImage("kiwi", Color.RED), 350, 50)
.placeImageXY(new TextImage("blueberry", Color.RED), 70, 150)
.placeImageXY(new TextImage("raspberry", Color.RED), 200, 250)
.placeImageXY(new TextImage("blackberry", Color.RED), 400, 400);
// Tests for the Utils class
Utils u = new Utils();
//Tests the isEmpty method
boolean testIsEmpty(Tester t) {
return t.checkExpect(new InactiveWord("",100,100).isEmpty(), true)
&& t.checkExpect(word1.isEmpty(), false)
&& t.checkExpect(word7.isEmpty(), false);
}
//Tests the addToEnd method
boolean testAddToEnd(Tester t) {
// Adds a word to the end that's already in the list
return t.checkExpect(new ConsLoWord(word1, empty).addToEnd(word1),
new ConsLoWord(word1, new ConsLoWord(word1, empty)))
// Adds a new word to the end
&& t.checkExpect(new ConsLoWord(word1, empty).addToEnd(word2),
new ConsLoWord(word1, new ConsLoWord(word2, empty)))
// Invoked by empty list
&& t.checkExpect(empty.addToEnd(word1),
new ConsLoWord(word1, new ConsLoWord(word1, empty)));
}
//Tests the helperCheckAndReduce method
boolean testHelperCheckAndReduce(Tester t) {
// Checks an empty case
return t.checkExpect(new ActiveWord("", 100, 100).helperCheckAndReduce("m"), empty)
// Checks a non-empty invoked on an empty string
&& t.checkExpect(word2.helperCheckAndReduce(""), word2)
// Checks where it removes the first letter
&& t.checkExpect(word1.helperCheckAndReduce("a"),
new ActiveWord("pple", 100, 100))
// Checks where it doesn't remove the first letter
&& t.checkExpect(word7.helperCheckAndReduce("f"), word7);
}
//Tests the checkAndReduce method
boolean testCheckAndReduce(Tester t) {
// Reduces all items in list since both start with inputted letter
return t.checkExpect(new ConsLoWord(word1, new ConsLoWord(word1, empty)).checkAndReduce("a"),
new ConsLoWord(new ActiveWord("pple", 100, 100),
new ConsLoWord(new ActiveWord("pple", 100, 100), empty)))
// Doesn't reduce any items since first letter doesn't match any
&& t.checkExpect(new ConsLoWord(word2, empty).checkAndReduce("Z"),
new ConsLoWord(word2, empty));
}
//Tests the beActive method
boolean testBeActive(Tester t) {
return t.checkExpect(word10.beActive(), true) && t.checkExpect(word1.beActive(), false);
}
//Tests the isActive method
boolean testIsActive(Tester t) {
return t.checkExpect(new ConsLoWord(word10, empty).isActive(), true)
&& t.checkExpect(new ConsLoWord(word1, empty).isActive(), false)
&& t.checkExpect(empty.isActive(), false);
}
//Tests the onTick method
boolean testOnTick(Tester t) {
return t.checkExpect(new ZTypeWorld().onTick(), new ZTypeWorld())
&& t.checkExpect(new ZTypeWorld().onTick(), new ZTypeWorld())
&& t.checkExpect(new ZTypeWorld().onTick(), new ZTypeWorld());
}
//Tests the moveDown method
boolean testMoveDown(Tester t) {
return t.checkExpect(word7.moveDown(), new ActiveWord("peach", 150, 201))
&& t.checkExpect(word1.moveDown(), new InactiveWord("apple", 100, 101));
}
//Tests the moveWordsDown method
boolean testMoveWordsDown(Tester t) {
return t.checkExpect(new ConsLoWord(word1, new ConsLoWord(word2, empty)).moveWordsDown(),
new ConsLoWord(new InactiveWord("apple", 100, 101),
new ConsLoWord(new InactiveWord("banana", 200, 31), empty)))
&& t.checkExpect(empty.moveWordsDown(), empty);
}
//Tests the remove method
boolean testRemove(Tester t) {
return t.checkExpect(empty.remove(), empty)
&& t.checkExpect(new ConsLoWord(word1, new ConsLoWord(word2, empty)).remove(),
new ConsLoWord(word1, new ConsLoWord(word2, empty)))
&& t.checkExpect(new ConsLoWord(word1, empty).remove(), empty);
}
//Tests the changeWord method
boolean testChangeWord(Tester t) {
return t.checkExpect(new ConsLoWord(word2, new ConsLoWord(word3, empty)).changeWord("p"),
new ConsLoWord(word2, new ConsLoWord(word3, empty)))
&& t.checkExpect(new ConsLoWord(word1, empty).changeWord("a"),
new ConsLoWord(new ActiveWord("pple", 100, 100), empty))
&& t.checkExpect(empty.changeWord("p"), empty);
}
//Tests onKeyEvent method
boolean testOnKeyEvent(Tester t) {
return t.checkExpect(new ZTypeWorld().onKeyEvent("l"), new ZTypeWorld())
&& t.checkExpect(new ZTypeWorld().onKeyEvent("f"), new ZTypeWorld())
&& t.checkExpect(new ZTypeWorld().onKeyEvent(""), new ZTypeWorld());
}
//Tests draw method
boolean testDraw(Tester t) {
return t.checkExpect(new ConsLoWord(word1, new ConsLoWord(word2, empty)).draw(blankScene),
expectedSceneInactiveWords) && t.checkExpect(empty.draw(blankScene), blankScene);
}
//Tests drawWord method
boolean testDrawWord(Tester t) {
return t.checkExpect(word1.drawWord(blankScene), expectedSceneInactiveWords);
}
//Tests the lastScene method
boolean testLastScene(Tester t) {
return t.checkExpect(new ZTypeWorld().lastScene("Game Over!"),
new WorldScene(300, 300)
.placeImageXY(new RectangleImage(300, 300, "solid", Color.BLACK), 150, 150)
.placeImageXY(new TextImage("Game Over!", Color.RED), 100, 100));
}
//Tests the bottomOfScreen method
boolean testBottomOfScreen(Tester t) {
return t.checkExpect(new ConsLoWord(word1, empty).bottomOfScreen(), false)
&& t.checkExpect(new ConsLoWord(word4, empty).bottomOfScreen(), false)
&& t.checkExpect(empty.bottomOfScreen(), false);
}
//Tests the reachedEnd method
boolean testReachedEnd(Tester t) {
return t.checkExpect(word1.reachedEnd(), false) && t.checkExpect(word5.reachedEnd(), false)
&& t.checkExpect(new InactiveWord("", 0, Utils.BACKGROUND_HEIGHT).reachedEnd(), true);
}
// Tests the randomSix method
boolean testRandomSix(Tester t) {
return t.checkExpect(u.randomSix(new Random(0)), "ssxvnj")
&& t.checkExpect(u.randomSix(new Random(10)), 0)
&& t.checkExpect(u.randomSix(new Random(20)), 0);
}
// Tests the randomSixAcc method
boolean testRandomSixAcc(Tester t) {
// Tests when RandomCount is 0
return t.checkExpect(u.randomSixAcc(new Random(0), "", 0), "")
// Tests when count is 1 and letters are inputted
&& t.checkExpect(u.randomSixAcc(new Random(10), "abc", 1), "abcp")
// Tests when count is 6 like in game
&& t.checkExpect(u.randomSixAcc(new Random(20), "", 6), "xsvphp");
}
// Tests the randomWord method
boolean testRandomWord(Tester t) {
return t.checkExpect(u.randomWord(0, new Random(0)), new MtLoWord())
&& t.checkExpect(u.randomWord(6, new Random(10)), "");
}
//Runs ZType game
boolean testBigBang(Tester t) {
ZTypeWorld world = new ZTypeWorld(new Random());
double tickRate = .1;
return world.bigBang(IUtils.BACKGROUND_WIDTH, IUtils.BACKGROUND_HEIGHT, tickRate);
}
//Runs empty ZType game (non-functional)
boolean testWorldScene(Tester t) {
return t.checkExpect(empty.draw(blankScene), blankScene);
}
}