-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstate.ml
332 lines (295 loc) · 9.4 KB
/
state.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
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
open Command
open Deck
open Person
type t = {
mutable people : Person.t array;
mutable curr_deck : Deck.t;
mutable card_pile : Deck.card;
mutable dos_pile : Deck.card;
mutable pos : int;
mutable game_ended : bool;
mutable curr_round : int;
mutable total_rounds : int;
}
(** Take in arrays of names *)
let rec draw d =
match !d with
| [] -> raise NoMoreCards
| h :: t ->
d := t;
if h.number = None then draw d else h
let init_state p_num p_name_array ai_num ai_name_array tot_rounds dos =
let dummy_person =
{ Person.hand = []; name = ""; position = 0; score = 0; ai = false }
in
let d = if dos then Deck.init_dos () else Deck.init () in
let i_state =
if dos then
{
people = Array.make (p_num + ai_num) dummy_person;
curr_deck = d;
card_pile = draw d;
dos_pile = draw d;
pos = 0;
game_ended = false;
curr_round = 1;
total_rounds = tot_rounds;
}
else
{
people = Array.make (p_num + ai_num) dummy_person;
curr_deck = d;
card_pile = draw d;
dos_pile = { number = None; color = None; ctype = Normal };
pos = 0;
game_ended = false;
curr_round = 1;
total_rounds = tot_rounds;
}
in
for i = 0 to p_num - 1 do
i_state.people.(i) <-
Person.init i_state.curr_deck p_name_array.(i) (i + 1) false;
Person.sort_hand i_state.people.(i);
i_state.curr_deck <- d
done;
for j = p_num to p_num + ai_num - 1 do
i_state.people.(j) <-
Person.init i_state.curr_deck
ai_name_array.(j - p_num)
(j + 1) true;
i_state.curr_deck <- d
done;
i_state
let reinitialize_state st next_round winner_pos dos =
let d = Deck.init () in
st.curr_round <- next_round;
st.people.(winner_pos).score <- st.people.(winner_pos).score + 1;
st.curr_deck <- d;
st.game_ended <- false;
for i = 0 to Array.length st.people - 1 do
let player = st.people.(i) in
player.hand <- [];
for i = 1 to 7 do
Person.draw player d;
st.curr_deck <-
( match !d with
| [] -> raise Deck.NoMoreCards
| h :: t ->
d := t;
d )
done
done;
st.card_pile <- draw d;
st.dos_pile <-
( if dos then draw d
else { number = None; color = None; ctype = Normal } );
st
let rec draw_st st pos d n =
if n > 0 then (
match !d with
| [] -> raise NoMoreCards
| h :: t ->
let old = st.people.(pos).hand in
st.people.(pos).hand <- old @ [ h ];
st.curr_deck <- ref t;
Person.sort_hand st.people.(pos);
draw_st st pos st.curr_deck (n - 1) )
else st
let red_d4 = { number = None; color = Some Red; ctype = DrawFour }
let green_d4 = { number = None; color = Some Green; ctype = DrawFour }
let yellow_d4 = { number = None; color = Some Yellow; ctype = DrawFour }
let blue_d4 = { number = None; color = Some Blue; ctype = DrawFour }
let red_wild = { number = None; color = Some Red; ctype = Wild }
let green_wild = { number = None; color = Some Green; ctype = Wild }
let yellow_wild = { number = None; color = Some Yellow; ctype = Wild }
let blue_wild = { number = None; color = Some Blue; ctype = Wild }
let size = ANSITerminal.size ()
let width = fst size
let height = snd size
let center_cursor str =
let len = String.length str in
let center = width / 2 in
ANSITerminal.set_cursor (center - (len / 2)) height
let print_endline_centered str =
center_cursor str;
print_endline str
let prompt_color () =
print_endline_centered
"Choose the next color of play (red, yellow, blue, or green)."
let rec enter_color st wild =
prompt_color ();
ANSITerminal.set_cursor ((width / 2) - 8) height;
print_string "> ";
match parse_colors (read_line ()) with
| Red ->
if wild then st.card_pile <- red_wild else st.card_pile <- red_d4;
st
| Blue ->
if wild then st.card_pile <- blue_wild
else st.card_pile <- blue_d4;
st
| Yellow ->
if wild then st.card_pile <- yellow_wild
else st.card_pile <- yellow_d4;
st
| Green ->
if wild then st.card_pile <- green_wild
else st.card_pile <- green_d4;
st
| exception Empty ->
print_endline_centered "Please enter a color!";
enter_color st wild
| exception Malformed ->
print_endline_centered "Please enter a color!";
enter_color st wild
let ai_color color st wild =
match parse_colors color with
| Red ->
if wild then st.card_pile <- red_wild else st.card_pile <- red_d4;
st
| Blue ->
if wild then st.card_pile <- blue_wild
else st.card_pile <- blue_d4;
st
| Yellow ->
if wild then st.card_pile <- yellow_wild
else st.card_pile <- yellow_d4;
st
| Green ->
if wild then st.card_pile <- green_wild
else st.card_pile <- green_d4;
st
let rand_choose_color () =
Random.self_init ();
let num = Random.int 4 in
match num with
| 0 -> "green"
| 1 -> "red"
| 2 -> "blue"
| 3 -> "yellow"
| _ -> "green"
(** [remove_ele n res hand] removes the nth element from hand.*)
let rec remove_ele n res = function
| [] -> res
| h :: t ->
if n <> 0 then remove_ele (n - 1) (h :: res) t
else remove_ele (n - 1) res t
(** [place_st st pos card_index] places the card at [card_index] from
the player at position [pos] and updates the state [st].*)
let place_st st pos card_index =
match st.people.(pos).hand with
| [] -> raise NoMoreCards
| h :: t -> (
let card = List.nth st.people.(pos).hand card_index in
let new_hand =
List.rev (remove_ele card_index [] st.people.(pos).hand)
in
let num_players = Array.length st.people in
let next_pos = (pos + 1) mod num_players in
if new_hand = [] then st.game_ended <- true;
match card.ctype with
| Normal ->
st.people.(pos).hand <- new_hand;
st.pos <- next_pos;
st.card_pile <- card;
st
| Skip ->
st.people.(pos).hand <- new_hand;
st.pos <- (pos + 2) mod num_players;
st.card_pile <- card;
st
| DrawTwo ->
let new_st = draw_st st next_pos st.curr_deck 2 in
new_st.people.(pos).hand <- new_hand;
new_st.pos <- next_pos;
new_st.card_pile <- card;
new_st
| DrawFour ->
let new_st = draw_st st next_pos st.curr_deck 4 in
new_st.people.(pos).hand <- new_hand;
new_st.pos <- next_pos;
if new_st.people.(pos).ai = true then
ai_color (rand_choose_color ()) new_st false
else enter_color new_st false
| Reverse ->
if pos - 1 < 0 then st.pos <- num_players - 1
else st.pos <- pos - 1;
st.people.(pos).hand <- new_hand;
st.card_pile <- card;
st
| Wild ->
st.people.(pos).hand <- new_hand;
st.pos <- next_pos;
if st.people.(pos).ai = true then
ai_color (rand_choose_color ()) st true
else enter_color st true
| WildDos ->
st.people.(pos).hand <- new_hand;
st.pos <- next_pos;
st.card_pile <- card;
st
| WildNum ->
st.people.(pos).hand <- new_hand;
st.pos <- next_pos;
st.card_pile <- card;
st )
let get_people s = s.people
let get_person s i = s.people.(i)
let get_pos s = s.pos
let get_curr_deck s = s.curr_deck
let get_card_pile s = s.card_pile
let get_dos_pile s = s.dos_pile
let get_game_ended s = s.game_ended
let get_curr_round s = s.curr_round
let set_curr_round s i = s.curr_round <- i
let get_total_rounds s = s.total_rounds
let set_total_rounds s i = s.total_rounds <- i
(** [place_st_dos_single st pos card_index] places the card at
[card_index] from the player at position [pos] and updates the state
[st].*)
let place_st_dos_single st pos card_index mch_pile =
match st.people.(pos).hand with
| [] -> raise NoMoreCards
| h :: t ->
let deck = get_curr_deck st in
let new_card = draw deck in
let new_hand =
List.rev (remove_ele card_index [] st.people.(pos).hand)
in
let num_players = Array.length st.people in
let next_pos = (pos + 1) mod num_players in
if new_hand = [] then st.game_ended <- true;
st.people.(pos).hand <- new_hand;
st.pos <- next_pos;
if mch_pile = 1 then st.card_pile <- new_card
else st.dos_pile <- new_card;
st
(** [place_st_dos_double st pos card_index] places the card at
[card_index] from the player at position [pos] and updates the state
[st].*)
let place_st_dos_double st pos grp_idx grps mch_pile =
match grps with
| [] -> raise NoMoreCards
| h :: t ->
let deck = get_curr_deck st in
let new_card = draw deck in
let num_players = Array.length st.people in
let next_pos = (pos + 1) mod num_players in
let grp = List.nth grps grp_idx in
let fst_idx = snd (List.hd grp) in
let snd_idx = snd (List.hd (List.rev grp)) in
(* Remove the first card from the player hand *)
let removed_first =
List.rev (remove_ele fst_idx [] st.people.(pos).hand)
in
(* Remove the second card from the player hand *)
let new_hand =
List.rev (remove_ele (snd_idx - 1) [] removed_first)
in
if new_hand = [] then st.game_ended <- true;
st.people.(pos).hand <- new_hand;
st.pos <- next_pos;
if mch_pile = 1 then st.card_pile <- new_card
else st.dos_pile <- new_card;
st