private static void ValidateDropdownApply(object sender, RoutedEventArgs e) { var grid = DependencyObjectExtensions.GetParent <Grid>((ComboBox)sender); var applyButton = grid.FirstOrDefaultChild <Button>(c => c.Content.ToString() == "Apply"); applyButton.IsEnabled = true; }
// change the error text to something more useful private static void ChangeErrorText(Rectangle rect) { var window = DependencyObjectExtensions.GetParent <Window>(rect); var errorBlock = window.FirstOrDefaultChild <TextBlock>(w => w.Name == "ErrorBlock"); errorBlock.Text = "\n Please place all your ships first!"; errorBlock.Visibility = Visibility.Visible; }
private static dynamic _algorithm; // TODO: this is ugly, better make an Algorithm class or interface // check if the player or the computer hit a rectangle containing a ship and return true or false public static void CheckHit(Rectangle rect, out bool hasHit, bool fromAi = false) { // this is a check to make sure the player has placed all of his ships before starting to sink enemy ships if (SavedSettings.AgainstCom && PlayerModel.ShipList.Any(s => s.ShipRectangle.Count == 0)) { ChangeErrorText(rect); hasHit = false; return; } // we abuse the name property of rectangles as the shipname. NoShip means it's water, everything else is a hit if (rect.Name != "NoShip") { rect.Fill = new SolidColorBrush(Color.FromRgb(255, 0, 0)); rect.IsEnabled = false; hasHit = true; } else { rect.IsEnabled = false; rect.Fill = new SolidColorBrush(Color.FromRgb(0, 0, 225)); hasHit = false; } // if it was a hit, update the model to make sure the ship loses in length if (hasHit) { var model = fromAi ? PlayerModel : _comModel; GetAndUpdateHitShip(rect.Uid, model); } // if turn counting is enabled, count the turns and update them CountTurns(); if (SavedSettings.ShowTurns) { UpdateCounter(DependencyObjectExtensions.GetParent <Window>(rect)); } // if either the com or the player has no remaining ships, the game must be over if (_comModel.ShipList.Count == 0 || PlayerModel.ShipList.Count == 0) { var window = DependencyObjectExtensions.GetParent <Window>(rect); CallGameOverWindow(window); return; } // after the player clicked a field, let the algorithm try its best if (SavedSettings.AgainstCom && !fromAi) { CombinedAlgorithm(); } }