Skip to content

Instantly share code, notes, and snippets.

@onigra
Forked from kei-q/amida.hs
Created June 12, 2012 17:21
Show Gist options
  • Save onigra/2918811 to your computer and use it in GitHub Desktop.
Save onigra/2918811 to your computer and use it in GitHub Desktop.
あみだくじ
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