Example #1
0
 public void InvalidPieceCharConvertedToIntThrows(char pieceChar)
 {
     Assert.Throws <ArgumentException>(() => NotationHelper.GetPiece(pieceChar));
 }
Example #2
0
        public Position Read(string fenString)
        {
            try
            {
                PieceArray whitePieces = new PieceArray(0),
                           blackPieces = new PieceArray(1);
                var  chunks            = fenString.Split();
                bool isChess960        = !chunks[2].ToLower().Any(c => c == 'q' || c == 'k');

                var boardChunks = chunks[0].Split(new[] { '/' });

                for (int rank = 0; rank < Constants.BoardRanks; rank++)
                {
                    var rankChunk = boardChunks[rank];
                    int file      = 0;
                    foreach (var item in rankChunk)
                    {
                        if (int.TryParse(item.ToString(), out var gap))
                        {
                            file += gap;
                        }
                        else
                        {
                            int square = rank * Constants.BoardFiles + file;
                            int piece  = NotationHelper.GetPiece(item);
                            if ((piece & Piece.White) != 0)
                            {
                                whitePieces.Add(piece & Piece.TypeMask, square);
                            }
                            else
                            {
                                blackPieces.Add(piece & Piece.TypeMask, square);
                            }

                            file++;
                        }
                    }
                }

                GameState initialGameState = new GameState();

                if (chunks[2] != "-")
                {
                    initialGameState[0] = NotationHelper.GetCastlingOptions(chunks[2], true, isChess960, whitePieces.Rooks, whitePieces.King);
                    initialGameState[1] = NotationHelper.GetCastlingOptions(chunks[2], false, isChess960, blackPieces.Rooks, blackPieces.King);
                }

                if (chunks[3] != "-")
                {
                    initialGameState.EnPassantSquare = NotationHelper.ParseSquare(chunks[3]);
                }

                int totalMoves = 1;
                if (chunks.Length > 4)
                {
                    initialGameState.HalfMoveClock = int.Parse(chunks[4]);
                    totalMoves = int.Parse(chunks[5]);
                }

                return(new Position(whitePieces, blackPieces, initialGameState)
                {
                    WhiteToMove = chunks[1].ToLower() == "w",
                    TotalMoves = totalMoves,
                    IsChess960 = isChess960
                });
            }
            catch (Exception e)
            {
                throw new FormatException($"Wrong fen string: {fenString}", e);
            }
        }
Example #3
0
 public void PieceCharConvertedToInt(char pieceChar, int expectedPieceCode)
 {
     Assert.AreEqual(expectedPieceCode, NotationHelper.GetPiece(pieceChar));
 }