-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgameplay.ml
More file actions
361 lines (320 loc) · 10.7 KB
/
Copy pathgameplay.ml
File metadata and controls
361 lines (320 loc) · 10.7 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
include Reader
include PInput
include Ai
type player = {
mutable wallet: float;
mutable bank: float;
mutable wager: float;
mutable num_right: int
}
type ai = {
difficulty:int;
}
type gameboard = {
size:int;
mutable player_pos:int;
mutable chaser_pos:int;
}
type actor = P of player | A of ai
type metadata = {
mutable player: player;
mutable ai: ai;
mutable difficulty: int;
mutable curr_cat: int;
mutable used_cat: int list;
mutable gameboard: gameboard;
mutable curr_question: Reader.question
}
let player = {
wallet = 0.;
bank = 0.;
wager = 0.;
num_right = 0
}
let ai = {
difficulty = 0;
}
let gameboard = {
size = 14;
player_pos = 0;
chaser_pos = 0;
}
let metadata = {
player=player;
ai = ai;
difficulty = 0;
curr_cat = 0;
used_cat = [];
gameboard = gameboard;
curr_question = {question="";answer="";point=0}
}
let send_question () =
let q = Reader.rand_question metadata.difficulty metadata.curr_cat in
metadata.curr_question <- q;
q.question
let categories = [(0, "Science");(1, "History");(2, "Music");(3, "Geography")]
let display_question (q:Reader.question) : unit =
Printf.printf "Question: %s\n" q.question
let respond_to_answer p =
match p with
| (correct, timeout) ->
if timeout then
"You timed out!\n"
else (if correct then
"That is correct!\n"
else "That is incorrect!\n")
let update_wallet n =
let new_val = n +. metadata.player.wallet in
metadata.player.wallet <- new_val
let receive_answer a =
(* Input will always be of form 'Answer:xxxxxx', so take substring after colon *)
let ans = String.sub a 7 ((String.length a) - 7) in
let (correct, timeout) = PInput.is_correct ans metadata.curr_question in
(if correct && not timeout then begin
metadata.player.num_right <- metadata.player.num_right + 1;
update_wallet (float_of_int metadata.curr_question.point)
end
else ());
(respond_to_answer(correct,timeout),string_of_float(metadata.player.wallet))
(* let serve_question () =
let q = Reader.rand_question metadata.difficulty metadata.curr_cat in
display_question q;
let ans = PInput.get_input () in
let (correct, timeout) = PInput.is_correct ans q in
respond_to_answer (correct, timeout);
(if correct && not timeout then begin
metadata.player.num_right <- metadata.player.num_right + 1;
update_wallet (float_of_int q.point)
end
else ());
Printf.printf "Your Wallet: %f\n" metadata.player.wallet *)
let send_wallet () =
"$" ^ (string_of_float metadata.player.wallet)
let send_total () =
(2. *. metadata.player.wallet) +. metadata.player.bank
let rec receive_diff diff =
match (String.lowercase diff) with
| "easy" -> metadata.difficulty <- 0; true
| "medium" -> metadata.difficulty <- 1; true
| "hard" -> metadata.difficulty <- 2; true
| _ -> false
(* let rec get_available_cat (left: (int * bytes) list) =
match left with
| [] -> ""
| h::t -> (match h with
| (id, name) -> if not (List.mem id metadata.used_cat) then
name ^ "\n" ^ (get_available_cat t)
else (get_available_cat t)) *)
let rec receive_cat cat =
match (String.lowercase cat) with
| "science" ->
if List.mem 0 metadata.used_cat then begin
false
end
else begin
metadata.used_cat <- (metadata.used_cat @ [0]);
metadata.curr_cat <- 0;
true
end
| "history" ->
if List.mem 1 metadata.used_cat then begin
false
end
else begin
metadata.used_cat <- (metadata.used_cat @ [1]);
metadata.curr_cat <- 1;
true
end
| "music" ->
if List.mem 2 metadata.used_cat then begin
false
end
else begin
metadata.used_cat <- (metadata.used_cat @ [2]);
metadata.curr_cat <- 2;
true
end
| "geography" ->
if List.mem 3 metadata.used_cat then begin
false
end
else begin
metadata.used_cat <- (metadata.used_cat @ [3]);
metadata.curr_cat <- 3;
true
end
| _ -> false
(* let phase_one () =
(* get_difficulty ();
get_category (); *)
let start_time = Unix.gettimeofday () in
let rec body () =
let curr_time = Unix.gettimeofday () in
if (curr_time -. start_time) <= 10. then begin
serve_question ();
body ()
end
else
Printf.printf "Time's up!\n" in
body () *)
let update_bank n =
let new_val = metadata.player.bank +. n in
metadata.player.bank <- new_val
let update_gameboard player_right ai_right =
match player_right, ai_right with
| true, true -> metadata.gameboard.player_pos <- metadata.gameboard.player_pos + 1;
metadata.gameboard.chaser_pos <- metadata.gameboard.chaser_pos + 1
| true, false -> metadata.gameboard.player_pos <- metadata.gameboard.player_pos + 1
| false, true -> metadata.gameboard.chaser_pos <- metadata.gameboard.chaser_pos + 1
| false, false -> ()
let caught () =
metadata.gameboard.player_pos = metadata.gameboard.chaser_pos
let at_bank () =
(* subtract 1 from gameboard size, positions are 0 indexed *)
metadata.gameboard.player_pos = (metadata.gameboard.size - 1)
(* let rec init_gameboard () =
Printf.printf "Please select an option to begin Round 2\n";
Printf.printf "(A) Start 4 spots away from the bank and wager 1/2 of the money in your wallet\n";
Printf.printf "(B) Start 5 spots away from the bank and wager 2/3 of the money in your wallet\n";
Printf.printf "(C) Start 6 spots away from the bank and wager all of the money in your wallet\n";
let ans = PInput.get_input () in
let curr_wallet = metadata.player.wallet in
match ans with
| "a" ->
metadata.gameboard.player_pos <- 10;
metadata.gameboard.chaser_pos <- 2;
metadata.player.wager <- (curr_wallet *. 0.5);
| "b" ->
metadata.gameboard.player_pos <- 9;
metadata.gameboard.chaser_pos <- 1;
metadata.player.wager <- (curr_wallet *. (2./.3.));
| "c" ->
metadata.gameboard.player_pos <- 8;
metadata.gameboard.chaser_pos <- 0;
metadata.player.wager <- (curr_wallet);
| _ -> Printf.printf "That is not a valid selection."; *) (* init_gameboard () *)
let receive_board ans =
let curr_wallet = metadata.player.wallet in
match (String.lowercase ans) with
| "a" ->
metadata.gameboard.player_pos <- 10;
metadata.gameboard.chaser_pos <- 2;
metadata.player.wager <- (curr_wallet *. 0.5);
true
| "b" ->
metadata.gameboard.player_pos <- 9;
metadata.gameboard.chaser_pos <- 1;
metadata.player.wager <- (curr_wallet *. (2./.3.));
true
| "c" ->
metadata.gameboard.player_pos <- 8;
metadata.gameboard.chaser_pos <- 0;
metadata.player.wager <- (curr_wallet);
true
| _ -> false
(* let rec head_to_head_question () =
(* Get question, display, and update metadata *)
let q = Reader.rand_question metadata.difficulty metadata.curr_cat in
display_question q;
(* Get the player's and ai's answers *)
let ans = PInput.timed_question 10. in
let ai_correct = Ai.ai_is_correct metadata.difficulty in
let ai_ans = Ai.ai_answer ai_correct q in
Printf.printf "The AI answered %s\n" ai_ans;
Printf.printf "The correct answer is %s\n" q.answer;
let (p_correct,p_timeout) = PInput.is_correct ans q in
(* respond_to_answer (p_correct,p_timeout); *)
update_gameboard p_correct ai_correct;
finished ()
and finished () =
(* If the player is at the bank *)
if metadata.gameboard.player_pos = metadata.gameboard.size then begin
Printf.printf "YOU WIN!\n";
Printf.printf "You sucessfully banked $%f!\n" metadata.player.wager;
update_bank metadata.player.wager
end
(* If the Chaser caught the player *)
else if metadata.gameboard.player_pos = metadata.gameboard.chaser_pos then begin
Printf.printf "Sorry, the chaser caught you\n";
metadata.player.wallet <- (metadata.player.wallet -. metadata.player.wager);
metadata.player.wager <- 0.
end
else
head_to_head_question () *)
let receive_positions () =
"Player at position " ^ (string_of_int metadata.gameboard.player_pos) ^ "\n" ^
"Chaser at position " ^ (string_of_int metadata.gameboard.chaser_pos) ^ "\n" ^
"Bank is at " ^ (string_of_int (metadata.gameboard.size - 1)) ^ "\n"
let phase_two_end win =
if win then begin
(* add player's wager to their bank if they win *)
update_bank metadata.player.wager;
let response = "You successfully banked $" ^ string_of_float(metadata.player.wager) in
response
end
else begin
(* remove player's wager from bank in they lose *)
metadata.player.wallet <- (metadata.player.wallet -. metadata.player.wager);
metadata.player.wager <- 0.;
(* if they ran out of money, the game is over *)
if metadata.player.wallet = 0. then
"You're out of money! Game over."
else
"You didn't win, but you're not out of money. On to phase three."
end
let receive_head_to_head s =
let response, new_bank = receive_answer s in
let ai_correct = Ai.ai_is_correct metadata.difficulty in
let ai_ans = Ai.ai_answer ai_correct metadata.curr_question in
let player_correct = response = "That is correct!\n" in
update_gameboard player_correct ai_correct;
(response, new_bank, ai_correct, ai_ans)
(* let phase_two () =
(* init_gameboard (); *)
(* get_category (); *)
Printf.printf "These are timed questions. You have 10 seconds to answer.\n";
head_to_head_question ();
if metadata.player.wallet = 0. then
Printf.printf "You're out of money! Game over\n"
else
Printf.printf "On to Phase Three!\n" *)
(* Generate a bool list *)
let ai_answer_questions () =
(* the AI 'sees' a random number of quetsions *)
let num_seen = 90 / ((Random.int 6) + 5) in
let rec create_list l i =
if i = 0 then []
else (Ai.ai_is_correct metadata.difficulty)::(create_list l (i-1)) in
create_list [] num_seen
let send_ai_list () =
ai_answer_questions ()
(* let rec show_answers count = function
| [] -> ()
| true::tl -> Printf.printf "The Chaser answered a question correctly.\n";
count := !count + 1;
show_answers count tl
| false::tl -> Printf.printf "The Chaser answered incorrectly. Your turn to try and answer.\n";
let q = Reader.rand_question metadata.difficulty metadata.curr_cat in
display_question q;
let ans = PInput.get_input () in
let (correct, timeout) = PInput.is_correct ans q in
(* respond_to_answer (correct, timeout); *)
(if correct then metadata.player.num_right <- metadata.player.num_right + 1
else ());
show_answers count tl *)
(* let phase_three () =
metadata.player.num_right <- 0;
phase_one ();
Printf.printf "%d\n" metadata.player.num_right;
let counter = ref 0 in
show_answers counter (ai_answer_questions ());
if metadata.player.num_right > !counter then
Printf.printf "YOU WIN MOTHERFUCKER\n"
else
Printf.printf "YOU LOSE MOTHERFUCKER\n" *)
(* let game_loop () =
Printf.printf "Welcome to The Chase!\n";
Printf.printf "For this phase, answer as many questions as you can in 90 seconds.\n";
phase_one ();
phase_two ();
phase_three () *)