5 from Text import class Text, instance Text String
7 from Data.Func import $
10 import Control.Monad.State
11 import Control.Applicative
15 :: Coord :== (Int, Int)
16 :: Position = Inv | Emp | Peg
17 :: PegBoard :== {#{Position}}
18 :: Move :== (Coord, Direction)
19 :: Direction = N | E | S | W
23 {{Inv, Inv, Peg, Peg, Peg, Inv, Inv}
24 ,{Inv, Inv, Peg, Peg, Peg, Inv, Inv}
25 ,{Peg, Peg, Peg, Peg, Peg, Peg, Peg}
26 ,{Peg, Peg, Peg, Emp, Peg, Peg, Peg}
27 ,{Peg, Peg, Peg, Peg, Peg, Peg, Peg}
28 ,{Inv, Inv, Peg, Peg, Peg, Inv, Inv}
29 ,{Inv, Inv, Peg, Peg, Peg, Inv, Inv}
32 instance toChar Position where
33 toChar p = case p of Inv = ' '; Emp = '.'; Peg = 'o'
35 instance == Position where
41 transform :: Coord Direction -> Coord
42 transform (x, y) d = case d of
48 getPos :: Coord -> StateT PegBoard Maybe Position
49 getPos c=:(x, y) = getState >>= \b->if (valid b c)
52 valid :: PegBoard Coord -> Bool
53 valid b (x, y) = y<0 || x<0 || y >= size b || x >= size b.[0] || b.[y].[x] == Inv
55 fail :: StateT PegBoard Maybe a
56 fail = StateT \s->Nothing
58 applyMove :: Move PegBoard -> Maybe PegBoard
59 applyMove (c=:(fx, fy), d) b = Nothing
60 //# sc=:(sx, sy) = transform c d
61 //# tc=:(tx, ty) = transform sc d
62 //= liftM3 tuple3 (getPos c b) (getPos sc b) (getPos tc b)
64 // (Peg, Peg, Emp) = Nothing//Just {b & [fx,fy]=Emp, [sx,sy]=Emp, [tx,ty]=Peg}
67 getCoords :: (Position -> Bool) PegBoard -> [Coord]
68 getCoords f b = [(x, y)\\r<-:b & y<-[0..] , c<-:r & x<-[0..] | f c]
70 win :: (PegBoard -> Bool)
71 win = isEmpty o getCoords ((<>)Peg)
73 printPegBoard :: PegBoard -> String
74 printPegBoard b = 'Text'.join "\n" [{#toChar x\\x<-:r}\\r <-: b]
77 ( getCoords ((==)Emp) european
79 , printPegBoard <$> applyMove ((3,2), S) european
81 //Start = printPegBoard european