Permalink
Browse files

fixed my brainlag of implementing the wrong rules ;-)

  • Loading branch information...
1 parent 51ff8ba commit 060286cd558ad729cdaa630ef58077a30a883e61 @epsilonhalbe committed Jun 14, 2011
Showing with 31 additions and 13 deletions.
  1. +8 −3 Mastermind.lhs
  2. BIN tMastermind
  3. +23 −10 tMastermind.hs
View
@@ -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
@@ -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
View
Binary file not shown.
View
@@ -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.