Esempio n. 1
0
        private void RebindState()
        {
            // Rebind the possible moves, now that the board has changed.
            PossibleMoves = new HashSet <ChessMove>(
                from ChessMove m in mBoard.GetPossibleMoves()
                select m
                );

            // Update the collection of squares by examining the new board state.
            var newSquares = BoardPosition.GetRectangularPositions(8, 8);
            int i          = 0;

            foreach (var pos in newSquares)
            {
                var piece = mBoard.GetPieceAtPosition(pos);
                mSquares[i].ChessPiece = piece;
                if (piece.PieceType == ChessPieceType.King && mBoard.IsCheck && piece.Player == mBoard.CurrentPlayer)
                {
                    mSquares[i].IsInCheck = true;
                }
                else
                {
                    mSquares[i].IsInCheck = false;
                }
                mSquares[i].IsSelected = false;
                i++;
            }
            mCurrentlySelected = null;
            OnPropertyChanged(nameof(BoardAdvantage));
            OnPropertyChanged(nameof(CurrentPlayer));
            OnPropertyChanged(nameof(CanUndo));
        }
Esempio n. 2
0
        private void Border_MouseUp(object sender, MouseButtonEventArgs e)
        {
            Border b      = sender as Border;
            var    square = b.DataContext as ChessSquare;
            var    vm     = FindResource("vm") as ChessViewModel;

            foreach (var move in vm.PossibleMoves)
            {
                if (move.StartPosition == square.Position)
                {
                    //if the seleceted square is not the same as before
                    if (selectedSquare != null && selectedSquare.Position != square.Position)
                    {
                        selectedSquare.IsSelected = false;
                    }
                    //initialize selectedSquare
                    selectedSquare       = square;
                    square.IsHighlighted = true;
                    square.IsSelected    = true;
                }
            }
            //if there is a selected square,
            if (selectedSquare != null && selectedSquare.Position != square.Position)
            {
                vm.ApplyMove(selectedSquare.Position, square.Position);
                selectedSquare.IsSelected = false;
                selectedSquare            = null;
            }
        }
Esempio n. 3
0
        private void Border_HoverMouseEnter(object sender, MouseEventArgs e)
        {
            if (!this.IsEnabled)
            {
                return;
            }
            Border b      = sender as Border;
            var    square = b.DataContext as ChessSquare;

            hovered_square = square;
            var vm = FindResource("vm") as ChessViewModel;

            foreach (var move in vm.PossibleMoves)
            {
                if (selected_square != null && move.StartPosition.Equals(selected_square.Position) && move.EndPosition.Equals(square.Position))
                {
                    square.IsHovered = true;
                }

                /*              else if(selected_square != null && selected_square.IsSelected == false && move.EndPosition.Equals(square.Position))
                 *                square.IsHovered = true;*/

                else if (selected_square != null && selected_square.IsSelected == false && move.StartPosition.Equals(square.Position))
                {
                    square.IsHovered = true;
                }
            }
        }
Esempio n. 4
0
        private async void Border_MouseUp(object sender, MouseButtonEventArgs e)
        {
            if (!IsEnabled)
            {
                return;
            }

            Border b      = sender as Border;
            var    square = b.DataContext as ChessSquare;
            var    vm     = FindResource("vm") as ChessViewModel;

            if (vm.PossibleStartMoves.Contains(square.Position))
            {
                if (selectedSquare != null)            // If it's selected
                {
                    selectedSquare.IsSelected = false; // Then unselect it
                }
                square.IsHighlighted = false;          // unhighlight current square
                square.IsSelected    = true;           // select current square

                selectedSquare = square;               // reset field to square we just clicked
            }

            else if (selectedSquare != null)
            {
                if (square.IsHighlighted)
                {
                    //if move is pawn promote
                    // open pawn promote window
                    if ((square.Position.Row == 0 || square.Position.Row == 7) && selectedSquare.Player.PieceType == ChessPieceType.Pawn)
                    {
                        var panel = new PawnPromote(vm, selectedSquare.Position, square.Position);
                        panel.Show();
                    }
                    else
                    {
                        IsEnabled = false;
                        selectedSquare.IsSelected = false;
                        await vm.ApplyMove(square.Position, selectedSquare.Position, "");

                        selectedSquare = null;
                        IsEnabled      = true;
                    }
                }
            }

            if (!vm.PossibleStartMoves.Contains(square.Position))
            {
                if (selectedSquare != null)
                {
                    selectedSquare.IsSelected = false;
                    selectedSquare            = null;
                }
            }
        }
Esempio n. 5
0
        public ChessSquare FindKingSquareInCheck()
        {
            ChessSquare chessSquare = null;

            foreach (ChessSquare square in mSquares)
            {
                if (square.IsInCheck)
                {
                    chessSquare = square;
                }
            }
            return(chessSquare);
        }
Esempio n. 6
0
        public ChessViewModel()
        {
            mBoard = new ChessBoard();

            // Initialize the squares objects based on the board's initial state.
            mSquares = new ObservableCollection <ChessSquare>(
                BoardPosition.GetRectangularPositions(8, 8)
                .Select(pos => new ChessSquare()
            {
                Position   = pos,
                ChessPiece = mBoard.GetPieceAtPosition(pos)
            })
                );

            PossibleMoves = new HashSet <ChessMove>(
                from ChessMove m in mBoard.GetPossibleMoves()
                select m
                );

            mCurrentlySelected = null;
        }
