public void GenerateShips(int count, Random rnd) { if (ShipPlates.Count == 0) { throw new Exception(message: "There must be plates to add ships"); } ShipsLeft = count; for (int i = 0; i < count; i++) { MinePlate plate = null; while (plate == null || plate.IsMined) { plate = ShipPlates[index : rnd.Next(maxValue : ShipPlates.Count)]; } plate.IsMined = true; #if DEBUG Button btn = plate.BtnButton as Button; if (btn == null) { throw new NullReferenceException(); } btn.Content = "X"; #endif } }
public bool RevealButton(Button btn) { //find plate MinePlate plate = MinePlates. FirstOrDefault(predicate: x => Equals(objA: x.BtnButton as Button, objB: btn)); if (plate == null) { throw new NullReferenceException(message: "Plate not found"); } if (plate != null && !plate.IsRevealed && !plate.IsFlagged) { if (plate.IsMined) { SetButtonVisuals(stat: MineStatEnum.IsBombed, btn: btn, plate: plate); RevealAll(); } else if (plate.IntDisplay > 0) { SetButtonVisuals(stat: MineStatEnum.IsNumbered, btn: btn, plate: plate); } else { SetButtonVisuals(stat: MineStatEnum.IsPlane, btn: btn, plate: plate); List <MinePlate> neighbours = GetNeighboursList(plate: plate); foreach (MinePlate neighbour in neighbours) { Button button = neighbour.BtnButton as Button; RevealButton(btn: button); } } } return(CheckGameEndingConditions()); }
private List <MinePlate> GetNeighboursList(MinePlate plate) { return(MinePlates.Where(predicate: x => (x.RowPos <= plate.RowPos + 1 && x.RowPos >= plate.RowPos - 1) && (x.ColumnPos <= plate.ColumnPos + 1 && x.ColumnPos >= plate.ColumnPos - 1) && x != plate) .ToList()); }
private void IncreasePlateValue(MinePlate plate) { plate.IntDisplay++; #if DEBUG Button btn = plate.BtnButton as Button; if (btn != null) { btn.Content = plate.IntDisplay; } #endif }
private void SetButtonVisuals(MineStatEnum stat, Button btn, MinePlate plate) { btn.IsEnabled = false; btn.Background = Brushes.DimGray; plate.IsRevealed = true; switch (stat) { case MineStatEnum.IsBombed: btn.Content = "Boom"; btn.Background = Brushes.Crimson; break; case MineStatEnum.IsNumbered: btn.Content = plate.IntDisplay; break; default: btn.Content = ""; break; } }
private void SetupMines(int count, Button avoidButton) { MinesLeft = count; Random rnd = new Random(); for (int i = 0; i < count; i++) { MinePlate plate = MinePlates[index : rnd.Next(maxValue : MinePlates.Count)]; while (plate.IsMined || avoidButton.Equals(obj: plate.BtnButton as Button)) { plate = MinePlates[index : rnd.Next(maxValue : MinePlates.Count)]; } plate.IsMined = true; #if DEBUG Button btn = plate.BtnButton as Button; if (btn == null) { throw new NullReferenceException(); } btn.Content = "X"; #endif } }
public bool MarkButton(Button btn) { //find plate MinePlate plate = MinePlates. FirstOrDefault(predicate: x => Equals(objA: x.BtnButton as Button, objB: btn)); if (plate == null) { throw new NullReferenceException(); } if (plate.IsFlagged) { btn.Background = Brushes.DodgerBlue; plate.IsFlagged = false; MinesLeft++; } else if (MinesLeft > 0) { btn.Background = Brushes.BlueViolet; plate.IsFlagged = true; MinesLeft--; } return(CheckGameEndingConditions()); }
public void AddPlate(Button btnButton, int x, int y) { MinePlate plate = new MinePlate(column: y, row: x, button: btnButton); MinePlates.Add(item: plate); }