-
Notifications
You must be signed in to change notification settings - Fork 0
/
logic.ml
60 lines (46 loc) · 1.14 KB
/
logic.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
(*
* logic.ml
* Copyright (C) 2016 yqiu <yqiu@f24-suntzu>
*
* Distributed under terms of the MIT license.
*)
type status = Playing | Win | Lose
type move = Up | Down | Left | Right
(* monad *)
type t = Board.Grid.t Board.t * status
let turns (b,_) = Board.turns b
let score (b,_) = Board.score b
let get_status t =
if Board.contains_win (fst t) then Win
else
begin
if Board.moves_available (fst t) then Playing else Lose
end
module Monad = struct
let return b = (b,Playing)
let bind t f =
let b = fst t in
let res = f b in
let status = get_status res in
fst res, status
let (>>=) = bind
let lift f = fun b -> return (f b)
end
(* fun action : board -> board * status *)
(* fun board.move_left : board -> board *)
open Monad
let spawn b = return (Board.spawn b)
let move t mv =
match mv with
| Left ->
t >>= lift Board.move_left >>= spawn
| Right ->
t >>= lift Board.move_right >>= spawn
| Up ->
t >>= lift Board.move_up >>= spawn
| Down ->
t >>= lift Board.move_down >>= spawn
let init n =
Random.self_init ();
let board = Board.init n n in
return board >>= spawn >>= spawn