private void InitializeStandardBoard() { // Clear all the squares InitializeBlankBoard(); // Setup white const string WHITE = "RNBQKBNRPPPPPPPP"; for (int i = 0; i < 16; i++) { _board[i] = new Piece(WHITE[i]); } // Setup black const string BLACK = "pppppppprnbqkbnr"; for (int i = 0; i < 16; i++) { _board[i + 48] = new Piece(BLACK[i]); } }
/// <summary> /// Construct from Forsyth–Edwards Notation (FEN), /// http://en.wikipedia.org/wiki/Forsyth%E2%80%93Edwards_Notation /// </summary> /// <param name="fen"></param> public Board(string fen) { var parts = fen.Split(new[] { ' ' }, 6); if (parts.Length != 6) { throw new ArgumentException("fen is in an incorrect format. It does not have 6 parts."); } var ranks = parts[0].Split(new[] { '/' }); if (ranks.Length != 8) { throw new ArgumentException("fen is in an incorrect format. The first part does not have 8 ranks."); } // Setup the board InitializeBlankBoard(); int i = 0; // FEN starts with rank 8 and ends with rank 1 for (int r = 7; r >= 0; r--) { foreach (char p in ranks[r]) { if (char.IsDigit(p)) { // Blank squares to skip int skip = p - '0'; if (skip > 8) { throw new ArgumentException("fen is in an incorrect format."); } i += skip; } else { if (i > 63) { throw new ArgumentException("fen is in an incorrect format."); } _board[i++] = new Piece(p); } } } // Active Colour _activeColour = parts[1].ToLowerInvariant() == "w" ? PieceColour.White : PieceColour.Black; // Castling availability _whiteKingside = false; _whiteQueenside = false; _blackKingside = false; _blackQueenside = false; foreach (char c in parts[2]) { switch (c) { case 'K': _whiteKingside = true; break; case 'Q': _whiteQueenside = true; break; case 'k': _blackKingside = true; break; case 'q': _blackQueenside = true; break; } } // En passant target square _enPassantTarget = IndexFromSquare(parts[3]); // Halfmove clock if (!Int32.TryParse(parts[4], NumberStyles.Integer, CultureInfo.InvariantCulture, out _halfMoveClock)) { throw new ArgumentException("fen is in an incorrect format. Halfmove clock is not an int."); } // Fullmove number if (!Int32.TryParse(parts[5], NumberStyles.Integer, CultureInfo.InvariantCulture, out _fullMoveNumber)) { throw new ArgumentException("fen is in an incorrect format. Fullmove number is not an int."); } }
private void InitializeBlankBoard() { for (int i = 0; i < 64; i++) { _board[i] = new Piece(); } }