-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.rkt
495 lines (398 loc) · 17.7 KB
/
Game.rkt
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
;; Please run the code first, (main START) to play
(require 2htdp/image)
(require 2htdp/universe)
;; =================
;; Constants:
(define BOARD-SIZE 20) ; The board is 10 cells by 10 cells
(define CELL-PIXELS 14) ; cells are square
(define BOARD-WIDTH (* BOARD-SIZE CELL-PIXELS)) ;
(define BOARD-HEIGHT BOARD-WIDTH) ;
(define EYES (beside (circle (/ CELL-PIXELS 8) "solid" "black")
(square (/ CELL-PIXELS 8) "solid" "green")
(circle (/ CELL-PIXELS 8) "solid" "black")))
(define HEAD (overlay/offset EYES
0 (/ CELL-PIXELS 3.5)
(square CELL-PIXELS "solid" "green")))
(define BODY (square CELL-PIXELS "solid" "red"))
(define FOOD (square CELL-PIXELS "solid" "blue"))
(define BOMB (circle (/ CELL-PIXELS 2) "solid" "black"))
(define MTS (square BOARD-WIDTH "solid" (make-color 200 200 200)))
(define MESSAGE (text "GAME OVER" 40 "purple"))
(define SCOREBOARD (overlay/align "right" "top"
(text "SCORE" 25 "black")
(rectangle BOARD-SIZE 60 "outline" "white")))
(define SPEED 0.3)
;; =================
;; Data Definitions:
;; Direction is one of:
;; - "U"
;; - "D"
;; - "L"
;; - "R"
;; interp. the four directions a snake could travel
(define D1 "U")
(define D2 "D")
(define D3 "L")
(define D4 "R")
#;
(define (fn-for-direction d)
(cond [(string=? "U" d) (...)]
[(string=? "D" d) (...)]
[(string=? "L" d) (...)]
[(string=? "R" d) (...)]))
;; -----------------------------------------------------------------------------
(define-struct cell (c r)) ; c and r stand for column and row
;; Cell is (make-cell Integer[-1, BOARD-SIZE] Integer[-1, BOARD-SIZE])
;; interp. a cell position on the board from top-left corner
;; -1 and BOARD-SIZE are on the edges of the board and indicate
;; "going out of bounds"/game-over condition
(define C1 (make-cell -1 200))
(define C2 (make-cell 15 BOARD-SIZE))
(define C3 (make-cell BOARD-SIZE -1))
#;
(define (fn-for-cell cl)
(... (cell-c cl)
(cell-r cl)))
;; -----------------------------------------------------------------------------
;; Body is one of:
;; - (cons Cell empty)
;; - (cons Cell Body)
;; interp. the body of a snake
(define B1 (cons (make-cell -1 200) empty))
(define B2 (cons (make-cell BOARD-SIZE 0) (cons (make-cell BOARD-SIZE 1) empty)))
(define B3 (cons (make-cell 14 20) (cons (make-cell 13 20) (cons (make-cell 12 20) empty))))
(define B4 (cons (make-cell 25 3) (cons (make-cell 26 3) (cons (make-cell 27 3) (cons (make-cell 28 3) empty)))))
#;
(define (fn-for-body b)
(cond [(empty? (rest b)) (... (fn-for-cell (first b)))]
[else
(... (fn-for-cell (first b))
(fn-for-body (rest b)))]))
;; -----------------------------------------------------------------------------
(define-struct snake (dir head body))
;; Snake is (make-snake Direction Cell Body)
;; interp. a snake with a head and body moving in some direction
(define S1 (make-snake "U" (make-cell -1 200) (cons (make-cell -1 199) empty)))
(define S2 (make-snake "R" (make-cell 15 BOARD-SIZE)
(cons (make-cell 14 BOARD-SIZE)
(cons (make-cell 13 BOARD-SIZE)
(cons (make-cell 12 BOARD-SIZE) empty)))))
(define S3 (make-snake "D" (make-cell BOARD-SIZE -1) (cons (make-cell BOARD-SIZE 0)(cons (make-cell BOARD-SIZE 1) empty))))
#;
(define (fn-for-snake s)
(... (fn-for-direction (snake-dir s))
(fn-for-cell (snake-head s))
(fn-for-body (snake-body s))))
;; -----------------------------------------------------------------------------
;; Food is Cell
;; interp. a food blob in a cell on the board
(define F1 (make-cell 50 25))
(define F2 (make-cell 0 BOARD-SIZE))
(define F3 (make-cell BOARD-SIZE 5))
#;
(define (fn-for-food f)
(... (fn-for-cell f)))
;; -----------------------------------------------------------------------------
(define-struct bomb (dir cell))
;; Bomb is (make-bomb Direction Cell)
;; interp. a bomb with a direction and a starting cell
(define BMB0 (make-bomb "L" (make-cell BOARD-SIZE 5))) ; bomb at right of board
(define BMB1 (make-bomb "D" (make-cell 6 -1))) ;bomb at top at board
#;
(define (fn-for-bomb bmb)
(... (fn-for-direction (bomb-dir bmb))
(fn-for-cell (bomb-cell bmb))))
;; -----------------------------------------------------------------------------
(define-struct game (food snake score bomb))
;; Game is (make-game Food Snake Score Bomb)
;; interp. the game state with the snake
(define G1 (make-game F1 S1 0 BMB0))
(define G2 (make-game F2 S2 3 BMB1))
(define G3 (make-game F3 S3 5 BMB0))
#;
(define (fn-for-game g)
(... (fn-for-cell (game-food g))
(fn-for-snake (game-snake g))
(fn-for-score (game-score g))
(fn-for-bomb (game-bomb g))))
(define START (make-game (make-cell 5 10)
(make-snake "R" (make-cell 0 5)
(list (make-cell 0 6)
(make-cell 0 7)
(make-cell 0 8)))
0
(make-bomb "U" (make-cell 2 (- BOARD-SIZE 1)))))
;; =================
;; Functions:
;; Game -> Game
;; start the world with (main START)
(define (main g)
(big-bang g ; WS
(on-tick tock SPEED) ; WS -> WS
(to-draw render) ; WS -> Image
(on-key handle-key) ; WS KeyEvent -> WS
(stop-when last-world? last-picture))) ; WS -> Boolean
;; -----------------------------------------------------------------------------
;; Game -> Game
; produce the next game
(define (tock g)
(make-game
(if (same-cell-head? (game-food g) (game-snake g))
(random-food (game-food g))
(game-food g))
(cond [(same-cell-head? (game-food g) (game-snake g))
(tock-snake-add-body (game-snake g))];add a body cell when snake eats food
[(same-cell-body? (game-bomb g) (game-snake g))
(injure-snake (game-bomb g) (game-snake g))]
[else (tock-snake (game-snake g))])
(if (same-cell-head? (game-food g) (game-snake g))
(+ 1 (game-score g))
(game-score g))
(if (outside-board--bomb? (game-bomb g))
(random-bomb (game-snake g))
(tock-bomb (game-bomb g)))))
;; -----------------------------------------------------------------------------
;; Cell Snake -> Boolean
;; produce true if Cell is in the same cell as the head of the snake
(define (same-cell-head? cl s)
(and (= (cell-c cl) (cell-c (snake-head s)))
(= (cell-r cl) (cell-r (snake-head s)))))
;; -----------------------------------------------------------------------------
;; Cell -> Cell
;; Given current location of snake's head produce a different random location for food
(define (random-food cl)
(random-food-check cl (make-cell (random BOARD-SIZE) (random BOARD-SIZE))))
;; -----------------------------------------------------------------------------
;; Cell Cell -> Cell
;; generative recursion
;; if cl is different from candidate produce candidate; otherwise try a new candidate
(define (random-food-check cl candidate)
(if (equal? cl candidate)
(random-food cl)
candidate))
;; -----------------------------------------------------------------------------
;; Snake -> Snake
;; given a snake, produce the next snake
(define (tock-snake s)
(make-snake (snake-dir s)
(next-cell (snake-dir s) (snake-head s))
(advance-body (snake-head s) (snake-body s))))
(define (tock-snake-add-body s)
(make-snake (snake-dir s)
(next-cell (snake-dir s) (snake-head s))
(cons (snake-head s) (snake-body s)))) ;; add body by not removing the
;; former position of the snake head
;; -----------------------------------------------------------------------------
;; Direction Cell -> Cell
;; given a direction and a cell, produce the next cell
(define (next-cell d cl)
(cond [(string=? "U" d) (if (= -1 (cell-r cl))
cl
(make-cell (cell-c cl) (- (cell-r cl) 1)))]
[(string=? "D" d) (if (= BOARD-SIZE (cell-r cl))
cl
(make-cell (cell-c cl) (+ (cell-r cl) 1)))]
[(string=? "L" d) (if (= -1 (cell-c cl))
cl
(make-cell (- (cell-c cl) 1) (cell-r cl)))]
[(string=? "R" d) (if (= BOARD-SIZE (cell-c cl))
cl
(make-cell (+ (cell-c cl) 1) (cell-r cl)))]))
;; -----------------------------------------------------------------------------
;; Body -> Body
;; produce the body without the last item
(define (remove-last b)
(cond [(empty? (rest b)) empty]
[else
(cons (first b)
(remove-last (rest b)))]))
;; -----------------------------------------------------------------------------
;; Cell Body -> Body
;; given a snake and a body, produce the new body
(define (advance-body cl b)
(cons cl (remove-last b)))
;; -----------------------------------------------------------------------------
;; Bomb -> Boolean
;; produce true if the bomb is outside the board
(define (outside-board--bomb? bmb)
(outside-board--cell? (bomb-cell bmb)))
;; -----------------------------------------------------------------------------
;; Cell -> Boolean
;; produce true if the cell is outside the game board
(define (outside-board--cell? cl)
(or (= -1 (cell-c cl))
(= BOARD-SIZE (cell-c cl))
(= -1 (cell-r cl))
(= BOARD-SIZE (cell-r cl))))
;; -----------------------------------------------------------------------------
;; Snake -> Bomb
;; produce a random bomb that starts at the edge of the board
;(define (random-bomb s)
; (make-bomb "U" (make-cell 3 BOARD-SIZE)))
(define (random-bomb s)
(local [(define random-number (random 4))]
(list-ref (list (make-bomb "L" (make-cell (- BOARD-SIZE 1) (random (- BOARD-SIZE 1))))
(make-bomb "R" (make-cell 0 (random (- BOARD-SIZE 1))))
(make-bomb "U" (make-cell (random (- BOARD-SIZE 1)) (- BOARD-SIZE 1)))
(make-bomb "D" (make-cell (random (- BOARD-SIZE 1)) 0)))
random-number)))
;; -----------------------------------------------------------------------------
;; Bomb -> Bomb
;; produce the next bomb
(define (tock-bomb bmb)
(make-bomb (bomb-dir bmb)
(next-cell-bomb (bomb-dir bmb) (bomb-cell bmb))))
;; -----------------------------------------------------------------------------
;; Direction Cell -> Cell
;; produce the next bomb cell
(define (next-cell-bomb d cl)
(cond [(string=? "U" d) ;(if (= -1 (cell-r cl))
;(bomb-cell (random-bomb cl))
(make-cell (cell-c cl) (- (cell-r cl) 1))]
[(string=? "D" d) ;(if (= BOARD-SIZE (cell-r cl))
;(bomb-cell (random-bomb cl))
(make-cell (cell-c cl) (+ (cell-r cl) 1))]
[(string=? "L" d) ;(if (= -1 (cell-c cl))
;(bomb-cell (random-bomb cl))
(make-cell (- (cell-c cl) 1) (cell-r cl))]
[(string=? "R" d) ;(if (= BOARD-SIZE (cell-c cl))
;(bomb-cell (random-bomb cl))
(make-cell (+ (cell-c cl) 1) (cell-r cl))]))
;; -----------------------------------------------------------------------------
;; Game -> Image
;; render the head of the snake onto the a cell position on the board
(define (render g)
(above/align "right"
(render-score (game-score g))
(rectangle 10 10 "solid" "white")
(render-snake (game-snake g) (render-bomb (game-bomb g) (render-food (game-food g))))))
;; -----------------------------------------------------------------------------
;; Bomb Snake -> Boolean
;; produce true if the bomb hits any cell in the body of the snake
(define (same-cell-body? bmb s)
(same-cell-body-helper? (bomb-cell bmb) (snake-body s)))
;; -----------------------------------------------------------------------------
;; Cell Body -> Boolean
;; produce true if given cell matches any of the body cells
(define (same-cell-body-helper? cl b)
(cond [(empty? b) false]
[else
(or (equal? cl (first b))
(same-cell-body-helper? cl (rest b)))]))
;; -----------------------------------------------------------------------------
;; Bomb Snake -> Snake
;; produce the new snake without all body cells after the one hit by bomb
(define (injure-snake bmb s)
(make-snake (snake-dir s) (snake-head s) (injure-snake-body-bmb bmb (snake-body s))))
;; -----------------------------------------------------------------------------
;; Bomb Body -> Body
;; produce the new body without all body cells after the one hit by bomb
(define (injure-snake-body-bmb bmb b)
;; rsf: (listof Cell) ; list of cells that were before the one hit by bomb
(local [(define (injure-snake-body-bmb-rsf bmb b rsf)
(cond [(empty? b) rsf]
[else
(if (equal? (bomb-cell bmb) (first b))
rsf
(injure-snake-body-bmb-rsf bmb (rest b)
(append rsf (list (first b)))))]))]
(injure-snake-body-bmb-rsf bmb b empty)))
;; -----------------------------------------------------------------------------
;; Snake -> Image
;; Given a snake, render it onto the board at x,y
(define (render-snake s img)
(place-in-cell (rotate-head (snake-dir s) HEAD)
(snake-head s)
(render-body (snake-body s) img)))
;; -----------------------------------------------------------------------------
;; Direction Image -> Image
;; rotate the image given based on the direction
(define (rotate-head d img)
(cond [(string=? "U" d) img]
[(string=? "D" d) (rotate 180 img)]
[(string=? "L" d) (rotate 90 img)]
[(string=? "R" d) (rotate 270 img)]))
;; -----------------------------------------------------------------------------
;; Body -> Image
;; render the body onto the board at x,y
(define (render-body b img)
(cond [(empty? b) empty-image]
[(empty? (rest b)) (place-in-cell BODY (first b) img)]
[else
(place-in-cell BODY (first b)
(render-body (rest b) img))]))
;; -----------------------------------------------------------------------------
;; Food -> Image
;; render image of food onto image of snake on board
(define (render-food f)
(place-in-cell FOOD f MTS))
;; -----------------------------------------------------------------------------
;; Natural - Image
;; render the score onto a scoreboard
(define (render-score n)
(overlay/align "right" "bottom"
(text (number->string n) 30 "black")
SCOREBOARD))
;; -----------------------------------------------------------------------------
;; Bomb Image -> Image
;; render image of bomb onto given image
(define (render-bomb bmb img)
(place-in-cell BOMB (bomb-cell bmb) img))
;; -----------------------------------------------------------------------------
;; Image Cell Image -> Image
;; place img in cell c given the board scene scn
(define (place-in-cell img c scn)
(place-image img
(+ (* (cell-c c) CELL-PIXELS) (/ CELL-PIXELS 2))
(+ (* (cell-r c) CELL-PIXELS) (/ CELL-PIXELS 2))
scn))
;; -----------------------------------------------------------------------------
;; Game KeyEvent -> Game
;; given a game and a keyevent, produce the new game
(define (handle-key g ke)
(cond [(key=? ke "up") (make-game (game-food g) (turn (game-snake g) "U") (game-score g) (game-bomb g))]
[(key=? ke "down") (make-game (game-food g) (turn (game-snake g) "D") (game-score g) (game-bomb g))]
[(key=? ke "left") (make-game (game-food g) (turn (game-snake g) "L") (game-score g) (game-bomb g))]
[(key=? ke "right") (make-game (game-food g) (turn (game-snake g) "R") (game-score g) (game-bomb g))]
[else g]))
;; -----------------------------------------------------------------------------
;; Snake Direction -> Snake
;; given a snake and a keyevent, produce the new snake
(define (turn s d)
(make-snake d (snake-head s) (snake-body s)))
;; -----------------------------------------------------------------------------
;; Game -> Boolean
;; stop game when head of the snake touches one of the walls, and display message
(define (last-world? g)
(last-snake? (game-snake g) (game-bomb g)))
;; -----------------------------------------------------------------------------
;; Snake Bomb -> Boolean
;; produce true if head of snake is touching the wall, or if the bomb
;; hit the snake's head, false otherwise
(define (last-snake? s bmb)
(or (last-cell? (snake-head s))
(equal? (snake-head s) (bomb-cell bmb))
(empty? (snake-body s))
(hit-itself? (snake-head s) (snake-body s))))
;; -----------------------------------------------------------------------------
;; Cell Body -> Boolean
;; produce true if the snake ran into itself
(define (hit-itself? cl b)
(same-cell-body-helper? cl b))
;; -----------------------------------------------------------------------------
;; Cell -> Boolean
;; produce true if the cell is on the wall of the box, false otherwise
(define (last-cell? cl)
(or (= -1 (cell-c cl))
(= BOARD-SIZE (cell-c cl))
(= -1 (cell-r cl))
(= BOARD-SIZE (cell-r cl))))
;; -----------------------------------------------------------------------------
;; Game -> Image
;; produce the message if the head is touching a wall of the box
(define (last-picture g)
(if (last-snake? (game-snake g) (game-bomb g))
(overlay MESSAGE
(render g))
(render g)))
;; -----------------------------------------------------------------------------