Skat Engine und AI auf Haskell Basis
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

89 行
2.5KB

  1. module Operations where
  2. import Control.Monad.State
  3. import System.Random (newStdGen, randoms)
  4. import Data.List
  5. import Data.Ord
  6. import Card
  7. import Skat
  8. import Pile
  9. import Player (chooseCard, Players(..), Player(..), PL(..),
  10. updatePlayer, playersToList, player)
  11. import Utils (shuffle)
  12. compareRender :: Card -> Card -> Ordering
  13. compareRender (Card t1 c1) (Card t2 c2) = case compare c1 c2 of
  14. EQ -> compare t1 t2
  15. v -> v
  16. sortRender :: [Card] -> [Card]
  17. sortRender = sortBy compareRender
  18. turnGeneric :: (PL -> Skat Card)
  19. -> Int
  20. -> Hand
  21. -> Skat (Int, Int)
  22. turnGeneric playFunc depth n = do
  23. table <- getp tableCards
  24. ps <- gets players
  25. let p = player ps n
  26. hand <- getp $ handCards n
  27. trCol <- gets trumpColour
  28. case length table of
  29. 0 -> playFunc p >> turnGeneric playFunc depth (next n)
  30. 1 -> do
  31. modify $ setTurnColour
  32. (Just $ effectiveColour trCol $ head table)
  33. playFunc p
  34. turnGeneric playFunc depth (next n)
  35. 2 -> playFunc p >> turnGeneric playFunc depth (next n)
  36. 3 -> do
  37. w <- evaluateTable
  38. if depth <= 1 || length hand == 0
  39. then countGame
  40. else turnGeneric playFunc (depth - 1) w
  41. turn :: Hand -> Skat (Int, Int)
  42. turn n = turnGeneric play 10 n
  43. evaluateTable :: Skat Hand
  44. evaluateTable = do
  45. trumpCol <- gets trumpColour
  46. turnCol <- gets turnColour
  47. table <- getp tableCards
  48. ps <- gets players
  49. let winningCard = highestCard trumpCol turnCol table
  50. Just winnerHand <- getp $ originOfCard winningCard
  51. let winner = player ps winnerHand
  52. modifyp $ cleanTable (team winner)
  53. modify $ setTurnColour Nothing
  54. return $ hand winner
  55. countGame :: Skat (Int, Int)
  56. countGame = getp count
  57. play :: (Show p, Player p) => p -> Skat Card
  58. play p = do
  59. liftIO $ putStrLn "playing"
  60. table <- getp tableCardsS
  61. turnCol <- gets turnColour
  62. trump <- gets trumpColour
  63. hand <- getp $ handCards (hand p)
  64. fallen <- getp played
  65. (card, p') <- chooseCard p table fallen hand
  66. modifyPlayers $ updatePlayer p'
  67. modifyp $ playCard card
  68. ps <- fmap playersToList $ gets players
  69. table' <- getp tableCardsS
  70. ps' <- mapM (\p -> onCardPlayed p (head table')) ps
  71. mapM_ (modifyPlayers . updatePlayer) ps'
  72. return card
  73. playOpen :: (Show p, Player p) => p -> Skat Card
  74. playOpen p = do
  75. --liftIO $ putStrLn $ show (hand p) ++ " playing open"
  76. card <- chooseCardOpen p
  77. modifyp $ playCard card
  78. return card