Esempio n. 7
0
        private void Border_LeftClickDown(object sender, MouseEventArgs e)
        {
            if (!this.IsEnabled)
            {
                return;
            }
            if (selected_square != null)
            {
                selected_square.IsSelected = false;
            }
            Border b      = sender as Border;
            var    square = b.DataContext as ChessSquare;

            selected_square = square;
            var vm = FindResource("vm") as ChessViewModel;

            if (vm.CurrentPlayer.Equals(square.Chess_Piece.Player))
            {
                square.IsSelected = true;
            }
        }
        private async void Border_MouseUp(object sender, MouseButtonEventArgs e)
        {
            Border      b      = sender as Border;
            var         square = b.DataContext as ChessSquare;
            ChessSquare possibleSelectedSquare;
            var         vm = FindResource("vm") as ChessViewModel;

            mIsSelected = !mIsSelected; //toggling selected

            if (mIsSelected)
            {
                possibleSelectedSquare = square;

                square.IsSelected = true;
                mSelectedSquare   = square;
            }
            else if (vm.IsPossibleEndPosition(mSelectedSquare.Position, square.Position))
            {
                if (mSelectedSquare.Piece.PieceType == Model.ChessPieceType.Pawn && (square.Position.Row == 0 || square.Position.Row == 7))//promotion
                {
                    var promotionWindow = new PromotionWindow(vm, mSelectedSquare.Position, square.Position);
                    promotionWindow.ShowDialog();
                }
                else if (vm.CanEnable)
                {
                    vm.CanEnable = false;
                    await vm.ApplyMove(mSelectedSquare.Position, square.Position, Model.ChessPieceType.Empty);

                    vm.CanEnable = true;
                }

                square.IsHighlighted       = true;
                mSelectedSquare.IsSelected = false;
            }
            else
            {
                mIsSelected = false;
                mSelectedSquare.IsSelected = false;
            }
        }
Esempio n. 9
0
        private async void Border_MouseUp(object sender, MouseButtonEventArgs e)
        {
            Border b      = sender as Border;
            var    square = b.DataContext as ChessSquare;
            var    vm     = FindResource("vm") as ChessViewModel;

            //only select squares that are possible start positions from all possible moves
            if (vm.PossibleStartPositions.Contains(square.Position))
            {
                //if no square has been selected currently, select the clicked square
                if (selectedSquare == null)
                {
                    square.IsSelected = true;
                    selectedSquare    = square;
                }
                //if a square is already select, determine if the same square or a different square is clicked
                else
                {
                    //if the same square is clicked, deselect the square
                    if (selectedSquare == square)
                    {
                        square.IsSelected = false;
                        selectedSquare    = null;
                    }
                    //if a different square is clicked, delselect the previous square and select the new one
                    else
                    {
                        //Fix Applied: Need to deselect before changing selectedSquare
                        selectedSquare.IsSelected = false;
                        //if (square.IsHighlighted)
                        //{
                        //    vm.ApplyMove(square.Position);
                        //    selectedSquare = null;
                        //}
                        //else
                        //{
                        square.IsSelected = true;
                        selectedSquare    = square;
                        //}
                    }
                }
                //square.IsSelected = (square.IsSelected) ? false:true;
            }
            else
            {
                //if the square that is not a starting position of possible move is selected, deselect the previous square
                if (selectedSquare != null)
                {
                    selectedSquare.IsSelected = false;
                    //True if square is ending position of possible move
                    if (square.IsHighlighted)
                    {
                        vm.StartBoardPosition = selectedSquare.Position;
                        //Check if move is pawn promotion
                        if (vm.GetPieceAtPosition(vm.StartBoardPosition).PieceType == ChessPieceType.Pawn &&
                            ((vm.CurrentPlayer == 1 && vm.StartBoardPosition.Row == 1) ||
                             (vm.CurrentPlayer == 2 && vm.StartBoardPosition.Row == 6)))
                        {
                            PromotionWindow promoteWin = new PromotionWindow(vm, vm.StartBoardPosition, square.Position);
                            promoteWin.ShowDialog();
                            //ChessPieceType checker = vm.PromotedPiece;
                        }
                        //else
                        //{
                        Window parentWindow = Window.GetWindow(this);
                        parentWindow.IsEnabled = false;
                        this.IsEnabled         = false;
                        //Apply Move needs to have an extra param to take PieceType for the pawn to promote to
                        await vm.ApplyMove(square.Position);

                        parentWindow.IsEnabled = true;
                        this.IsEnabled         = true;
                        //}
                    }
                    selectedSquare = null;
                }
            }
        }
        private bool IncomingSelectionIsValidChessPiece(ChessSquare square)
        {
            var possibleStartPositions = GetPossibleMovesByStartPosition(square);

            return(possibleStartPositions.Any());
        }
 private IEnumerable <ChessMove> GetPossibleMovesByStartPosition(ChessSquare square)
 {
     return(from ChessMove m in ChessViewModel.PossibleMoves
            where m.StartPosition.Equals(square.Position)
            select m);
 }