-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ml
155 lines (147 loc) · 4.67 KB
/
main.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
open Unix
open ANSITerminal
open Command
open Card
open Partialdeck
open Round
open Print
let rec display_history state =
match Print.internal_display_history state with
| exception _ -> display_history state
| v -> v
let get_card i state =
PartialDeck.find i (Round.hand state)
let get_difficulty = function
| "easy" -> Round.Easy
| "medium" -> Round.Medium
| "hard" -> Round.Hard
| _ -> Round.Invalid
let rec home_loop bl state =
let state' = if bl then (Print.score_table state; display_history state)
else state in
if fst (Round.game_over state) then
begin if snd (Round.game_over state) then Print.print_player_win ()
else Print.print_bot_win () end else
let (w,h) = size () in
match Print.read_line_safe () with
| Quit -> Print.erase_print "Quit";
exit 0
| Pass (i1,i2,i3) ->
begin
match (get_card i1 state'),
(get_card i2 state'), (get_card i3 state') with
| Some x1, Some x2, Some x3 ->
begin
match Round.pass [x1;x2;x3] state' with
| Invalid msg ->
Print.erase_print msg;
Print.score_table state';
Print.print_pile (Round.pile state') (w/2) (2*h/3);
Print.print_hand (Round.hand state') 1 1;
home_loop false state'
| Valid t ->
home_loop true t
end
| _,_,_ ->
Print.erase_print "Card not found";
Print.score_table state';
Print.print_pile (Round.pile state') (w/2) (2*h/3);
Print.print_hand (Round.hand state') 1 1;
home_loop true state'
end
| Play (i) ->
begin
match get_card i state' with
| Some x ->
begin
match Round.play x state' with
| Invalid msg ->
Print.erase_print msg;
Print.score_table state';
Print.print_pile (Round.pile state') (w/2) (2*h/3);
Print.print_hand (Round.hand state') 1 1;
home_loop false state'
| Valid t ->
home_loop true t
end
| None ->
Print.erase_print "Card not found";
Print.score_table state';
Print.print_pile (Round.pile state') (w/2) (2*h/3);
Print.print_hand (Round.hand state') 1 1;
home_loop true state'
end
| Help ->
Print.erase_print "Help";
Print.print_help_menu ();
home_loop false state'
| Debug ->
erase Screen;
save_cursor ();
set_cursor 1 (h/2);
print_string [] (Round.string_of_round state);
restore_cursor ();
home_loop false state'
| Restart ->
Print.erase_print "Restart";
main ()
| Back -> Print.erase_print "Back";
Print.print_pile (match Round.pile state with
| exception Failure _ -> []
| x -> x ) (w/2) (2*h/3);
Print.print_hand (match Round.hand state with
| exception Failure _ -> PartialDeck.empty
| x -> x) 1 1;
home_loop true state'
| Select s -> Print.erase_print "Invalid Command";
home_loop true state'
| Deal ->
begin
let new_st = Round.deal state' in
match new_st with
| Invalid msg ->
Print.erase_print msg;
Print.score_table state';
Print.print_pile (Round.pile state') (w/2) (2*h/3);
Print.print_hand (Round.hand state') 1 1;
home_loop true state'
| Valid t ->
Print.score_table t;
Print.print_pile (Round.pile state') (w/2) (2*h/3);
Print.print_hand (Round.hand state') 1 1;
home_loop true t
end
and
main () =
Print.print_start_menu ();
Unix.sleepf 1.5;
erase Screen;
name_select ();
and name_select () =
Print.print_name_prompt ();
let (w,h) = size () in
set_cursor (1) (h-1);
match (print_string [on_default] "> "; read_line ()) with
| s -> if String.length s > 8 && s <> ""
then name_select () else difficulty () true s
and safe_create d name =
match Round.new_round (d) (name) |> Round.deal with
| Valid t -> erase Screen; home_loop true t
| _ -> safe_create d name
and difficulty () b name =
let () = if b then (Print.print_bot_levels ())
else Print.print_help_menu () in
begin match Print.read_line_safe () with
| Select s ->
begin match get_difficulty s with
| Round.Invalid -> difficulty () true s
| d -> safe_create d name
end
| Quit -> Print.erase_print "Quit";
exit 0
| Help ->
Print.erase_print "Help";
Print.print_help_menu ();
difficulty () false name
| _ -> erase Screen; difficulty () true name end
let () = main ()