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; } }
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; } } }