SHARE
TWEET

Untitled

a guest Oct 21st, 2019 74 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- You can add more imports as you need them.
  2. import Hearts.Types
  3. import Cards
  4.  
  5. playCard :: PlayFunc
  6. playCard _ hand trick prevTrick = ((pickCardAI hand trick prevTrick), "memory")
  7.  
  8. suit :: Card -> Suit
  9. suit (Card s _) = s
  10.  
  11. playLead :: [Card] -> Card
  12. playLead hand = playTwoClubsOrMin hand (head hand)
  13.     where
  14.         playTwoClubsOrMin [] smallest = smallest
  15.         playTwoClubsOrMin (x:xs) smallest
  16.             | x == Card Club Two = Card Club Two
  17.             | x < smallest = playTwoClubsOrMin xs x
  18.             | otherwise = playTwoClubsOrMin xs smallest
  19.  
  20. decidePlayFollowSuitOrPlayScore :: [Card] -> [(Card, PlayerId)] -> Maybe ([(Card, PlayerId)], String) -> Card
  21. decidePlayFollowSuitOrPlayScore hand trick prevState = playFollowSuitOrScoreCard
  22.     where
  23.         followSuitCards = getFollowSuitCards hand trick
  24.         playFollowSuitOrScoreCard = if (followSuitCards /= [])
  25.                                     then (playFollowSuit followSuitCards trick prevState)
  26.                                     else (playScore hand)
  27.  
  28. playScore :: [Card] -> Card
  29. playScore hand = maximum $ spadeQueen ++ hearts ++ highestScore
  30.     where
  31.         spadeQueen = filter (\x -> x == (Card Spade Queen)) hand
  32.         hearts = if (spadeQueen == [])
  33.                     then filter (\x -> suit x == Heart) hand
  34.                     else []
  35.         highestScore = if (hearts == [])
  36.                         then (hand)
  37.                         else []
  38.  
  39. playFollowSuit :: [Card] -> [(Card, PlayerId)] -> Maybe ([(Card, PlayerId)], String) -> Card
  40. playFollowSuit followSuitCards trick prevState = lessThanLeadOrMinMax
  41.     where
  42.         leadCard = trickLeadCard trick
  43.         tryFollowSuitLessThanLead = getLessThanLeadCards followSuitCards leadCard
  44.         lessThanLeadOrMinMax = if (tryFollowSuitLessThanLead == [])
  45.                         then (getMinOrMaxDependingOnTurn followSuitCards trick prevState)
  46.                         else maximum tryFollowSuitLessThanLead
  47.  
  48. trickLeadCard :: [(Card, PlayerId)] -> Card
  49. trickLeadCard trick = fst $ last $ trick
  50. -- trickLeadCard [] = error "should be covered in pickCardAI"
  51.  
  52. getFollowSuitCards :: [Card] -> [(Card, PlayerId)] -> [Card]
  53. getFollowSuitCards hand trick = followSuitCards
  54.     where
  55.         leadCard = trickLeadCard trick
  56.         followSuitCards = filter (\x -> suit x == suit leadCard) hand
  57.  
  58. getLessThanLeadCards :: [Card] -> Card -> [Card]
  59. getLessThanLeadCards followSuitCards leadCard = lessThanLead
  60.     where
  61.         lessThanLead = filter (\x -> x < leadCard) followSuitCards
  62.  
  63. getMinOrMaxDependingOnTurn :: [Card] -> [(Card, PlayerId)] -> Maybe ([(Card, PlayerId)], String) -> Card
  64. getMinOrMaxDependingOnTurn followSuitCards trick prevState = minOrMaxCard
  65.     where
  66.         minOrMaxCard = if (isLast (lengthPreviousTrick prevState) (length trick))
  67.                         then (maximum followSuitCards)
  68.                         else (minimum followSuitCards)
  69.  
  70. isLast :: Int -> Int -> Bool
  71. isLast lenPrevTrick lenCurTrick
  72.     | lenPrevTrick > lenCurTrick = False
  73.     | otherwise = True
  74.  
  75. lengthPreviousTrick :: Maybe ([(Card, PlayerId)], String) -> Int
  76. lengthPreviousTrick Nothing = 0
  77. lengthPreviousTrick (Just (prevTrick, _)) = length prevTrick
  78.  
  79.  
  80. pickCardAI :: [Card] -> [(Card, PlayerId)] -> Maybe ([(Card, PlayerId)], String) -> Card
  81. pickCardAI hand trick prevState
  82.     | trick == [] = playLead hand
  83.     | otherwise = decidePlayFollowSuitOrPlayScore hand trick prevState
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top