diff options
author | Alex AUVOLAT <alex.auvolat@ens.fr> | 2014-11-10 10:46:37 +0100 |
---|---|---|
committer | Alex AUVOLAT <alex.auvolat@ens.fr> | 2014-11-10 10:46:37 +0100 |
commit | 80e625a9c8d33c71fe69a375c211868fcc1938a5 (patch) | |
tree | f516d701cc8cb50e9a1d003afe6e7d1f05b457ca | |
parent | d343fcb803e955504b0d6b5c9c852620886c2994 (diff) | |
download | CompetIA-80e625a9c8d33c71fe69a375c211868fcc1938a5.tar.gz CompetIA-80e625a9c8d33c71fe69a375c211868fcc1938a5.zip |
Start work on players!
-rw-r--r-- | .gitignore | 5 | ||||
-rw-r--r-- | judge/core.ml | 19 | ||||
-rw-r--r-- | judge/dummy_game.ml | 18 | ||||
-rw-r--r-- | judge/dummy_judge.ml | 6 | ||||
-rw-r--r-- | judge/dummy_player.ml | 49 | ||||
-rw-r--r-- | judge/morpion_rec.ml | 65 | ||||
-rw-r--r-- | judge/morpion_rec_judge.ml | 6 | ||||
-rw-r--r-- | judge/player.ml | 61 | ||||
-rw-r--r-- | morpion_rec/FeirisuNyanNyan/_tags | 2 | ||||
-rw-r--r-- | morpion_rec/FeirisuNyanNyan/feirisu.ml | 17 | ||||
l--------- | morpion_rec/FeirisuNyanNyan/lib | 1 | ||||
-rw-r--r-- | morpion_rec/Mayushii/_tags | 2 | ||||
l--------- | morpion_rec/Mayushii/lib | 1 | ||||
-rw-r--r-- | morpion_rec/Mayushii/mayushii.ml | 14 | ||||
-rw-r--r-- | morpion_rec/NiAh/main.c | 5 |
15 files changed, 171 insertions, 100 deletions
@@ -1,10 +1,13 @@ *.swp *~ -judge/_build/* +*/_build/* +*/*/_build/* *.native *.byte +*.log + *.o morpion_rec/*/player diff --git a/judge/core.ml b/judge/core.ml index 8ffdc47..dd6c31c 100644 --- a/judge/core.ml +++ b/judge/core.ml @@ -4,6 +4,8 @@ open Protocol (* Description of data structures *) +exception Eliminated_ex of string + type player = P1 | P2 let other_player = function P1 -> P2 | P2 -> P1 @@ -28,7 +30,7 @@ module type GAME = sig val new_game : game - val play : game -> player -> string -> game + val play : game -> string -> game val s : game -> game_status val display_game : game -> (string * string) -> unit @@ -65,8 +67,6 @@ end module Core (G: GAME) : CORE = struct module G : GAME = G - exception Eliminated_ex of string - type player = { name: string; binary: string; @@ -84,7 +84,9 @@ module Core (G: GAME) : CORE = struct mutable s: player_proc_status; } let send_m pp m = - output_string pp.o (encode m ^ "\n"); + let m = encode m in + Format.printf ">%s< %s@." pp.p.name m; + output_string pp.o (m ^ "\n"); flush pp.o type game = { @@ -282,12 +284,17 @@ module Core (G: GAME) : CORE = struct let pi = if Unix.descr_of_in_channel g.p1.i = fd then P1 else P2 in let p = match pi with P1 -> g.p1 | P2 -> g.p2 in let op = match pi with P1 -> g.p2 | P2 -> g.p1 in - begin try match decode (input_line p.i), p.s with + begin try + let l = input_line p.i in + Format.printf "<%s> %s@." p.p.name l; + match decode l, p.s with | Hello x, Loading when x = G.id -> p.s <- StandBy !game_time; | Play act, Thinking (time, beg_r) -> let end_r = Unix.gettimeofday () in - let new_g = G.play (List.hd g.hist) pi act in + if G.s (List.hd g.hist) <> TurnOf pi then + raise (Eliminated_ex "not your turn (assert failed)"); + let new_g = G.play (List.hd g.hist) act in let new_s = G.s new_g in send_m p OK; send_m op (Play act); diff --git a/judge/dummy_game.ml b/judge/dummy_game.ml index 1e72682..4ac4eac 100644 --- a/judge/dummy_game.ml +++ b/judge/dummy_game.ml @@ -1,16 +1,15 @@ open Core open Main -module Dummy : GAME = struct +module G : GAME = struct type game = int * (player * string) list * game_status let new_game = (10, [], TurnOf P1) - let play (g, l, s0) p xx = - if s0 <> TurnOf p || g <= 0 then - (g, l, Eliminated p) - else + let play (g, l, s0) xx = + match s0 with + | TurnOf p when g > 0 -> let op = other_player p in (g-1, l@[p, xx], if g - 1 = 0 then @@ -21,6 +20,8 @@ module Dummy : GAME = struct else TurnOf op ) + | TurnOf x -> (g, l, Eliminated x) + | _ -> raise (Eliminated_ex "not someone's turn!") let s (_, _, s) = s @@ -46,10 +47,3 @@ module Dummy : GAME = struct let name = "Dummy game for testing purposes" end - -module C = Core(Dummy) -module Main = Juge(C) - -let () = - Random.self_init (); - Main.run () diff --git a/judge/dummy_judge.ml b/judge/dummy_judge.ml new file mode 100644 index 0000000..1f80175 --- /dev/null +++ b/judge/dummy_judge.ml @@ -0,0 +1,6 @@ +module C = Core.Core(Dummy_game.G) +module Main = Main.Juge(C) + +let () = + Random.self_init (); + Main.run () diff --git a/judge/dummy_player.ml b/judge/dummy_player.ml index 44a24ce..1897442 100644 --- a/judge/dummy_player.ml +++ b/judge/dummy_player.ml @@ -1,7 +1,6 @@ - let words = [| - "banane"; "hippopotame"; "povrion"; + "banane"; "hippopotame"; "poivron"; "pourquoi???"; "un ange passe"; "television"; "ceci n'est pas..."; "environ 12"; "septante"; "Philipp Glass"; "nyaaa"; "tu crois ?"; "hallo"; @@ -19,44 +18,18 @@ let words = [| "j'ai une grosse courgette"; "bouilloire"; "morning coffee"; |] -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 +module Dummy_IA : Player.IA = struct + + module G = Dummy_game.G + + let play _ = + if Random.int 2 = 0 then Unix.sleep 1; + words.(Random.int (Array.length words)) -let finished _ = - print_string "Fair enough\n" +end -let rec turn _ = - expect [ - "Your turn", - (fun _ -> - if Random.int 2 = 0 then Unix.sleep 1; - Format.printf "Play %s@." - words.(Random.int (Array.length words)); - expect [ "OK", turn ]); - "Play ", turn; - "Tie", finished; - "You win", finished; - "You lose", finished; - "Eliminated", finished - ] +module Dummy = Player.P(Dummy_IA) let () = Random.self_init (); - expect [ - "Hello dummy_game", - (fun _ -> print_string "Hello dummy_game\n"; - turn "") - ]; + Dummy.run() diff --git a/judge/morpion_rec.ml b/judge/morpion_rec.ml index 15af169..d2b1c14 100644 --- a/judge/morpion_rec.ml +++ b/judge/morpion_rec.ml @@ -3,22 +3,7 @@ open Main let ( |> ) x f = f x -module Morpion_rec : sig - - type game (* immutable structure *) - - val name : string - val id : string - - val new_game : game - - val possibilities : game -> string list - val play : game -> player -> string -> game - val s : game -> game_status - - val display_game : game -> (string * string) -> unit - -end = struct +module G = struct exception Invalid_pos @@ -126,28 +111,28 @@ end = struct | l when List.exists ((=) Empty) l -> Empty | _ -> T - let play (gs, m, pgo) player act = - let elim = (Eliminated player, m, pgo) in - let op = other_player player in + let play (gs, m, pgo) act = let (pg, pp) = decode act in - if - gs = TurnOf player - && (match pgo with - | None -> true - | Some x when full_pm (getp1 m x) -> true - | Some x when pg = x -> true - | _ -> false) - && getp m (pg, pp) = Empty - then - let new_m = setp m (pg, pp) (match player with P1 -> X | P2 -> O) in - let new_s = match reduce_m (reduce_m (fun x -> x)) new_m with - | Empty -> TurnOf op - | X -> Won P1 - | O -> Won P2 - | T -> Tie - in - (new_s, new_m, Some pp) - else elim + match gs with + | TurnOf player when + (match pgo with + | None -> true + | Some x when full_pm (getp1 m x) -> true + | Some x when pg = x -> true + | _ -> false) + && getp m (pg, pp) = Empty + -> + let op = other_player player in + let new_m = setp m (pg, pp) (match player with P1 -> X | P2 -> O) in + let new_s = match reduce_m (reduce_m (fun x -> x)) new_m with + | Empty -> TurnOf op + | X -> Won P1 + | O -> Won P2 + | T -> Tie + in + (new_s, new_m, Some pp) + | TurnOf x -> (Eliminated x, m, pgo) + | _ -> raise (Eliminated_ex "not someone's turn!") let s (s, _, _) = s @@ -158,9 +143,3 @@ end = struct end -module C = Core(Morpion_rec) -module Main = Juge(C) - -let () = Main.run () - - diff --git a/judge/morpion_rec_judge.ml b/judge/morpion_rec_judge.ml new file mode 100644 index 0000000..77308f8 --- /dev/null +++ b/judge/morpion_rec_judge.ml @@ -0,0 +1,6 @@ +module C = Core.Core(Morpion_rec.G) +module Main = Main.Juge(C) + +let () = Main.run () + + diff --git a/judge/player.ml b/judge/player.ml new file mode 100644 index 0000000..8f74fe7 --- /dev/null +++ b/judge/player.ml @@ -0,0 +1,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 diff --git a/morpion_rec/FeirisuNyanNyan/_tags b/morpion_rec/FeirisuNyanNyan/_tags new file mode 100644 index 0000000..c653205 --- /dev/null +++ b/morpion_rec/FeirisuNyanNyan/_tags @@ -0,0 +1,2 @@ +"lib": include +true: use_unix, use_graphics diff --git a/morpion_rec/FeirisuNyanNyan/feirisu.ml b/morpion_rec/FeirisuNyanNyan/feirisu.ml new file mode 100644 index 0000000..be6cc63 --- /dev/null +++ b/morpion_rec/FeirisuNyanNyan/feirisu.ml @@ -0,0 +1,17 @@ + +module Mayushii : Player.IA = struct + + module G = Morpion_rec.G + + let play g = + let cc = G.possibilities g in + List.nth cc + (Random.int (List.length cc)) + +end + +module P = Player.P(Mayushii) + +let () = + Random.self_init(); + P.run() diff --git a/morpion_rec/FeirisuNyanNyan/lib b/morpion_rec/FeirisuNyanNyan/lib new file mode 120000 index 0000000..e834a2a --- /dev/null +++ b/morpion_rec/FeirisuNyanNyan/lib @@ -0,0 +1 @@ +../../judge
\ No newline at end of file diff --git a/morpion_rec/Mayushii/_tags b/morpion_rec/Mayushii/_tags new file mode 100644 index 0000000..c653205 --- /dev/null +++ b/morpion_rec/Mayushii/_tags @@ -0,0 +1,2 @@ +"lib": include +true: use_unix, use_graphics diff --git a/morpion_rec/Mayushii/lib b/morpion_rec/Mayushii/lib new file mode 120000 index 0000000..e834a2a --- /dev/null +++ b/morpion_rec/Mayushii/lib @@ -0,0 +1 @@ +../../judge
\ No newline at end of file diff --git a/morpion_rec/Mayushii/mayushii.ml b/morpion_rec/Mayushii/mayushii.ml new file mode 100644 index 0000000..57ec3aa --- /dev/null +++ b/morpion_rec/Mayushii/mayushii.ml @@ -0,0 +1,14 @@ + +module Mayushii : Player.IA = struct + + module G = Morpion_rec.G + + let play g = + let cc = G.possibilities g in + List.hd cc + +end + +module P = Player.P(Mayushii) + +let () = P.run() diff --git a/morpion_rec/NiAh/main.c b/morpion_rec/NiAh/main.c index 1cee771..7447085 100644 --- a/morpion_rec/NiAh/main.c +++ b/morpion_rec/NiAh/main.c @@ -46,10 +46,12 @@ int main() if(strcmp(input, "Hello morpion_rec") == 0) { printf("Hello morpion_rec\n"); + fflush(stdout); } else { printf("Who are you, crazy bastard?\n"); + fflush(stdout); return EXIT_FAILURE; } @@ -95,6 +97,7 @@ int main() if(strstr(input,"You win")==input || strstr(input,"You lose")==input || strstr(input,"Tie")==input || strstr(input,"Cheater")==input) { printf("Fair enough\n"); + fflush(stdout); return EXIT_SUCCESS; } } @@ -115,11 +118,13 @@ int main() play(grid, subgrid, move, CROSS); updateWon(grid, &won, swon, subgrid, move); printf("Play %d %d %d %d\n", subgrid%3+1, subgrid/3+1, move%3+1, move/3+1); + fflush(stdout); subgrid=move; printGrid(grid, swon); if(won==CROSS) fprintf(stderr, "I won !!"); + fflush(stdout); } return EXIT_SUCCESS; |