Exemplo n.º 1
0
        public Move AddMove(string from, string to, Pieces fromPiece, string fen, Move m, bool isPastGame, bool isSetFen)
        {
            #region On Before
            if (m == null)
            {
                return(null);
            }

            if (BeforeAddMove != null)
            {
                BeforeAddMove(this, EventArgs.Empty);
            }
            #endregion

            if (isPastGame)
            {
                Clock.SetClock(m);
            }
            else
            {
                #region IsVariation
                if (Flags.IsVariation)
                {
                    if (AddVariationMove())
                    {
                        return(null);
                    }
                    else
                    {
                        m.Flags.VariationType = this.VariationType;
                    }
                }
                #endregion

                #region Prepare Move
                if (!m.Flags.IsPromotion) // promoted piece is already assigned in promotion dialog
                {
                    m.Piece = fromPiece;
                }

                m.Id      = NextMoveId;
                m.Pid     = NextMovePid;
                m.MoveNo  = NextMoveNo;
                m.IsWhite = NextMoveIsWhite;
                m.From    = from;
                m.To      = to;
                m.Fen     = fen;
                #endregion

                #region Set Clock
                if (Flags.IsOnline)
                {
                    if (!m.IsWhite)
                    {
                        Clock.WhiteTime += DbGame.GainPerMoveMin;
                    }
                    else
                    {
                        Clock.BlackTime += DbGame.GainPerMoveMin;
                    }
                }

                m.MoveTimeWhite = Clock.WhiteTime;
                m.MoveTimeBlack = Clock.BlackTime;

                m.MoveTime = Clock.MoveTime;

                Clock.Start();
                #endregion
            }

            CurrentMove = m.Clone();

            SetFenCastlingNotation(m.Piece, m.From, m.To);

            Flags.IsPieceMovedSuccessfully = true;

            CompareFlags(CurrentMove);
            Notations.SetMove(CurrentMove);
            Book.SetMove(CurrentMove);

            //if (CurrentMove.CapturedPiece != Pieces.NONE)
            //{
            //    SetCapturedPiecesParameters();
            //    CapturedPieces.AddPiece();
            //}

            Flags.IsMoveInProgress    = false;
            Flags.IsForceEngineToMove = false;
            Flags.IsManualMove        = false;
            Flags.IsSpacebarClick     = false;

            if (Flags.IsMated)
            {
                Mated();
                Flags.IsMoveInProgress = true;
            }

            if (Flags.IsStaleMated)
            {
                StaleMated();
            }

            if (Flags.IsThreeFoldRepetition)
            {
                ThreefoldRepetition(false);
            }

            if (this.HalfMovesCounter >= 100)
            {
                FifityMoves();
            }

            TogglePlayers();

            if (isSetFen)
            {
                SetFen(m.Fen);
            }

            Moves.Import(CurrentMove);

            VariationType = VariationTypeE.None;

            #region On After
            if (AfterAddMove != null)
            {
                AfterAddMove(this, EventArgs.Empty);
            }

            #endregion

            return(m);
        }