Created
May 8, 2015 10:32
-
-
Save myuon/a30151b794f73aff1eea to your computer and use it in GitHub Desktop.
Hit & Blow
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module HitBlow where | |
| import System.Random | |
| import Control.Monad | |
| import Data.List | |
| decideNums :: IO [Int] | |
| decideNums = do | |
| ixs <- forM [0..3] $ \i -> randomRIO (0,9-i) | |
| return $ fst $ foldl (\(a,a') i -> ((a'!!i):a,delete i a')) ([],[0..9]) ixs | |
| countHitBlow :: [Int] -> [Int] -> (Int, Int) | |
| countHitBlow ns ks = (hit, blow) where | |
| hit = length $ filter (\(n,k) -> n == k) $ zip ns ks | |
| blow = length (intersect ns ks) - hit | |
| runGame ns = do | |
| putStr "number>" | |
| ks <- fmap ((read :: String -> Int) . return) <$> getLine | |
| if ks == ns | |
| then putStrLn "Exactly!" | |
| else do | |
| let (h,b) = countHitBlow ns ks | |
| putStrLn $ "hit: " ++ show h ++ " blow: " ++ show b | |
| runGame ns | |
| main = do | |
| ns <- decideNums | |
| runGame ns | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment