pilatch / elm-chess / Piece

Types


type alias Piece =
Internal.Piece

Type representing a chess piece, with a color (white or black) and a type (pawn, bishop, knight, etc.).

Creating and Manipulating Pieces

make : PieceColor -> PieceType -> Piece

Create a piece with the given color and type.

make PieceColor.white PieceType.queen == whiteQueen

make PieceColor.black PieceType.knight == blackKnight

color : Piece -> PieceColor

Get the color of a piece.

color whiteBishop == PieceColor.white

color blackQueen == PieceColor.black

kind : Piece -> PieceType

Get the type of a piece. Unfortunately, the name type is taken, so we have to use an awkward name.

kind whiteBishop == PieceType.bishop

kind blackQueen == PieceType.queen

Useful Constants

all : List Piece

List of all pieces

whitePawn : Piece

A white pawn.

whiteKnight : Piece

A white knight.

whiteBishop : Piece

A white bishop.

whiteRook : Piece

A white rook.

whiteQueen : Piece

A white queen.

whiteKing : Piece

A white king.

blackPawn : Piece

A black pawn.

blackKnight : Piece

A black knight.

blackBishop : Piece

A black bishop.

blackRook : Piece

A black rook.

blackQueen : Piece

A black queen.

blackKing : Piece

A black king.

Converting Pieces to/from Chars and Strings

fromChar : Char -> Maybe Piece

Tries to convert a character to a piece, using Forsyth-Edwards notation. Uppercase letters become white pieces, lowercase letters become black pieces. If the character is not a valid piece letter, returns Nothing.

fromChar 'R' == Just whiteRook

fromChar 'q' == Just blackQueen

fromChar 'x' == Nothing

fromString : String -> Maybe Piece

Tries to convert a string to a piece by inspecting the first character and using Forsyth-Edwards notation. Uppercase letters become white pieces, lowercase letters become black pieces. If the character is not a valid piece letter, or if the string is empty, returns Nothing.

fromString "R" == Just whiteRook

fromString "Kbb" == Just whiteKing

fromString "q" == Just blackQueen

fromString "x" == Nothing

toChar : Piece -> Char

Converts a piece to its corresponding character in Forsyth-Edwards notation. White pieces become uppercase letters, black pieces become lowercase letters.

toChar whiteRook == 'R'

toChar blackQueen == 'q'

toString : Piece -> String

Converts a piece to a single-character string containing the pices's corresponding character in Forsyth-Edwards notation. White pieces become uppercase letters, black pieces become lowercase letters.

toString whiteRook == "R"

toString blackQueen == "q"