Exemplo n.º 1
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;

            IEnumerable <ChessSquare> playerSquares   = vm.Squares.Where(s => s.Player == vm.CurrentPlayer);
            IEnumerable <ChessSquare> selectedSquares = vm.Squares.Where(s => s.IsSelected == true);

            //first click, select piece to be moved
            if (selectedSquares.Count() == 0)
            {
                if (playerSquares.Contains(square))
                {
                    square.IsSelected    = true;
                    square.IsHighlighted = false;
                }
            }

            //second click
            else if (selectedSquares.Count() == 1)
            {
                //click on possible move, move is applied
                if (vm.PossibleEndMoves.Contains(square.Position))
                {
                    if (selectedSquares.FirstOrDefault().Piece.PieceType == ChessPieceType.Pawn)
                    {
                        if (selectedSquares.FirstOrDefault().Player == 1 && square.Position.Row == 0)
                        {
                            var p = new PawnPromotion(vm, selectedSquares.FirstOrDefault().Position, square.Position);
                            p.ShowDialog();
                        }
                        else if (selectedSquares.FirstOrDefault().Player == 2 && square.Position.Row == 7)
                        {
                            var p = new PawnPromotion(vm, selectedSquares.FirstOrDefault().Position, square.Position);
                            p.ShowDialog();
                        }
                        else
                        {
                            IsEnabled = false;
                            await vm.ApplyMove(selectedSquares.FirstOrDefault().Position, square.Position, selectedSquares.FirstOrDefault().Piece.PieceType);

                            IsEnabled = true;
                        }
                    }
                    else
                    {
                        IsEnabled = false;
                        await vm.ApplyMove(selectedSquares.FirstOrDefault().Position, square.Position, selectedSquares.FirstOrDefault().Piece.PieceType);

                        IsEnabled = true;
                    }
                    square.IsHighlighted = false;
                    selectedSquares.FirstOrDefault().IsSelected = false;
                }

                //click on not possible move, move is cancelled and is no longer selected
                else
                {
                    selectedSquares.FirstOrDefault().IsSelected = false;
                }
            }
        }
        private async void Border_MouseUpAsync(object sender, MouseButtonEventArgs e)
        {
            Border b                 = sender as Border;
            var    square            = b.DataContext as ChessSquare;
            var    currentlySelected = ChessViewModel.CurrentlySelected;

            if (currentlySelected != null)
            {
                if (currentlySelected.Position.Equals(square.Position))
                {
                    square.IsSelected                = false;
                    square.IsHighlighted             = true;
                    ChessViewModel.CurrentlySelected = null;
                }
                else
                {
                    var possibleMoves = from ChessMove m in GetPossibleMovesByStartPosition(currentlySelected)
                                        where m.EndPosition.Equals(square.Position)
                                        select m;
                    if (possibleMoves.Any())
                    {
                        ChessMove move = possibleMoves.First();
                        if (move.MoveType == ChessMoveType.PawnPromote)
                        {
                            PawnPromotion window = new PawnPromotion(ChessViewModel, currentlySelected.Position, square.Position)
                            {
                                ResizeMode  = ResizeMode.NoResize,
                                WindowStyle = WindowStyle.None
                            };
                            window.ShowDialog();
                        }
                        else
                        {
                            if (ChessViewModel.IsCheck)
                            {
                                ChessViewModel.FindKingSquareInCheck().IsInCheck = false;
                            }
                            var window = Window.GetWindow(this);
                            window.IsEnabled = false;
                            await ChessViewModel.ApplyMove(move);

                            window.IsEnabled = true;
                        }
                        square.IsHighlighted = false;
                    }
                    else
                    {
                        ChessViewModel.CurrentlySelected.IsSelected = false;
                        if (IncomingSelectionIsValidChessPiece(square))
                        {
                            square.IsSelected = true;
                            ChessViewModel.CurrentlySelected = square;
                        }
                        else
                        {
                            ChessViewModel.CurrentlySelected = null;
                        }
                    }
                }
            }
            else
            {
                if (IncomingSelectionIsValidChessPiece(square))
                {
                    square.IsHighlighted             = false;
                    square.IsSelected                = true;
                    ChessViewModel.CurrentlySelected = square;
                }
            }
        }