-
Notifications
You must be signed in to change notification settings - Fork 0
/
gameplay.ml
217 lines (195 loc) · 7.47 KB
/
gameplay.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
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
open Types
open Gamelogic
let () = Random.self_init ()
(* [is_empty_square s] checks if [s] is an empty square. *)
let is_empty_square (s : square) =
square_value s = 0
(* [init_board ()] initializes a 4 by 4 matrix.
* Starts with square of 2 in bottom left corner. *)
let init_board () =
let b = Array.make_matrix 4 4 None in
let s = ref 0 in
let e = ref false in
b.(3).(3) <- Some 2;
{
evil = e;
s = s;
b = b;
}
(* [check_2048_square s] returns if 2048 square has been formed. *)
let check_2048_square (s : square) =
square_value s = 2048
(* [is_empty_col b col size] returns whether or not column [col]
* is empty in board [b] of length/width [size] *)
let rec is_empty_col b col size =
if size = 0 then true else
if b.(size-1).(col) = None then
is_empty_col b col (size-1) else false
(* [is_valid_move_left b row col] returns a bool true if
* a left move is valid. *)
(* Helper function for [is_valid_move] *)
let rec is_valid_move_left b row col =
if row = 0 then false else
if is_empty_row b (row-1) col then is_valid_move_left b (row-1) col else
if b.(row-1).(0) = None then true else
if b.(row-1).(0) <> None && b.(row-1).(1) = None
&& (b.(row-1).(2) <> None || b.(row-1).(3) <> None) then true
else if b.(row-1).(0) <> None && b.(row-1).(1) <> None && b.(row-1).(2) = None
&& b.(row-1).(3) <> None then true
else if is_valid_merge_horizontal b (row-1) 0 1
|| is_valid_merge_horizontal b (row-1) 1 2
|| is_valid_merge_horizontal b (row-1) 2 3
then true else is_valid_move_left b (row-1) col
(* [is_valid_move_right b row col] returns a bool true if
* a right move is valid. *)
(* Helper function for [is_valid_move] *)
let rec is_valid_move_right b row col =
if row = 0 then false else
if is_empty_row b (row-1) col then is_valid_move_right b (row-1) col else
if b.(row-1).(3) = None then true else
if b.(row-1).(3) <> None && b.(row-1).(2) = None && (b.(row-1).(1) <> None
|| b.(row-1).(0) <> None) then true else
if b.(row-1).(3) <> None && b.(row-1).(2) <> None && b.(row-1).(1) = None
&& b.(row-1).(0) <> None then true else
if is_valid_merge_horizontal b (row-1) 0 1
|| is_valid_merge_horizontal b (row-1) 1 2
|| is_valid_merge_horizontal b (row-1) 2 3
then true else is_valid_move_right b (row-1) col
(* [is_valid_move_up b row col] returns a bool true if
* a up move is valid. *)
(* Helper function for [is_valid_move] *)
let rec is_valid_move_up b row col =
if col = 0 then false else
if is_empty_col b (col-1) row then is_valid_move_up b row (col-1) else
if b.(0).(col-1) = None then true else
if b.(0).(col-1) <> None && b.(1).(col-1) = None && (b.(2).(col-1) <> None
|| b.(3).(col-1) <> None) then true
else if b.(0).(col-1) <> None && b.(1).(col-1) <> None
&& b.(2).(col-1) = None && b.(3).(col-1) <> None then true
else if is_valid_merge_vertical b (col-1) 0 1
|| is_valid_merge_vertical b (col-1) 1 2
|| is_valid_merge_vertical b (col-1) 2 3
then true else is_valid_move_up b row (col-1)
(* [is_valid_move_down b row col] returns a bool true if
* a down move is valid. *)
(* Helper function for [is_valid_move] *)
let rec is_valid_move_down b row col =
if col = 0 then false else
if is_empty_col b (col-1) row then is_valid_move_down b row (col-1) else
if b.(3).(col-1) = None then true else
if b.(3).(col-1) <> None && b.(2).(col-1) = None && (b.(1).(col-1) <> None
|| b.(0).(col-1) <> None) then true
else if b.(3).(col-1) <> None && b.(2).(col-1) <> None && b.(1).(col-1) = None
&& b.(0).(col-1) <> None then true
else if is_valid_merge_vertical b (col-1) 0 1
|| is_valid_merge_vertical b (col-1) 1 2
|| is_valid_merge_vertical b (col-1) 2 3
then true else is_valid_move_down b row (col-1)
(* [is_valid_move m b] is [true] if shifting [board] in the direction
* [move] results in a change in the game board. *)
let is_valid_move m b =
match m with
| Left -> is_valid_move_left b (Array.length b) (Array.length b)
| Right -> is_valid_move_right b (Array.length b) (Array.length b)
| Up -> is_valid_move_up b (Array.length b) (Array.length b)
| Down -> is_valid_move_down b (Array.length b) (Array.length b)
| (Regular|Evil|Null) -> true
(* [check_winning_board b] is [true] if the 2048 tile has been created *)
let check_winning_board (b : board) =
let win = ref false in
for i = 0 to (Array.length b) - 1 do
if Array.exists check_2048_square b.(i) then win := true
done;
!win
(* [random_nth_list l] returns a random member of list [l] *)
let random_nth_list l =
let len = List.length l in
List.nth l (Random.int len)
let (>>=) l f = List.concat (List.map f l)
let list_index = [0;1;2;3]
(* [not_avail_squares b] Returns a list of non-empty squares in [b] *)
let not_avail_squares b =
let all_indicies =
list_index >>= fun i ->
list_index >>= fun j ->
[(i, j)]
in
List.filter (fun (i, j) -> b.(i).(j) <> None) all_indicies
(* [empty_squares b] Returns a list of empty squares in [b] *)
let empty_squares b =
let all_indicies =
list_index >>= fun i ->
list_index >>= fun j ->
[(i, j)]
in
List.filter (fun (i, j) -> b.(i).(j) = None) all_indicies
(* [random_avail b] Returns a tuple (i,j) of a random open position
* in [b] in row i, column j.
* Precondition: [b] has at least one open position. *)
let random_avail b =
let avail = empty_squares b in
random_nth_list avail
(* Inspiration taken from: http://langref.org/ocaml/
* numbers/mathematical/distance-between-points
* [distance a b] finds distance between points a b *)
let distance a b =
sqrt((float(fst a) -. float(fst b))**2.
+. (float(snd a) -. float(snd b))**2.)
(* [find_max_sq b] Given list of current squares, find max
* returns (i,j) of the largest square *)
let find_max_sq (b:board) =
let lst = not_avail_squares b in
let max_ref = ref 0 in
let max_pos = ref (0,0) in
for i = 0 to (List.length lst - 1) do
let pos = List.nth lst i in
let sq_val = Gamelogic.square_value b.(fst pos).(snd pos) in
if sq_val > !max_ref then begin
max_ref := sq_val;
max_pos := pos;
end
else ()
done;
!max_pos
(* [insert_evil_square b] places new square in the worst possible
* position. Determined by max square and relative distance to that
* square *)
let insert_evil_square (b:board) =
let max_pos = find_max_sq b in
let empty_sq = empty_squares b in
let min_ref = ref 10. in
let min_pos = ref (0,0) in
for i = 0 to (List.length empty_sq - 1) do
let pos = List.nth empty_sq i in
let dist = distance max_pos pos in
if dist < !min_ref then begin
min_ref := dist;
min_pos := pos;
end
else ()
done;
!min_pos
(* [random_sq_value] returns value for new square, with 90% probability
* of 2, 10% for 4 *)
let random_sq_value () =
let prob = Random.int 10 in
if prob = 0 then (Some 4) else (Some 2)
(* [insert_square b evil] inserts pre-determined square [sq] into board [b];
* checks if in evil mode *)
let insert_square (b : board) evil : unit =
(* let (i, j) = random_avail b in *)
let (i, j) =
if !evil then insert_evil_square b
else random_avail b in
let sq = random_sq_value () in
b.(i).(j) <- sq
(* [check_end_game b] checks end game condition; no more valid moves *)
let check_end_game (b : board) =
not (is_valid_move Left b || is_valid_move Right b ||
is_valid_move Up b || is_valid_move Down b)
(* [key_press m b s evil] checks for wining board; inserts new square
* otherwise *)
let key_press m b s evil =
if is_valid_move m b then (move m b s;
if check_winning_board b then () else insert_square b evil)
else ()