-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpoint.ml
55 lines (47 loc) · 1.67 KB
/
point.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
open Core
type t = int [@@deriving compare,sexp]
let empty = 0
let create occupier count =
let count = Int.max 0 count in
match occupier with
| Player.Forwards -> count
| Backwards -> - count
let occupier t =
match Int.sign t with
| Sign.Neg -> Some Player.Backwards
| Zero -> None
| Pos -> Some Player.Forwards
let count t player =
match player with
| Player.Forwards -> Int.max 0 t
| Backwards -> Int.max 0 (-t)
let remove_exn t player =
match Option.map (occupier t) ~f:(Player.equal player) with
| None | Some false ->
failwithf "No counters of player %c on point to remove" (Player.char player) ()
| Some true -> t - create player 1
let add_exn t player =
match Option.map (occupier t) ~f:(Player.equal player) with
| Some false ->
failwithf "Counters of player %c prevent addition of counters of player %c"
(Player.char (Player.flip player)) (Player.char player) ()
| None | Some true -> t + create player 1
let to_representation t version =
let forwards_representation t =
[ if Int.equal t 1 then 1. else 0.
; if Int.(t >= 2) then 1. else 0.
; (match version with
| `Original -> if Int.equal t 3 then 1. else 0.
| `Modified | `Expanded -> if Int.(t >= 3) then 1. else 0.)
] @
match version with
| `Original | `Modified -> [if Int.(t >= 4) then Float.(/) (Int.to_float (t - 3)) 2. else 0.]
| `Expanded ->
[ if Int.(t >= 4) then 1. else 0.
; if Int.(t >= 5) then 1. else 0.
; if Int.(t >= 6) then Float.(/) (Int.to_float (t - 5)) 2. else 0.
]
in
Per_player.create (function
| Player.Forwards -> forwards_representation t
| Backwards -> forwards_representation (-t))