{-# LANGUAGE CPP, MultiParamTypeClasses, FlexibleInstances, Safe, DeriveGeneric, RecordWildCards #-}
module Game.Hanabi(
main, selfplay, start, createGame, startFromCards, createGameFromCards, run, createDeck,
prettyEndGame, isMoveValid, checkEndGame, help,
Strategies, Strategy(..), StrategyDict(..), mkSD, DynamicStrategy, mkDS, mkDS', Verbose(..), STDIO, stdio, Blind, ViaHandles(..), Verbosity(..), verbose, quiet,
Peeker, peek,
GameSpec(..), defaultGS, Rule(..), defaultRule, isRuleValid, colors, handSize, setHandSize,
Move(..), Index, State(..), PrivateView(..), PublicInfo(..), Result(..), EndGame(..),
Card(..), Color(..), Number(..), Marks, Possibilities, cardToInt, intToCard, readsColorChar, readsNumberChar,
isCritical, isUseless, bestPossibleRank, achievedRank, isPlayable, isHinted, currentScore, achievableScore,
isMoreObviouslyUseless, isObviouslyUseless, isDefinitelyUseless, isMoreObviouslyPlayable, isObviouslyPlayable, isDefinitelyPlayable, obviousChopss, definiteChopss, isDoubleDrop,
tryMove, (|||), ifA,
what'sUp, what'sUp1, ithPlayer, recentEvents, prettyPI, prettySt, ithPlayerFromTheLast, view, replaceNth, shuffle, showPossibilities, showColorPossibilities, showNumberPossibilities, showTrial) where
import qualified Data.IntMap as IM
import System.Random
import Control.Applicative((<*>))
import Control.Monad(when)
import Control.Monad.IO.Class(MonadIO, liftIO)
import Data.Char(isSpace, isAlpha, isAlphaNum, toLower, toUpper)
import Data.Maybe(fromJust)
import Data.List(isPrefixOf, group, zipWith4)
import System.IO
import Data.Dynamic
import Data.Bits hiding (rotate)
import GHC.Generics hiding (K1)
data Number = Empty | K1 | K2 | K3 | K4 | K5 deriving (Eq, Ord, Show, Read, Enum, Bounded, Generic)
data Color = White | Yellow | Red | Green | Blue | Multicolor
deriving (Eq, Show, Read, Enum, Bounded, Generic)
readsColorChar :: ReadS Color
readsColorChar (c:str)
| isSpace c = readsColorChar str
| otherwise = case lookup (toUpper c) [(head $ show i, i) | i <- [White .. Multicolor]] of
Nothing -> []
Just i -> [(i, str)]
readsColorChar [] = []
readsNumberChar :: ReadS Number
readsNumberChar ('0':rest) = [(Empty,rest)]
readsNumberChar str = reads ('K':str)
data Card = C {color :: Color, number :: Number} deriving (Eq, Generic)
instance Show Card where
showsPrec _ (C color number) = (head (show color) :) . (show (fromEnum number) ++)
instance Read Card where
readsPrec _ str = [(C i k, rest) | (i, xs) <- readsColorChar str, (k, rest) <- readsNumberChar xs]
cardToInt :: Card -> Int
cardToInt c = fromEnum (color c) * (succ $ fromEnum (maxBound::Number)) + fromEnum (number c)
intToCard :: Int -> Card
intToCard i = case i `divMod` (succ $ fromEnum (maxBound::Number)) of (c,k) -> C (toEnum c) (toEnum k)
type Index = Int
data Move = Drop {index::Index}
| Play {index::Index}
| Hint Int (Either Color Number)
deriving (Eq, Generic)
instance Show Move where
showsPrec _ (Drop i) = ("Drop"++) . shows i
showsPrec _ (Play i) = ("Play"++) . shows i
showsPrec _ (Hint i eith) = ("Hint"++) . shows i . (either (\c -> (take 1 (show c) ++)) (\k -> tail . shows k) eith)
instance Read Move where
readsPrec _ str
= let (cmd,other) = span (not.isSpace) str'
str' = dropWhile isSpace str
in case span (not . (`elem` "dDpP")) cmd of
(tk, d:dr) | all (not.isAlphaNum) tkdr && null (drop 1 $ group tkdr) -> [((if toLower d == 'd' then Drop else Play) $ length tk, other)]
where tkdr = tk++dr
_ -> case span isAlpha str' of
(kw, xs) | kwl `isPrefixOf` "hint" -> parseHint xs
| kwl `isPrefixOf` "drop" -> [(Drop i, rest) | (i, rest) <- reads xs]
| kwl `isPrefixOf` "play" -> [(Play i, rest) | (i, rest) <- reads xs]
where kwl = map toLower kw
_ -> []
where parseHint xs = [(Hint i eith, rest) | let (istr, ys) = splitAt 1 $ dropWhile isSpace xs
, (i, _) <- reads istr
, let ys' = dropWhile isSpace ys
, (eith, rest) <- [ (Left c, zs) | (c,zs) <- readsColorChar ys' ] ++ [ (Right c, zs) | (c,zs) <- readsNumberChar ys' ] ]
help :: String
help = "`Play0', `play0', `P0', `p0', etc. ... play the 0th card from the left (0-origin).\n"
++"`Drop1', `drop1', `D1', `d1', etc. ... drop the 1st card from the left (0-origin).\n"
++"`Hint2W', `hint2w', `h2w', `H2W', `2w', etc. ... tell the White card(s) of the 2nd next player.\n"
++"`Hint14', `h14', `H14', `14', etc. ... tell the Rank-4 card(s) of the next player.\n"
++"`---P-', `@@@p@', `___P', `...p', etc. ... play the 3rd card from the left (0-origin). Letters other than p or P must not be alphanumeric. Also note that just `p' or `P' means playing the 0th card.\n"
++"`D////', `d~~~~', `D', `d', etc. ... drop the 0th card from the left (0-origin). Letters other than d or D must not be alphanumeric.\n"
data Rule = R { numBlackTokens :: Int
, funPlayerHand :: [Int]
, numColors :: Int
, prolong :: Bool
, earlyQuit :: Bool
, numMulticolors :: [Int]
} deriving (Show, Read, Eq, Generic)
isRuleValid :: Rule -> Bool
isRuleValid rl@R{..} = numBlackTokens > 0 && and [ h>0 && h<=mh | (h,mh) <- zip funPlayerHand maxPlayerHand ] && numColors>0 && numColors <=6 && (numColors < 6 || all (>0) numMulticolors)
where maxPlayerHand = [ succ (numberOfCards rl) `div` numP | numP <- [2..]]
defaultRule :: Rule
defaultRule = R { numBlackTokens = 3
, funPlayerHand = [5,5]++take 8 (repeat 4)
, numColors = 5
, prolong = False
, earlyQuit = False
, numMulticolors = replicate 5 0
}
defaultGS :: GameSpec
defaultGS = GS{numPlayers=2, rule=defaultRule}
numberOfCards :: Rule -> Int
numberOfCards rl = sum (take (numColors rl) $ [10,10,10,10,10]++[sum (numMulticolors rl)])
initialPileNum :: GameSpec -> Int
initialPileNum gs = numberOfCards (rule gs)
- handSize gs * numPlayers gs
handSize :: GameSpec -> Int
handSize GS{..} = (funPlayerHand rule ++ repeat 1) !! (numPlayers - 2)
setHandSize :: GameSpec -> Int -> Rule
setHandSize GS{..} n = rule{funPlayerHand = snd $ replaceNth (numPlayers - 2) n $ funPlayerHand rule}
data GameSpec = GS {numPlayers :: Int, rule :: Rule} deriving (Read, Show, Eq, Generic)
data State = St { publicState :: PublicInfo
, pile :: [Card]
, hands :: [[Card]]
} deriving (Read, Show, Eq, Generic)
data PublicInfo = PI { gameSpec :: GameSpec
, pileNum :: Int
, played :: IM.IntMap Number
, discarded :: IM.IntMap Int
, nonPublic :: IM.IntMap Int
, turn :: Int
, lives :: Int
, hintTokens :: Int
, deadline :: Maybe Int
, givenHints :: [[Marks]]
, possibilities :: [[Possibilities]]
, result :: Result
} deriving (Read, Show, Eq, Generic)
type Marks = (Maybe Color, Maybe Number)
type Possibilities = (Int, Int)
bestPossibleRank :: PublicInfo -> Color -> Number
bestPossibleRank pub iro = toEnum $ length $ takeWhile (/=0) $ zipWith subtract (numEachCard (gameSpec pub) iro)
(map ((discarded pub IM.!) . cardToInt . C iro) [K1 .. K5])
numEachCard :: GameSpec -> Color -> [Int]
numEachCard gs iro = if iro==Multicolor then numMulticolors $ rule gs else [3,2,2,2,1]
isUseless :: PublicInfo -> Card -> Bool
isUseless pub card = number card <= achievedRank pub (color card)
|| number card > bestPossibleRank pub (color card)
isCritical :: PublicInfo -> Card -> Bool
isCritical pub card = not (isUseless pub card)
&& succ (discarded pub IM.! cardToInt card) == (numEachCard (gameSpec pub) (color card) !! (pred $ fromEnum $ number card))
isPlayable :: PublicInfo -> Card -> Bool
isPlayable pub card = pred (number card) == achievedRank pub (color card)
isHinted :: Marks -> Bool
isHinted = not . (==(Nothing, Nothing))
isMostObviouslyPlayable :: PublicInfo -> Marks -> Bool
isMostObviouslyPlayable pub (Just c, Just n) = isPlayable pub $ C c n
isMostObviouslyPlayable _ _ = False
isMoreObviouslyPlayable :: PublicInfo -> Marks -> Bool
isMoreObviouslyPlayable pub = iOP (nonPublic pub) pub
isObviouslyPlayable :: PublicInfo -> Possibilities -> Bool
isObviouslyPlayable pub (pc,pn) = all (\card -> (nonPublic pub IM.! cardToInt card) == 0 || isPlayable pub card)
[ C color number | color <- colorPossibilities pc, number <- numberPossibilities pn ]
isDefinitelyPlayable :: PrivateView -> Marks -> Possibilities -> Bool
isDefinitelyPlayable pv (Just c, Just n) _ = isPlayable (publicView pv) $ C c n
isDefinitelyPlayable pv _ (pc,pn) = all (\card -> (invisibleBag pv IM.! cardToInt card) == 0 || isPlayable (publicView pv) card)
[ C color number | color <- colorPossibilities pc, number <- numberPossibilities pn ]
iOP :: IM.IntMap Int -> PublicInfo -> (Maybe Color, Maybe Number) -> Bool
iOP _ pub (Just c, Just n) = isPlayable pub $ C c n
iOP bag pub (Nothing,Just n) = all (\card -> (bag IM.! cardToInt card) == 0 || isPlayable pub card) [ C color n | color <- colors pub ]
iOP _ _ _ = False
isMoreObviouslyUseless :: PublicInfo -> Marks -> Bool
isMoreObviouslyUseless pub (Just c, Just n) = isUseless pub $ C c n
isMoreObviouslyUseless pub (Just c, Nothing) = bestPossibleRank pub c == achievedRank pub c
isMoreObviouslyUseless pub (Nothing, Just n) = all (\c -> n <= achievedRank pub c || bestPossibleRank pub c < n) $ colors pub
isMoreObviouslyUseless _ (Nothing, Nothing) = False
isObviouslyUseless :: PublicInfo -> Possibilities -> Bool
isObviouslyUseless pub (pc,pn) = all (\card@(C c n) -> n <= achievedRank pub c || bestPossibleRank pub c < n || (nonPublic pub IM.! cardToInt card) == 0 )
[ C color number | color <- colorPossibilities pc, number <- numberPossibilities pn ]
isDefinitelyUseless :: PrivateView -> Marks -> Possibilities -> Bool
isDefinitelyUseless pv (Just c, Just n) _ = isUseless (publicView pv) $ C c n
isDefinitelyUseless pv _ (pc,pn) = all (\card@(C c n) -> n <= achievedRank pub c || bestPossibleRank pub c < n || (invisibleBag pv IM.! cardToInt card) == 0 )
[ C color number | color <- colorPossibilities pc, number <- numberPossibilities pn ]
where pub = publicView pv
choppiri :: [Marks] -> [(Index, Marks)]
choppiri = reverse . filter (not . isHinted . snd) . zip [0..]
definiteChopss :: PrivateView -> [Marks] -> [Possibilities] -> [[Index]]
definiteChopss pv hls ps = (if null useless then id else (useless :)) $ map ((:[]) . fst) (choppiri hls)
where useless = map fst $ filter (uncurry (isDefinitelyUseless pv) . snd) (zip [0..] $ zip hls ps)
obviousChopss :: PublicInfo -> [Marks] -> [Possibilities] -> [[Index]]
obviousChopss pub hls ps = (if null useless then id else (useless :)) $ map ((:[]) . fst) (choppiri hls)
where useless = map fst $ filter (isObviouslyUseless pub . snd) (zip [0..] ps)
chops :: PublicInfo -> [Marks] -> [Possibilities] -> [Index]
chops pub hls ps = concat $ map reverse $ obviousChopss pub hls ps
isDoubleDrop :: PrivateView -> Result -> [Index] -> (Int, Int) -> Bool
isDoubleDrop pv@PV{publicView=pub} (Discard c@C{..}) [_i] (pc,pn) = not (any (==(Just color, Just number)) myHints) &&
isCritical pub c &&
color `elem` colorPossibilities pc &&
number `elem` numberPossibilities pn &&
(invisibleBag pv IM.! cardToInt c) > 0
where myHints = head $ givenHints pub
isDoubleDrop _pv _lastresult _chopset _ps = False
colors :: PublicInfo -> [Color]
colors pub = take (numColors $ rule $ gameSpec pub) [minBound .. maxBound]
achievedRank :: PublicInfo -> Color -> Number
achievedRank pub k = case IM.lookup (fromEnum k) (played pub) of
Just n -> n
#ifdef DEBUG
Nothing | numColors (rule $ gameSpec pub) <= k -> error "requesting invalid color."
| otherwise -> error "PublicInfo is not initialized."
#else
Nothing -> Empty
#endif
currentScore :: PublicInfo -> Int
currentScore pub = sum [ fromEnum $ achievedRank pub k | k <- colors pub ]
achievableScore :: PublicInfo -> Int
achievableScore pub = sum [ fromEnum $ bestPossibleRank pub k | k <- colors pub ]
tryMove :: PrivateView -> Move -> Move -> Move
tryMove pv m alt | isMoveValid pv m = m
| otherwise = alt
(|||) :: (PrivateView -> Move) -> (PrivateView -> Move) -> PrivateView -> Move
a ||| b = tryMove <*> a <*> b
ifA :: (PrivateView -> Bool) -> (PrivateView -> Move) -> (PrivateView -> Move) -> PrivateView -> Move
ifA pred at af = (\pv t f -> if pred pv then t else f) <*> at <*> af
data Result = None
| Discard Card | Success Card | Fail Card deriving (Read, Show, Eq, Generic)
data PrivateView = PV { publicView :: PublicInfo
, handsPV :: [[Card]]
, invisibleBag :: IM.IntMap Int
} deriving (Generic)
instance Show PrivateView where
showsPrec p (PV pub h _) = showsPrec p (pub,h)
instance Read PrivateView where
readsPrec p str = [ (mkPV pub hs, rest) | ((pub,hs), rest) <- readsPrec p str ]
instance Eq PrivateView where
PV pub1 hs1 _ == PV pub2 hs2 _ = (pub1,hs1) == (pub2,hs2)
mkPV :: PublicInfo -> [[Card]] -> PrivateView
mkPV pub hs = PV pub hs $ foldr (IM.update (Just . pred)) (nonPublic pub) $ map cardToInt $ concat $ [ C c n | (Just c, Just n) <- head $ givenHints pub ] : hs
prettyPV :: Verbosity -> PrivateView -> String
prettyPV v pv@PV{publicView=pub} = prettyPI pub ++ "\nMy hand:\n"
++ concat (replicate (length myHand) $ wrap "+--+") ++ "\n"
++ concat (replicate (length myHand) $ wrap "|**|") ++ "\n"
++ (if markHints v then showHintLine wrap myHand else "")
++ concat [ wrap $
'+':(if warnDoubleDrop v && isDoubleDrop pv (result pub) chopSet p && d `elem` chopSet then ('D':) else
if markChops v && d `elem` chopSet then ('X':) else shows d)
(if markObviouslyUseless v && isDefinitelyUseless pv h p then ".+" else
if markObviouslyPlayable v && isDefinitelyPlayable pv h p then "^+" else "-+") | (d,h,p) <- zip3 [0..] myHand myPos ]
++"\n"
++ (if markPossibilities v then showPosLines $ head $ possibilities pub else "")
++ concat (zipWith4 (prettyHand v pub (ithPlayer $ numPlayers $ gameSpec pub)) [1..] (handsPV pv) (tail $ givenHints pub) (tail $ possibilities pub))++"\n"
where myHand = head (givenHints pub)
myPos = head (possibilities pub)
wrap xs | markPossibilities v = " "++xs++" "
| otherwise = xs
chopSet = concat $ take 1 $ definiteChopss pv myHand myPos
prettySt :: (Int -> Int -> String) -> State -> String
prettySt ithP st@St{publicState=pub} = prettyPI pub ++ concat (zipWith4 (prettyHand verbose pub (ithP $ numPlayers $ gameSpec pub)) [0..] (hands st) (givenHints pub) (possibilities pub))
verbose, quiet :: Verbosity
verbose = V{warnCritical=True, markUseless=True, markPlayable=True, markObviouslyUseless=True, markObviouslyPlayable=True, markHints=True, markPossibilities=True, markChops=True, warnDoubleDrop=True}
quiet = V{warnCritical=False,markUseless=False,markPlayable=False,markObviouslyUseless=False,markObviouslyPlayable=False,markHints=False,markPossibilities=False,markChops=False,warnDoubleDrop=False}
prettyHand :: Verbosity -> PublicInfo -> (Int->String) -> Int -> [Card] -> [Marks] -> [Possibilities] -> String
prettyHand v pub ithPnumP i cards hl ps = "\n\n" ++ ithPnumP i ++ " hand:\n"
++ concat [ wrap $
if markUseless v && isUseless pub card then "+..+"
else case (warnCritical v && tup==(Nothing,Nothing) && isCritical pub card, markPlayable v && isPlayable pub card) of
(True, True) -> "+!^+"
(True, False) -> "+!!+"
(False,True) -> "+-^+"
(False,False) -> "+--+"
| (card, tup) <- zip cards hl ] ++"\n"
++ concat [ wrap $ '|':shows card "|" | card <- cards ] ++"\n"
++ (if markHints v then showHintLine wrap hl else "")
++ concat [ wrap $
'+':(if markChops v && d `elem` (concat $ take 1 $ obviousChopss pub hl ps) then ('X':) else ('-':))
(if markObviouslyUseless v && isObviouslyUseless pub h then ".+" else
if markObviouslyPlayable v && isObviouslyPlayable pub h then "^+" else "-+") | (d,h) <- zip [0..] ps ]++"\n"
++ (if markPossibilities v then showPosLines ps else "")
where wrap xs | markPossibilities v = " "++xs++" "
| otherwise = xs
showHintLine :: (String -> String) -> [Marks] -> String
showHintLine wrapper hl = concat [ wrapper $ '|' : maybe ' ' (head . show) mc : maybe ' ' (head . show . fromEnum) mk : "|" | (mc,mk) <- hl] ++ "\n"
showPosLines :: [Possibilities] -> String
showPosLines ps = concat [ ' ' : showColorPossibilities cs | (cs,_) <- ps] ++ "\n"
++ concat [ showNumberPossibilities ns ++" " | (_,ns) <- ps]
showColorPossibilities, showNumberPossibilities :: Int -> String
showColorPossibilities = reverse . showPossibilities ' ' colorkeys
showNumberPossibilities = reverse . showPossibilities ' ' "54321 "
colorkeys :: String
colorkeys = map (head . show) [maxBound, pred maxBound .. minBound::Color]
showPossibilities :: a -> [a] -> Int -> [a]
showPossibilities _ [] _ = []
showPossibilities blank (x:xs) pos = (if odd pos then x else blank) : showPossibilities blank xs (pos `div` 2)
colorPossibilities :: Int -> [Color]
colorPossibilities = concat . showPossibilities [] (map (:[]) [maxBound, pred maxBound .. minBound])
numberPossibilities :: Int -> [Number]
numberPossibilities = concat . showPossibilities [] (map (:[]) [K5,K4 .. K1])
data Verbosity = V { warnCritical :: Bool
, markUseless :: Bool
, markPlayable :: Bool
, markObviouslyUseless :: Bool
, markObviouslyPlayable :: Bool
, markChops :: Bool
, warnDoubleDrop:: Bool
, markHints :: Bool
, markPossibilities :: Bool
} deriving (Read, Show, Eq, Generic)
prettyPI :: PublicInfo -> String
prettyPI pub
= let
showDeck 0 = if prolong $ rule $ gameSpec pub then "Deck: 0, " else "Deck: 0 (" ++ shows (fromJust $ deadline pub) " turn(s) left), "
showDeck 1 = "Deck: 1, "
showDeck n = "Deck: " ++ shows n ", "
in "Turn: "++ shows (turn pub) ", " ++ showDeck (pileNum pub) ++ "Lives: " ++ shows (lives pub) ", Hints: " ++ shows (hintTokens pub) ";\n\n"
++ "played (" ++ shows (currentScore pub) " / " ++ shows (achievableScore pub) "):"
++ concat [ " " ++ concat ( [ show $ C c k | k <- [K1 .. achievedRank pub c] ] ++ replicate (possible - fromEnum playedMax) "__" ++ replicate (5 - possible) "XX")
| c <- colors pub
, let playedMax = achievedRank pub c
possible = fromEnum $ bestPossibleRank pub c
]
++ "\ndropped: " ++ concat [ '|' : concat (replicate n $ show $ intToCard ci) | (ci,n) <- IM.toList $ discarded pub ] ++"|\n"
view :: State -> PrivateView
view st = mkPV (publicState st) (tail $ hands st)
main :: IO ()
main = selfplay defaultGS
selfplay :: GameSpec -> IO ()
selfplay gs
= do g <- newStdGen
((finalSituation,_),_) <- start gs [] [stdio] g
putStrLn $ prettyEndGame finalSituation
prettyEndGame :: (EndGame, [State], [Move]) -> String
prettyEndGame (eg,sts@(st:_),mvs)
= unlines $ recentEvents ithPlayerFromTheLast (map view sts) mvs :
replicate 80 '!' :
surround (replicate 40 '!') (show eg) :
replicate 80 '!' :
map (surround $ replicate 38 ' ' ++"!!") (lines $ prettySt ithPlayerFromTheLast st) ++
[ replicate 80 '!' ]
surround :: [a] -> [a] -> [a]
surround ys xs = let len = length xs
len2 =len `div` 2
in reverse (drop len2 ys) ++ xs ++ drop (len - len2) ys
type Peeker m = State -> [Move] -> m ()
peek :: Peeker IO
peek st [] = putStrLn $ prettySt ithPlayerFromTheLast st
peek st (mv:_) = putStrLn $ replicate 20 '-' ++ '\n' :
showTrial (const "") undefined (view st) mv ++ '\n' :
replicate 20 '-' ++ '\n' : prettySt ithPlayerFromTheLast st
start :: (RandomGen g, Monad m, Strategies ps m) =>
GameSpec -> [Peeker m] -> ps -> g -> m (((EndGame, [State], [Move]), ps), g)
start gs audience players gen = let
(st, g) = createGame gs gen
in fmap (\e -> (e,g)) $ run audience [st] [] players
startFromCards :: (Monad m, Strategies ps m) =>
GameSpec -> [Peeker m] -> ps -> [Card] -> m ((EndGame, [State], [Move]), ps)
startFromCards gs audience players shuffled = let
st = createGameFromCards gs shuffled
in run audience [st] [] players
run :: (Monad m, Strategies ps m) => [Peeker m] -> [State] -> [Move] -> ps -> m ((EndGame, [State], [Move]), ps)
run audience states moves players = do
((mbeg, sts, mvs), ps) <- runARound (\sts@(st:_) mvs -> let myOffset = turn (publicState st) in mapM_ (\p -> p st mvs) audience >> broadcast (zipWith rotate [-myOffset, 1-myOffset ..] sts) mvs players (myOffset `mod` numPlayers (gameSpec $ publicState st)) >> return ()) states moves players
case mbeg of Nothing -> run audience sts mvs ps
Just eg -> return ((eg, sts, mvs), ps)
class Monad m => Strategy p m where
strategyName :: m p -> m String
move :: [PrivateView]
-> [Move]
-> p
-> m (Move, p)
observe :: [PrivateView]
-> [Move]
-> p
-> m ()
observe _pvs _moves _st = return ()
data StrategyDict m s = SD{sdName :: String, sdMove :: Mover s m, sdObserve :: Observer s m, sdState :: s}
type Mover s m = [PrivateView] -> [Move] -> s -> m (Move, s)
type Observer s m = [PrivateView] -> [Move] -> s -> m ()
mkSD :: (Monad m, Typeable s, Strategy s m) => String -> s -> StrategyDict m s
mkSD name s = SD{sdName=name, sdMove=move, sdObserve=observe, sdState=s}
instance Monad m => Strategy (StrategyDict m s) m where
strategyName mp = do p <- mp
return $ sdName p
move pvs mvs s = sdMove s pvs mvs (sdState s) >>= \ (m, nexts) -> return (m, s{sdState=nexts})
observe pvs mvs s = sdObserve s pvs mvs $ sdState s
type DynamicStrategy m = StrategyDict m Dynamic
mkDS :: (Monad m, Typeable s, Strategy s m) => String -> s -> DynamicStrategy m
mkDS name s = mkDS' $ mkSD name s
mkDS' :: (Monad m, Typeable s) => StrategyDict m s -> DynamicStrategy m
mkDS' gs = SD{sdName = sdName gs,
sdMove = \pvs mvs dyn -> fmap (\(m,p)->(m, toDyn p)) $ sdMove gs pvs mvs (fromDyn dyn (error "mkDS': impossible")),
sdObserve = \pvs mvs dyn -> sdObserve gs pvs mvs (fromDyn dyn (error "mkDS': impossible")),
sdState = toDyn $ sdState gs}
class Strategies ps m where
runARound :: ([State] -> [Move] -> m ()) -> [State] -> [Move] -> ps -> m ((Maybe EndGame, [State], [Move]), ps)
broadcast :: [State] -> [Move] -> ps -> Int -> m ([State], Int)
instance (Strategies p1 m, Strategies p2 m, Monad m) => Strategies (p1,p2) m where
runARound hook states moves (p,ps) = runARound hook states moves p >>= \(tup@(mbeg,sts,mvs), p') -> case mbeg of
Nothing -> do (tups,ps') <- runARound hook sts mvs ps
return (tups, (p',ps'))
_ -> return (tup, (p',ps))
broadcast states moves (p1,p2) offset = do (sts, ofs) <- broadcast states moves p1 offset
broadcast sts moves p2 ofs
instance (Strategy p m, Monad m) => Strategies [p] m where
runARound _ _ _ [] = error "It takes at least one algorithm to play Hanabi!"
runARound hook states moves [p] = hook states moves >> runATurn states moves p >>= \(tup, p') -> return (tup, [p'])
runARound hook states moves (p:ps) = hook states moves >> runATurn states moves p >>= \(tup@(mbeg,sts,mvs), p') -> case mbeg of
Nothing -> do (tups,ps') <- runARound hook sts mvs ps
return (tups, (p':ps'))
_ -> return (tup, (p':ps))
broadcast _ _ [] _ = error "It takes at least one algorithm to play Hanabi!"
broadcast states moves [p] ofs = when (ofs/=0) (observe (map view states) moves p) >> return (map (rotate 1) states, pred ofs)
broadcast states moves (p:ps) ofs = when (ofs/=0) (observe (map view states) moves p) >> broadcast (map (rotate 1) states) moves ps (pred ofs)
runATurn :: (Strategy p m, Monad m) => [State] -> [Move] -> p -> m ((Maybe EndGame, [State], [Move]), p)
runATurn states moves p = let alg = move (map view $ zipWith rotate [0..] states) moves p in
do (mov, p') <- alg
case proceed (head states) mov of
Nothing -> do name <- strategyName (fmap snd alg)
error $ show mov ++ " by " ++ name ++ ": invalid move!"
Just st -> let nxt = rotate 1 st
in return ((checkEndGame $ publicState nxt, nxt:states, mov:moves), p')
data Verbose p = Verbose {unV :: p, verbV :: Verbosity} deriving (Read, Show)
instance (Strategy p m, MonadIO m) => Strategy (Verbose p) m where
strategyName mp = do name <- strategyName $ fmap unV mp
return $ if name == "Blind" then "STDIO" else "Verbose " ++ name
move views@(_:_) moves (Verbose p verb) = let alg = move views moves p in
do name <- strategyName (fmap (\a -> Verbose (snd a) verb) alg)
liftIO $ putStrLn $ what'sUp verb name views moves
(mv,p') <- alg
return (mv, Verbose p' verb)
observe _ [] _ = return ()
observe (v:_) (m:_) (Verbose _ verb) = liftIO $ putStrLn $ what'sUp1 verb v m
what'sUp :: Verbosity -> String -> [PrivateView] -> [Move] -> String
what'sUp verb name views@(v:_) moves = replicate 20 '-' ++ '\n' :
recentEvents ithPlayer views moves ++ '\n' :
replicate 20 '-' ++ '\n' :
"Algorithm: " ++ name ++ '\n' :
prettyPV verb v ++ "\nYour turn.\n"
what'sUp1 :: Verbosity -> PrivateView -> Move -> String
what'sUp1 verb v m = replicate 20 '-' ++ '\n' :
showTrial (const "") undefined v m ++ '\n' :
replicate 20 '-' ++ '\n' :
prettyPV verb v
recentEvents :: (Int -> Int -> String) -> [PrivateView] -> [Move] -> String
recentEvents ithP vs@(v:_) ms = unlines $ reverse $ zipWith3 (showTrial $ ithP nump) [pred nump, nump-2..0] vs ms
where nump = numPlayers $ gameSpec $ publicView v
showTrial :: (Int -> String) -> Int -> PrivateView -> Move -> String
showTrial ithP i v m = ithP i ++ " move: " ++ replicate (length (ithP 2) - length (ithP i)) ' ' ++ show m ++
case result $ publicView v of Discard c -> ", which revealed "++shows c "."
Success c -> ", which succeeded revealing "++shows c "."
Fail c -> ", which failed revealing " ++ shows c "."
_ -> "."
ithPlayer :: Int -> Int -> String
ithPlayer _ 0 = "My"
ithPlayer _ i = "The " ++ ith i ++"next player's"
ith :: Int -> String
ith 1 = ""
ith 2 = "2nd "
ith 3 = "3rd "
ith i = shows i "th "
ithPlayerFromTheLast :: Int -> Int -> String
ithPlayerFromTheLast nump j = "The " ++ ith (nump-j) ++"last player's"
type STDIO = Verbose Blind
stdio :: Verbose Blind
stdio = Verbose Blind verbose
data Blind = Blind
instance (MonadIO m) => Strategy Blind m where
strategyName _ = return "Blind"
move (v:_) _ _ = do mov <- liftIO $ repeatReadingAMoveUntilSuccess stdin stdout v
return (mov, Blind)
data ViaHandles = VH {hin :: Handle, hout :: Handle, verbVH :: Verbosity}
instance (MonadIO m) => Strategy ViaHandles m where
strategyName _ = return "via handles"
move views@(v:_) moves vh = liftIO $ do hPutStrLn (hout vh) $ what'sUp (verbVH vh) "via handles" views moves
mov <- repeatReadingAMoveUntilSuccess (hin vh) (hout vh) v
return (mov, vh)
repeatReadingAMoveUntilSuccess :: Handle -> Handle -> PrivateView -> IO Move
repeatReadingAMoveUntilSuccess hin hout v = do
str <- hGetLine hin
case reads str of [(mv, rest)] | all isSpace rest -> if isMoveValid v mv then return mv else hPutStrLn hout "Invalid Move" >> repeatReadingAMoveUntilSuccess hin hout v
_ -> hPutStr hout ("Parse error.\n"++help) >> repeatReadingAMoveUntilSuccess hin hout v
createGameFromCards :: GameSpec -> [Card] -> State
createGameFromCards gs = splitCons (numPlayers gs) []
where splitCons 0 hnds stack
= St {publicState = PI {gameSpec = gs,
pileNum = initialPileNum gs,
played = IM.fromAscList [ (i, Empty) | i <- [0 .. pred $ numColors $ rule gs] ],
discarded = IM.fromList [ (cardToInt $ C i k, 0) | i <- take (numColors $ rule gs) [White .. Multicolor],
k <- [K1 ..K5] ],
nonPublic = cardMap $ rule gs,
turn = 0,
lives = numBlackTokens $ rule gs,
hintTokens = 8,
deadline = Nothing,
givenHints = replicate (numPlayers gs) $ replicate (handSize gs) (Nothing, Nothing),
possibilities = replicate (numPlayers gs) $ replicate (handSize gs) (unknown gs),
result = None
},
pile = stack,
hands = hnds
}
splitCons n hnds stack = case splitAt (handSize gs) stack of (tk,dr) -> splitCons (pred n) (tk:hnds) dr
createGame :: RandomGen g => GameSpec -> g -> (State, g)
createGame gs gen = (createGameFromCards gs shuffled, g) where
(shuffled, g) = createDeck (rule gs) gen
createDeck :: RandomGen g => Rule -> g -> ([Card], g)
createDeck r gen = shuffle (cardBag r) gen
numAssoc :: [(Number, Int)]
numAssoc = zip [K1 ..K5] [3,2,2,2,1]
cardAssoc :: Rule -> [(Card,Int)]
cardAssoc rule = take (5 * numColors rule) $
[ (C i k, n) | i <- [White .. pred Multicolor], (k,n) <- numAssoc ] ++ [ (C Multicolor k, n) | (k, n) <- zip [K1 ..K5] (numMulticolors rule) ]
cardBag :: Rule -> [Card]
cardBag rule = concat [ replicate n c | (c,n) <- cardAssoc rule ]
cardMap :: Rule -> IM.IntMap Int
cardMap rule = IM.fromList [ (cardToInt c, n) | (c,n) <- cardAssoc rule ]
unknown :: GameSpec -> Possibilities
unknown gs = (64 - bit (6 - numColors (rule gs)), 31)
shuffle :: RandomGen g => [c] -> g -> ([c], g)
shuffle xs = shuf [] xs $ length xs
shuf :: RandomGen g => [a] -> [a] -> Int -> g -> ([a], g)
shuf result _ 0 gen = (result, gen)
shuf result xs n gen = let (i, g) = randomR (0, pred n) gen
(nth,rest) = pickNth i xs
in shuf (nth:result) rest (pred n) g
isMoveValid :: PrivateView -> Move -> Bool
isMoveValid PV{publicView=pub} (Drop ix) = hintTokens pub < 8 && length (head $ givenHints pub) > ix && ix >= 0
isMoveValid PV{publicView=pub} (Play ix) = length (head $ givenHints pub) > ix && ix >= 0
isMoveValid PV{publicView=pub,handsPV=tlHands} (Hint hintedpl eck)
= hintTokens pub > 0 &&
hintedpl > 0 && hintedpl < numPlayers (gameSpec pub) &&
not (null $ filter willBeHinted (tlHands !! pred hintedpl))
where willBeHinted :: Card -> Bool
willBeHinted = either (\c -> (==c).color) (\k -> (==k).number) eck
pickNth :: Int -> [a] -> (a, [a])
pickNth n xs = case splitAt n xs of (tk,nth:dr) -> (nth,tk++dr)
replaceNth :: Int -> a -> [a] -> (a, [a])
replaceNth n x xs = case splitAt n xs of (tk,nth:dr) -> (nth,tk++x:dr)
updateNth :: Int -> (a -> a) -> [a] -> (a, [a])
updateNth n f xs = case splitAt n xs of (tk,nth:dr) -> (nth,tk++f nth:dr)
proceed :: State -> Move -> Maybe State
proceed st@(St{publicState=pub@PI{gameSpec=gS}}) mv = if (isMoveValid (view st) mv) then return (prc mv) else Nothing where
(nth, droppedHand) = pickNth (index mv) playersHand where playersHand = head $ hands st
(_ , droppedHint) = pickNth (index mv) playersHint where playersHint = head $ givenHints pub
(_ , droppedPos) = pickNth (index mv) playersPos where playersPos = head $ possibilities pub
(nextHand,nextHint,nextPos,nextPile, nextPileNum) = case pile st of [] -> (droppedHand, droppedHint, droppedPos, [], 0)
d:ps -> (d:droppedHand, (Nothing,Nothing):droppedHint, unknown gS : droppedPos, ps, pred $ pileNum pub)
nextHands = nextHand : tail (hands st)
nextHints = nextHint : tail (givenHints pub)
nextPoss = nextPos : tail (possibilities pub)
nextDeadline = case deadline pub of Nothing | nextPileNum==0 && not (prolong $ rule $ gameSpec pub) -> Just $ numPlayers gS
| otherwise -> Nothing
Just i -> Just $ pred i
prc (Drop _) = st{pile = nextPile,
hands = nextHands,
publicState = pub{pileNum = nextPileNum,
discarded = IM.update (Just . succ) (cardToInt nth) $ discarded pub,
nonPublic = IM.update (Just . pred) (cardToInt nth) $ nonPublic pub,
turn = succ $ turn pub,
hintTokens = succ $ hintTokens pub,
givenHints = nextHints,
possibilities = nextPoss,
deadline = nextDeadline,
result = Discard nth}}
prc (Play i) | failure = let newst@St{publicState=newpub} = prc (Drop i) in newst{publicState=newpub{hintTokens = hintTokens pub, lives = pred $ lives pub, result = Fail nth}}
| otherwise = st{pile = nextPile,
hands = nextHands,
publicState = pub{pileNum = nextPileNum,
played = IM.update (Just . succ) (fromEnum $ color nth) (played pub),
nonPublic = IM.update (Just . pred) (cardToInt nth) $ nonPublic pub,
turn = succ $ turn pub,
hintTokens = if hintTokens pub < 8 && number nth == K5 then succ $ hintTokens pub else hintTokens pub,
givenHints = nextHints,
possibilities = nextPoss,
deadline = nextDeadline,
result = Success nth}}
where failure = not $ isPlayable pub nth
prc (Hint hintedpl eik) = st{publicState = pub{hintTokens = pred $ hintTokens pub,
turn = succ $ turn pub,
givenHints = snd $ updateNth hintedpl newHints (givenHints pub),
possibilities = snd $ updateNth hintedpl newPoss (possibilities pub),
deadline = case deadline pub of Nothing -> Nothing
Just i -> Just $ pred i,
result = None}}
where newHints hs = zipWith zipper (hands st !! hintedpl) hs
zipper (C ir ka) (mi,mk) = case eik of Left i | i==ir -> (Just i, mk)
Right k | k==ka -> (mi, Just k)
_ -> (mi, mk)
newPoss hs = zipWith zipperPos (hands st !! hintedpl) hs
zipperPos (C ir ka) (c,n) = case eik of Left i | i == ir -> (bit ibit, n)
|otherwise-> (clearBit c ibit, n)
where ibit = 5 - fromEnum i
Right k | k == ka -> (c, bit kbit)
|otherwise-> (c, clearBit n kbit)
where kbit = 5 - fromEnum k
rotate :: Int -> State -> State
rotate num st@(St{publicState=pub@PI{gameSpec=gS}}) = st{hands = rotateList $ hands st,
publicState = pub{givenHints = rotateList $ givenHints pub, possibilities = rotateList $ possibilities pub}}
where rotateList xs = case splitAt (num `mod` numPlayers gS) xs of (tk,dr) -> dr++tk
data EndGame = Failure | Soso Int | Perfect deriving (Show,Read,Eq,Generic)
checkEndGame :: PublicInfo -> Maybe EndGame
checkEndGame pub | lives pub == 0 = Just Failure
| all (==K5) [ achievedRank pub k | k <- colors pub ] = Just Perfect
| deadline pub == Just 0 ||
(earlyQuit (rule $ gameSpec pub) && currentScore pub == achievableScore pub)
= Just $ Soso $ IM.foldr (+) 0 $ fmap fromEnum $ played pub
| hintTokens pub == 0 && null (head $ givenHints pub) = Just Failure
| otherwise = Nothing