예제 #1
0
        public void AddMoves(DataTable moves)
        {
            moves.DefaultView.Sort = "Id Asc";
            moves = moves.DefaultView.ToTable(moves.TableName);

            Move m = null;

            for (int i = 0; i < moves.Rows.Count; i++)
            {
                m      = new Move(moves.Rows[i]);
                m.Game = this;

                if (Flags.IsOnline && i == moves.Rows.Count - 1 && !Flags.IsGameFinished)
                {
                    AddMove(m.From, m.To, m.Piece, m.Fen, m, false);

                    MoveTo(MoveToE.Last);

                    DoEngineMoveIfNeeded();
                }
                else
                {
                    CurrentMove = m;
                    Notations.AddMove(m);
                    Moves.Import(m);
                    SetCurrentLine(m);
                }
            }

            if (Flags.IsOffline || (Flags.IsOnline && Flags.IsGameFinished))
            {
                MoveTo(MoveToE.Last);
            }
        }
예제 #2
0
        public Move AddMove(string from, string to, Pieces fromPiece, string fen, Move m, bool isSetFen)
        {
            #region On Before
            if (m == null)
            {
                return(null);
            }

            if (BeforeAddMove != null)
            {
                EngineMoveEventArgs em = new EngineMoveEventArgs();
                em.BestMove   = from + to;
                em.PonderMove = PonderMove;
                BeforeAddMove(this, em);
            }
            #endregion

            #region Before CurrentMove Import
            if (Flags.IsOnline && Flags.IsGameFinished) // due to time expire etc.
            {
                return(null);
            }

            if (Flags.IsOnline && (Flags.IsNotMyTurn || Flags.IsGameFinished))
            {
                Clock.ToggleClock(m);
            }
            else
            {
                #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;

                if (Flags.IsEvalRequired)
                {
                    if (!String.IsNullOrEmpty(CurrentPlayer.Engine.Points) && !String.IsNullOrEmpty(CurrentPlayer.Engine.Depth))
                    {
                        m.SetEval(CurrentPlayer.Engine.Points, CurrentPlayer.Engine.Depth);
                    }
                }

                if (Flags.IsExpectedMoveRequired)
                {
                    if (!String.IsNullOrEmpty(CurrentPlayer.Engine.ExpectedMove))
                    {
                        m.ExpectedMove = CurrentPlayer.Engine.ExpectedMove;
                    }
                }
                #endregion

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

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

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

                m.MoveTime = Clock.MoveTime;

                if (this.Flags.IsFirtMove && this.Flags.IsInfiniteAnalysisOff)
                {
                    Clock.Start();
                }

                #endregion
            }

            #endregion

            Notations.AddMove(m);
            CurrentMove = m.Clone();
            Moves.Import(CurrentMove);

            #region After CurrentMove Import

            SetCurrentLine(CurrentMove);
            CheckNextLongGamePhase(CurrentMove);
            CheckThreefoldRepetition();
            SetCastlingFlags(CurrentMove.Piece, CurrentMove.From, CurrentMove.To);

            Flags.IsPieceMovedSuccessfully = true;

            #region Point Book
            if (GameMode != GameMode.EngineVsEngine && GameMode != GameMode.OnlineEngineVsEngine)
            {
                Book.PointTo(CurrentMove);
            }

            #endregion

            #region Update Captured Pieces

            if (CurrentMove.Flags.IsCapture)
            {
                CapturedPieces.Update(CurrentMove);
            }

            #endregion

            Flags.IsMoveInProgress    = false;
            Flags.IsForceEngineToMove = false;

            #region Check Game Finish
            if (!Flags.IsDatabaseGame)
            {
                #region Mated
                if (Flags.IsMated)
                {
                    Mated();
                    Flags.IsMoveInProgress = true;
                }

                #endregion

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

                #endregion

                #region ThreefoldRepetition
                if (Flags.IsThreeFoldRepetition)
                {
                    ThreefoldRepetition(false);
                }
                #endregion

                #region InsufficientMaterial
                if (Flags.IsInsufficientMaterial)
                {
                    InsufficientMaterial();
                }
                #endregion
            }
            #endregion

            #region SetFen
            if (isSetFen)
            {
                SetFen(CurrentMove.Fen);
            }

            #endregion

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

            #region Set ECO, Play Sound etc.
            SearchEco();
            CheckMoveLimit();
            PlaySounds(m);
            Clock.ResetMoveTime();
            VariationType = VariationTypeE.None;

            if (Flags.IsClockStartRequired)
            {
                Clock.Start();
            }

            #endregion

            #region Send Move To Opponent
            if (Flags.IsUpdateGameRequired)
            {
                SocketClient.UpdateGameDataByGameID(DbGame.GameID, GetLastMoveXml(), GameResult, Flags.Flags, DbGame.OpponentUserID);
            }
            #endregion

            #endregion

            #region On After

            if (AfterAddMove != null)
            {
                AfterAddMove(this, EventArgs.Empty);
            }

            #endregion

            return(CurrentMove);
        }