Skip to content

Instantly share code, notes, and snippets.

@myuon
Created May 8, 2015 10:32
Show Gist options
  • Save myuon/a30151b794f73aff1eea to your computer and use it in GitHub Desktop.
Save myuon/a30151b794f73aff1eea to your computer and use it in GitHub Desktop.
Hit & Blow
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