summaryrefslogtreecommitdiff
path: root/Player.hs
blob: 5aad025937e9469f153fe8022d8bba967e593409 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
                     , cards :: [Card]
                     }

-- | Update `i`-th player in `ps` to `p`.
update :: [Player] -> Int -> Player -> [Player]
update ps i p =
    let (left, right) = splitAt i ps
     in left ++ [p] ++ (tail right)

-- | Same player after shedding card specified.
shed :: Player -> Card -> Player
shed (Player n p cs) c = Player n p $ L.delete c cs

-- | Same player after drawing cards specified.
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 -> Int -> String
showCards (Player _ _ cs) prev att =
    unlines $ zipWith joinAndColorize [1..] cs
        where joinAndColorize n c =
                  if C.isValid prev att 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)