public ChessPosition Clone() { ChessPosition pos = new ChessPosition(); for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { pos.Board[i, j] = Board[i, j]; } } if (LastMove.StartLocation != null) { pos.LastMove = new ChessMove() { StartLocation = LastMove.StartLocation.Clone(), EndLocation = LastMove.EndLocation.Clone(), Promotion = LastMove.Promotion, EnPassant = LastMove.EnPassant, Castling = LastMove.Castling }; } pos.WhiteToMove = WhiteToMove; for (int i = 0; i < 4; i++) { pos.Castling[i] = Castling[i]; } return(pos); }
public bool IsMoveLegal(ChessMove move) { if (move.MovedPiece == ChessPiece.EMPTY) { return(false); } else if (IsPieceWhite(move.MovedPiece) != WhiteToMove) { return(false); } else if (move.Castling != ChessMove.NONE) { if (IsKingInCheck(WhiteToMove)) { return(false); } //TODO: implement castling through check } if (GetLegalDestinations(move.StartLocation).Contains(move.EndLocation)) { ChessPosition clone = Clone(); clone.MakeMoveOnBoard(move); if (!clone.IsKingInCheck(WhiteToMove)) { return(true); } } return(false); }
private static ChessMove ParseTokens(ChessPosition currentPosition, List <string> tokens) { ChessMove currentMove = null; List <string> annotationTokens = new List <string>(); bool inAnnotation = false; int testNum = 0; foreach (string token in tokens) { if (token == "(") { inAnnotation = true; } else if (token == ")") { inAnnotation = false; if (currentMove != null) { currentMove.Alternatives.Add(ParseTokens(currentPosition.Clone(), annotationTokens)); annotationTokens.Clear(); } } else if (inAnnotation) { annotationTokens.Add(token); } else if (token[0] == '{') { if (currentMove != null) { currentMove.Comment = token.Substring(1, token.Length - 2); } } else if (int.TryParse(token, out testNum)) { continue; } else { bool isMove = true; List <char> special = new List <char>() { '_', '+', '#', '=', ':', '-' }; foreach (char c in token) { if (!char.IsDigit(c) && !char.IsLetter(c) && !special.Contains(c)) { isMove = false; } } if (isMove) { if (currentMove != null) { currentPosition.MakeMoveOnBoard(currentMove); } ChessMove newMove = currentPosition.BuildMove(token); newMove.LastMove = currentMove; if (currentMove != null) { currentMove.NextMove = newMove; } currentMove = newMove; } } } while (currentMove.LastMove != null) { currentMove = currentMove.LastMove; } return(currentMove); }
public static ChessGame LoadFromPGN(string pgnText) { List <string> tokens = new List <string>(); bool stringToken = false; bool commentToken = false; bool escape = false; string text = ""; foreach (char c in pgnText) { if (_selfTerminating.Contains(c) && !stringToken && !commentToken) { if (text.Length > 0) { tokens.Add(text); text = ""; } tokens.Add(c.ToString()); } else if (c == '\\') { escape = true; } else if (c == '"') { if (!escape) { stringToken = !stringToken; if (!stringToken) { tokens.Add("\"" + text + "\""); text = ""; } } else { tokens.Add("\""); escape = false; } } else if (c == '{') { commentToken = true; } else if (c == '}') { commentToken = false; if (text.Length > 0) { tokens.Add("{" + text + "}"); text = ""; } } else if (!char.IsWhiteSpace(c) || stringToken || commentToken) { if (char.IsWhiteSpace(c)) { text += " "; } else { text += c; } } else if (char.IsWhiteSpace(c)) { if (text.Length > 0) { tokens.Add(text); text = ""; } } } if (text.Length > 0) { tokens.Add(text); text = ""; } ChessGame game = new ChessGame(); ChessPosition builder = new ChessPosition(); bool tagPair = false; string name = ""; string value = ""; List <string> moveTokens = new List <string>(); Dictionary <string, ChessResult> results = new Dictionary <string, ChessResult>(); results["1-0"] = ChessResult.WHITE_WINS; results["0-1"] = ChessResult.BLACK_WINS; results["1/2-1/2"] = ChessResult.DRAW; results["*"] = ChessResult.UNAVAILABLE; foreach (string token in tokens) { if (token == "[") { tagPair = true; } else if (token == "]") { tagPair = false; game.AddTagPair(name, value); name = ""; value = ""; } else if (tagPair) { if (name.Length < 1) { name = token; } else { value = token; } } else if (results.ContainsKey(token)) { break; } else { moveTokens.Add(token); } } game.Moves = ParseTokens(builder, moveTokens); return(game); }
public ChessMove ParseMoves(ChessPosition startingPosition, List <string> tokens) { return(null); }