Commits

Tony Morris  committed c2a53f0

Testing

  • Participants
  • Parent commits b6c6f3d

Comments (0)

Files changed (1)

File Cards/Poker/Hand.hs

 import Control.Monad.State.Lazy
 import Data.Function
 import Data.List
+import qualified Data.Set as S
 import Cards.Card
 import Cards.Rank
 import Cards.Suit
 }
 
 instance Show Hand where
-  show (Hand c1 c2 c3 c4 c5) = "[[ " ++ intercalate " " (map show [c1, c2, c3, c4, c5]) ++ " ]]"
+  show (Hand c1 c2 c3 c4 c5) = "[[ " ++ intercalate " " (show <$> [c1, c2, c3, c4, c5]) ++ " ]]"
 
 instance Eq Hand where
   (==) = (==) `on` score
 hands :: [a] -> [[a]]
 hands = combinations 5
 
-allHands :: [Hand]
-allHands = (\[c1, c2, c3, c4, c5] -> hand c1 c2 c3 c4 c5) <$> hands allCards
+allHands :: S.Set Hand
+allHands = S.fromList ((\[c1, c2, c3, c4, c5] -> hand c1 c2 c3 c4 c5) <$> hands allCards)
 
 cards :: Hand -> [Card]
 cards (Hand c1 c2 c3 c4 c5) = [c1, c2, c3, c4, c5]
                             [[k1], [k2], [k3], [i, _]] -> OnePair i k1 k2 k3
                             _ -> High r1 r2 r3 r4 r5
 
-allScores :: [(Hand, Score)]
-allScores = ((,) <*> score) <$> allHands
+allScores :: S.Set (Hand, Score)
+allScores = S.map ((,) <*> score) allHands
 
 -- utility
 enumFromPred :: (Bounded a, Eq a, Enum a) => a -> [a]
 instance Arbitrary Hand where
   arbitrary = liftM5 hand arbitrary arbitrary arbitrary arbitrary arbitrary
 
+prop_cardX :: (Hand -> Card) -> Int -> Card -> Card -> Card -> Card -> Card -> Bool
 prop_cardX f n c1 c2 c3 c4 c5 = f (hand c1 c2 c3 c4 c5) == [c1, c2, c3, c4, c5] !! n
 
 prop_card1 :: Card -> Card -> Card -> Card -> Card -> Bool
 
 prop_cards :: [Card -> Card -> Card -> Card -> Card -> Bool]
 prop_cards = [prop_card1, prop_card2, prop_card3, prop_card4, prop_card5]
+
+prop_ranksX :: (Hand -> Card) -> Int -> Hand -> Bool
+prop_ranksX f n h = let (r1, r2, r3, r4, r5) = ranks h
+                    in rank (f h) == [r1, r2, r3, r4, r5] !! n
+
+prop_ranks1 :: Hand -> Bool
+prop_ranks1 = prop_ranksX card1 0
+
+prop_ranks2 :: Hand -> Bool
+prop_ranks2 = prop_ranksX card2 1
+
+prop_ranks3 :: Hand -> Bool
+prop_ranks3 = prop_ranksX card3 2
+
+prop_ranks4 :: Hand -> Bool
+prop_ranks4 = prop_ranksX card4 3
+
+prop_ranks5 :: Hand -> Bool
+prop_ranks5 = prop_ranksX card5 4
+
+prop_ranks :: [Hand -> Bool]
+prop_ranks = [prop_ranks1, prop_ranks2, prop_ranks3, prop_ranks4, prop_ranks5]
+
+prop_suitsX :: (Hand -> Card) -> Int -> Hand -> Bool
+prop_suitsX f n h = let (s1, s2, s3, s4, s5) = suits h
+                    in suit (f h) == [s1, s2, s3, s4, s5] !! n
+
+prop_suits1 :: Hand -> Bool
+prop_suits1 = prop_suitsX card1 0
+
+prop_suits2 :: Hand -> Bool
+prop_suits2 = prop_suitsX card2 1
+
+prop_suits3 :: Hand -> Bool
+prop_suits3 = prop_suitsX card3 2
+
+prop_suits4 :: Hand -> Bool
+prop_suits4 = prop_suitsX card4 3
+
+prop_suits5 :: Hand -> Bool
+prop_suits5 = prop_suitsX card5 4
+
+prop_suits :: [Hand -> Bool]
+prop_suits = [prop_suits1, prop_suits2, prop_suits3, prop_suits4, prop_suits5]