aboutsummaryrefslogtreecommitdiff
path: root/judge/player.ml
blob: 8f74fe7bbe142da5625f4c8a824058bec911b5eb (plain) (blame)
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
open Core

module type IA = sig

  module G : GAME

  val play : G.game -> string

end

module P (W : IA) : sig

  val run : unit -> unit

end = struct

  module G = W.G

  let expect mgs =
    let l = read_line () in
    begin try
    let (s, f) = List.find
      (fun (s, _) ->
         String.length l >= String.length s
      && String.sub l 0 (String.length s) = s)
      mgs
    in f (String.sub l (String.length s)
        (String.length l - String.length s))
    with
    Not_found ->
      Format.eprintf "Unexpected '%s'.@." l;
      exit 1
    end

  let finished _ =
    print_string "Fair enough\n"

  let rec turn g _ =
    expect [
      "Your turn",
        (fun _ ->
        let act = W.play g in
        Format.printf "Play %s@." act;
        let g' = G.play g act in
        expect [ "OK", turn g' ]);
      "Play ", (fun act -> turn (G.play g act) "");
      "Tie", finished;
      "You win", finished;
      "You lose", finished;
      "Eliminated", finished
    ]

  let run () =
    Random.self_init ();
    expect [
    "Hello " ^ G.id,
    (fun _ -> Format.printf "Hello %s@." G.id;
      turn (G.new_game) "")
    ];

end