-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.java
More file actions
379 lines (320 loc) · 10.1 KB
/
Copy pathmain.java
File metadata and controls
379 lines (320 loc) · 10.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
import java.util.*;
import java.io.*;
import java.lang.Math;
public class main{
static Scanner s = new Scanner(System.in);
static int boardWidth = 20;
static int boardLength = 20;
static int timeSleep = 500;
static int startLength = 5;
// static thread1 t1 = new thread1();
static String com = "d";
static int[][] gameWindow = new int[boardWidth][boardLength];
static int[] p = {5,5};
static Snake python = new Snake(boardWidth/2, boardLength/2);
static BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out));
static String border = "";
static int score;
static int topScore;
static Snake.Coord food;
static boolean pause = false;
static{
for(int i = 0; i < boardLength*1.2; i++){
border += "%";
}
}
public static void cls(){
try{
new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
}
catch(Exception e){
System.out.println(e);
}
}
public static void enterWait(){
System.out.print("Press Enter to continue...");
try{
s.nextLine();
}catch(Exception e){
System.out.println("No Input");
}
}
public static void displayGame() throws Exception{
boolean head = true;
output.write("Score : " + score + "\n");
// output.write("Food at "+ food.x + " " + food.y + "\n");
output.write(border + "\n");
for(int i = 0; i < boardWidth; i++ ) {
// System.out.print("/");
output.write('%');
for(int j = 0; j < boardLength; j++){
if(gameWindow[i][j]==1){
output.write('#');
gameWindow[i][j] = 0;
}else if(gameWindow[i][j] == 2){
output.write('*');
}else if(gameWindow[i][j] == 3){
output.write('@');
gameWindow[i][j] = 0;
}
else{
output.write(' ');
}
}
output.write("%\n");
}
// System.out.println("////////////");
output.write(border + "\n");
output.flush();
}
public static boolean move(){
boolean is_valid = python.play(com);
Snake.Coord temp = python.snakePoints.get(0);
gameWindow[temp.x][temp.y] = 3;
for(int i = 1; i < python.snakePoints.size(); i++){
int x = python.snakePoints.get(i).x;
int y = python.snakePoints.get(i).y;
gameWindow[x][y] = 1;
}
return is_valid;
}
public static void bestScore(){
cls();
System.out.println("Top Score : " + topScore);
enterWait();
}
public static void instruction(){
cls();
System.out.println("Direction Keys\nA - Left\nW - Up\nD - Right\nS - Down");
System.out.println("Press the key [w,s,a,d] once and press enter to change snake direction");
System.out.println("One Snack Adds One Score and also One Size to Snake");
enterWait();
}
public static void difficulty(){
cls();
System.out.print("Enter Difficulty (1 - 5) : ");
int diff = s.nextInt();
diff = diff>5?5:diff;
diff = diff<1?1:diff;
timeSleep = 500 - (diff - 1)*100;
startLength = diff*2;
}
public static void welcome(){
int com1 = 1;
while(com1 != 0){
cls();
System.out.println(" Snake Classic ");
// System.out.println("Invalid Choise");
System.out.print("1 - New Game\n2 - Best Score\n3 - Instruction\n4 - Difficulty\n0 - Quit\nChoise : ");
com1 = s.nextInt();s.nextLine();
if(com1 == 1){
game();
}else if(com1 == 2){
bestScore();
}else if(com1 == 3){
instruction();
}else if(com1==4){
difficulty();
}else if(com1 == 0){
break;
}
else{
com1 = -1;
}
}
}
public static void game(){
python = new Snake(boardWidth/2, boardLength/2);
thread1 t1 = new thread1();
t1.start();
for(int i = 0 ; i < startLength; i++){
python.extend(com);
}
python.initFood();
cls();
while(!com.equals("e")){
if(pause){
pause = false;
enterWait();
t1.resume();
}
if(move()){
cls();
try{displayGame();}catch(Exception e){}
}else{
System.out.println("Snake Bit itself");
// t1.stop();
break;
}
try{Thread.sleep(timeSleep);}catch(Exception e){}
}
t1.stop();
System.out.println("Game Exit");
if(score > topScore){topScore = score;}
score = 0;
com = "s";
gameWindow = new int[boardWidth][boardLength];
enterWait();
}
public static void main(String[] args) {
welcome();
}
}
class Snake{
class Coord{
int x;
int y;
public Coord(int x, int y){
this.x = x;
this.y = y;
}
public boolean equals(Coord c){
if(c.x == x && c.y == y){
return true;
}return false;
}
}
ArrayList<Coord> snakePoints = new ArrayList<Coord>();
Coord food;
public Snake(int x, int y){
Coord head = new Coord(x,y);
snakePoints.add(head);
}
public boolean contains(Coord c){
for(int i = 0; i < snakePoints.size(); i++){
Coord temp = snakePoints.get(i);
if(temp.x == c.x && temp.y == c.y){
return true;
}
}
return false;
}
public Coord randCoord(int w, int l){
int randX = (int)(Math.random()*(l));
int randY = (int)(Math.random()*(w));
Coord rtn = new Coord(randX, randY);
return rtn;
}
public void initFood(){
int w = main.boardWidth, l = main.boardLength;
Coord temp_food;
do{
temp_food = randCoord(l,w);
}
while(contains(temp_food));
food = temp_food;
main.food = temp_food;
main.gameWindow[food.x][food.y] = 2;
}
public boolean play(String dir){
Coord head = new Coord(0,0);
Coord prevHead = snakePoints.get(0);
int w = main.boardWidth, l = main.boardLength;
if(dir.equals("s")){
head.y = prevHead.y;
if(prevHead.x == w-1){
head.x = 0;
}else{
head.x = prevHead.x + 1;
}
}else if(dir.equals("w")){
head.y = prevHead.y;
if(prevHead.x == 0){
head.x = w-1;
}else{
head.x = prevHead.x - 1;
}
}else if(dir.equals("d")){
head.x = prevHead.x;
if(prevHead.y == l-1){
head.y = 0;
}else{
head.y = prevHead.y + 1;
}
}else if(dir.equals("a")){
head.x = prevHead.x;
if(prevHead.y == 0){
head.y = l-1;
}else{
head.y = prevHead.y - 1;
}
}
if(contains(head)){
return false;
}else if(head.equals(food)){
snakePoints.add(0, head);
main.score++;
initFood();
}else{
snakePoints.add(0, head);
snakePoints.remove(snakePoints.size()-1);
}
return true;
}
public boolean move(String dir){
boolean is_valid = extend(dir);
snakePoints.remove(snakePoints.size()-1);
return is_valid;
}
public boolean extend(String dir){
Coord head = new Coord(0,0);
Coord prevHead = snakePoints.get(0);
int w = main.boardWidth, l = main.boardLength;
if(dir.equals("s")){
head.y = prevHead.y;
if(prevHead.x == w-1){
head.x = 0;
}else{
head.x = prevHead.x + 1;
}
}else if(dir.equals("w")){
head.y = prevHead.y;
if(prevHead.x == 0){
head.x = w-1;
}else{
head.x = prevHead.x - 1;
}
}else if(dir.equals("d")){
head.x = prevHead.x;
if(prevHead.y == l-1){
head.y = 0;
}else{
head.y = prevHead.y + 1;
}
}else if(dir.equals("a")){
head.x = prevHead.x;
if(prevHead.y == 0){
head.y = l-1;
}else{
head.y = prevHead.y - 1;
}
}
if(!contains(head)){
snakePoints.add(0, head);
return true;
}else{
return false;
}
}
}
class thread1 extends Thread{
Scanner s = new Scanner(System.in);
public void run() {
String com = "d";
while(!com.equals("e")){
com = s.next();
if((main.com.equals("s") || main.com.equals("w")) && (com.equals("a") || com.equals("d"))){
main.com = com;
}else if((main.com.equals("a") || main.com.equals("d")) && (com.equals("w") || com.equals("s"))){
main.com = com;
}else if(com.equals("e")){
main.com = com;
s.close();
stop();
}else if(com.equals("q")){
main.pause = true;
suspend();
}
}
}
}