private int GetMovingPiece(Board board, PGNMove move, int fromX, int fromY) { // figure out which piece is moving int from = -1; var pieces = board.FindByPieceAndColor(move.Piece, board.PlayerTurn); var possible = new List <int>(); foreach (var p in pieces) { var moves = Moves.GetValidMoves(board, p); if (moves.Contains(move.To)) { possible.Add(p); } } if (possible.Count == 0) { throw new Exception("No piece has a valid move to tile " + Notation.TileToText(move.To)); } if (possible.Count > 1) { // filter possible pieces by using the hints if (fromX != -1) { possible = possible.Where(k => Board.X(k) == fromX).ToList(); } if (fromY != -1) { possible = possible.Where(k => Board.Y(k) == fromY).ToList(); } } if (possible.Count != 1) { throw new Exception("No piece has a valid move to tile " + Notation.TileToText(move.To)); } from = possible[0]; return(from); }
public static PGNGame DecodeGame(string encodedString) { var s = encodedString; int i = 0; Color player = Color.White; int move = 1; var moves = new List <PGNMove>(); PGNMove lastmove = null; while (i < s.Length - 1) { // check if the last move wa actually a promotion if (Decode[s[i]] == SpecialInstruction) { lastmove.Promotion = (Piece)Decode[s[i + 1]]; continue; } var from = Decode[s[i]]; var to = Decode[s[i + 1]]; lastmove = new PGNMove() { From = from, To = to, MoveNumber = move, Color = player /*, Piece = ...*/ }; if (player == Color.Black) { move++; } player = (player == Color.White) ? Color.Black : Color.White; moves.Add(lastmove); i += 2; } var variation = new GameVariation(moves.Select(x => (IPGNElement)x).ToList()); var game = new PGNGame(new Dictionary <string, string>(), variation); return(game); }
/// <summary> /// Returns information about the move. /// Is not guaranteed to return the "From" or "Color" field. /// Includes: /// Piece /// To /// Promotion /// Check /// Mate /// </summary> /// <returns></returns> public PGNMove GetMove(Board board) { var val = Value.Trim(); var move = new PGNMove(); move.Check = val.Contains('+') || val.Contains('#'); move.Mate = val.Contains('#'); move.Capture = val.Contains('x'); move.Color = board.PlayerTurn; move.MoveNumber = board.MoveCount; val = Regex.Replace(val, @"[x\.\+\#]", ""); move.Piece = Notation.GetPiece(val); move.Promotion = Notation.GetPromotion(val); // remove piece type and promotion if (move.Piece != Piece.Pawn) { val = val.Substring(1); } if (move.Promotion != Piece.None) { val = val.Replace("=", ""); val = val.Substring(0, val.Length - 1); } if (val == "O-O") { move.From = (move.Color == Color.White) ? 4 : 60; move.To = (move.Color == Color.White) ? 6 : 62; move.Piece = Piece.King; } else if (val == "O-O-O") { move.From = (move.Color == Color.White) ? 4 : 60; move.To = (move.Color == Color.White) ? 2 : 58; move.Piece = Piece.King; } else { move.To = Notation.TextToTile(val.Substring(val.Length - 2)); int fromX = -1; int fromY = -1; // check if there is additional information in the move, e.g. "From" tile if (val.Length == 4) { move.From = Notation.TextToTile(val.Substring(0, 2)); } else if (val.Length == 3) { char ch = val[0]; if (Char.IsLetter(ch)) { fromX = Notation.FileToInt(ch); } else { fromY = Notation.RankToInt(ch); } } move.From = GetMovingPiece(board, move, fromX, fromY); } return(move); }
/// <summary> /// Returns information about the move. /// Is not guaranteed to return the "From" or "Color" field. /// Includes: /// Piece /// To /// Promotion /// Check /// Mate /// </summary> /// <returns></returns> public PGNMove GetMove(Board board) { var val = Value.Trim(); var move = new PGNMove(); move.Check = val.Contains('+') || val.Contains('#'); move.Mate = val.Contains('#'); move.Capture = val.Contains('x'); move.Color = board.PlayerTurn; move.MoveNumber = board.MoveCount; val = Regex.Replace(val, @"[x\.\+\#]", ""); move.Piece = Notation.GetPiece(val); move.Promotion = Notation.GetPromotion(val); // remove piece type and promotion if(move.Piece != Piece.Pawn) val = val.Substring(1); if(move.Promotion != Piece.None) { val = val.Replace("=", ""); val = val.Substring(0, val.Length - 1); } if(val == "O-O") { move.From = (move.Color == Color.White) ? 4 : 60; move.To = (move.Color == Color.White) ? 6 : 62; move.Piece = Piece.King; } else if (val == "O-O-O") { move.From = (move.Color == Color.White) ? 4 : 60; move.To = (move.Color == Color.White) ? 2 : 58; move.Piece = Piece.King; } else { move.To = Notation.TextToTile(val.Substring(val.Length - 2)); int fromX = -1; int fromY = -1; // check if there is additional information in the move, e.g. "From" tile if (val.Length == 4) { move.From = Notation.TextToTile(val.Substring(0, 2)); } else if (val.Length == 3) { char ch = val[0]; if (Char.IsLetter(ch)) fromX = Notation.FileToInt(ch); else fromY = Notation.RankToInt(ch); } move.From = GetMovingPiece(board, move, fromX, fromY); } return move; }
private int GetMovingPiece(Board board, PGNMove move, int fromX, int fromY) { // figure out which piece is moving int from = -1; var pieces = board.FindByPieceAndColor(move.Piece, board.PlayerTurn); var possible = new List<int>(); foreach (var p in pieces) { var moves = Moves.GetValidMoves(board, p); if (moves.Contains(move.To)) possible.Add(p); } if (possible.Count == 0) throw new Exception("No piece has a valid move to tile " + Notation.TileToText(move.To)); if (possible.Count > 1) { // filter possible pieces by using the hints if (fromX != -1) possible = possible.Where(k => Board.X(k) == fromX).ToList(); if (fromY != -1) possible = possible.Where(k => Board.Y(k) == fromY).ToList(); } if (possible.Count != 1) throw new Exception("No piece has a valid move to tile " + Notation.TileToText(move.To)); from = possible[0]; return from; }
public static string EncodeMove(PGNMove move) { return EncodeMove(move.From, move.To, move.Promotion); }
public static PGNGame DecodeGame(string encodedString) { var s = encodedString; int i = 0; Color player = Color.White; int move = 1; var moves = new List<PGNMove>(); PGNMove lastmove = null; while(i < s.Length - 1) { // check if the last move wa actually a promotion if (Decode[s[i]] == SpecialInstruction) { lastmove.Promotion = (Piece)Decode[s[i + 1]]; continue; } var from = Decode[s[i]]; var to = Decode[s[i + 1]]; lastmove = new PGNMove() { From = from, To = to, MoveNumber = move, Color = player/*, Piece = ...*/ }; if(player == Color.Black) move++; player = (player == Color.White) ? Color.Black : Color.White; moves.Add(lastmove); i += 2; } var variation = new GameVariation(moves.Select(x => (IPGNElement)x).ToList()); var game = new PGNGame(new Dictionary<string, string>(), variation); return game; }
public static string EncodeMove(PGNMove move) { return(EncodeMove(move.From, move.To, move.Promotion)); }