diff --git a/.gitignore b/.gitignore index c368d453bcdba8d4c085d94567fb0da522934695..5872271f7658da252f8d12da218af7673f3cdee9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .stack-work/ -*~ \ No newline at end of file +*~ +twenty-one.cabal diff --git a/app/Main.hs b/app/Main.hs index fa4e3de8cc2db2656dcf03ccbbf1c521e899d21d..5cf9ed142c3df3d97aabee2e797fe311baf9daae 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -1,28 +1,59 @@ module Main where import Control.Monad +import Card import Lib +import System.Random main :: IO () main = do - putStrLn "How many random integer numbers should we generate?" - count <- read <$> getLine :: IO Int - - putStrLn "What would be the minimum value of those numbers?" - min <- read <$> getLine :: IO Int + putStrLn "\n\nWelcome to play Twenty-One game!" + putStrLn "\nLet's start....\n" + putStrLn $ take 100 (repeat '-') + + -- Generate random numbers + g <- getStdGen + let listOfNums = take 10 (randomRs (0, 51) g :: [Int]) - putStrLn "What would be the maximum value of those numbers?" - max <- read <$> getLine :: IO Int - - if count <= 0 then error "The count must be a positive number" - else if max < min then error "The minimum cannot be larger than the maximum" - else putStrLn $ "Very well, producing " ++ - (if count == 1 then "a number" else show count ++ " numbers") ++ - " between " ++ show min ++ " and " ++ show max ++ "!" + let num = (listOfNums!!0) + + tellCard num + + --let value = getValue $ showCard (pullCardFromDeck deck num) + + --putStrLn $ "You have now" ++ show value ++ "/21" + + putStrLn "Do you want another card? (y/n)" + + wantsCard <- getChar + + if wantsCard == 'y' then tellCard (listOfNums!!1) + else simulateComputer + + + + putStrLn "Game over!" + + +askForCard :: IO () +askForCard = do + putStrLn "Do you want another card? (y/n)" + + wantsCard <- getChar + + if wantsCard == 'y' then askForCard + else simulateComputer + + +tellCard :: Int -> IO () +tellCard a = do + let card = showCard (pullCardFromDeck deck a) - putStrLn $ "In real life, we could emulate that with " ++ - show count ++ " x " ++ - show (max-min+1) ++ " sided dice rolls." - - forM_ [1..count] (generateNumber min max) + putStrLn $ "You got a card: " ++ card ++ "\n" + +simulateComputer :: IO () +simulateComputer = do + putStrLn "Player stays." + putStrLn $ take 100 (repeat '-') + putStrLn "Computer's turn:" \ No newline at end of file diff --git a/package.yaml b/package.yaml index 09d57a59096faa8ddcdd54c66a8594d65c6472d7..f66642a5b01831872db3e3344c9f1ce70e690a2b 100644 --- a/package.yaml +++ b/package.yaml @@ -22,8 +22,6 @@ description: Please see the README on GitHub at <https://github.com/gith dependencies: - base >= 4.7 && < 5 - random >= 1.1 -- containers -- array library: source-dirs: src @@ -38,7 +36,6 @@ executables: - -with-rtsopts=-N dependencies: - twenty-one - - transformers tests: twenty-one-test: diff --git a/src/Card.hs b/src/Card.hs index 3f3ad3507636ca68801ec428ffb0a487605a76fa..86d9e1a138d1251b3dbbbe199132831736530e24 100644 --- a/src/Card.hs +++ b/src/Card.hs @@ -2,6 +2,10 @@ module Card ( Suit (..) , Value (..) , Card (..) +,showCard +,deck +,pullCardFromDeck +,getValue ) where data Suit = Spades | Diamonds | Clubs | Hearts @@ -12,7 +16,7 @@ data Value = Two | Three | Four | Five | Six | Seven | Eight | Nine | Ten | Jack data Card = Card { _suit :: Suit ,_value :: Value - } deriving (Eq, Show) + } deriving (Eq, Show, Bounded) type Deck = [Card] @@ -29,4 +33,7 @@ showCard :: Card -> String showCard (Card {_suit = s, _value = v}) = show v ++ " of " ++ show s pullCardFromDeck :: Deck -> Int -> Card -pullCardFromDeck deck num = deck!!num \ No newline at end of file +pullCardFromDeck deck num = deck!!num + +getValue :: Card -> Value +getValue (Card {_suit = s, _value = v}) = v \ No newline at end of file diff --git a/src/Lib.hs b/src/Lib.hs index 17659664d725e6d69007955ffe86995c1cfc0b6b..0c9610e49fdb9e19ddbf43eeda896ebb8a7e56ef 100644 --- a/src/Lib.hs +++ b/src/Lib.hs @@ -3,9 +3,6 @@ module Lib ) where import System.Random -import Data.Map -import Data.Array.IO -import Control.Monad generateNumber:: Int -> Int -> Int -> IO () generateNumber min max n = do @@ -14,19 +11,6 @@ generateNumber min max n = do --- | Randomly shuffle a list --- /O(N)/ -shuffle :: [a] -> IO [a] -shuffle xs = do - ar <- newArray n xs - forM [1..n] $ \i -> do - j <- randomRIO (i,n) - vi <- readArray ar i - vj <- readArray ar j - writeArray ar j vi - return vj - where - n = length xs - newArray :: Int -> [a] -> IO (IOArray Int a) - newArray n xs = newListArray (1,n) xs - +--getRandomNums :: Int -> [Int] +--getRandomNums = randomRs (2,52) g + --where let g = mkStdGen 10 \ No newline at end of file