Esempio n. 1
0
        public PawnPromotion(ChessViewModel viewM, BoardPosition start, BoardPosition end)
        {
            InitializeComponent();

            StartPosition = start;
            EndPosition   = end;

            vm = viewM;

            if (vm.CurrentPlayer == 1)
            {
                black_knight.Visibility = Visibility.Hidden;
                black_bishop.Visibility = Visibility.Hidden;
                black_rook.Visibility   = Visibility.Hidden;
                black_queen.Visibility  = Visibility.Hidden;
            }
            else
            {
                white_knight.Visibility = Visibility.Hidden;
                white_bishop.Visibility = Visibility.Hidden;
                white_rook.Visibility   = Visibility.Hidden;
                white_queen.Visibility  = Visibility.Hidden;
            }
        }
        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;
                }
            }
        }