From c7d4746542303188bcbd081519f3893dc60ecf27 Mon Sep 17 00:00:00 2001 From: Frederick Yin Date: Mon, 9 Jan 2023 15:43:42 +0800 Subject: Auto mode can be toggled --- Game.hs | 27 ++++++++++++++------------- Main.hs | 2 +- 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 () -- cgit v1.2.3