/// <summary> /// Helper function to get the UI element at (<paramref name="x"/>, <paramref name="y"/>). /// </summary> /// <param name="x">The 0-based column number of a <see cref="Cell"/></param> /// <param name="y">The 0-based row number of a <see cref="Cell"/></param> /// <returns>The <see cref="Label"/> representing the <see cref="Cell"/> at (<paramref name="x"/>, <paramref name="y"/>)</returns> private Label GetLabelFromCoord(int x, int y) { return((from Label lbl in MineFieldGrid.Children where Grid.GetColumn(lbl) == x && Grid.GetRow(lbl) == y select lbl).First()); }
/// <summary> /// Called when the user attempts to clear a <see cref="Cell"/>. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Cell_MouseLeftUp(object sender, RoutedEventArgs e) { var lbl = (Label)sender; int x = Grid.GetColumn(lbl); int y = Grid.GetRow(lbl); if (newGame != null) { newGame.Invoke(x, y); newGame = null; } var changedCells = new List <ChangedCell>(); if (App.TryClearCell(x, y, ref changedCells)) { // we didn't blow up, so let's show the user what changed foreach (var cell in changedCells) { lbl = GetLabelFromCoord(cell.coordinate); // set the text first, so we don't need to duplicate the line so many times // if the mine count is 0, it will get set to null lbl.Content = cell.neighboringMines.ToString(); // yay colors switch (cell.neighboringMines) { case 1: lbl.Foreground = ONE_MINE_COLOR; break; case 2: lbl.Foreground = TWO_MINE_COLOR; break; case 3: lbl.Foreground = THREE_MINE_COLOR; break; case 4: lbl.Foreground = FOUR_MINE_COLOR; break; case 5: lbl.Foreground = FIVE_MINE_COLOR; break; case 6: lbl.Foreground = SIX_MINE_COLOR; break; case 7: lbl.Foreground = SEVEN_MINE_COLOR; break; case 8: lbl.Foreground = EIGHT_MINE_COLOR; break; default: lbl.Content = null; lbl.Foreground = null; break; } lbl.Background = CELL_CLEARED_BACKGROUND; } statusImage.Background = HAPPY_IMAGE; } else { // RIP } e.Handled = true; }