[Haskell-beginners] Re: Chessboard Module, opinions on...

Christian Maeder Christian.Maeder at dfki.de
Wed Oct 28 07:52:41 EDT 2009


iæfai schrieb:
> 
> I have just recently finished a 'ChessBoard' module that is meant to
> represent a chess board. I could use some opinions and/or suggestions on
> the module.
> 
> To give an example of how this can be used right now, and was my
> immediate goal, you can do this:
> 
> *ChessBoard> putStr $ cout defaultBoard
> +----+----+----+----+----+----+----+----+
> | RB | NB | BB | QB | KB | BB | NB | RB |
> +----+----+----+----+----+----+----+----+
> | PB | PB | PB | PB | PB | PB | PB | PB |
> +----+----+----+----+----+----+----+----+
> |    |    |    |    |    |    |    |    |
> +----+----+----+----+----+----+----+----+
> |    |    |    |    |    |    |    |    |
> +----+----+----+----+----+----+----+----+
> |    |    |    |    |    |    |    |    |
> +----+----+----+----+----+----+----+----+
> |    |    |    |    |    |    |    |    |
> +----+----+----+----+----+----+----+----+
> | PW | PW | PW | PW | PW | PW | PW | PW |
> +----+----+----+----+----+----+----+----+
> | RW | NW | BW | QW | KW | BW | NW | RW |
> +----+----+----+----+----+----+----+----+

nice!

> data Piece = Bishop | Rook | Knight | King | Queen | Pawn | NoPiece
>     deriving (Show, Eq)
> 
> instance NiceLook Piece where
>         cout Bishop = "B"
>         cout Rook   = "R"
>         cout Knight = "N"
>         cout Queen  = "Q"
>         cout Pawn   = "P"
>         cout King   = "K"
>         cout _      = " "
> 
> data Colour = Black | White | NoColour
>     deriving (Show, Eq)
> 
> instance NiceLook Colour where
>         cout Black = "B"
>         cout White = "W"
>         cout NoColour = " "
> 
>         -- error "..." might be useful
> 
> data Square = Square Piece Colour
>     deriving (Show, Eq)
> 
> instance NiceLook (Square) where
>         cout (Square p c) = (cout p) ++ (cout c)

I'd omit NoPiece and NoColour in Piece and Colour and add an "Empty" to
Square!

"cout" (of "nice") for Piece and Colour could then be simply "take 1 .
show", if you change Knight to Nknight.

  nice s = case s of
     Square p c -> take 1 (show p) ++ take 1 (show c)
     Empty -> "  "

Maybe an extra class NiceLook is not even necessary.

Cheers Christian



More information about the Beginners mailing list