1
Fork 0

Added a deck of cards functionality

This commit is contained in:
Tibo De Peuter 2022-11-07 08:04:11 +01:00
parent 58f9a03123
commit 7a4ef79bf9
5 changed files with 76 additions and 15 deletions

46
lib/CardDeck.hs Normal file
View file

@ -0,0 +1,46 @@
module CardDeck (
generateCards,
generateShuffledCards
) where
import Shuffle
-- Colors of cards
data CardType = Clubs
| Diamonds
| Hearts
| Spades
| NoneType
deriving (Show, Enum, Eq)
-- Values of cards
data CardValue = Ace
| Two
| Three
| Four
| Five
| Six
| Seven
| Eight
| Nine
| Ten
| Jack
| Queen
| King
| NoneValue
deriving (Show, Enum, Eq)
-- A card has a type and a value
type Card = (CardType, CardValue)
-- A deck of cards
type Deck = [Card]
generateCards :: Deck
generateCards = [(cType, cValue) | cType <- types, cValue <- values]
where types = init $ enumFrom Clubs
values = init $ enumFrom Ace
generateShuffledCards :: Deck
generateShuffledCards = shuffle generateCards

24
lib/Shuffle.hs Normal file
View file

@ -0,0 +1,24 @@
module Shuffle (
shuffle
) where
import Data.List
import System.Random
seed = 20
-- Shuffle a list of values.
shuffle :: [a] -> [a]
shuffle l = map (l !!) $ generateIndices $ length l
-- Generate indices to map the elements of a list over so that they
-- are randomly shuffled.
generateIndices :: Int -> [Int]
generateIndices size = take size uniqueList
where randomList = randomRs (0, size - 1) randomGen
uniqueList = nub randomList
-- Generate a random generator
-- TODO Écht random maken?
randomGen :: StdGen
randomGen = mkStdGen seed

View file

@ -1,10 +0,0 @@
module VoorbeeldModule
( hoi -- oplijsting van de publieke functies - als je deze lijst en de haakjes weglaat, wordt alles publiek
, hallo
) where
hoi :: String
hoi = "Hoi"
hallo :: String
hallo = "Hallo"

View file

@ -1,19 +1,19 @@
name: patience name: patience
version: 1.0.0 version: 1.0.0
author: Author name here author: Tibo De Peuter
cabal-version: 1.12 cabal-version: 1.12
build-type: Simple build-type: Simple
library library
hs-source-dirs: lib hs-source-dirs: lib
build-depends: base >= 4.7 && <5 build-depends: base >= 4.7 && <5, random >= 1.1 && < 1.4
exposed-modules: VoorbeeldModule exposed-modules: CardDeck, Shuffle
executable patience executable patience
main-is: Main.hs main-is: Main.hs
hs-source-dirs: src hs-source-dirs: src
default-language: Haskell2010 default-language: Haskell2010
build-depends: base >= 4.7 && <5, patience build-depends: base >= 4.7 && <5, gloss >= 1.11 && < 1.14, gloss-juicy >= 0.2.3, patience
test-suite patience-test test-suite patience-test
type: exitcode-stdio-1.0 type: exitcode-stdio-1.0

View file

@ -35,7 +35,8 @@ packages:
# These entries can reference officially published versions as well as # These entries can reference officially published versions as well as
# forks / in-progress versions pinned to a git hash. For example: # forks / in-progress versions pinned to a git hash. For example:
# #
# extra-deps: extra-deps:
- gloss-juicy-0.2.3@sha256:0c3bca95237cbf91f8b3b1936a0661f1e0457acd80502276d54d6c5210f88b25,1618
# - acme-missiles-0.3 # - acme-missiles-0.3
# - git: https://github.com/commercialhaskell/stack.git # - git: https://github.com/commercialhaskell/stack.git
# commit: e7b331f14bcffb8367cd58fbfc8b40ec7642100a # commit: e7b331f14bcffb8367cd58fbfc8b40ec7642100a