Skip to content

Commit

Permalink
fixed my brainlag of implementing the wrong rules ;-)
Browse files Browse the repository at this point in the history
  • Loading branch information
epsilonhalbe committed Jun 14, 2011
1 parent 51ff8ba commit 060286c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 13 deletions.
11 changes: 8 additions & 3 deletions Mastermind.lhs
Expand Up @@ -3,6 +3,7 @@
{- my (=ε/2) haskellous version of the game called mastermind -}

> import Data.List.Split ( splitOneOf )
> import Data.List ( delete )

-- | has to be fetched with
-- >>> foo@bar~> cabal update
Expand Down Expand Up @@ -106,10 +107,14 @@
> -- | otherwise = 1
> -- @

> whites :: (Num a) => [a]->[a]->[a]
> whites randoms guesses = map (const 1) (
> filter (\x -> elem x guesses) (zipWith (*) randoms helper))
> whites :: (Num a) => [a] -> [a] -> [a]
> whites randoms guesses = map (const 1) (w [] guesses cleaned)
> where helper = (map (signum.abs) (zipWith (-) randoms guesses))
> cleaned = zipWith (*) randoms helper
> w xs [] _ = xs
> w xs (g:gs) rs
> | (elem g rs) = w (g:xs) gs (delete g rs)
> | otherwise = w xs gs rs

> -- | /maybeReads/ safely parses input from getLine

Expand Down
Binary file added tMastermind
Binary file not shown.
33 changes: 23 additions & 10 deletions tMastermind.hs
Expand Up @@ -4,18 +4,31 @@ import TAP
import Mastermind

main = runTests $ do
planTests 9
planTests 10

let list_of_randoms = [2,2,3,5]

is (reds [2,2,3,4] list_of_randoms) [0,0,0] $ Just "correct number and position"
is (reds [] list_of_randoms) [] $ Just "correct num and pos - fringe case: []"
is (reds [2,2,2,2,2] list_of_randoms) [0,0] $ Just "correct num and pos - fringe case: input list longer than list_of_randoms"
is (reds [2,2,3,4] list_of_randoms) [0,0,0] $
Just "correct number and position"
is (reds [] list_of_randoms) [] $
Just "correct num and pos - fringe case: []"
is (reds [2,2,2,2,2] list_of_randoms) [0,0] $
Just "correct num and pos - fringe case:\
\input list longer than list_of_randoms"

is (whites [1,4,2,2] list_of_randoms) [1,1] $ Just "correct number, but wrong position"
is (whites [] list_of_randoms) [] $ Just "correct num, wrong pos - fringe case: []"
is (whites [1,1,2,2,2] list_of_randoms) [1,1] $ Just "correct num, wrong pos - fringe case: input list longer than list_of_randoms"
is (whites [1,4,2,2] list_of_randoms) [1,1] $
Just "correct number, but wrong position"
is (whites [] list_of_randoms) [] $
Just "correct num, wrong pos - fringe case: []"
is (whites [1,1,2,2,2] list_of_randoms) [1,1] $
Just "correct num, wrong pos - fringe case:\
\input list longer than list_of_randoms"
is (whites [1,2,1,1,1] [2,1,2,2,2]) [1,1] $ Just "testing the rules"

is (parseInts "1,2,3,4,5") [1,2,3,4,5] $ Just "fetch number list from user input"
is (parseInts "") [] $ Just "fetch number list from user input - fringe case: \"\""
is (parseInts "a,b,c,1,2,3") [1,2,3] $ Just "fetch number list from user input - fringe case: letters in string"
is (parseInts "1,2,3,4,5") [1,2,3,4,5] $
Just "fetch number list from user input"
is (parseInts "") [] $
Just "fetch number list from user input - fringe case: \"\""
is (parseInts "a,b,c,1,2,3") [1,2,3] $
Just "fetch number list from user input -\
\fringe case: letters in string"

0 comments on commit 060286c

Please sign in to comment.