2018-03-10 23:25:44 +01:00
|
|
|
{-# LANGUAGE NamedFieldPuns #-}
|
|
|
|
module Hanafuda.Player where
|
|
|
|
|
|
|
|
import Hanafuda (Card, Pack, contains, packOfCards, remove)
|
2018-03-15 22:32:24 +01:00
|
|
|
import Data.Map (Map, fromList)
|
|
|
|
import System.Random (Random(..))
|
2018-03-10 23:25:44 +01:00
|
|
|
|
2018-03-15 22:32:24 +01:00
|
|
|
data Seat =
|
2018-03-10 23:25:44 +01:00
|
|
|
Player1
|
|
|
|
| Player2
|
2018-03-15 22:32:24 +01:00
|
|
|
deriving (Eq, Ord, Show, Enum)
|
2018-03-10 23:25:44 +01:00
|
|
|
|
2018-03-15 22:32:24 +01:00
|
|
|
instance Random Seat where
|
|
|
|
randomR (lo, hi) g =
|
|
|
|
let (n, g') = randomR (fromEnum lo, fromEnum hi) g in
|
|
|
|
(toEnum n, g')
|
|
|
|
|
|
|
|
random = randomR (Player1, Player2)
|
|
|
|
|
|
|
|
next :: Seat -> Seat
|
2018-03-10 23:25:44 +01:00
|
|
|
next Player1 = Player2
|
|
|
|
next _ = Player1
|
|
|
|
|
2018-03-15 22:32:24 +01:00
|
|
|
data Player a = Player {
|
2018-03-10 23:25:44 +01:00
|
|
|
hand :: Pack
|
|
|
|
, meld :: Pack
|
|
|
|
, yakus :: a
|
|
|
|
} deriving (Show)
|
2018-03-15 22:32:24 +01:00
|
|
|
type Players a = Map Seat (Player a)
|
2018-03-10 23:25:44 +01:00
|
|
|
|
2018-03-15 22:32:24 +01:00
|
|
|
deal :: [a] -> Map Seat a
|
2018-03-10 23:25:44 +01:00
|
|
|
deal = fromList . zip [Player1, Player2]
|
|
|
|
|
2018-03-15 22:32:24 +01:00
|
|
|
new :: Monoid a => [Card] -> Player a
|
|
|
|
new cards = Player {
|
2018-03-10 23:25:44 +01:00
|
|
|
hand = packOfCards cards
|
|
|
|
, meld = packOfCards []
|
|
|
|
, yakus = mempty
|
|
|
|
}
|
|
|
|
|
2018-03-15 22:32:24 +01:00
|
|
|
plays :: Player a -> Card -> Either String (Player a)
|
|
|
|
plays player@(Player {hand}) card =
|
2018-03-10 23:25:44 +01:00
|
|
|
if hand `contains` card
|
|
|
|
then Right $ player {hand = remove hand card}
|
|
|
|
else Left "You don't have this card"
|
|
|
|
|
|
|
|
type Points = Int
|
|
|
|
|
2018-03-15 22:32:24 +01:00
|
|
|
score :: (a -> Points) -> Player a -> Int
|
2018-03-10 23:25:44 +01:00
|
|
|
score rater = rater . yakus
|
|
|
|
|
2018-03-15 22:32:24 +01:00
|
|
|
type Scores = Map Seat Points
|