-
Notifications
You must be signed in to change notification settings - Fork 2
/
command.ml
210 lines (200 loc) · 5.72 KB
/
command.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
type menu_command =
|Draft
|Start
|Exit
|Help
type game_command =
|Attack of (int * int)
|End
|HPow of int option
|PCard of int * int option
|LookH
|Concede
|Help
(*parses the input for the menu options and if it is not a valid input
*it tells the user and asks for input again
*)
let rec parse_menu () =
let cmd = read_line () in
let str = String.lowercase (String.trim cmd) in
match str with
|"draft" -> Draft
|"start" -> Start
|"exit" -> Exit
|"help" -> Help
|x -> Printf.printf "Invalid command\n"; parse_menu ()
(*Returns a string that is the second word in the input string
* - str = the string being split
* - first = the first word in the string
*)
let next_word str first : string =
if(String.contains str ' ') then
let sp = String.index str ' ' in
String.sub str (sp+1) ((String.length str)-(String.length first) - 1)
else ""
(*returns a bool telling if the command inputted is for an attack*)
let valid_attack str : bool =
let len = String.length str in
if(len > 6) then
let cmd = String.sub str 0 6 in
let num = String.trim (next_word str cmd) in
if((cmd = "attack") && (String.contains num ' ')) then
let space = String.index num ' ' in
let si1 = String.sub num 0 space in
let si2 = String.trim (next_word num si1) in
let bi1 =
try
let _ = int_of_string si1 in true
with
|x -> false in
let bi2 =
try
let _ = int_of_string si2 in true
with
|x -> false in
(bi1 && bi2)
else false
else false
(*returns a bool telling if the command inputted is for playing a card*)
let valid_pcard str : bool =
let len = String.length str in
if(len > 5) then
let cmd = String.sub str 0 5 in
if(cmd = "pcard") then
let num = String.trim (next_word str cmd) in
if(String.contains num ' ') then
let space = String.index num ' ' in
let si1 = String.sub num 0 space in
let si2 = String.trim (next_word num si1) in
let bi1 =
try
let _ = int_of_string si1 in true
with
|x -> false in
let bi2 =
try
let _ = int_of_string si2 in true
with
|x -> false in
(bi1 && bi2)
else
try
let _ = int_of_string num in true
with
|x -> false
else false
else false
(*returns a bool telling if the command inputted is for hero power*)
let valid_hpow str : bool =
let len = String.length str in
if(len > 4) then
let cmd = String.sub str 0 4 in
if(cmd = "hpow") then
let num = String.trim (next_word str cmd) in
try
let _ = int_of_string num in true
with
|x -> false
else false
else false
(*parses the input for the game commands and if it is not a valid command
*it tells the user and asks for input again
*)
let rec parse_game () =
let cmd = read_line () in
let str = String.lowercase (String.trim cmd) in
match str with
|"end" -> End
|"lookh" -> LookH
|"concede" -> Concede
|"help" -> Help
|"hpow" -> HPow None
|s when (valid_attack str) -> do_attack str
|s when (valid_pcard str) -> do_pcard str
|s when (valid_hpow str) -> do_hpow str
|_ -> Printf.printf "Invalid command\n"; parse_game ()
(*output the attack command if input is valid attack input but ask for input
*again if command is not a valid command
*)
and do_attack str =
let len = String.length str in
if(len > 6) then
let cmd = String.sub str 0 6 in
let num = String.trim (next_word str cmd) in
if((cmd = "attack") && (String.contains num ' ')) then
let space = String.index num ' ' in
let si1 = String.sub num 0 space in
let si2 = String.trim (next_word num si1) in
let bi1 =
try
let _ = int_of_string si1 in true
with
|x -> false in
let bi2 =
try
let _ = int_of_string si2 in true
with
|x -> false in
if (bi1 && bi2) then
let i1 = int_of_string si1 in
let i2 = int_of_string si2 in
Attack (i1,i2)
else parse_game ()
else parse_game ()
else parse_game ()
(*output the pcard command if input is valid pcard input but ask for input again
*if command is not a valid command
*)
and do_pcard str =
let len = String.length str in
if(len > 5) then
let cmd = String.sub str 0 5 in
if(cmd = "pcard") then
let num = String.trim (next_word str cmd) in
if(String.contains num ' ') then
let space = String.index num ' ' in
let si1 = String.sub num 0 space in
let si2 = String.trim (next_word num si1) in
let bi1 =
try
let _ = int_of_string si1 in true
with
|x -> false in
let bi2 =
try
let _ = int_of_string si2 in true
with
|x -> false in
if (bi1 && bi2) then
let i1 = int_of_string si1 in
let i2 = int_of_string si2 in
PCard (i1, Some i2)
else parse_game ()
else
let bi =
try
let _ = int_of_string num in true
with
|x -> false in
if(bi) then PCard ((int_of_string num), None)
else parse_game ()
else parse_game ()
else parse_game ()
(*output the hpow command if input is valid hpow input but ask for input again
*if command is not a valid command
*)
and do_hpow str =
let len = String.length str in
if(len > 4) then
let cmd = String.sub str 0 4 in
let num = String.trim (next_word str cmd) in
if(cmd = "hpow") then
let bi =
try
let _ = int_of_string num in true
with
|x -> false in
if(bi) then HPow (Some (int_of_string num))
else parse_game ()
else parse_game ()
else parse_game ()