Skat Engine und AI auf Haskell Basis
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

137 lignes
3.2KB

  1. {-# LANGUAGE MultiParamTypeClasses #-}
  2. {-# LANGUAGE FlexibleInstances #-}
  3. module Card where
  4. import Data.List
  5. import System.Random (newStdGen)
  6. import Utils
  7. import Control.DeepSeq
  8. class Countable a b where
  9. count :: a -> b
  10. data Type = Seven
  11. | Eight
  12. | Nine
  13. | Queen
  14. | King
  15. | Ten
  16. | Ace
  17. | Jack
  18. deriving (Eq, Ord, Show, Enum)
  19. instance Countable Type Int where
  20. count Ace = 11
  21. count Ten = 10
  22. count King = 4
  23. count Queen = 3
  24. count Jack = 2
  25. count _ = 0
  26. data Colour = Diamonds
  27. | Hearts
  28. | Spades
  29. | Clubs
  30. deriving (Eq, Ord, Show, Enum, Read)
  31. data Card = Card Type Colour
  32. deriving (Eq, Show, Ord)
  33. getColour :: Card -> Colour
  34. getColour (Card _ c) = c
  35. getID :: Card -> Int
  36. getID (Card t _) = case t of
  37. Seven -> 0
  38. Eight -> 0
  39. Nine -> 0
  40. Queen -> 2
  41. King -> 4
  42. Ten -> 8
  43. Ace -> 16
  44. Jack -> 32
  45. instance Countable Card Int where
  46. count (Card t _) = count t
  47. instance Countable [Card] Int where
  48. count = sum . map count
  49. instance NFData Card where
  50. rnf (Card t c) = t `seq` c `seq` ()
  51. equals :: Colour -> Maybe Colour -> Bool
  52. equals col (Just x) = col == x
  53. equals col Nothing = True
  54. isTrump :: Colour -> Card -> Bool
  55. isTrump trumpCol (Card tp col)
  56. | tp == Jack = True
  57. | otherwise = col == trumpCol
  58. effectiveColour :: Colour -> Card -> Colour
  59. effectiveColour trumpCol card@(Card _ col) =
  60. if trump then trumpCol else col
  61. where trump = isTrump trumpCol card
  62. isAllowed :: Colour -> Maybe Colour -> [Card] -> Card -> Bool
  63. isAllowed trumpCol turnCol cs card =
  64. if col `equals` turnCol
  65. then True
  66. else not $ any (\ca -> effectiveColour trumpCol ca `equals` turnCol && ca /= card) cs
  67. where col = effectiveColour trumpCol card
  68. compareCards :: Colour
  69. -> Maybe Colour
  70. -> Card
  71. -> Card
  72. -> Ordering
  73. compareCards _ _ (Card Jack col1) (Card Jack col2) = compare col1 col2
  74. compareCards trumpCol turnCol c1@(Card tp1 col1) c2@(Card tp2 col2) =
  75. case (trp1, trp2) of
  76. (True, True) -> compare tp1 tp2
  77. (False, False) -> case compare (col1 `equals` turnCol)
  78. (col2 `equals` turnCol) of
  79. EQ -> compare tp1 tp2
  80. v -> v
  81. _ -> compare trp1 trp2
  82. where trp1 = isTrump trumpCol c1
  83. trp2 = isTrump trumpCol c2
  84. sortCards :: Colour -> Maybe Colour -> [Card] -> [Card]
  85. sortCards trumpCol turnCol cs = sortBy (compareCards trumpCol turnCol) cs
  86. highestCard :: Colour -> Maybe Colour -> [Card] -> Card
  87. highestCard trumpCol turnCol cs = maximumBy (compareCards trumpCol turnCol) cs
  88. shuffleCards :: IO [Card]
  89. shuffleCards = do
  90. gen <- newStdGen
  91. return $ shuffle gen allCards
  92. -- TESTING VARS
  93. c1 :: Card
  94. c1 = Card Jack Spades
  95. c2 :: Card
  96. c2 = Card Ace Diamonds
  97. c3 :: Card
  98. c3 = Card Queen Diamonds
  99. c4 :: Card
  100. c4 = Card Queen Hearts
  101. c5 :: Card
  102. c5 = Card Jack Clubs
  103. h1 :: [Card]
  104. h1 = [c1,c2,c3,c4,c5]
  105. allCards :: [Card]
  106. allCards = [ Card t c | t <- tps, c <- cols ]
  107. where tps = [Seven .. Jack]
  108. cols = [Diamonds .. Clubs]