PlayingCards (empty) → 0.1.0.0
raw patch · 4 files changed
+147/−0 lines, 4 filesdep +HUnitdep +basesetup-changed
Dependencies added: HUnit, base
Files
- LICENSE +30/−0
- PlayingCards.cabal +21/−0
- Setup.hs +2/−0
- src/Deck.hs +94/−0
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c) 2014, Dillon Huff++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++ * Redistributions of source code must retain the above copyright+ notice, this list of conditions and the following disclaimer.++ * Redistributions in binary form must reproduce the above+ copyright notice, this list of conditions and the following+ disclaimer in the documentation and/or other materials provided+ with the distribution.++ * Neither the name of Dillon Huff nor the names of other+ contributors may be used to endorse or promote products derived+ from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ PlayingCards.cabal view
@@ -0,0 +1,21 @@+-- Initial PlayingCards.cabal generated by cabal init. For further +-- documentation, see http://haskell.org/cabal/users-guide/++name: PlayingCards+version: 0.1.0.0+synopsis: Playing cards api+-- description: +license: BSD3+license-file: LICENSE+author: Dillon Huff+maintainer: dillonhuff@gmail.com+-- copyright: +category: Game+build-type: Simple+cabal-version: >=1.8++library+ exposed-modules: Deck+ -- other-modules: + build-depends: base ==4.6.*, HUnit ==1.2.*+ hs-source-dirs: src
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ src/Deck.hs view
@@ -0,0 +1,94 @@+module Deck(+ card,+ spades, hearts, clubs, diamonds,+ ace, king, queen, jack, ten, nine, eight,+ seven, six, five, four, three, two,+ dealCard,+ containsCard,+ newDeck,+ cardsLeft) where++import Data.List+import Test.QuickCheck++data Deck = Deck [Card]+ deriving (Eq, Ord, Show)++data Card = Flat Suit Rank+ deriving (Eq, Ord, Show)++card = Flat++data Suit = Spades+ | Hearts+ | Diamonds+ | Clubs+ deriving (Enum, Eq, Ord, Bounded, Show)++spades = Spades+hearts = Hearts+diamonds = Diamonds+clubs = Clubs++data Rank = Ace+ | King+ | Queen+ | Jack+ | Ten+ | Nine+ | Eight+ | Seven+ | Six+ | Five+ | Four+ | Three+ | Two+ deriving (Enum, Eq, Ord, Bounded, Show)++ace = Ace+king = King+queen = Queen+jack = Jack+ten = Ten+nine = Nine+eight = Eight+seven = Seven+six = Six+five = Five+four = Four+three = Three+two = Two++newDeckCards :: [Card]+newDeckCards = [Flat s r | s <- [minBound..maxBound], r <- [minBound..maxBound]]++newDeck :: Deck+newDeck = Deck newDeckCards++cardsLeft :: Deck -> Int+cardsLeft (Deck d) = length d++dealCard :: Deck -> (Card, Deck)+dealCard (Deck []) = error "Attempt to deal from empty deck"+dealCard (Deck (c:rest)) = (c, Deck rest)++containsCard :: Card -> Deck -> Bool+containsCard c (Deck d) = elem c d++instance Arbitrary Suit where+ arbitrary = elements [Spades, Hearts, Clubs, Diamonds]++instance Arbitrary Rank where+ arbitrary =+ elements [Ace, King, Queen, Jack, Ten, Nine, Eight, Seven, Six, Five, Four, Three, Two]++instance Arbitrary Card where+ arbitrary = do+ suit <- arbitrary+ rank <- arbitrary+ return $ Flat suit rank++instance Arbitrary Deck where+ arbitrary = do+ cardsToRemove <- arbitrary+ return $ Deck (newDeckCards \\ cardsToRemove)