Permalink
Browse files

[enhance] add winning_hint (currently unused)

  • Loading branch information...
1 parent c9d464e commit 7594742c4d9b5963d7b87c22dadc70b42d94acf9 Mathieu Barbin committed Jul 22, 2011
Showing with 37 additions and 0 deletions.
  1. +18 −0 src/grid.opa
  2. +19 −0 src/ia.opa
View
18 src/grid.opa
@@ -90,6 +90,19 @@ type Grid.t('content) = {
for_i(i) =
for(min2, max2, (j -> iter(i, j)))
for(min, max, for_i)
+
+ /**
+ * Same than [for] for with an accumulator
+ **/
+ fold(min, max, fold : int, 'acc -> 'acc, acc) =
+ rec aux(i, acc) =
+ if i > max then acc else acc = fold(i, acc) ; aux(succ(i), acc)
+ aux(min, acc)
+
+ fold2((min, max), (min2, max2), f : int, int, 'acc -> 'acc, acc) =
+ fold_i(i, acc) =
+ fold(min2, max2, (j, acc -> f(i, j, acc)), acc)
+ fold(min, max, fold_i, acc)
}}
/**
@@ -234,4 +247,9 @@ type Grid.t('content) = {
lines = dimensions.lines
Loop.for2((0, pred(columns)), (0, pred(lines)), iter)
+ foldij(grid : Grid.t, fold, acc : 'acc) =
+ dimensions = grid.dimensions
+ columns = dimensions.columns
+ lines = dimensions.lines
+ Loop.fold2((0, pred(columns)), (0, pred(lines)), fold, acc)
}}
View
19 src/ia.opa
@@ -103,6 +103,25 @@ IA_Winning = {{
win
/**
+ * Given a winning_grid, count how much location are winning, with at least one
+ * empty location lower. We count if there are on an (even, odd) position.
+ **/
+ compute_hint(grid, win : IA.winning_grid, player : Game.player) : (int, int) =
+ fold(i, j, (even, odd) as acc) =
+ if j == 0 then acc else
+ wloc = Grid.getij(win, i, j)
+ if read(wloc, player) then
+ // at least one empty lower
+ if Grid.getij(grid, i, j-1) == { free }
+ then
+ if mod(j, 2) == 0
+ then (succ(even), odd)
+ else (even, succ(odd))
+ else acc
+ else acc
+ Grid.foldij(grid, fold, (0, 0))
+
+ /**
* Once the winning grid is computed, we can have a few interresting informations.
**/

0 comments on commit 7594742

Please sign in to comment.