test: create testing functions for average number of guesses over all possible targets
This commit is contained in:
parent
e16c6bbf7d
commit
1f28cc7cda
2 changed files with 21 additions and 4 deletions
5
Main.hs
5
Main.hs
|
|
@ -39,7 +39,4 @@ toChord = fromJust . mapM toPitch . words
|
|||
-- | Prompt for a target and use guessTest to try to guess it.
|
||||
main :: IO ()
|
||||
main = do
|
||||
putStr "Target chord (3 pitches separated by spaces): "
|
||||
hFlush stdout
|
||||
text <- getLine
|
||||
guessTest $ toChord text
|
||||
putStr $ show avgGuesses
|
||||
|
|
|
|||
20
Proj2.hs
20
Proj2.hs
|
|
@ -54,6 +54,7 @@ module Proj2
|
|||
GameState,
|
||||
initialGuess,
|
||||
nextGuess,
|
||||
avgGuesses,
|
||||
)
|
||||
where
|
||||
|
||||
|
|
@ -250,3 +251,22 @@ allChords =
|
|||
| note <- [minBound .. maxBound],
|
||||
octave <- [minBound .. maxBound]
|
||||
]
|
||||
|
||||
-- ==== TESTING ===============================================================
|
||||
|
||||
guessTest :: [Pitch] -> Int
|
||||
guessTest target = loop target guess other 1
|
||||
where
|
||||
(guess, other) = initialGuess
|
||||
|
||||
loop :: [Pitch] -> [Pitch] -> GameState -> Int -> Int
|
||||
loop target guess other guesses
|
||||
| answer == (3, 0, 0) = guesses
|
||||
| otherwise = loop target guess' other' (guesses + 1)
|
||||
where
|
||||
answer = feedback target guess
|
||||
(guess', other') = nextGuess (guess, other) answer
|
||||
|
||||
avgGuesses = fromIntegral (sum results) / fromIntegral (length results)
|
||||
where
|
||||
results = map guessTest allChords
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue