Permalink
Browse files

n/a: introduce Rules module

  • Loading branch information...
1 parent 8d8def9 commit 05f5d321fb6f4f0c172f0d8d1f918bffe6f87027 @rizsotto committed Nov 17, 2011
Showing with 61 additions and 50 deletions.
  1. +1 −26 src/Domina/Game.hs
  2. +1 −24 src/Domina/Line.hs
  3. +46 −0 src/Domina/Rules.hs
  4. +13 −0 src/Domina/Types.hs
View
@@ -4,14 +4,9 @@ module Domina.Game where
import Domina.Types
import Domina.Set
import Domina.Line
+import Domina.Rules
import Data.List
--- game state is where the dominos can be:
--- somebody's hand,
--- in the line,
--- and face down in the stock.
-data Game = G [Hand] Line Stock
-
setUpGame :: [Player] -> IO Game
setUpGame ps = do
stock <- shuffle $ generate 6
@@ -26,10 +21,6 @@ everybodyDrawSevenFromStock players stock =
playerDrawSeven s p = let (h, s') = splitAt 7 s
in (s', (p, h))
-block :: Game -> Bool
-block (G hands line stock) =
- (not $ any (canExtend line) hands) && (null stock)
-
updateGame :: Game -> Event -> Game
updateGame game@(G hands line stock) (player, move) =
case move of
@@ -48,26 +39,10 @@ updateGame game@(G hands line stock) (player, move) =
let dominos' = delete domino dominos
in (player, dominos')
--- turn is a view on a game where, player can see:
--- his/her own dominos,
--- the line,
--- how many dominos are in the stock,
--- how many dominos are in the opponents' hand.
-data Turn = T Hand Line Int [(Player, Int)]
-
makeTurn :: Game -> Player -> Turn
makeTurn (G hands line stock) player =
T hand line (length stock) others'
where
([hand], others) = partition (\(p,_) -> p == player) hands
others' = map (\(p,h) -> (p, length h)) others
-checkMove :: Turn -> Move -> Bool
-checkMove (T hand line st _) Pass = (not $ canExtend line hand) && (st == 0)
-checkMove (T hand line st _) Draw = (not $ canExtend line hand) && (st /= 0)
-checkMove (T (_, dominos) line _ _) move@(M domino _) =
- elem domino dominos && match move line
- where
- match :: Move -> Line -> Bool
- match (M domino L) line = canExtendOnLeft line domino
- match (M domino R) line = canExtendOnRight line domino
View
@@ -1,9 +1,7 @@
-- Copyright 2011 by Laszlo Nagy [see file COPYRIGHT]
module Domina.Line
- ( canExtend
+ ( fit
, extend
- , canExtendOnLeft
- , canExtendOnRight
) where
import Domina.Types
@@ -12,27 +10,6 @@ import Data.Tuple
fit :: Domino -> Domino -> Bool
fit (_,a) (b,_) = (a == b)
-canExtend :: Line -> Hand -> Bool
-canExtend _ (_, []) = False
-canExtend line (_, dominos) =
- any (match line) dominos
- where
- match :: Line -> Domino -> Bool
- match [] _ = True
- match li d = canExtendOnLeft li d || canExtendOnRight li d
-
-canExtendOnLeft :: Line -> Domino -> Bool
-canExtendOnLeft [] _ = True
-canExtendOnLeft line d = let e = head line
- q = swap d
- in (d `fit` e) || (q `fit` e)
-
-canExtendOnRight :: Line -> Domino -> Bool
-canExtendOnRight [] _ = True
-canExtendOnRight line d = let e = last line
- q = swap d
- in (e `fit` d) || (e `fit` q)
-
extend :: Line -> Move -> Line
extend line (M domino end) =
case end of
View
@@ -0,0 +1,46 @@
+-- Copyright 2011 by Laszlo Nagy [see file COPYRIGHT]
+module Domina.Rules
+ ( block
+ , valid
+ ) where
+
+import Domina.Types
+import Domina.Line
+import Data.List
+import Data.Tuple
+
+block :: Game -> Bool
+block (G hands line stock) =
+ (not $ any (canExtend line) hands) && (null stock)
+
+valid :: Move -> Turn -> Bool
+valid Pass (T hand line st _) = (not $ canExtend line hand) && (st == 0)
+valid Draw (T hand line st _) = (not $ canExtend line hand) && (st /= 0)
+valid move@(M domino _) (T (_, dominos) line _ _) =
+ elem domino dominos && match move line
+ where
+ match :: Move -> Line -> Bool
+ match (M domino L) line = canExtendOnLeft line domino
+ match (M domino R) line = canExtendOnRight line domino
+
+canExtend :: Line -> Hand -> Bool
+canExtend _ (_, []) = False
+canExtend line (_, dominos) =
+ any (match line) dominos
+ where
+ match :: Line -> Domino -> Bool
+ match [] _ = True
+ match li d = canExtendOnLeft li d || canExtendOnRight li d
+
+canExtendOnLeft :: Line -> Domino -> Bool
+canExtendOnLeft [] _ = True
+canExtendOnLeft line d = let e = head line
+ q = swap d
+ in (d `fit` e) || (q `fit` e)
+
+canExtendOnRight :: Line -> Domino -> Bool
+canExtendOnRight [] _ = True
+canExtendOnRight line d = let e = last line
+ q = swap d
+ in (e `fit` d) || (e `fit` q)
+
View
@@ -29,6 +29,19 @@ type Event = (Player, Move)
-- history is a list of events,
type History = [Event]
+-- game represents where the dominos can be:
+-- somebody's hand,
+-- in the line,
+-- and face down in the stock.
+data Game = G [Hand] Line Stock
+
-- and the result of the game
data Result = Drawn
| Winner Player
+
+-- turn is a view on a game where, player can see:
+-- his/her own dominos,
+-- the line,
+-- how many dominos are in the stock,
+-- how many dominos are in the opponents' hand.
+data Turn = T Hand Line Int [(Player, Int)]

0 comments on commit 05f5d32

Please sign in to comment.