我正在写一个 Cardguess 游戏,用户设置答案并让 PC 猜测。对于我的项目,输入卡号是 2。
然后,我需要编写cardguess算法。我已经创建了卡片模块。
data Suit = Club | Diamond | Heart | Spade
deriving (Eq, Ord, Bounded, Enum)
suitchars = "CDHS"
data Rank =
R2 | R3 | R4 | R5 | R6 | R7 | R8 | R9 | R10 |
Jack | Queen | King | Ace
deriving (Eq, Ord, Bounded, Enum)
rankchars = "23456789TJQKA"
data Card = Card {suit::Suit, rank::Rank}
deriving (Eq, Bounded)
instance Ord Card where
compare (Card s1 r1) (Card s2 r2) =
let suitorder = compare s1 s2
in if suitorder == EQ then compare r1 r2 else suitorder
instance Enum Card where
fromEnum (Card s r) = (fromEnum s)*13 + (fromEnum r)
toEnum n = (Card s r)
where s = toEnum (n `div` 13)
r = toEnum (n `mod` 13)
而我的策略是将 Suit 和 Rank 分成两部分。最初的猜测可能Card Club R5
是Card Heart Jack
然后我会得到反馈,根据它,我会尝试nextGuess
,直到得到正确的答案。
我已经写了initialGuess
函数。
我把剩下的卡放在GameState
.
initialGuess :: Int -> ([Card], GameState)
initialGuess card_number
| card_number == 2 = ((Card Club R5):(Card Heart J),cardDeck)
现在,我需要编写nextGuess
代码。
nextGuess :: ([Card],GameState) -> (Int,Int,Int,Int,Int) ->([Card],GameState)
(Int,Int,Int,Int,Int)
是反馈值。我如何表示第一个参数([Card],GameState)
,这意味着以前的猜测卡和以前的GameState
。