private void SetPosition(string fen)
        {
            IsSettingPosition = true;

            BoardHistory.SetInitialPosition(fen);
            MoveHistory.Clear();
            Plies = 0;
            MoveHistory.Rows.Add();
            MoveHistory.Last().No = 1;
            if (BoardHistory.Current().GCD.WhoseTurn == Player.Black)
            {
                BoardHistory.DuplicateLast();
                MoveHistory.Last().WhiteDetailedMove = null;
                Plies = 1;
                SetSelection(1);
            }
            FirstPly = Plies;

            if (int.TryParse(fen.Split(' ').Last(), out int n))
            {
                BaseMoveNumber             = n;
                MoveHistory.BaseMoveNumber = n;
                foreach (MoveHistoryDataRow row in MoveHistory.Rows)
                {
                    row.UpdateBaseMoveNumber(n);
                }
            }

            UpdateFenTextBox(fen);

            IsSettingPosition = false;
        }
        private bool TryDragPiece(Point from, Point to)
        {
            Position fromSquare = ConvertPointToSquare(from);
            Position toSquare   = ConvertPointToSquare(to);

            if (fromSquare == null || toSquare == null)
            {
                return(false);
            }

            Player player = BoardHistory.Current().GCD.WhoseTurn;
            Move   move   = new Move(fromSquare, toSquare, player);

            if (BoardHistory.NeedsToBePromotion(move))
            {
                using (var dialog = new PromotionSelectionForm(PieceImages, player))
                {
                    var result = dialog.ShowDialog();
                    if (result == DialogResult.OK)
                    {
                        move.Promotion = dialog.PromotedPieceType;
                    }
                    else
                    {
                        return(false);
                    }
                }
            }

            return(DoMove(move));
        }
        private void DrawSquares(Graphics g, Rectangle squaresSpace)
        {
            var game  = BoardHistory.Current();
            var board = game.GetBoard();

            // We draw the square from which a piece is dragged last
            // so that it's not occluded by others.

            Position fromSquare =
                MouseFrom.HasValue
                ? ConvertPointToSquare(MouseFrom.Value)
                : null;

            for (int x = 0; x < 8; ++x)
            {
                for (int y = 0; y < 8; ++y)
                {
                    if (fromSquare != null && (int)fromSquare.File == x && fromSquare.Rank == 8 - y)
                    {
                        // this draw is deferred
                        continue;
                    }

                    Piece piece = board[y][x];
                    DrawSquare(g, piece, x, y, squaresSpace);
                }
            }
        }
        public string GetPrevMoveEran()
        {
            if (Plies <= FirstPly)
            {
                return(null);
            }

            var e = BoardHistory.Prev();

            if (e == null)
            {
                return(null);
            }

            var move = BoardHistory.Current().Move;

            if (move == null)
            {
                return(null);
            }

            return(Eran.MakeFromBoardAndMove(
                       new ChessGame(e.GCD),
                       move
                       ));
        }
        private void MoveHistoryGridView_SelectionChanged(object sender, EventArgs e)
        {
            if (moveHistoryGridView.SelectedCells.Count == 0)
            {
                return;
            }

            var cell = moveHistoryGridView.SelectedCells[0];

            int row = cell.RowIndex;
            int col = cell.ColumnIndex;
            int ply = row * 2 + col;

            if (ply > Plies)
            {
                return;
            }

            BoardHistory.SetCurrentPly(ply);
            string fen = BoardHistory.Current().GetFen();

            if (!IsSettingPosition)
            {
                UpdateFenTextBox(fen);
            }

            chessBoardPanel.Refresh();
        }
        private void DrawDraggedPiece(Graphics g, Rectangle squaresSpace)
        {
            var game  = BoardHistory.Current();
            var board = game.GetBoard();

            // We draw the square from which a piece is dragged last
            // so that it's not occluded by others.

            Position fromSquare =
                MouseFrom.HasValue
                ? ConvertPointToSquare(MouseFrom.Value)
                : null;

            if (fromSquare != null)
            {
                int x = (int)fromSquare.File;
                int y = 8 - fromSquare.Rank;

                Piece piece = board[y][x];
                DrawSquare(g, piece, x, y, squaresSpace);
            }
        }
        public bool DoMove(Move move, bool silent = false)
        {
            if (!BoardHistory.IsMoveValid(move))
            {
                return(false);
            }

            // We only synch when te move was valid
            if (!silent)
            {
                SynchronizeMoveListWithHistory();
            }

            BoardHistory.DoMove(move);
            AddMoveToMoveHistory(BoardHistory.Current().Move, silent);
            if (!silent)
            {
                moveHistoryGridView.Refresh();
                chessBoardPanel.Refresh();
            }

            return(true);
        }
        public bool DoMove(string san, bool silent = false)
        {
            Move move = San.ParseSan(new ChessGame(BoardHistory.Current().GCD), san);

            return(DoMove(move, silent));
        }
 public string GetLastMoveSan()
 {
     return(BoardHistory.Current().GetSan());
 }
 public string GetFen()
 {
     return(BoardHistory.Current().GetFen());
 }
 internal Player SideToMove()
 {
     return(BoardHistory.Current().GCD.WhoseTurn);
 }