-
Notifications
You must be signed in to change notification settings - Fork 0
/
SnakeGame.java
264 lines (247 loc) · 6.33 KB
/
SnakeGame.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
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
import java.awt.Color;
import java.util.ArrayList;
import java.io.*;
import java.util.*;
import java.lang.reflect.Field;
/**
* Write a description of class Snake here.
*
* @author Jin Kim
* @author David Melisso
* @version April 4, 2016
*/
public class SnakeGame implements ArrowListener
{
public boolean playay;
private MyBoundedGrid<Block> grid;
public BlockDisplay display;
public Snake snake;
private int waitTime;
private Location foodSpawningLocationVariable;
/**
* Constructor for the SnakeGame with waitTime and Color
*/
public SnakeGame(Color color, int time)
{
playay = true;
grid = new MyBoundedGrid<Block> (15,20);
snake = new Snake(color, grid, this);
display = new BlockDisplay(grid);
display.setArrowListener(this);
display.setTitle("Snake");
waitTime = time;
play();
}
/**
* Constructor for SnakeGame with Wait Time
*
* @param time the wait time to put in
*/
public SnakeGame(int time)
{
this(Color.BLUE, time);
}
/**
* Constructor for SnakeGame with Color
*/
public SnakeGame(Color color)
{
this(color, 100);
}
/**
* Default Constructor for the SnakeGame
*/
public SnakeGame()
{
this(Color.BLUE, 100);
}
public MyBoundedGrid<Block> getGrid()
{
return grid;
}
public int getWaitTime()
{
return waitTime;
}
/**
* Runs the game
*/
public void play()
{
spawnFoodStuff();
while(playay)
{
try
{
Thread.sleep(waitTime);
if(snake.determineDirection()) //we need a new method to get the direction of the snake.
{
display.showBlocks();
}
else
{
//snake dies here.
gameOver(false);
}
}
catch(InterruptedException e)
{
//ignore
}
}
}
/**
* Up is pressed.
*/
public void upPressed()
{
if(snake.changeDirection("UP"))
{
display.showBlocks();
}
}
/**
* Down is pressed.
*/
public void downPressed()
{
if(snake.changeDirection("DOWN"))
{
display.showBlocks();
}
}
/**
* Left is pressed.
*/
public void leftPressed()
{
if(snake.changeDirection("LEFT"))
{
display.showBlocks();
}
}
/**
* Right is pressed.
*/
public void rightPressed()
{
if(snake.changeDirection("RIGHT"))
{
display.showBlocks();
}
}
/**
* Spawns a foodstuff
*/
public void spawnFoodStuff()
{
ArrayList<Location> emptyLocs = grid.getUnoccupiedLocations();
int size = emptyLocs.size();
if (size==0)
{
gameOver(true);
return;
}
int rand = (int)(Math.random()*size);
Location newFoodStuff = emptyLocs.get(rand);
foodSpawningLocationVariable = newFoodStuff;
Block square = new Block();
square.putSelfInGrid(grid, foodSpawningLocationVariable);
square.setColor(Color.GREEN);
}
public Location getFoodStuffSpawningLocation()
{
return foodSpawningLocationVariable;
}
/**
* Ends the game
*
* @param won whether game was won or lost
*/
public void gameOver(boolean won)
{
playay = false;
display.showBlocks();
if (!won)
{
new Tetris(true);
System.out.println("You lost");
System.out.println("You ate " + snake.getNumItemsEaten() + " blocks.");
}
else
{
System.out.println("You won!");
}
}
/**
* Oversees the operation of this class.
*
* @param args user's information from the command line
*
* @throws IOException if file with the hurricane information cannot be found
*/
public static void main (String [] args)
{
boolean playing = true;
while(playing)
{
Scanner in = new Scanner(System.in);
System.out.println("Choose Difficulty:");
System.out.println("Type 1 for Hard");
System.out.println("Type 2 for Medium");
System.out.println("Type 3 for Easy");
System.out.println("Type 4 for the AI to play it.");
System.out.println("Type 5 for the Custom AI to play it.");
System.out.println("Type 6 for a Custom game");
System.out.println("Type 7 for a AI to play it forever");
System.out.println("Type 8 to quit.");
System.out.println("Note* Difficulty will vary by the speed of snake.");
int choice = in.nextInt();
if(choice==1)
{
SnakeGame hardGame = new SnakeGame(50);
}
else if(choice==2)
{
SnakeGame mediumGame = new SnakeGame(100);
}
else if(choice==3)
{
SnakeGame easyGame = new SnakeGame(200);
}
else if(choice == 4)
{
SnakeAI computer = new SnakeAI();
}
else if(choice == 5)
{
System.out.println("Enter the speed of the AI");
int speed = in.nextInt();
SnakeAI computer = new SnakeAI(speed);
}
else if(choice == 6)
{
System.out.println("Enter the speed of the game");
int speed = in.nextInt();
SnakeGame customGame = new SnakeGame(speed);
}
else if(choice == 7)
{
System.out.println("Enter the speed of the AI");
int speed = in.nextInt();
while (true)
{
SnakeAI computer = new SnakeAI(speed);
}
}
else if(choice == 8)
{
playing = false;
}
else
{
throw new IllegalArgumentException("Not a valid entry");
}
}
}
}