summaryrefslogtreecommitdiff
path: root/puzzle.hs
diff options
context:
space:
mode:
authorJustus Winter <4winter@informatik.uni-hamburg.de>2014-10-06 20:25:26 +0200
committerJustus Winter <4winter@informatik.uni-hamburg.de>2014-10-06 20:25:26 +0200
commitae7023ba666bbac6a02d9b8fbd9a33083bc944e3 (patch)
treec3d6f692b88eacd7f4575257cbce0871ace52447 /puzzle.hs
parent61233a0c7f9f80905d24bbbeb73ce8abbcc08461 (diff)
cmdargs parsing
Diffstat (limited to 'puzzle.hs')
-rw-r--r--puzzle.hs12
1 files changed, 11 insertions, 1 deletions
diff --git a/puzzle.hs b/puzzle.hs
index 5da1ff0..687b676 100644
--- a/puzzle.hs
+++ b/puzzle.hs
@@ -1,11 +1,15 @@
+{-# LANGUAGE DeriveDataTypeable #-}
+
module Main where
+import Control.Monad (when)
import Data.Array (Array, array, bounds, (!), (//))
import Data.Functor ((<$>))
import Data.Maybe
import Data.List (sort)
import Data.Ord (compare)
import Data.Char (toUpper)
+import System.Console.CmdArgs (Data, Typeable, cmdArgs)
import System.Random (RandomGen, getStdGen, split, randomR, randomRs)
import System.Random.Shuffle
@@ -105,6 +109,7 @@ insert w p b =
insertAll :: RandomGen gen => gen -> [String] -> Board -> Maybe Board
insertAll _ [] b = Just b
insertAll rnd (w:ws) b = do
+ when (null moves) Nothing
b' <- insert w (pose $ sort moves !! nth) b
insertAll rnd' ws b'
where
@@ -119,12 +124,17 @@ fill rnd b = b // zip [(x, y) | x <- [sX..eX], y <- [sY..eY], (b ! (x, y)) == em
where
((sX, sY), (eX, eY)) = bounds b
+data Puzzle = Puzzle { blanks :: Bool
+ , dimension :: (Int, Int)
+ } deriving (Data, Typeable, Show)
+
main :: IO ()
main = do
+ args <- cmdArgs $ Puzzle False (30, 30)
rnd <- getStdGen
c <- getContents
let guests = lines (map toUpper c) in
- case (showBoard . fill rnd) <$> insertAll rnd guests (mkBoard (40, 30)) of
+ case (showBoard . (if blanks args then id else fill rnd)) <$> insertAll rnd guests (mkBoard $ dimension args) of
Just s -> putStr s
Nothing -> putStr "goesnt"