From acd9a60a2232cea6efceee64d1c442d19c52235d Mon Sep 17 00:00:00 2001 From: Frederick Yin Date: Thu, 5 Jan 2023 21:27:27 +0800 Subject: prompt returns IO (Maybe Card), turn is skippable --- Game.hs | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) (limited to 'Game.hs') diff --git a/Game.hs b/Game.hs index 27d7804..77b2e92 100644 --- a/Game.hs +++ b/Game.hs @@ -17,11 +17,11 @@ data Game = Game { players :: [Player] , discardPile :: [Card] } -prompt :: Game -> IO Card +prompt :: Game -> IO (Maybe Card) prompt game@(Game plyrs pidx att _ _ _) = do let player = plyrs !! pidx let cards = P.cards player - putStrLn $ P.name player ++ "'s turn" + putStrLn $ P.name player ++ "'s turn (input 0 to skip turn and draw card)" putStrLn $ P.showCards player cardIdxStr <- getLine if any (== False) $ map isDigit cardIdxStr @@ -30,8 +30,12 @@ prompt game@(Game plyrs pidx att _ _ _) = do prompt game else do let cardIdx = read cardIdxStr - 1 - if cardIdx < 0 || cardIdx >= length cards + if cardIdx == -1 then do - putStrLn "This card does not exist, try again" - prompt game - else return $ cards !! cardIdx + return Nothing + else do + if cardIdx < 0 || cardIdx >= length cards + then do + putStrLn "This card does not exist, try again" + prompt game + else return $ Just (cards !! cardIdx) -- cgit v1.2.3