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.

133 lignes
3.1KB

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