Beispiel #1
0
        public void MoveTo(MoveToE moveTo, bool moveFromBook)
        {
            Move m = null;

            if ((Flags.IsFirstMoveSelected || Flags.IsRootMoveSelected) && (moveTo == MoveToE.Previous || moveTo == MoveToE.First))
            {
                if (Flags.IsRootMoveSelected)
                {
                    return;
                }

                SetInitialPosition();
                return;
            }

            if (moveFromBook && moveTo == MoveToE.Next)
            {
                Book.MoveTo(moveTo, true);
                return;
            }

            if (Flags.IsFirtMove && moveTo == MoveToE.Next)
            {
                return;
            }

            switch (moveTo)
            {
            case MoveToE.First:
                m = Moves.First;
                break;

            case MoveToE.Last:
                m = Moves.Last;
                break;

            case MoveToE.Next:
                m = GetNextMove(CurrentMove);
                break;

            case MoveToE.Previous:
                m = Moves.Prev(CurrentMove);
                break;

            case MoveToE.Up:
                break;

            case MoveToE.Down:
                break;

            default:
                break;
            }


            Flags.IsRetracMove = false;

            CurrentMove = m.Clone();

            Clock.MoveTo(moveTo);
            Notations.MoveTo(moveTo);
            Book.MoveTo(moveTo, false);
            CapturedPieces.MoveTo(moveTo);

            SetFen(CurrentMove.Fen);
            SwapPlayersIfNeeded();
        }
        private bool AddVariationMove(Move m)
        {
            if (GameMode == GameMode.Kibitzer)
            {
                CurrentMove = Moves.Last;
                return(false);
            }

            if (Flags.IsRetracMove)
            {
                this.VariationType = VariationTypeE.Overwrite;
                return(false);
            }

            if (!Flags.IsFirtMove)
            {
                Moves children = Moves.GetChildren(CurrentMove);

                if (children.Count > 0)
                {
                    if (m.Flags.MoveBy == MoveByE.Human)
                    {
                        if (m.From == children[0].From && m.To == children[0].To)
                        {
                            MoveTo(children[0]);
                            return(true);
                        }
                    }
                    else if (m.Flags.MoveBy == MoveByE.Book)
                    {
                        for (int i = 0; i < children.Count; i++)
                        {
                            if (m.From == children[i].From && m.To == children[i].To)
                            {
                                MoveTo(children[i]);
                                return(true);
                            }
                        }
                    }
                }
            }

            FormClosingEventArgs e = new FormClosingEventArgs(CloseReason.None, false);

            if (Flags.IsVariation)
            {
                if (m.Flags.MoveBy != MoveByE.Human)
                {
                    this.VariationType = VariationTypeE.Variation;
                    return(false);
                }

                if (AddNewVariation != null)
                {
                    AddNewVariation(this, e);
                }
            }

            if (e.Cancel)
            {
                SetFen(CurrentMove.Fen);
            }

            return(e.Cancel);
        }
 public void Paste(Moves moves)
 {
     Paste(moves.DataTable);
 }
        public void MoveTo(MoveToE moveTo)
        {
            Move m = null;

            switch (moveTo)
            {
            case MoveToE.First:
                m = Moves.First;
                //Notations.MoveTo(moveTo);
                break;

            case MoveToE.Last:
                m = Moves.Last;
                break;

            case MoveToE.Next:
                m = Moves.Next(CurrentMove);
                //Notations.MoveTo(moveTo);
                break;

            case MoveToE.Previous:
                m = Moves.Prev(CurrentMove);
                //Notations.MoveTo(moveTo);
                break;

            case MoveToE.Up:
                break;

            case MoveToE.Down:
                break;

            default:
                break;
            }


            if (m == null && moveTo == MoveToE.Previous)
            {
                return;
            }

            if (m == null)
            {
                Book.MoveTo(moveTo, true);
                return;
            }

            if (CurrentMove.Id == m.Id)
            {
                Book.MoveTo(moveTo, true);
            }
            else
            {
                CurrentMove = m.Clone();

                Flags.IsMoveInProgress = true;
                Flags.IsClickedByUser  = true;

                Clock.Stop();
                Clock.SetClock(CurrentMove);

                Flags.IsRetracMove     = false;
                Flags.IsMoveInProgress = false;

                Notations.Set();
                Book.MoveTo(moveTo, false);
                SetCapturedPiecesParameters();
                CapturedPieces.MoveTo(moveTo);

                SetFen(CurrentMove.Fen);
            }
        }
        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);
        }
Beispiel #6
0
 private Moves GetLastMovesBranch(int pid)
 {
     NotationsLastMoveBranch = new Moves();
     return(DoGetMovesBranch(pid));
 }
