From cf97c134b2bff6672171ac05d05b399085f9ad3c Mon Sep 17 00:00:00 2001 From: Frederick Yin Date: Mon, 9 Jan 2023 15:24:25 +0800 Subject: Allow regular card once attack is done; Auto mode --- Card.hs | 8 ++++---- Game.hs | 15 ++++++++++++--- Player.hs | 6 +++--- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/Card.hs b/Card.hs index 98e76fe..c674b5d 100644 --- a/Card.hs +++ b/Card.hs @@ -54,8 +54,8 @@ isAttack (Card _ rk) | otherwise = False -- | Check if `card` is valid after `prev`. --- | If `prev` attacks, `card` must be special. -isValid :: Card -> Card -> Bool -isValid prev@(Card st' rk') card@(Card st rk) = - match && (not (isAttack prev) || isSpecial card) +-- | If `att > 1`, player is under attack and `card` must be special. +isValid :: Card -> Int -> Card -> Bool +isValid prev@(Card st' rk') att card@(Card st rk) = + match && (att < 2 || isSpecial card) where match = (st == st') || (rk == rk') diff --git a/Game.hs b/Game.hs index 275b951..ea327a9 100644 --- a/Game.hs +++ b/Game.hs @@ -33,13 +33,14 @@ beginTurn game@(Game plyrs pidx att dir prev stock disc) = do putStrLn $ replicate 80 '-' putStrLn $ P.name player ++ "'s turn (input 0 to skip turn and draw card)" - putStrLn $ P.showCards player prev + putStrLn $ P.showCards player prev att putStrLn $ "Stock: " ++ (show $ length stock) ++ ", Discard: " ++ (show $ length disc) putStrLn $ "Current attack: " ++ show att putStrLn $ "Prev card: " ++ C.showCard prev - decision <- prompt game + -- decision <- prompt game + let decision = auto game game' <- case decision of Nothing -> drawAndSkip game Just card -> shedAndContinue card game @@ -112,12 +113,20 @@ prompt game@(Game plyrs pidx att _ prev _ _) = do prompt game else do let card = cards !! cardIdx - if C.isValid prev card + if C.isValid prev att card then return $ Just card else do putStrLn "You cannot play this card, try again" prompt game +-- | Make an automated decision to draw/shed card. +auto :: Game -> Maybe Card +auto game@(Game plyrs pidx att _ prev _ _) = + if null validCards + then Nothing + else Just (head validCards) + where validCards = filter (C.isValid prev att) $ P.cards (plyrs !! pidx) + -- | Deal c cards to each player in game. dealCards :: Int -> Game -> Game dealCards c game@(Game plyrs _ _ _ _ stock disc) diff --git a/Player.hs b/Player.hs index 6f2804d..5aad025 100644 --- a/Player.hs +++ b/Player.hs @@ -25,11 +25,11 @@ draw :: Player -> [Card] -> Player draw (Player n p cs) cs' = Player n p $ L.sort (cs' ++ cs) -- | Show player's cards, e.g. "1. Spade 2" -showCards :: Player -> Card -> String -showCards (Player _ _ cs) prev = +showCards :: Player -> Card -> Int -> String +showCards (Player _ _ cs) prev att = unlines $ zipWith joinAndColorize [1..] cs where joinAndColorize n c = - if C.isValid prev c + if C.isValid prev att c then if C.isSpecial c then Color.green $ join n c else join n c -- cgit v1.2.3