general-games 1.0.5 → 1.1.1
raw patch · 4 files changed
+30/−10 lines, 4 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
- Game.Implement.Card: class (Enum c, Eq c, Ord c, Bounded c) => Card c where choose 0 _ = [[]] choose n lst = do { (x : xs) <- tails lst; rest <- choose (n - 1) xs; return $ x : rest } fullDeck = [minBound .. maxBound] dedupe l = nub l shuffle deck = shuffleM deck randomCard = let minB = minBound :: (Bounded c, Card c) => c maxB = maxBound :: (Bounded c, Card c) => c in do { randomd <- getRandomR (fromEnum minB, fromEnum maxB); return $ toEnum randomd } draw handSizeLst deck | let total = (foldl1' (+) handSizeLst) anyNeg = (length (filter (\ n -> n < 0) handSizeLst)) > 0 in (total > (length deck)) || (total < 1) || anyNeg = Nothing | otherwise = let draw2 [] (houtput, doutput) = ((reverse houtput), doutput) draw2 (nToTake : hst) (handOutput, deckOutput) = let newHand = take nToTake deckOutput newDeck = drop nToTake deckOutput in draw2 hst (newHand : handOutput, newDeck) in Just (draw2 handSizeLst ([], deck)) draw_ handSizes deck = let f (Just (h, _)) = Just h f _ = Nothing in f $ draw handSizes deck draw1 handSize deck = let f (Just ([h], d)) = Just (h, d) f _ = Nothing in f $ draw [handSize] deck draw1_ handSize deck = let f (Just ([h], _)) = Just h f _ = Nothing in f $ draw [handSize] deck
+ Game.Implement.Card: class (Enum c, Eq c, Ord c, Bounded c) => Card c
- Game.Implement.Card: class (Card c) => OrderedCard c o where highestCardBy o cl = maximumBy (compareCardBy o) cl lowestCardBy o cl = minimumBy (compareCardBy o) cl sortCardsBy o cl = sortBy (compareCardBy o) cl
+ Game.Implement.Card: class (Card c) => OrderedCard c o
- Game.Implement.Card: class (Card c) => ValuedCard c v where toValueLst l = map toValue l
+ Game.Implement.Card: class (Card c) => ValuedCard c v
Files
- general-games.cabal +2/−2
- src/Game/Game/Poker.hs +2/−2
- src/Game/Implement/Card/Standard/Poker.hs +6/−6
- test/Spec.hs +20/−0
general-games.cabal view
@@ -1,5 +1,5 @@ name: general-games-version: 1.0.5+version: 1.1.1 synopsis: Library supporting simulation of a number of games homepage: https://github.com/cgorski/general-games bug-reports: https://github.com/cgorski/general-games/issues@@ -45,7 +45,7 @@ , HUnit , hspec , MonadRandom- ghc-options: -Wall -threaded -rtsopts -with-rtsopts=-N1 -O+ ghc-options: -Wall -threaded -rtsopts -with-rtsopts=-N1 default-language: Haskell2010 source-repository head
src/Game/Game/Poker.hs view
@@ -362,7 +362,7 @@ hasConsecutiveRanks order hand = let handlst = map (\x -> Just x) $ sortCardsBy order hand ff (Just c0) (Just c1) =- case (toOrderedValue order RankValueType c0)-(toOrderedValue order RankValueType c1) of+ case (toOrderedValue order RankValueType c1)-(toOrderedValue order RankValueType c0) of 1 -> Just c1 _ -> Nothing ff _ _ = Nothing@@ -635,7 +635,7 @@ slst :: [PlayingCard] = sortCardsBy AceHighRankOrder hand rlst = toValueLst slst in- if (rlst == [Ace, King, Queen, Jack, Ten])+ if (rlst == [Ten, Jack, Queen, King, Ace]) then Just (PokerHand RoyalFlush hand) else Nothing else Nothing
src/Game/Implement/Card/Standard/Poker.hs view
@@ -31,19 +31,19 @@ data ValueType = RankValueType | SuitValueType instance OrderedCard PlayingCard Order where- compareCardBy AceHighRankOrder (PlayingCard Ace _) (PlayingCard _ _) = LT- compareCardBy AceHighRankOrder (PlayingCard _ _) (PlayingCard Ace _) = GT+ compareCardBy AceHighRankOrder (PlayingCard Ace _) (PlayingCard _ _) = GT+ compareCardBy AceHighRankOrder (PlayingCard _ _) (PlayingCard Ace _) = LT compareCardBy AceHighRankOrder (PlayingCard r1 _) (PlayingCard r2 _) = if r1 == r2 then EQ- else r2 `compare` r1+ else r1 `compare` r2 - compareCardBy AceLowRankOrder (PlayingCard Ace _) (PlayingCard _ _) = GT- compareCardBy AceLowRankOrder (PlayingCard _ _) (PlayingCard Ace _) = LT+ compareCardBy AceLowRankOrder (PlayingCard Ace _) (PlayingCard _ _) = LT+ compareCardBy AceLowRankOrder (PlayingCard _ _) (PlayingCard Ace _) = GT compareCardBy AceLowRankOrder (PlayingCard r1 _) (PlayingCard r2 _) = if r1 == r2 then EQ- else r2 `compare` r1+ else r1 `compare` r2 compareCardBy SuitOrder (PlayingCard _ s1) (PlayingCard _ s2) = s1 `compare` s2
test/Spec.hs view
@@ -3,6 +3,7 @@ import Game.Game.Poker import Game.Implement.Card import Game.Implement.Card.Standard+import Game.Implement.Card.Standard.Poker allHandsCount :: Int@@ -113,6 +114,17 @@ shuffledDeck :: RandomGen g => Rand g [PlayingCard] shuffledDeck = shuffle $ fullDeck +clubsDefaultSortAceLow :: [PlayingCard]+clubsDefaultSortAceLow =+ let clubs = flip PlayingCard Clubs <$> ranks+ in sortCardsBy AceLowRankOrder $ clubs++clubsDefaultSortAceHigh :: [PlayingCard]+clubsDefaultSortAceHigh =+ let clubs = flip PlayingCard Clubs <$> ranks+ in sortCardsBy AceHighRankOrder $ clubs++ main :: IO () main = do@@ -202,6 +214,14 @@ length (fullDeck :: [PlayingCard]) `shouldBe` 52 it "returns unique cards" $ do isUnique (fullDeck :: [PlayingCard]) `shouldBe` True++ describe "Game.Game.Poker.compareCardBy" $ do+ it "orders low to high by default, check ace low" $ do+ clubsDefaultSortAceLow `shouldBe` [PlayingCard Ace Clubs, PlayingCard Two Clubs, PlayingCard Three Clubs, PlayingCard Four Clubs, PlayingCard Five Clubs, PlayingCard Six Clubs, PlayingCard Seven Clubs, PlayingCard Eight Clubs, PlayingCard Nine Clubs, PlayingCard Ten Clubs, PlayingCard Jack Clubs, PlayingCard Queen Clubs, PlayingCard King Clubs]+ it "orders low to high by default, check ace high" $ do+ clubsDefaultSortAceHigh `shouldBe` [PlayingCard Two Clubs, PlayingCard Three Clubs, PlayingCard Four Clubs, PlayingCard Five Clubs, PlayingCard Six Clubs, PlayingCard Seven Clubs, PlayingCard Eight Clubs, PlayingCard Nine Clubs, PlayingCard Ten Clubs, PlayingCard Jack Clubs, PlayingCard Queen Clubs, PlayingCard King Clubs, PlayingCard Ace Clubs]++ describe "Game.Game.Poker.isHand" $ do it "confirms that an Ace low straight flush exists" $ do