split GUI into several modules
[hs-pgms.git] / src / SimpleStrat.hs
blob8eacc6953a3ccadd0aa381e4ccb058609e02e2bd
1 {-
2 - beginner: ~ 60%
3 - intermediate: ~ 30%
4 - expert: ~ 0.7%
5 -}
6 module SimpleStrat (simpleStrat) where
8 import Mine
9 import System.Random
10 import Data.Array
11 import Control.Monad
13 simpleStrat :: Strategy
14 simpleStrat = defaultStrategy {
15 sName = "SimpleStrat",
16 sAuthor = "Bertram Felgenhauer <int-e@gmx.de>",
17 sDescription =
18 "This strategy makes inferences from exposed cells and their immediate \
19 \neighbourhood, and picks a random cell if no inference can be made.\n\
20 \It should be equivalent to the PGMS Single Point Strategy.",
21 sRun = \s -> getConfig >>= flip worker s
24 worker :: Config -> StdGen -> StrategyM String
25 worker cfg gen = do
26 view <- getView
27 let actions = scan cfg view
28 if null actions then randomMove cfg view gen >>= worker cfg
29 else sequence_ actions >> worker cfg gen
31 randomMove :: Config -> View -> StdGen -> StrategyM StdGen
32 randomMove cfg@Config { cSize = Pos sX sY } view gen = let
33 (x, gen') = randomR (1, sX) gen
34 (y, gen'') = randomR (1, sY) gen'
35 p = Pos x y
37 if view ! p == Hidden then move p >> return gen''
38 else randomMove cfg view gen''
40 scan :: Config -> View -> [StrategyM ()]
41 scan cfg view = concat
42 [ moves
43 | (p, Exposed num) <- assocs view,
44 let ns = neighbours cfg p
45 ms = [n | n <- ns, view ! n == Marked]
46 us = [n | n <- ns, view ! n == Hidden]
47 moves | num == length ms = [move_ n | n <- us]
48 | num == length ms + length us = [mark n | n <- us]
49 | otherwise = []]