Skip to content

Commit

Permalink
Faster lookAround
Browse files Browse the repository at this point in the history
  • Loading branch information
nomeata committed Apr 22, 2009
1 parent dedf465 commit b8306a2
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions Data.hs
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,39 @@ flipStones gf (i1,i2) = gf // [(i1, gf ! i2),(i2, gf ! i1)]
possibleMoves gf = filter works $ filter valid $ allMoves
where allMoves = concat [ [((i,j),(i+1,j)), ((i,j),(i,j+1))] | (i,j) <- range ((0,0),(9,9)) ]
valid (_,(i,j)) = i < 10 && j < 10 -- first coordinate is always correct
works move@(i1,i2) = let gf' = flipStones gf move in lookAround i1 gf' || lookAround i2 gf'
works move@(i1,i2) = let gf' = flipStones gf move
in lookAround i1 gf' || lookAround i2 gf'
{- The following code is a bit more haskellish, but too slow. Replacing
- it with the code below give a 30% time boost.
lookAround (i,j) gf' = any ((>=3).length) $
(groupBy combineable $ [ gf' ! (i',j) | i' <- range (max 0 (i-2), min 9 (i+2)) ]) ++
(groupBy combineable $ [ gf' ! (i,j') | j' <- range (max 0 (j-2), min 9 (j+2)) ])

-}
{- A
B
FGCHK
D
E -}
lookAround (i,j) gf' = let
a = if i < 8 then gf' ! (i+2,j) else Unknown
b = if i < 9 then gf' ! (i+1,j) else Unknown
c = gf' ! (i,j)
d = if i > 0 then gf' ! (i-1,j) else Unknown
e = if i > 1 then gf' ! (i-2,j) else Unknown
f = if j > 1 then gf' ! (i,j-2) else Unknown
g = if j > 0 then gf' ! (i,j-1) else Unknown
h = if j < 9 then gf' ! (i,j+1) else Unknown
k = if j < 8 then gf' ! (i,j+2) else Unknown
ab = a `combineable` b
bc = b `combineable` c
cd = c `combineable` d
ch = c `combineable` h
de = d `combineable` e
fg = f `combineable` g
gc = g `combineable` c
hk = h `combineable` k
in bc && (ab || cd) || cd && de ||
gc && (fg || ch) || ch && hk

removeTiles :: GameField -> [[Stone]] -> (GameField, [[Stone]])
removeTiles field sequences =
Expand Down

0 comments on commit b8306a2

Please sign in to comment.