board-games 0.1.0.4 → 0.1.0.5
raw patch · 2 files changed
+32/−30 lines, 2 files
Files
- board-games.cabal +2/−2
- src/Game/Test/Mastermind.hs +30/−28
board-games.cabal view
@@ -1,5 +1,5 @@ Name: board-games-Version: 0.1.0.4+Version: 0.1.0.5 License: GPL License-File: LICENSE Author: Henning Thielemann <haskell@henning-thielemann.de>@@ -36,7 +36,7 @@ location: http://code.haskell.org/~thielema/games/ Source-Repository this- tag: 0.1.0.4+ tag: 0.1.0.5 type: darcs location: http://code.haskell.org/~thielema/games/
src/Game/Test/Mastermind.hs view
@@ -8,7 +8,8 @@ import qualified Data.Set as Set import Control.Monad (liftM2, ) -import Test.QuickCheck (Arbitrary(arbitrary), quickCheck, )+import qualified Test.QuickCheck as QC+import Test.QuickCheck (Property, Arbitrary(arbitrary), quickCheck, ) alphabet :: Set.Set Int@@ -42,27 +43,28 @@ CodeSetTree.member secret $ MM.remaining alphabet attempt (MM.evaluate secret attempt) -evalFromInt :: Int -> Int -> MM.Eval-evalFromInt size ident =- let (rem0, x) = divMod ident size- y = mod rem0 size- rightPlaces = min x y- rightSymbols = max x y - rightPlaces- in MM.Eval rightPlaces rightSymbols+genEval :: Int -> QC.Gen MM.Eval+genEval size = do+ total <- QC.frequency $ map (\k -> (k+1, return k)) [1 .. size]+ rightPlaces <- QC.choose (0,total)+ return $ MM.Eval rightPlaces (total - rightPlaces) -remainingNotMember :: Int -> CodePair -> Bool-remainingNotMember evalIdent (CodePair secret attempt) =- let eval = evalFromInt (length secret) evalIdent- in (eval == MM.evaluate secret attempt)+forAllEval :: QC.Testable prop => [a] -> (MM.Eval -> prop) -> Property+forAllEval code = QC.forAll (genEval (length code))++remainingNotMember :: CodePair -> Property+remainingNotMember (CodePair secret attempt) =+ forAllEval secret $ \eval ->+ (eval == MM.evaluate secret attempt) == (CodeSetTree.member secret $ MM.remaining alphabet attempt eval) -remainingDisjoint :: Int -> Int -> Code -> Bool-remainingDisjoint evalIdent0 evalIdent1 (Code attempt) =- let eval0 = evalFromInt (length attempt) evalIdent0- eval1 = evalFromInt (length attempt) evalIdent1- remaining0 = MM.remaining alphabet attempt eval0+remainingDisjoint :: Code -> Property+remainingDisjoint (Code attempt) =+ forAllEval attempt $ \eval0 ->+ forAllEval attempt $ \eval1 ->+ let remaining0 = MM.remaining alphabet attempt eval0 remaining1 = MM.remaining alphabet attempt eval1 in eval0 == eval1 || CodeSetTree.null@@ -74,10 +76,10 @@ == MM.evaluate attempt secret -evaluateRemaining :: Int -> Code -> Bool-evaluateRemaining evalIdent (Code attempt) =- let eval = evalFromInt (length attempt) evalIdent- in all ((eval ==) . MM.evaluate attempt) $+evaluateRemaining :: Code -> Property+evaluateRemaining (Code attempt) =+ forAllEval attempt $ \eval ->+ all ((eval ==) . MM.evaluate attempt) $ take 100 $ CodeSet.flatten $ (MM.remaining alphabet attempt eval :: CodeSetTree.T Int)@@ -87,7 +89,7 @@ that for different numbers of rightPlace and rightSymbol the codesets are disjoint and their union is the set of all possible codes.-To this we need a union with simplification or a subset test.+To this end we need a union with simplification or a subset test. -} partitionSizes :: Code -> Bool partitionSizes (Code attempt) =@@ -96,10 +98,10 @@ sum (map snd (MM.partitionSizes alphabet attempt)) -selectFlatten :: Int -> Code -> Bool-selectFlatten evalIdent (Code attempt) =- let eval = evalFromInt (length attempt) evalIdent- set :: CodeSetTree.T Int+selectFlatten :: Code -> Property+selectFlatten (Code attempt) =+ forAllEval attempt $ \eval ->+ let set :: CodeSetTree.T Int set = MM.remaining alphabet attempt eval in map (CodeSet.select set) [0 .. min 100 (CodeSet.size set) - 1] ==@@ -116,8 +118,8 @@ attempt:_ -> recourse $ CodeSet.intersection remain $ MM.remaining alphabet attempt $ MM.evaluate secret attempt- in recourse $- (CodeSet.cube alphabet (length secret) :: CodeSetTree.T Int)+ in recourse+ (CodeSet.cube alphabet (length secret) :: CodeSetTree.T Int) {-