aboutsummaryrefslogtreecommitdiff
path: root/morpion_rec/AmaneSuzuha/amane.ml
blob: 7ac321f031b002cbc1f1184d3c1c7dfa9eddfb44 (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
module Amane : Player.IA = struct

  module G = Morpion_rec.G
  open Core

  let take_random cc =
    List.nth cc
      (Random.int (List.length cc))

  let won_game g = match G.s g with Won _ -> true | _ -> false

  let play g =
    let cc = G.possibilities g in
    match List.partition
      (fun act -> won_game (G.play g act))
      cc
    with
    | win::_, _ -> win
    | [], other ->
      let o' = List.filter
        (fun act ->
          try
            let g' = G.play g act in
            let adv_win =
              G.possibilities g'
              |> List.map (G.play g')
              |> List.exists won_game
            in not adv_win
          with _ -> true)
        other
      in
      if List.length o' > 0 then
        take_random o'
      else
        take_random other

end

module P = Player.P(Amane)

let () =
  Random.self_init();
  P.run()