Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[enhance] slightly optim about victory detection

  • Loading branch information...
commit f1a1e756d173b8c5363df14cbd2c5b227fc2f2f7 1 parent fd1ef64
Mathieu Barbin authored
Showing with 25 additions and 14 deletions.
  1. +10 −0 src/colset.opa
  2. +15 −14 src/ia.opa
View
10 src/colset.opa
@@ -50,6 +50,16 @@ type ColSet.t = int
aux(succ(elt), Bitwise.lsr(set, 1), acc)
aux(0, set, acc)
+ find(cond, set : ColSet.t) =
+ rec aux(elt, set) =
+ if set == 0 then {none}
+ else
+ if (Bitwise.land(1, set) == 1) && cond(elt)
+ then some(elt)
+ else
+ aux(succ(elt), Bitwise.lsr(set, 1))
+ aux(0, set)
+
map(map, set : ColSet.t) : ColSet.t =
aux(elt, set) = add(map(elt), set)
fold(aux, set, empty)
View
29 src/ia.opa
@@ -107,20 +107,17 @@ IA_Winning = {{
**/
/**
- * Return the set of action leading to the subite victory for the player [p]
+ * Return a possible action leading to the subite victory for the player [p]
* given a start set of possible actions.
**/
victory(grid : Game.grid, win : IA.winning_grid, actions : ColSet.t, p : Game.player) =
- fold(column, acc) =
+ find(column) =
match GameUtils.free_line(grid, column) with
| { some = line } ->
w = Grid.getij(win, column, line)
- if read(w, p)
- then ColSet.add(column, acc)
- else acc
- | _ -> acc
- actions = ColSet.fold(fold, actions, ColSet.empty)
- actions
+ read(w, p)
+ | _ -> false
+ ColSet.find(find, actions)
/**
* In a set of actions, remove the one making so that the player [p] can play up and win.
@@ -242,15 +239,19 @@ IA_Winning = {{
// Victory
victory = IA_Winning.victory(grid, win, choices, player)
- do log("victory", victory)
- choices = ColSet.specialize(choices, victory)
- do log("choices", choices)
+ match victory with
+ | {some = choice} ->
+ do jlog("victory : {choice}")
+ choice
+ | _ ->
// Blocking victory
block_victory = IA_Winning.victory(grid, win, choices, other_player)
- do log("block_victory", block_victory)
- choices = ColSet.specialize(choices, block_victory)
- do log("choices", choices)
+ match block_victory with
+ | {some=choice} ->
+ do jlog("blocking : {choice}")
+ choice
+ | _ ->
// Anti-victory
anti_victory = IA_Winning.anti_victory(grid, win, choices, other_player)
Please sign in to comment.
Something went wrong with that request. Please try again.