Skip to content

Instantly share code, notes, and snippets.

@VoQn
Created January 25, 2012 05:11
Show Gist options
  • Save VoQn/1674861 to your computer and use it in GitHub Desktop.
Save VoQn/1674861 to your computer and use it in GitHub Desktop.
オレオしりとりゲーム
module Main ( main ) where
import Control.Applicative ( (<$>) )
import qualified System.Console.ANSI as Console
import qualified Test.QuickCheck as QuickCheck
data ConsoleMode = Battle | Combo | Result deriving ( Eq, Show )
main :: IO a
main = do
Console.setTitle "λ オレオ しりとり λ"
discussion 0 []
genQ :: QuickCheck.Gen String
genQ = QuickCheck.frequency
[ (100, select [ "oreo", "OREO" ])
, (5, select [ "magi", "neko" ])
, (1, select [ "アメッシュ","クチュクチュ", "オレオ", "んっ…" ])
]
where
select = QuickCheck.elements
question :: IO String
question = (!! 1) <$> QuickCheck.sample' genQ
discussion :: Int -> [String] -> IO a
discussion n st = do
changeConsoleView Battle
q <- question
putStrLn q
x <- getLine
eval q x n st
eval :: String -> String -> Int -> [String] -> IO a
eval q x n st
| q == x = challengeNext n (q:st)
| otherwise = faildAndRevenge n st
where
challengeNext c s = do
success $ c + 1
discussion ( c + 1 ) s
faildAndRevenge n s = do
displayResult n s
discussion 0 []
success :: Int -> IO ()
success n = do
changeConsoleView Combo
putStrLn $ "!!! " ++ show n ++ " Combo !!!"
displayResult :: Int -> [String] -> IO ()
displayResult n st = do
changeConsoleView Result
mapM_ putStrLn $ result n st
result :: Int -> [String] -> [String]
result n st = header ++ st ++ footer n where
header =
[ "(;_;) ... Failed ... (;_;)"
, "++++++ Result ++++++"
]
footer x =
[ "=========================="
, "total: " ++ show x ++ " combo"
]
changeConsoleView :: ConsoleMode -> IO ()
changeConsoleView = Console.setSGR . viewStyle
viewStyle :: ConsoleMode -> [Console.SGR]
viewStyle mode = case mode of
Battle ->
[ Console.SetConsoleIntensity Console.NormalIntensity
, Console.SetColor Console.Foreground Console.Vivid Console.White
]
Combo ->
[ Console.SetConsoleIntensity Console.BoldIntensity
, Console.SetColor Console.Foreground Console.Vivid Console.Green
]
Result ->
[ Console.SetConsoleIntensity Console.NormalIntensity
, Console.SetColor Console.Foreground Console.Vivid Console.Red
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment