-
Notifications
You must be signed in to change notification settings - Fork 1
/
command.ml
56 lines (50 loc) · 1.37 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
type command =
| Place of string
| Draw
| PlaceDos
type color_command =
| Red
| Yellow
| Blue
| Green
exception Empty
exception Malformed
let parse str =
if String.length (String.trim str) = 0 then raise Empty
else
let words = String.split_on_char ' ' str in
let spaces_removed =
List.filter (fun x -> String.length x > 0) words
in
match spaces_removed with
| [] -> raise Empty
| [ h; t ] when h = "place" -> Place t
| [ h ] when h = "draw" -> Draw
| [ h ] when h = "place" -> PlaceDos
| h :: t -> raise Malformed
let parse_colors color =
if String.length (String.trim color) = 0 then raise Empty
else
let words = String.split_on_char ' ' color in
let spaces_removed =
List.filter (fun x -> String.length x > 0) words
in
match spaces_removed with
| [] -> raise Empty
| [ h ] when h = "red" -> Red
| [ h ] when h = "blue" -> Blue
| [ h ] when h = "yellow" -> Yellow
| [ h ] when h = "green" -> Green
| _ -> raise Malformed
let parse_pile str =
if String.length (String.trim str) = 0 then raise Empty
else
let words = String.split_on_char ' ' str in
let spaces_removed =
List.filter (fun x -> String.length x > 0) words
in
match spaces_removed with
| [] -> raise Empty
| [ h ] when h = "1" -> 1
| [ h ] when h = "2" -> 2
| _ -> raise Malformed