summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFrederick Yin <fkfd@fkfd.me>2023-01-09 11:08:02 +0800
committerFrederick Yin <fkfd@fkfd.me>2023-01-09 11:08:02 +0800
commitd034b9c9273a7f3ff91554c4b605dd488207caa1 (patch)
tree2eb992ddb5a35371ea355050f9d83e7a5eccf4ef
parent552b8a0a8ed89fdc85459037f19ae0a13bfe6702 (diff)
Display player's cards in color
-rw-r--r--Color.hs13
-rw-r--r--Game.hs2
-rw-r--r--Player.hs14
3 files changed, 25 insertions, 4 deletions
diff --git a/Color.hs b/Color.hs
new file mode 100644
index 0000000..1d63fcd
--- /dev/null
+++ b/Color.hs
@@ -0,0 +1,13 @@
+module Color where
+
+red :: String -> String
+red s = "\x001b[31m" ++ s ++ "\x001b[0m"
+
+green :: String -> String
+green s = "\x001b[32m" ++ s ++ "\x001b[0m"
+
+yellow :: String -> String
+yellow s = "\x001b[33m" ++ s ++ "\x001b[0m"
+
+blue :: String -> String
+blue s = "\x001b[34m" ++ s ++ "\x001b[0m"
diff --git a/Game.hs b/Game.hs
index b9ad8bd..69c1d08 100644
--- a/Game.hs
+++ b/Game.hs
@@ -33,7 +33,7 @@ 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
+ putStrLn $ P.showCards player prev
putStrLn $ "Current attack: " ++ show att
putStrLn $ "Prev card: " ++ C.showCard prev
diff --git a/Player.hs b/Player.hs
index 6dab74c..6f2804d 100644
--- a/Player.hs
+++ b/Player.hs
@@ -3,6 +3,7 @@ module Player where
import qualified Data.List as L
import Card (Card)
import qualified Card as C
+import qualified Color
data Player = Player { name :: String
, penalty :: Integer
@@ -24,6 +25,13 @@ 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 -> String
-showCards (Player _ _ cs) =
- unlines $ zipWith (\i c -> (show i) ++ ". " ++ C.showCard c) [1..] cs
+showCards :: Player -> Card -> String
+showCards (Player _ _ cs) prev =
+ unlines $ zipWith joinAndColorize [1..] cs
+ where joinAndColorize n c =
+ if C.isValid prev c
+ then if C.isSpecial c
+ then Color.green $ join n c
+ else join n c
+ else Color.red $ join n c
+ join n c = (show n) ++ ". " ++ (C.showCard c)