Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module Mastermind
- where
- import System.Random
- import Text.Read
- import Data.Maybe
- import Data.Char
- data Color = Red | Green | Blue | Yellow | Brown | Orange | Black | White deriving (Show, Read, Eq, Ord, Bounded, Enum)
- type Combination = [Color]
- boolToInt x = if x then 1 else 0
- checkWhites:: Combination -> Combination -> Int
- checkWhites secret guess =
- sum $ fmap boolToInt $ fmap (\x -> x `elem` secret) guess
- checkBlacks:: Combination -> Combination -> Int
- checkBlacks secret guess =
- let pairs = zip secret guess in
- sum $ fmap boolToInt $ [ x == y | (x,y) <- pairs]
- check:: Combination -> Combination -> (Int, Int)
- check secret guess = let blacks = checkBlacks secret guess in
- (checkWhites secret guess -blacks, blacks)
- parse:: String -> Maybe Combination
- parse x = sequence $ fmap (\x -> readMaybe ((toUpper $ head x) : tail x) :: Maybe Color) (words x)
- guess:: Combination -> Int -> Combination -> IO ()
- guess secret moves guess = do
- let (whites, blacks) = check secret guess
- if blacks == length secret then
- putStrLn "You win! 🎉"
- else do
- putStrLn ("Result: " ++ show whites ++ " whites " ++ show blacks ++ " blacks. Moves " ++ show (moves-1))
- turn secret (moves-1)
- takeNoRepeats:: (Eq a) => Int -> [a] -> [a] -> [a]
- takeNoRepeats n [] [] = []
- takeNoRepeats 0 x y = x
- takeNoRepeats n [] (h:t) = takeNoRepeats (n-1) [h] t
- takeNoRepeats n x (y:t)
- | y `elem` x = takeNoRepeats (n-1) x t
- | otherwise = let taken =
- x ++ [y]
- in (takeNoRepeats (n-1) taken t)
- mkguess:: Combination -> Int -> IO ()
- mkguess secret moves = do
- putStrLn "\nEnter a guess:"
- string <- getLine
- let userGuess = parse string
- if isJust userGuess then
- guess secret moves (fromJust userGuess)
- else
- turn secret moves
- randomInList:: StdGen -> [a] -> [a]
- randomInList gen allVals = let results = (randomRs (0, (length allVals -1)) gen)
- in
- [ allVals !! x | x <- results ]
- turn:: Combination -> Int -> IO ()
- turn secret moves = if moves == 0 then
- print $ "You lose. 😭 " ++ show secret
- else mkguess secret moves
- mastermind:: Int -> Int -> IO()
- mastermind len moves = do
- gen <- getStdGen
- let allColors = [ Red .. White]
- putStrLn $ "Available colors: " ++ (show allColors)
- let secret = takeNoRepeats len [] $ randomInList gen allColors
- in do -- print secret
- turn secret moves
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement