-
Notifications
You must be signed in to change notification settings - Fork 163
/
Copy pathgame.ml
77 lines (66 loc) · 2.15 KB
/
game.ml
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
open! Base
type t =
{ mutable snake : Snake.t
; mutable apple : Apple.t
; mutable game_state : Game_state.t
; height : int
; width : int
; amount_to_grow : int
}
[@@deriving sexp_of]
(* TODO: Implement [in_bounds]. *)
let in_bounds t position = failwith "For you to implement"
(* TODO: Implement [create].
Make sure that the game returned by [create] is in a valid state. In particular, we
should fail with the message "unable to create initial apple" if [Apple.create] is
unsuccessful, and "unable to create initial snake" if the initial snake is invalid
(i.e. goes off the board). *)
let create ~height ~width ~initial_snake_length ~amount_to_grow =
failwith "For you to implement"
;;
let snake t = t.snake
let apple t = t.apple
let game_state t = t.game_state
(* TODO: Implement [set_direction]. *)
let set_direction t direction = ()
(* TODO: Implement [step].
[step] should:
- move the snake forward one square
- check for collisions (end the game with "Wall collision" or "Self collision")
- if necessary:
-- consume apple
-- if apple cannot be regenerated, win game; otherwise, grow the snake *)
let step t = ()
module For_testing = struct
let create_apple_force_location_exn ~height ~width ~location =
let invalid_locations =
List.init height ~f:(fun row ->
List.init width ~f:(fun col -> { Position.row; col }))
|> List.concat
|> List.filter ~f:(fun pos -> not ([%compare.equal: Position.t] location pos))
in
match Apple.create ~height ~width ~invalid_locations with
| None -> failwith "[Apple.create] returned [None] when [Some _] was expected!"
| Some apple -> apple
;;
let create_apple_and_update_game_exn t ~apple_location =
let apple =
create_apple_force_location_exn
~height:t.height
~width:t.width
~location:apple_location
in
t.apple <- apple
;;
let create_game_with_apple_exn
~height
~width
~initial_snake_length
~amount_to_grow
~apple_location
=
let t = create ~height ~width ~initial_snake_length ~amount_to_grow in
create_apple_and_update_game_exn t ~apple_location;
t
;;
end