Beispiel #7
0
        /// <summary>
        /// Import MainLine
        /// 1.Iterate on each move in MainLine, check if any move is already exists in book then update otherwise add.
        /// </summary>
        /// <param name="gameItem"></param>
        /// <param name="branchMoves"></param>
        private void ImportBranch(GameItem gameItem, Moves branchMoves)
        {
            bool   currentNodeMatched = false;
            string filter             = string.Empty;
            string fen = gameItem.GameData.InitialBoardFen;

            if (ChessLibrary.FenParser.IsInitialFen(fen) || string.IsNullOrEmpty(fen))
            {
                lastMoveIdProcessed = 0;// 0 is the root row(move), by default root is the "last move" being processed in import game.
            }
            else
            {
                lastMoveIdProcessed = -1;// -1 is the root row(move), if game not started from initial board fen.
            }

            // as we have root row already in book,
            // and every new branch's first move treated as child of this root move
            // so we assume that "lastNodeMatched = true" for every new branch's first move
            bool lastNodeMatched = true; // can also be callled "ParentNode"

            foreach (DataRow moveRow in branchMoves.DataTable.Rows)
            {
                // set current node(moves) matched = false
                currentNodeMatched = false;

                // if last node(move) matched(means current move's parent is matched)
                MoveKey = moveRow[Moves.No].ToString() + moveRow[Moves.White].ToString() + lastMoveIdProcessed.ToString() + moveRow[Moves.FromSquare].ToString() + moveRow[Moves.ToSquare].ToString();
                if (lastNodeMatched)
                {
                    // current move = current row;
                    // check current move's these field to know if this move is already present in book :
                    // 1. Move Number
                    // 2. Is White Move ?
                    // 3. From (square)
                    // 4. To (square)
                    // 5. Parent (last moveId processed)

                    //filter = BookMove.ColumnMoveNumber + " = " + moveRow[Moves.No] + " and " + BookMove.ColumnWhiteMove + " = " + moveRow[Moves.White];
                    //filter += " and " + BookMove.ColumnFrom + " = '" + moveRow[Moves.FromSquare] + "' and ";
                    //if (lastMoveIdProcessed != 0)
                    //{
                    //    filter += BookMove.ColumnParentId + " = " + lastMoveIdProcessed.ToString() + " and ";
                    //}
                    //filter += BookMove.ColumnTo + " = '" + moveRow[Moves.ToSquare] + "'";

                    //DataRow[] bookRows = BookMoves.DataTable.Select(filter);

                    //filter = BookMove.ColumnMoveKey + " = '" + MoveKey + "'";
                    //DataRow[] bookRows = BookMoves.DataTable.Select(filter);

                    if (BookMovesCollection.ContainsKey(MoveKey))
                    //if(bookRows.Length > 0)
                    {
                        DataRow bookRow = BookMovesCollection[MoveKey] as DataRow;
                        //DataRow bookRow = bookRows[0] as DataRow;

                        if (bookRow != null) // if move already present in book
                        {
                            currentNodeMatched = true;
                            // now this current node(move) is now become our last move
                            lastMoveIdProcessed = Convert.ToInt64(bookRow[BookMove.ColumnId]);
                            // now update this row(move) in book
                            UpdateBookRow(gameItem, moveRow, bookRow, false);
                        }
                    }
                }

                // if current node(move) not matched
                if (!currentNodeMatched)
                {
                    // set last node(move) matched = false,
                    // this is current node (and not matched)
                    // so it behaves as "lastNodeMatched =false" in next iteration
                    lastNodeMatched = false;
                    if (!BookMovesCollection.ContainsKey(MoveKey))
                    {
                        UpdateBookRow(gameItem, moveRow, null, true);
                    }
                }
            }
        }
Beispiel #8
0
        /// <summary>
        /// Transpose MainLine
        /// 1. Get Last Move
        /// 2. Find matching Moves(from book, if any exists) for LastMove's fen(position)
        /// 3. Tranpose(Update some columns like, NoOfGame,EloAverage, Result etc ) matched items
        ///    Tranpose : Update fields for all games(or moves) of book whereever LastMove's fen is matched
        /// </summary>
        /// <param name="gameItem"></param>
        /// <param name="branchMoves"></param>
        /// <returns></returns>
        private bool TransposeBranch(GameItem gameItem, Moves branchMoves)
        {
            bool isTransposed = false;

            lastMoveIdProcessed = 0;
            string filter = string.Empty;

            DataRow movesLastRow = null;

            // 1. Get Last Move
            if (branchMoves.DataTable.Rows.Count > 0)
            {
                movesLastRow = branchMoves.DataTable.Rows[branchMoves.Count - 1];
            }

            string fen = ChessLibrary.FenParser.GetOnlyFen(movesLastRow[Moves.Fen].ToString());

            // 2. Find matching Moves(from book, if any exists) for LastMove's fen(position)
            //filter = BookMove.ColumnFen + " like '%" + fen + "%'";
            StringBuilder transposedMovesRowKey = new StringBuilder();

            if (BookMovesFen.ContainsValue(fen))
            {
                int fenMatchCount = 0;
                foreach (KeyValuePair <string, string> item in BookMovesFen)
                {
                    if (item.Value == fen)
                    {
                        if (fenMatchCount > 0)
                        {
                            transposedMovesRowKey.Append(",");
                        }
                        transposedMovesRowKey.Append(item.Key);
                        fenMatchCount++;
                    }
                }

                string[] matchedKeys = transposedMovesRowKey.ToString().Split(',');

                foreach (string item in matchedKeys)
                {
                    DataRow bookRow = BookMovesCollection[item] as DataRow;
                    UpdateBookRow(gameItem, movesLastRow, bookRow, false);
                    isTransposed = true;

                    // set NoOfTranpose and MoveId to be tranposed for temporary purpose which we will use in ImportBranch
                    switch (gameItem.GameData.Result)
                    {
                    case "1-0":
                        NoOfTranspos = Convert.ToInt32(bookRow[BookMove.ColumnWinCount]);      // no of games (transposed)
                        break;

                    case "1/2-1/2":
                        NoOfTranspos = Convert.ToInt32(bookRow[BookMove.ColumnDrawCount]);     // no of games (transposed)
                        break;

                    case "0-1":
                        NoOfTranspos = Convert.ToInt32(bookRow[BookMove.ColumnLoseCount]);     // no of games (transposed)
                        break;
                    }

                    //NoOfTranspos = Convert.ToInt32(bookRow[BookMove.ColumnWinCount]);
                }
            }
            return(isTransposed);
        }
Beispiel #9
0
 public GameItem(GameData gameData, Moves moves)
 {
     this.GameData = gameData;
     this.Moves    = moves;
 }
Beispiel #10
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);
        }