-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTypes.hs
executable file
·306 lines (245 loc) · 10.6 KB
/
Types.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
{-|
Module : Catan.Board
Description : Contains all types used by the game and their methods
Copyright : (c) Dylan Mann, David Cao 2017
License : GPL-3
Maintainer : [email protected]
Stability : experimental
Portability : POSIX
This is a module where all game primitives are defined. The game is based on a
record that will be maintained and updated by a StateT.
-}
{-# OPTIONS_HADDOCK prune, show-extensions #-}
{-# LANGUAGE ConstraintKinds, RecordWildCards #-}
{-# OPTIONS -fwarn-tabs -fwarn-incomplete-patterns -Wall #-}
module Types(ProgressCard(..),
DevCard(..),
devCards,
Building(..),
defaultBuildings,
buildingColor,
buildingLoc,
buildingTileLocs,
buildingTiles,
Name,
Resources,
produces,
getResource,
updResource,
allResources,
emptyResources,
Players,
Player(..),
Color(..),
getPlayer,
allPlayers,
updPlayer,
makePlayers,
nextPlayer,
validPlayer,
Road,
Roads,
mkRoad,
getRoad,
defaultRoads,
newLongestRoad,
Game(..),
CatanMVars(..),
PlayerAction(..),
module Board)
where
import qualified Data.Map as Map
import Data.Map(Map)
import Data.Maybe(fromMaybe, fromJust)
import Data.List as List
import Data.Ord
import Board
import Control.Concurrent.MVar.Lifted
data Color = Blue | Red | Orange | White
deriving (Enum, Read, Show, Eq, Ord)
data Road = Road (CornerLocation, CornerLocation, Color)
deriving(Read, Show, Eq)
mkRoad :: (CornerLocation, CornerLocation, Color) -> Maybe Road
mkRoad r@(cl1, cl2, _) = if cl1 `elem` adjacentCorners cl2 then
Just (Road r) else Nothing
getRoad :: Road -> (CornerLocation, CornerLocation, Color)
getRoad (Road r) = r
type Roads = [Road]
data Building = Settlement Color CornerLocation
| City Color CornerLocation
deriving (Read, Show, Eq)
data ProgressCard = RoadBuilding
| YearOfPlenty
| Monopoly
deriving (Eq, Read, Show)
data DevCard = VictoryPoint
| Knight
| Progress ProgressCard
deriving (Read, Show, Eq)
devCards :: [DevCard]
devCards = replicate 14 Knight ++ replicate 5 VictoryPoint ++ replicate 2 (Progress RoadBuilding) ++ replicate 2 (Progress YearOfPlenty) ++ replicate 2 (Progress Monopoly)
validPlayer :: Player -> Bool
validPlayer p = all (>= 0) rs where
Resources rs = resources p
buildingColor :: Building -> Color
buildingColor (City c _) = c
buildingColor (Settlement c _) = c
buildingLoc :: Building -> CornerLocation
buildingLoc (City _ l) = l
buildingLoc (Settlement _ l) = l
buildingTileLocs :: Board -> Building -> [TileLocation]
buildingTileLocs b (City _ l) = rewardLocs $ getCorner b l
buildingTileLocs b (Settlement _ l) = rewardLocs $ getCorner b l
buildingTiles :: Board -> Building -> [Tile]
buildingTiles b (City _ l) = rewardTiles b $ getCorner b l
buildingTiles b (Settlement _ l) = rewardTiles b $ getCorner b l
type Name = String
data Resources = Resources (Map Resource Int)
deriving (Read, Eq)
emptyResources :: Resources
emptyResources = Resources $ foldr (\x -> Map.insert (toEnum x) 0) Map.empty [0..4]
data Player = Player {name::Name,
resources::Resources,
cards ::[DevCard],
knights :: Int}
deriving (Read)
newPlayer :: Name -> Player
newPlayer n = Player n emptyResources [] 0
makePlayers :: [(Color,Name)] -> Players
makePlayers l = Players (foldr add Map.empty l)
where add (c, n) m = let p = newPlayer n in
Map.insert c p m
data Players = Players (Map Color Player)
deriving (Read)
edges :: Color -> CornerLocation -> Roads -> [CornerLocation]
edges c l = foldr step []
where step :: Road -> [CornerLocation] -> [CornerLocation]
step (Road (l1, l2, c1)) acc | c == c1 && l1 == l = l2 : acc
step (Road (l1, l2, c1)) acc | c == c1 && l2 == l = l1 : acc
step _ acc = acc
uninterruptedLength :: Color -> Roads -> [CornerLocation] -> Int
uninterruptedLength _ _ [] = 0
uninterruptedLength c r (x:xs) | stop x = 1
| otherwise = 1 + uninterruptedLength c r xs
where stop hd = any (\(Road (l1, l2, c1)) -> c1 /= c && (l1 == hd || l2 == hd)) r
longestPathFrom :: Roads -> Color -> CornerLocation -> CornerLocation -> [CornerLocation]
longestPathFrom = aux [] where
aux seen roads c start end
| start == end = [start]
| notIn = []
| otherwise =
maximumBy (comparing $ uninterruptedLength c roads) $
[]:[start : aux (start : seen) roads c e end |
e <- edges c start roads, e `notElem` seen]
where notIn = all (\(Road (l1, l2, c1)) -> c1 /= c || (l1 /= start && l2 /= start
&& l1 /= end && l2 /= end)) roads
longestPath :: Color -> Roads -> [CornerLocation]
longestPath c r = maximumBy (comparing $ uninterruptedLength c r)
[longestPathFrom r c l1 l2 | l1 <- cornerIndices, l2 <- cornerIndices]
newLongestRoad :: Maybe Color -> Roads -> Maybe Color
newLongestRoad oldWinner roads =
let ps = foldr (\c -> Map.insert c (longestPath c roads)) Map.empty colors
lengths = Map.map length ps
longest = Map.findMax lengths
in
case oldWinner of
Nothing | snd longest >= 5 -> Just $ fst longest
Just c | snd longest > Map.findWithDefault 0 c lengths -> Just $ fst longest
m -> m
where colors = List.nub (map (\(Road (_,_,c)) -> c) roads)
-- Game is going to be updated via the State monad throughout the
-- execution of the program
data Game = Game {board :: Board,
players :: Players,
roads :: Roads,
buildings :: [Building],
robberTile :: TileLocation,
longestRoad :: Maybe Color,
largestArmy :: Maybe Color,
deck :: [DevCard],
currentPlayer :: Color,
errorMessage :: Maybe String,
pendingCards :: [DevCard],
mvars :: CatanMVars}
deriving (Read)
-- | methods for getting and setting resources and players, in case the
-- | implementation changes, so that the API doesn't have to
getResource :: Resource -> Resources -> Int
getResource r (Resources rs) = fromMaybe 0 $ Map.lookup r rs
updResource :: (Int -> Int) -> Resource -> Resources -> Resources
updResource f r (Resources rs) = Resources $ Map.adjust f r rs
updPlayer :: (Player -> Player) -> Color -> Players -> Players
updPlayer f c (Players ps) = Players $ Map.adjust f c ps
getPlayer :: Color -> Players -> Player
getPlayer c (Players ps) = fromJust $ Map.lookup c ps
allPlayers :: Players -> [(Color, Player)]
allPlayers (Players ps) = Map.toList ps
allResources :: Player -> [Resource]
allResources p = concatMap comb (Map.toList rs)
where (Resources rs) = resources p
comb (r, amount) = replicate amount r
produces :: Token -> Tile -> Maybe Resource
produces roll (Paying t token) | roll == token =
(Just . toEnum . fromEnum) t
produces _ _ = Nothing
-- TODO: refactor so that we can play with 3 colors
nextPlayer :: Color -> Color
nextPlayer White = Blue
nextPlayer c = succ c
defaultColorOrder :: [Color]
defaultColorOrder = [White, Blue, Orange, Blue, Red, White, Red, Orange]
defaultBuildings :: [Building]
defaultBuildings = zipWith Settlement defaultColorOrder defaultBuildingLocations
defaultRoads :: Roads
defaultRoads = map Road $ uncurry zip3 (unzip defaultRoadLocations) defaultColorOrder
instance Show Game where
show Game{..} = unlines
["Game { board = " ++ "board" ++ ",",
" players = " ++ show players ++ ",",
" roads = " ++ show roads ++ ",",
" buildings = " ++ show buildings ++ ",",
" robberTile = " ++ show robberTile ++ ",",
" longestRoad = " ++ show longestRoad ++ ",",
" largestArmy = " ++ show largestArmy ++ ",",
" deck = " ++ show deck ++ ",",
" currentPlayer = " ++ show currentPlayer ++ ",",
" error = " ++ show errorMessage,
"}"]
instance Show Players where
show (Players ps) = unlines $ map show $ Map.toList ps
instance Show Resources where
show (Resources rs) = show $ Map.toList rs
instance Show Player where
show Player{..} = unlines ["Player { name = " ++ show name,
" resources = " ++ show resources,
" cards = " ++ show cards,
" army = " ++ show knights,
"}"]
data CatanMVars = CatanMVars{nameVar :: MVar Name,
actionVar :: MVar PlayerAction,
robberVar :: MVar TileLocation,
colorVar :: MVar Color,
gameVar :: MVar Game,
rollVar :: MVar Int,
stealVar :: MVar [(Name, Color)]}
-- | show and read instances so that the game can be shown
instance Show CatanMVars where
show _ = "CatanMVars"
instance Read CatanMVars where
readsPrec _ = undefined
-- | Player Actions that can be recieved by the server from the UI thread
-- | after a `NextMove` `Request`
data PlayerAction = BuildRoad CornerLocation CornerLocation
| BuildCity CornerLocation
| BuildSettlement CornerLocation
| PlayMonopoly Resource
| PlayYearOfPlenty Resource Resource
| PlayRoadBuilding CornerLocation CornerLocation CornerLocation CornerLocation
| PlayKnight
| BuyCard
| TradeWithBank Resource Resource Int
| TradeWithPlayer [Resource] Color [Resource]
| EndTurn
| EndGame
| Cheat [Resource]
deriving(Read, Show, Eq)