summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFrederick Yin <fkfd@fkfd.me>2023-01-09 15:43:42 +0800
committerFrederick Yin <fkfd@fkfd.me>2023-01-09 15:43:42 +0800
commitc7d4746542303188bcbd081519f3893dc60ecf27 (patch)
tree3d3956da802bc7f915752ad5d3ef6c6fa210b3e3
parentcf97c134b2bff6672171ac05d05b399085f9ad3c (diff)
Auto mode can be toggled
-rw-r--r--Game.hs27
-rw-r--r--Main.hs2
2 files changed, 15 insertions, 14 deletions
diff --git a/Game.hs b/Game.hs
index ea327a9..5de5ce5 100644
--- a/Game.hs
+++ b/Game.hs
@@ -17,18 +17,18 @@ data Game = Game { players :: [Player]
, discardPile :: [Card]
}
--- | Begin new round of game.
-beginRounds :: Int -> Game -> IO Game
-beginRounds n game = do
- if n == 0
+-- | Begin `r` rounds of game.
+beginRounds :: Int -> Bool -> Game -> IO Game
+beginRounds r auto game = do
+ if r == 0
then return game
else do
- game' <- beginTurn game
- beginRounds (n - 1) game'
+ game' <- beginTurn auto game
+ beginRounds (r - 1) auto game'
-- | Let current player take their turn.
-beginTurn :: Game -> IO Game
-beginTurn game@(Game plyrs pidx att dir prev stock disc) = do
+beginTurn :: Bool -> Game -> IO Game
+beginTurn auto game@(Game plyrs pidx att dir prev stock disc) = do
let player = plyrs !! pidx
putStrLn $ replicate 80 '-'
@@ -39,15 +39,16 @@ beginTurn game@(Game plyrs pidx att dir prev stock disc) = do
putStrLn $ "Current attack: " ++ show att
putStrLn $ "Prev card: " ++ C.showCard prev
- -- decision <- prompt game
- let decision = auto game
+ decision <- if auto
+ then return $ automate game
+ else prompt game
game' <- case decision of
Nothing -> drawAndSkip game
Just card -> shedAndContinue card game
if playerIdx game' == pidx -- shedAndContinue does this when player wins
then return game'
- else beginTurn game'
+ else beginTurn auto game'
-- | Game state after player draws card(s) and skips turn.
drawAndSkip :: Game -> IO Game
@@ -120,8 +121,8 @@ prompt game@(Game plyrs pidx att _ prev _ _) = do
prompt game
-- | Make an automated decision to draw/shed card.
-auto :: Game -> Maybe Card
-auto game@(Game plyrs pidx att _ prev _ _) =
+automate :: Game -> Maybe Card
+automate game@(Game plyrs pidx att _ prev _ _) =
if null validCards
then Nothing
else Just (head validCards)
diff --git a/Main.hs b/Main.hs
index aaae87c..70c9df7 100644
--- a/Main.hs
+++ b/Main.hs
@@ -34,5 +34,5 @@ main = do
, discardPile = discard
}
- beginRounds 1 $ dealCards 6 game
+ beginRounds 1 False $ dealCards 6 game
print ()