Improve code in Haskell solution for Problem 54
This commit is contained in:
parent
f0ed5540b4
commit
74d32924a7
@ -46,14 +46,15 @@
|
|||||||
|
|
||||||
import Data.List
|
import Data.List
|
||||||
import Data.Function
|
import Data.Function
|
||||||
|
import Data.Maybe
|
||||||
import System.IO
|
import System.IO
|
||||||
|
|
||||||
data Value = Two | Three | Four | Five | Six | Seven | Eight | Nine | Ten | Jack | Queen | King | Ace
|
data Value = Two | Three | Four | Five | Six | Seven | Eight | Nine | Ten | Jack | Queen | King | Ace
|
||||||
deriving (Eq, Ord, Show, Read, Bounded, Enum)
|
deriving (Eq, Ord, Show, Bounded, Enum)
|
||||||
data Suit = Hearts | Diamonds | Clubs | Spades deriving (Eq, Show)
|
data Suit = Hearts | Diamonds | Clubs | Spades deriving (Eq, Show)
|
||||||
|
|
||||||
data Card = Card { value :: Value
|
data Card = Card { value :: Maybe Value
|
||||||
, suit :: Suit
|
, suit :: Maybe Suit
|
||||||
} deriving (Eq, Show)
|
} deriving (Eq, Show)
|
||||||
|
|
||||||
instance Ord Card where
|
instance Ord Card where
|
||||||
@ -65,26 +66,28 @@ data PokerGame = PokerGame { player1 :: Hand
|
|||||||
, player2 :: Hand
|
, player2 :: Hand
|
||||||
} deriving (Eq, Show)
|
} deriving (Eq, Show)
|
||||||
|
|
||||||
getValue :: Char -> Value
|
getValue :: Char -> Maybe Value
|
||||||
getValue '2' = Two
|
getValue '2' = Just Two
|
||||||
getValue '3' = Three
|
getValue '3' = Just Three
|
||||||
getValue '4' = Four
|
getValue '4' = Just Four
|
||||||
getValue '5' = Five
|
getValue '5' = Just Five
|
||||||
getValue '6' = Six
|
getValue '6' = Just Six
|
||||||
getValue '7' = Seven
|
getValue '7' = Just Seven
|
||||||
getValue '8' = Eight
|
getValue '8' = Just Eight
|
||||||
getValue '9' = Nine
|
getValue '9' = Just Nine
|
||||||
getValue 'T' = Ten
|
getValue 'T' = Just Ten
|
||||||
getValue 'J' = Jack
|
getValue 'J' = Just Jack
|
||||||
getValue 'Q' = Queen
|
getValue 'Q' = Just Queen
|
||||||
getValue 'K' = King
|
getValue 'K' = Just King
|
||||||
getValue 'A' = Ace
|
getValue 'A' = Just Ace
|
||||||
|
getValue _ = Nothing
|
||||||
|
|
||||||
getSuit :: Char -> Suit
|
getSuit :: Char -> Maybe Suit
|
||||||
getSuit 'H' = Hearts
|
getSuit 'H' = Just Hearts
|
||||||
getSuit 'D' = Diamonds
|
getSuit 'D' = Just Diamonds
|
||||||
getSuit 'C' = Clubs
|
getSuit 'C' = Just Clubs
|
||||||
getSuit 'S' = Spades
|
getSuit 'S' = Just Spades
|
||||||
|
getSuit _ = Nothing
|
||||||
|
|
||||||
readCard :: String -> Card
|
readCard :: String -> Card
|
||||||
readCard (v:s:_) = Card{value=getValue v,suit=getSuit s}
|
readCard (v:s:_) = Card{value=getValue v,suit=getSuit s}
|
||||||
@ -95,14 +98,14 @@ readGame g = PokerGame{player1=map readCard (take 5 (words g)), player2=map read
|
|||||||
royalFlush :: Hand -> Bool
|
royalFlush :: Hand -> Bool
|
||||||
royalFlush h =
|
royalFlush h =
|
||||||
let hs = sort h
|
let hs = sort h
|
||||||
in length (head (groupBy ((==) `on` suit) hs)) == 5 && [Ten .. Ace] == map value hs
|
in length (head (groupBy ((==) `on` suit) hs)) == 5 && [Ten .. Ace] == map (fromJust . value) hs
|
||||||
|
|
||||||
straightFlush :: Hand -> Bool
|
straightFlush :: Hand -> Bool
|
||||||
straightFlush h =
|
straightFlush h =
|
||||||
let hs@(x:_) = sort h
|
let hs@(x:_) = sort h
|
||||||
start = value x
|
start = fromJust . value $ x
|
||||||
in length (head (groupBy ((==) `on` suit) hs)) == 5 &&
|
in length (head (groupBy ((==) `on` suit) hs)) == 5 &&
|
||||||
take 5 [start..] == map value hs
|
take 5 [start..] == map (fromJust . value) hs
|
||||||
|
|
||||||
fourOfAKind :: Hand -> Bool
|
fourOfAKind :: Hand -> Bool
|
||||||
fourOfAKind h =
|
fourOfAKind h =
|
||||||
@ -121,8 +124,8 @@ flush h =
|
|||||||
straight :: Hand -> Bool
|
straight :: Hand -> Bool
|
||||||
straight h =
|
straight h =
|
||||||
let hs@(x:_) = sort h
|
let hs@(x:_) = sort h
|
||||||
start = value x
|
start = fromJust . value $ x
|
||||||
in take 5 [start..] == map value hs
|
in take 5 [start..] == map (fromJust . value) hs
|
||||||
|
|
||||||
three :: Hand -> Bool
|
three :: Hand -> Bool
|
||||||
three h =
|
three h =
|
||||||
|
Loading…
x
Reference in New Issue
Block a user