Created
September 9, 2012 08:45
-
-
Save apstndb/3683362 to your computer and use it in GitHub Desktop.
第4回 スタートHaskell2 数当てゲーム解答
This file contains 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
import System.IO | |
import Data.List | |
import System.Random | |
main = do | |
gen <- getStdGen | |
let answer = take 4 (randomRs ('1', '4') gen) | |
putStrLn "Hello, guess the number!" | |
guessNumber answer | |
guessNumber::String->IO () | |
guessNumber answer = do | |
putStr "> " | |
hFlush stdout | |
line <- getLine | |
if line == answer | |
then putStrLn "correct!" | |
else do | |
if ((length line) == (length answer)) | |
then do | |
print $ countSamePos answer line | |
print $ countSameNum (sort answer) (sort line) | |
else | |
putStrLn "input 4 numbers!" | |
guessNumber answer | |
countSamePos xs ys = length . filter id $ zipWith (==) xs ys | |
countSameNum xss@(x:xs) yss@(y:ys) | |
| x == y = 1 + countSameNum xs ys | |
| x < y = countSameNum xs yss | |
| otherwise = countSameNum xss ys | |
countSameNum _ _ = 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment