-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathUno.hs
290 lines (251 loc) · 8.11 KB
/
Uno.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
{-# LANGUAGE DataKinds #-}
{-# HLINT ignore "Redundant id" #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
-- https://downloads.haskell.org/ghc/latest/docs/users_guide/using-warnings.html#ghc-flag--Wmissing-deriving-strategies
{-# OPTIONS_GHC -Wno-missing-deriving-strategies #-}
-- https://downloads.haskell.org/ghc/latest/docs/users_guide/using-warnings.html#ghc-flag--Wunrecognised-pragmas
{-# OPTIONS_GHC -Wno-unrecognised-pragmas #-}
-- https://downloads.haskell.org/ghc/latest/docs/users_guide/using-warnings.html#ghc-flag--Wunticked-promoted-constructors
{-# OPTIONS_GHC -Wno-unticked-promoted-constructors #-}
-- https://downloads.haskell.org/ghc/latest/docs/users_guide/using-warnings.html#ghc-flag--Wunused-type-patterns
{-# OPTIONS_GHC -Wno-unused-type-patterns #-}
-- | Porting of https://github.com/thinkbeforecoding/UnoCore/blob/solution/Uno/Game.fs
module Crem.Example.Uno where
import Crem.BaseMachine (InitialState (..))
import Crem.Decider (Decider (..), EvolutionResult (..))
import Crem.Render.RenderableVertices (AllVertices (..), RenderableVertices)
import Crem.Topology
import "singletons-base" Data.Singletons.Base.TH
import Prelude hiding (id, init, reverse)
import Prelude qualified (id)
-- * Domain
newtype PlayerCount = PlayerCount Int
deriving newtype (Eq, Ord, Show)
data Digit
= Zero
| One
| Two
| Three
| Four
| Five
| Six
| Seven
| Eigth
| Nine
deriving stock (Eq, Show)
data Colour
= Red
| Green
| Blue
| Yellow
deriving stock (Eq, Show)
data Card
= DigitCard Digit Colour
| Skip Colour
| Kickback Colour
deriving stock (Eq, Show)
colour :: Card -> Colour
colour (DigitCard _ c) = c
colour (Skip c) = c
colour (Kickback c) = c
sameColour :: Card -> Card -> Bool
sameColour card1 card2 =
colour card1 == colour card2
data CardValue
= DigitValue Digit
| SkipValue
| KickbackValue
deriving stock (Eq)
value :: Card -> CardValue
value (DigitCard d _) = DigitValue d
value (Skip _) = SkipValue
value (Kickback _) = KickbackValue
sameValue :: Card -> Card -> Bool
sameValue card1 card2 =
value card1 == value card2
newtype PlayerId = PlayerId Int
deriving newtype (Eq, Show)
data Direction
= Clockwise
| CounterClockwise
reverseDirection :: Direction -> Direction
reverseDirection Clockwise = CounterClockwise
reverseDirection CounterClockwise = Clockwise
data Player = Player
{ id :: PlayerId
, count :: PlayerCount
, direction :: Direction
}
init :: PlayerId -> PlayerCount -> Player
init playerId count =
Player
{ id = playerId
, count = count
, direction = Clockwise
}
next :: Player -> Player
next (Player (PlayerId id) (PlayerCount count) direction) =
case direction of
Clockwise ->
Player
{ id = PlayerId $ (id + 1) `mod` count
, count = PlayerCount count
, direction
}
CounterClockwise ->
Player
{ id = PlayerId $ (id - 1) `mod` count
, count = PlayerCount count
, direction
}
skip :: Player -> Player
skip = next . next
reverse :: Player -> Player
reverse (Player id count direction) =
Player id count $ reverseDirection direction
nextPlayer :: Card -> Player -> Player
nextPlayer card player = case card of
DigitCard _ _ -> next player
Skip _ -> skip player
Kickback _ -> next . reverse $ player
set :: PlayerId -> Player -> Player
set playerId player = player {id = playerId}
-- * Commands and events
data InitialData = InitialData
{ players :: PlayerCount
, firstCard :: Card
}
deriving stock (Eq, Show)
data PlayData = PlayData
{ playerId :: PlayerId
, card :: Card
}
deriving stock (Eq, Show)
data Command
= StartGame InitialData
| PlayCard PlayData
deriving stock (Show)
-- deriving (Arbitrary) via (GenericArbitrary Command)
data Event
= GameStarted InitialData PlayerId
| CardPlayed PlayData
| CardPlayedAndTurnBegan PlayData PlayerId
| WrongCardPlayed PlayData
| PlayerPlayedAtWrongTurn PlayData
deriving stock (Eq, Show)
-- * Topology
$( singletons
[d|
data UnoVertex
= Initial
| Started
deriving stock (Eq, Show, Enum, Bounded)
unoTopology :: Topology UnoVertex
unoTopology = Topology [(Initial, [Started])]
|]
)
deriving via AllVertices UnoVertex instance RenderableVertices UnoVertex
-- * State
data StateData = StateData
{ topCard :: Card
, currentPlayer :: Player
}
data UnoState (vertex :: UnoVertex) where
UnoInitialState :: UnoState 'Initial
UnoStartedState :: StateData -> UnoState 'Started
-- * Errors
data GameError
= TooFewPlayers
| GameAlreadyStarted
| GameNotStarted
deriving stock (Eq, Show)
-- * Machine
-- | A decider with the logic of the Uno game
--
-- It emits one event for every transition, not a list of events, because an
-- event represents a state transition and a state machine perform one single
-- state transition at every step
unoDecider
:: InitialState UnoState
-> Decider UnoTopology Command (Either GameError Event)
unoDecider initialState =
Decider
{ deciderInitialState = initialState
, decide = \command state ->
case (state, command) of
(_, StartGame initialData)
| players initialData < PlayerCount 2 ->
Left TooFewPlayers
(UnoStartedState _, StartGame _) ->
Left GameAlreadyStarted
(_, StartGame initialData) ->
Right $
GameStarted
initialData
( id $
nextPlayer
(firstCard initialData)
(init (PlayerId 0) $ players initialData)
)
(UnoInitialState, PlayCard _) ->
Left GameNotStarted
(UnoStartedState stateData, PlayCard playData)
| id (currentPlayer stateData) /= playerId playData ->
if sameColour (topCard stateData) (card playData) && sameValue (topCard stateData) (card playData)
then Right $ CardPlayed playData
else Right $ PlayerPlayedAtWrongTurn playData
(UnoStartedState stateData, PlayCard playData) ->
if sameColour (topCard stateData) (card playData) || sameValue (topCard stateData) (card playData)
then
Right $
CardPlayedAndTurnBegan
playData
( id $
nextPlayer
(card playData)
(currentPlayer stateData)
)
else Right $ WrongCardPlayed playData
, evolve = \state eitherErrorEvent ->
case eitherErrorEvent of
Left _ -> EvolutionResult state
Right event -> case (state, event) of
(UnoInitialState, GameStarted initialData playerId) ->
initialResult initialData playerId
(UnoStartedState _, GameStarted initialData playerId) ->
initialResult initialData playerId
(UnoStartedState stateData, CardPlayed playData) ->
playResult stateData playData Nothing
(UnoStartedState stateData, CardPlayedAndTurnBegan playData playerId) ->
playResult stateData playData (Just playerId)
_ -> EvolutionResult state
}
where
initialResult
:: (AllowedTransition UnoTopology initialVertex 'Started)
=> InitialData
-> PlayerId
-> EvolutionResult UnoTopology UnoState initialVertex (Either GameError Event)
initialResult initialData playerId =
EvolutionResult $
UnoStartedState $
StateData
{ topCard = firstCard initialData
, currentPlayer = init playerId $ players initialData
}
playResult
:: StateData
-> PlayData
-> Maybe PlayerId
-> EvolutionResult UnoTopology UnoState 'Started (Either GameError Event)
playResult stateData playData maybePlayerId =
EvolutionResult $
UnoStartedState $
StateData
{ topCard = card playData
, currentPlayer = maybe Prelude.id set maybePlayerId $ currentPlayer stateData
}