Beispiel #1
0
        // continue from FEN(Forsyth–Edwards Notation) position
        public static ChessGame ContinueFromFEN(string fen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1")
        {
            var parts = fen.Split();

            var board = BoardFactory.LoadBoardFromFen(out int turnCounter, fen);

            var game = new ChessGame()
            {
                board = board
            };

            game.TurnCounter = turnCounter;

            return(game);
        }
Beispiel #2
0
        // this parsing is too simplistic to catch all the nuances of proper PGN, but it works for simple examples,
        // and it might even get complex examples correct(but it shouldn't relied upon)
        public static void parseBody(string body, ChessGame game)
        {
            body = removeComments(body);
            body = body.Replace("\n", " ");
            body = body.Replace("\r", " ");
            var parts = body.Split(" ");
            var moves = game.Moves();

            foreach (var part in parts)
            {
                if (part.Contains("."))
                {
                    continue;
                }
                var move = moves.Find(move => move.san == part.Trim());
                if (move != null)
                {
                    game.Move(move.move);
                    moves = game.Moves();
                }
            }
        }
Beispiel #3
0
        public static ChessGame parse(string pgnString)
        {
            var       pgn   = new PGN();
            var       lines = pgnString.Split("\n");
            ChessGame game  = null;

            bool headerSection = true;
            var  body          = "";

            foreach (var line in lines)
            {
                if (line.Trim() == "")
                {
                    if (headerSection == false)
                    {
                        return(game);
                    }
                    headerSection = false;
                    game          = ChessGame.ContinueFromFEN(pgn.FEN);
                    game.pgn      = pgn;
                    continue;
                }
                if (headerSection)
                {
                    parseHeader(line, pgn);
                }
                else
                {
                    body += line + "\n";
                }
            }

            parseBody(body, game);


            return(game);
        }
Beispiel #4
0
 public static String AsciiBoard(ChessGame game, List <Move> moves = null, bool displayCount = false)
 {
     return(AsciiBoard(game.board, moves, displayCount));
 }
Beispiel #5
0
        public static ChessState GameToState(ChessGame game)
        {
            var chessState = new ChessState();
            //var moves = board.GetMoves().Where(move => board.IsLegalMove(move));
            var moves = game.Moves();

            for (int column = 0; column < 8; column++)
            {
                for (int row = 0; row < 8 * BoardStateOffset.ROW_OFFSET; row += BoardStateOffset.ROW_OFFSET)
                {
                    var   position = row + column;
                    Piece piece    = game.board.GetPiece(position);
                    if (piece != Piece.EMPTY)
                    {
                        var from       = BoardPosition.x88PositionToCoordinate(position);
                        var chessPiece = new ChessPiece()
                        {
                            piece   = PieceParser.ToChar(piece).ToString(),
                            row     = from.row,
                            column  = from.column,
                            isWhite = (piece & Piece.IS_WHITE) == Piece.IS_WHITE
                        };

                        chessState.pieces.Add(chessPiece);

                        foreach (var move in moves)
                        {
                            if (move.move.fromPosition == position)
                            {
                                var to = BoardPosition.x88PositionToCoordinate(move.move.targetPosition);
                                chessPiece.options.Add(new PieceOption()
                                {
                                    row         = to.row,
                                    column      = to.column,
                                    isPromotion = (Piece)move.move.promotion != Piece.EMPTY,
                                    isCastle    = ((MoveFlags)move.move.moveFlags & MoveFlags.CASTLING) == MoveFlags.CASTLING,
                                    isEnpassant = (((MoveFlags)move.move.moveFlags & MoveFlags.ENPASSANT) == MoveFlags.ENPASSANT),
                                    san         = move.san
                                });
                            }
                        }
                    }
                }
            }
            var winner = game.Winner();

            if (winner == Winner.DRAW)
            {
                chessState.isDraw = true;
            }
            else if (winner == Winner.WINNER_BLACK)
            {
                chessState.blackWins = true;
            }
            else if (winner == Winner.WINNER_WHITE)
            {
                chessState.whiteWins = true;
            }

            chessState.fen = game.FEN;

            return(chessState);
        }