module Sudoku.Type(
Sudoku
, Difficulty(..)
, boardsize
, boxsize
, cellvals
, blankval
, fromString
, toString
) where
import Util
newtype Sudoku = Sudoku String
data Difficulty = Easy | Medium | Hard | Evil deriving (Read, Show, Eq, Enum)
instance Show Sudoku where
show (Sudoku s) = "\n" ++ (concat $ map showThird grouped) ++ line
where grouped = groupBy boxsize . map (groupBy boxsize) . groupBy boardsize $ s
showThird t = line ++ (concat $ map showRow t)
showRow r = (concat $ map showRowThird r) ++ "|\n"
showRowThird rt = "|" ++ (concat $ map showCell rt)
showCell c = " " ++ (c : " ")
line = (concat $ replicate boardsize "---") ++ (concat $ replicate boxsize "-") ++ "-\n"
boardsize :: Int
boardsize = 9
boxsize :: Int
boxsize = 3
cellvals :: [Char]
cellvals = "123456789"
blankval :: Char
blankval = ' '
valid :: String -> Bool
valid s = (length s == (boardsize * boardsize)) &&
(all (`elem` blankval:cellvals) s)
fromString :: String -> Maybe Sudoku
fromString s
| valid s = Just $ Sudoku s
| otherwise = Nothing
toString :: Sudoku -> String
toString (Sudoku s) = s