summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFrederick Yin <fkfd@fkfd.me>2023-01-08 17:28:11 +0800
committerFrederick Yin <fkfd@fkfd.me>2023-01-08 17:28:11 +0800
commitec0654bf0ca057affb37a20ed0588a907e71130f (patch)
tree41fe6b5dc1cb5b579d4d728d07a72bdf0a5f683d
parentd82c464c395887afd5ed7d143d486e614e91ee26 (diff)
Game ends when player sheds all cards
-rw-r--r--Game.hs16
-rw-r--r--Main.hs4
2 files changed, 17 insertions, 3 deletions
diff --git a/Game.hs b/Game.hs
index ad3d8bc..203b66f 100644
--- a/Game.hs
+++ b/Game.hs
@@ -17,6 +17,15 @@ data Game = Game { players :: [Player]
, discardPile :: [Card]
}
+-- | Begin new round of game.
+nextRound :: Int -> Game -> IO Game
+nextRound n game = do
+ if n == 0
+ then return game
+ else do
+ game' <- nextTurn game
+ nextRound (n - 1) game'
+
-- | Let current player take their turn.
nextTurn :: Game -> IO Game
nextTurn game@(Game plyrs pidx att dir stock disc) = do
@@ -34,12 +43,17 @@ nextTurn game@(Game plyrs pidx att dir stock disc) = do
let (left, right) = splitAt pidx plyrs
let plyrs' = left ++ [player'] ++ (tail right)
let pidx' = (pidx + 1) `mod` (length plyrs)
+ let game' = Game plyrs' pidx' att dir stock' disc'
+
case decision of
Nothing -> do
putStrLn $ (P.name player) ++ " draws " ++ (C.showCard $ head stock)
Just card -> do
putStrLn $ (P.name player) ++ " plays " ++ (C.showCard card)
- nextTurn (Game plyrs' pidx' att dir stock' disc')
+
+ if null $ P.cards player'
+ then return game'
+ else nextTurn game'
-- | Prompt player to play a card (or draw card and skip turn).
prompt :: Game -> IO (Maybe Card)
diff --git a/Main.hs b/Main.hs
index 685c43b..fd10cdf 100644
--- a/Main.hs
+++ b/Main.hs
@@ -10,7 +10,7 @@ import Game (Game(..),
discardPile,
prompt,
dealCards,
- nextTurn,
+ nextRound,
)
import Player (Player(..))
import qualified Player as P
@@ -34,5 +34,5 @@ main = do
, discardPile = discard
}
- nextTurn $ dealCards 6 game
+ nextRound 1 $ dealCards 1 game
print ()