-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameTest.java
190 lines (138 loc) · 4.57 KB
/
GameTest.java
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
package org.cis1200.snake;
import org.junit.jupiter.api.*;
import javax.swing.*;
import static org.junit.jupiter.api.Assertions.*;
public class GameTest {
// edge case
@Test
public void testOriginalState() {
JLabel player1status = new JLabel();
JLabel blueFruit = new JLabel();
GameCourt g = new GameCourt(player1status, blueFruit);
g.reset();
// checking original snake size
assertEquals(4, g.player1Snake().size());
// checking each snake's original status
assertEquals(0, g.snake1Eaten());
}
@Test
public void testEatingFruit() {
JLabel player1status = new JLabel();
JLabel blueFruit = new JLabel();
GameCourt g = new GameCourt(player1status, blueFruit);
g.reset();
g.tick();
g.tick();
g.tick();
g.tick();
g.tick();
g.tick();
g.tick();
g.tick();
g.tick();
g.tick();
// checking fruit list size
assertEquals(3, GameCourt.getFruitList().size());
// checking snake size
assertEquals(1, g.snake1Eaten());
assertEquals(5, g.player1Snake().size());
// checking each snake's eating status
assertEquals(1, g.snake1Eaten());
// reset to check game states
g.reset();
assertEquals(4, g.player1Snake().size());
assertEquals(0, g.snake1Eaten());
}
@Test
public void testHitWall() {
JLabel player1status = new JLabel();
JLabel blueFruit = new JLabel();
GameCourt g = new GameCourt(player1status, blueFruit);
g.reset();
for (int i = 0; i < 14; i++) {
g.tick();
}
// checking velocity of each snake
assertEquals(25, g.velocityXSnake1());
assertEquals(0, g.velocityYSnake1());
// check the playing state is false
assertTrue(g.getPlayingState());
}
@Test
public void testEatBurger() {
JLabel player1status = new JLabel();
JLabel blueFruit = new JLabel();
GameCourt g = new GameCourt(player1status, blueFruit);
g.reset();
for (int i = 0; i < 15; i++) {
g.tick();
}
assertTrue(g.getPlayingState());
g.player1Snake().get(0).setVx(0);
g.player1Snake().get(0).setVy(-25);
for (int i = 0; i < 8; i++) {
g.tick();
}
assertTrue(g.getPlayingState());
// checking velocity of snake when eats burger
assertEquals(0, g.velocityXSnake1());
assertEquals(-12, g.velocityYSnake1());
g.tick();
// checking velocity of snake when it died
assertEquals(0, g.velocityXSnake1());
assertEquals(0, g.velocityYSnake1());
// check the playing state is false - hit a wall and died
assertFalse(g.getPlayingState());
assertEquals(6, g.player1Snake().size());
}
@Test
public void testEatGrape() {
JLabel player1status = new JLabel();
JLabel blueFruit = new JLabel();
GameCourt g = new GameCourt(player1status, blueFruit);
g.reset();
for (int i = 0; i < 14; i++) {
g.tick();
}
assertTrue(g.getPlayingState());
g.player1Snake().get(0).setVx(0);
g.player1Snake().get(0).setVy(25);
for (int i = 0; i < 13; i++) {
g.tick();
}
assertFalse(g.getPlayingState());
// checking velocity of snake when it died
assertEquals(0, g.velocityXSnake1());
assertEquals(0, g.velocityYSnake1());
assertEquals(6, g.player1Snake().size());
// testing high score
assertEquals(1, g.getBlueHighScore());
}
@Test
public void testKillingItself() {
JLabel player1status = new JLabel();
JLabel blueFruit = new JLabel();
GameCourt g = new GameCourt(player1status, blueFruit);
g.reset();
for (int i = 0; i < 8; i++) {
g.tick();
}
assertTrue(g.getPlayingState());
// going up
g.player1Snake().get(0).setVx(0);
g.player1Snake().get(0).setVy(-25);
g.tick();
// going left
g.player1Snake().get(0).setVx(-25);
g.player1Snake().get(0).setVy(0);
g.tick();
// going down
g.player1Snake().get(0).setVx(0);
g.player1Snake().get(0).setVy(25);
g.tick();
assertFalse(g.getPlayingState());
// checking velocity of snake when it died
assertEquals(0, g.velocityXSnake1());
assertEquals(0, g.velocityYSnake1());
}
}