Skat Engine und AI auf Haskell Basis
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

122 wiersze
2.9KB

  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. instance Countable Card Int where
  35. count (Card t _) = count t
  36. instance Countable [Card] Int where
  37. count = sum . map count
  38. equals :: Colour -> Maybe Colour -> Bool
  39. equals col (Just x) = col == x
  40. equals col Nothing = True
  41. isTrump :: Colour -> Card -> Bool
  42. isTrump trumpCol (Card tp col)
  43. | tp == Jack = True
  44. | otherwise = col == trumpCol
  45. effectiveColour :: Colour -> Card -> Colour
  46. effectiveColour trumpCol card@(Card _ col) =
  47. if trump then trumpCol else col
  48. where trump = isTrump trumpCol card
  49. isAllowed :: Colour -> Maybe Colour -> [Card] -> Card -> Bool
  50. isAllowed trumpCol turnCol cs card =
  51. if col `equals` turnCol
  52. then True
  53. else not $ any (\ca -> effectiveColour trumpCol ca `equals` turnCol && ca /= card) cs
  54. where col = effectiveColour trumpCol card
  55. compareCards :: Colour
  56. -> Maybe Colour
  57. -> Card
  58. -> Card
  59. -> Ordering
  60. compareCards _ _ (Card Jack col1) (Card Jack col2) = compare col1 col2
  61. compareCards trumpCol turnCol c1@(Card tp1 col1) c2@(Card tp2 col2) =
  62. case (trp1, trp2) of
  63. (True, True) -> compare tp1 tp2
  64. (False, False) -> case compare (col1 `equals` turnCol)
  65. (col2 `equals` turnCol) of
  66. EQ -> compare tp1 tp2
  67. v -> v
  68. _ -> compare trp1 trp2
  69. where trp1 = isTrump trumpCol c1
  70. trp2 = isTrump trumpCol c2
  71. sortCards :: Colour -> Maybe Colour -> [Card] -> [Card]
  72. sortCards trumpCol turnCol cs = sortBy (compareCards trumpCol turnCol) cs
  73. highestCard :: Colour -> Maybe Colour -> [Card] -> Card
  74. highestCard trumpCol turnCol cs = maximumBy (compareCards trumpCol turnCol) cs
  75. shuffleCards :: IO [Card]
  76. shuffleCards = do
  77. gen <- newStdGen
  78. return $ shuffle gen allCards
  79. -- TESTING VARS
  80. c1 :: Card
  81. c1 = Card Jack Spades
  82. c2 :: Card
  83. c2 = Card Ace Diamonds
  84. c3 :: Card
  85. c3 = Card Queen Diamonds
  86. c4 :: Card
  87. c4 = Card Queen Hearts
  88. c5 :: Card
  89. c5 = Card Jack Clubs
  90. h1 :: [Card]
  91. h1 = [c1,c2,c3,c4,c5]
  92. allCards :: [Card]
  93. allCards = [ Card t c | t <- tps, c <- cols ]
  94. where tps = [Seven .. Jack]
  95. cols = [Diamonds .. Clubs]