-
Notifications
You must be signed in to change notification settings - Fork 0
/
DSL.hs
107 lines (84 loc) · 2.35 KB
/
DSL.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
{-# LANGUAGE DeriveFunctor #-}
module DSL where
import Data.List (delete)
import Control.Applicative ((<$>))
import qualified Control.Exception as Ex (assert)
import Control.Monad.State
import Control.Monad.Trans.Free
import Control.Monad.Trans.Class
import Types
import GameState (GameState)
import qualified GameState as GS
import CardFuncs
data GameAction nxt
= AttackWith Card nxt
| PassWith Card nxt
| Defend PlayedCard Card nxt
| ReinforceWith [Card] nxt
| WinTurn Role nxt
| Result Int nxt
deriving (Functor)
type GameDSL = FreeT GameAction GameState
-- | assertions for the DSL programmer
assert test = return $ Ex.assert test $ Pure ()
attackWith :: Card -> GameDSL ()
attackWith card = do
playCard Offense card
liftF $ AttackWith card ()
playCard :: Role -> Card -> GameDSL ()
playCard role card = do
hand <- getHand role
assert (card `elem` hand)
lift $ GS.updateHand role (delete card)
lift $ GS.playCard card
getHand = lift . GS.getHand
getTrump :: GameDSL Suit
getTrump = lift GS.getTrump
getTable :: GameDSL [PlayedCard]
getTable = lift GS.getTable
trumpCards role = do
t <- getTrump
filter ((== t) . cSuit) <$> getHand role
passWith card = do
playCard Defense card
swapRoles
liftF $ PassWith card id
swapRoles :: GameDSL ()
swapRoles = lift $ GS.swapRoles
defend :: PlayedCard -> Card -> GameDSL ()
defend pc c = do
table <- getTable
hand <- getHand Defense
assert (pc `elem` table)
assert (c `elem` hand)
lift $ GS.updateHand Defense (delete c)
lift $ GS.coverCard pc c
liftF $ Defend pc c ()
playedCards :: GameDSL [Card]
playedCards = do
t <- getTable
return $ concatMap bothCards t
where
bothCards (PC ca co) = case co of
Just co' -> [ca, co']
_ -> [ca]
reinforceWith :: [Card] -> GameDSL ()
reinforceWith cs = do
mapM_ (playCard Offense) cs
liftF $ ReinforceWith cs ()
winTurn role = do
case role of
Offense -> do
allCards <- playedCards
lift $ GS.updateHand Defense (++ allCards)
Defense -> swapRoles
lift $ GS.clearTable
liftF $ WinTurn role ()
tieGame :: GameDSL ()
tieGame = liftF $ Result 0 ()
winner :: Role -> GameDSL ()
winner role = do
n <- playerNum role
liftF $ Result n ()
playerNum :: Role -> GameDSL Int
playerNum r = lift $ GS.playerNum r