Skip to content

Commit

Permalink
No commit message
Browse files Browse the repository at this point in the history
  • Loading branch information
anonymous@github.com authored and daxim committed Apr 4, 2011
1 parent 96491cd commit 00ecc77
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions mastermind.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import Data.List.Split (splitOneOf)
import Random

main = do
putStrLn "\n please do give me a length"
putStrLn "\nplease do give me a length"
let laength = 4 {- still hard coded fixeme -}
let colours = 5 {- --"-- -}
list_of_randoms <- return (take laength (randomRs (1,colours) (mkStdGen 42)::[Int])){-todo mkStdGen with system time-}
game_loop list_of_randoms
putStrLn "Wanna play again??"
putStrLn "\nWant to play again??"
c <- getChar
regame c
where regame c
Expand All @@ -22,14 +22,14 @@ main = do
main
| elem c "nN" = putStrLn "\nGame Over"
| otherwise = do
putStrLn "you must type one of Yy to confirm or nN to abort"
putStrLn "\nyou must type one of Yy to confirm or nN to abort"
regame c

{-the tricky thing about haskell is the return values - so i left it in this case - otherwise the type checker won't let me pass -}
game_loop list_of_randoms = do
list_of_guesses <- get_n_check_guesslist
if (list_of_guesses == list_of_randoms)
then putStrLn "correctamundo mi amigo"
then putStrLn "\ncorrect guess"
else do
putStrLn (show ((reds list_of_randoms list_of_guesses) ++ (whites list_of_randoms list_of_guesses)))
game_loop list_of_randoms
Expand All @@ -38,7 +38,7 @@ reds :: (Num a) => [a]->[a]->[a]{-denotes the right colors & right positions -}
reds randoms guesses = filter (==0) (zipWith (-) randoms guesses)
{- reds [1,2,3,4,5] [5,4,3,2,1] ~~> filter (==0) [-4,-2,0,2,5] ~~> [0] -}

whites :: (Num a) => [a]->[a]->[a]
whites :: (Num a) => [a]->[a]->[a] {- denotes the right colours but on wrong positions -}
whites randoms guesses = map (const 1) (
filter (\x -> elem x guesses) (zipWith (*) randoms helper))
where helper = (map (signum.abs) (zipWith (-) randoms guesses))
Expand All @@ -61,7 +61,7 @@ helper [1,2,3,4,5] [5,4,3,2,1]

get_n_check_guesslist :: IO [Int]
get_n_check_guesslist = do
putStrLn "please do give me a sequence of 4 numbers between 1 and 5 separated by \",\"" {- 4 should be replaced by 'length' and 5 by 'colours' - fixeme -}
putStrLn "\nplease do give me a sequence of 4 numbers between 1 and 5 separated by \",\"" {- 4 should be replaced by 'length' and 5 by 'colours' - fixeme -}
line <- getLine
let tmp = splitOneOf ";:,. " line
return $ map (\x -> read x:: Int) tmp
Expand Down

0 comments on commit 00ecc77

Please sign in to comment.