-
-
Save onigra/2918811 to your computer and use it in GitHub Desktop.
あみだくじ
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.Random (randomRIO) | |
import Data.List (subsequences) | |
import Control.Applicative ((<$>)) | |
import Control.Monad (replicateM) | |
main :: IO () | |
main = test 4 10 | |
test :: Int -> Int -> IO () | |
test r c = genAmida r c >>= drawAmida | |
data AMIDA = AMIDA Int Int [[Int]] deriving Show | |
amidaRows n = tail $ subsequences [1..n] | |
validAmidaRows = filter pred . amidaRows | |
where | |
pred xs = notElem 1 $ zipWith (-) (tail xs) xs | |
genAmida :: Int -> Int -> IO AMIDA | |
genAmida row col = AMIDA row col <$> amida | |
where | |
amida = replicateM col $ (possibility !!) <$> randomRIO(0, length possibility - 1) | |
possibility = validAmidaRows row | |
drawAmida :: AMIDA -> IO () | |
drawAmida (AMIDA rn cn lines) = mapM_ aux lines | |
where | |
aux row = putStr "|" >> mapM_ (putStr . conv . (`elem` row)) [1..rn] >> putStrLn "" | |
conv True = "---|" | |
conv False = " |" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment