test: create testing functions for average number of guesses over all possible targets

This commit is contained in:
wi11-holdsworth 2025-10-07 19:39:10 +11:00
parent 46ece35fde
commit 28532a3b3a
2 changed files with 21 additions and 4 deletions

View file

@ -39,7 +39,4 @@ toChord = fromJust . mapM toPitch . words
-- | Prompt for a target and use guessTest to try to guess it. -- | Prompt for a target and use guessTest to try to guess it.
main :: IO () main :: IO ()
main = do main = do
putStr "Target chord (3 pitches separated by spaces): " putStr $ show avgGuesses
hFlush stdout
text <- getLine
guessTest $ toChord text

View file

@ -51,6 +51,7 @@ module Proj2
GameState, GameState,
initialGuess, initialGuess,
nextGuess, nextGuess,
avgGuesses,
) )
where where
@ -239,3 +240,22 @@ allChords =
| note <- [minBound .. maxBound], | note <- [minBound .. maxBound],
octave <- [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