Skip to content

Commit

Permalink
Adapts the code so that it can compile with TinyGo.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mariano Gappa committed Mar 29, 2020
1 parent 0f23ceb commit 346c256
Show file tree
Hide file tree
Showing 10 changed files with 2,037 additions and 2,031 deletions.
13 changes: 6 additions & 7 deletions api/api.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
package api

import (
"fmt"
)
import "errors"

type API struct{}

func New() API { return API{} }

var (
errInvalidInputGame = fmt.Errorf("invalid input game: please supply a valid fenString or a board")
errAlgebraicSquareInvalidOrOutOfBounds = fmt.Errorf("invalid algebraic square: empty or out of bounds")
errInvalidPieceTypeName = fmt.Errorf("invalid piece type name: please use one of {Queen|King|Bishop|Knight|Rook|Pawn} or empty string")
errInvalidActionForGivenGame = fmt.Errorf("the specified action is invalid for the specified game")
errInvalidInputGame = errors.New("invalid input game: please supply a valid fenString or a board")
errAlgebraicSquareInvalidOrOutOfBounds = errors.New("invalid algebraic square: empty or out of bounds")
errInvalidPieceTypeName = errors.New("invalid piece type name: please use one of {Queen|King|Bishop|Knight|Rook|Pawn} or empty string")
errInvalidActionForGivenGame = errors.New("the specified action is invalid for the specified game")
)

func (a API) DefaultGame() OutputGame {
var defaultGame, _ = newGameFromFEN("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1")
return mapGameToOutputGame(defaultGame)
}

Expand Down
1 change: 1 addition & 0 deletions api/api_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ func (a API) parseGame(g InputGame) (game, error) {
)
switch {
case g.DefaultGame:
var defaultGame, _ = newGameFromFEN("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1")
parsedGame = defaultGame
case g.FENString != "":
parsedGame, err = newGameFromFEN(g.FENString)
Expand Down
31 changes: 16 additions & 15 deletions api/board.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package api

import (
"errors"
"fmt"
"strings"
)
Expand All @@ -18,21 +19,21 @@ type board struct {
}

var (
errBoardInvalidEnPassantTargetSquare = fmt.Errorf("enPassantTargetSquare must be either empty string or valid algebraic notation square e.g. d6")
errBoardTurnMustBeBlackOrWhite = fmt.Errorf("turn must be either Black or White")
errBoardDuplicateKing = fmt.Errorf("board has two kings of the same color")
errBoardKingMissing = fmt.Errorf("board is missing one of the kings")
errBoardDimensionsWrong = fmt.Errorf("board dimensions are wrong; should be 8x8")
errBoardImpossibleBlackCastle = fmt.Errorf("impossible for black to castle since king has moved")
errBoardImpossibleBlackQueensideCastle = fmt.Errorf("impossible for black to queenside castle since rook has moved")
errBoardImpossibleBlackKingsideCastle = fmt.Errorf("impossible for black to kingside castle since rook has moved")
errBoardImpossibleWhiteCastle = fmt.Errorf("impossible for white to castle since king has moved")
errBoardImpossibleWhiteQueensideCastle = fmt.Errorf("impossible for white to queenside castle since rook has moved")
errBoardImpossibleWhiteKingsideCastle = fmt.Errorf("impossible for white to kingside castle since rook has moved")
errBoardImpossibleEnPassant = fmt.Errorf("impossible en passant target square, since there's no pawn of the right color next to it")
errBoardPawnInImpossibleRank = fmt.Errorf("impossible rank for pawn")
errBoardBlackHasMoreThan16Pieces = fmt.Errorf("black has more than 16 pieces")
errBoardWhiteHasMoreThan16Pieces = fmt.Errorf("white has more than 16 pieces")
errBoardInvalidEnPassantTargetSquare = errors.New("enPassantTargetSquare must be either empty string or valid algebraic notation square e.g. d6")
errBoardTurnMustBeBlackOrWhite = errors.New("turn must be either Black or White")
errBoardDuplicateKing = errors.New("board has two kings of the same color")
errBoardKingMissing = errors.New("board is missing one of the kings")
errBoardDimensionsWrong = errors.New("board dimensions are wrong; should be 8x8")
errBoardImpossibleBlackCastle = errors.New("impossible for black to castle since king has moved")
errBoardImpossibleBlackQueensideCastle = errors.New("impossible for black to queenside castle since rook has moved")
errBoardImpossibleBlackKingsideCastle = errors.New("impossible for black to kingside castle since rook has moved")
errBoardImpossibleWhiteCastle = errors.New("impossible for white to castle since king has moved")
errBoardImpossibleWhiteQueensideCastle = errors.New("impossible for white to queenside castle since rook has moved")
errBoardImpossibleWhiteKingsideCastle = errors.New("impossible for white to kingside castle since rook has moved")
errBoardImpossibleEnPassant = errors.New("impossible en passant target square, since there's no pawn of the right color next to it")
errBoardPawnInImpossibleRank = errors.New("impossible rank for pawn")
errBoardBlackHasMoreThan16Pieces = errors.New("black has more than 16 pieces")
errBoardWhiteHasMoreThan16Pieces = errors.New("white has more than 16 pieces")
// TODO check if King is in checkmate that couldn't have been reached
// TODO don't allow more than 8 pawns of any color
// TODO check if both are in check
Expand Down
Loading

0 comments on commit 346c256

Please sign in to comment.