Example #1
0
        /// <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);
        }