data Tile           = Gopher | Hole | Flat

instance Show Tile where
  show Gopher = "Gopher"
  show Hole   = "Hole"
  show Flat   = "Flat"

instance Eq Tile where
  Gopher == Gopher = True
  Hole == Hole     = True
  Flat == Flat     = True
  _ == _           = False

-- generating all permuations
permutations        :: [[Tile]] -> [[[Tile]]]

permutations []     =  [[]]
permutations (x:xs) =  concatMap splice (permutations xs)
        where
        splice ys   =  [take d ys ++ z : (drop d ys)
                        | d <- [0 .. length ys], z <- allflips x]


allflips            :: [Tile] -> [[Tile]]
allflips t          =  unique [t, reverse t, fliptile t, (reverse . fliptile) t]

fliptile xs         = map flip1 xs
   where
      flip1 Gopher  = Flat
      flip1 Flat    = Gopher
      flip1 Hole    = Hole

-- checking those permuations for legality
legal board = all id [legalmatch (top!!row!!col) (bot!!col!!row)
		      | row <- [0 .. 2], col <- [0 .. 2]]
      where top = take 3 board
	    bot = drop 3 board

legalmatch Gopher Flat   = False
legalmatch Flat Gopher   = False
legalmatch Gopher Gopher = False  
legalmatch _ _           = True

unique []     = []
unique (x:xs) = x:(filter (\y -> not (x == y)) (unique xs))



main = print "Done"


