public void FirstMove(int x, int y, Random rand, GameBoard gameBoard) { //For any board, take the user's first revealed panel + any neighbors of that panel to X depth, and mark them as unavailable for mine placement. var depth = 0.125 * Width; //12.5% (1/8th) of the board width becomes the depth of unavailable panels var neighbors = GetNeighbors(x, y, (int)depth); //Get all neighbors to specified depth neighbors.Add(GameBoardPanels.First(panel => panel.X == x && panel.Y == y)); //Don't place a mine in the user's first move! //Select random panels from set of panels which are not excluded by the first-move rule var mineList = GameBoardPanels.Except(neighbors).OrderBy(user => rand.Next()); var mineSlots = mineList.Take(gameBoard.MineCount).ToList().Select(z => new { z.X, z.Y }); //Place the mines foreach (var mineCoord in mineSlots) { GameBoardPanels.Single(panel => panel.X == mineCoord.X && panel.Y == mineCoord.Y).IsMine = true; } //For every panel which is not a mine, determine and save the adjacent mines. foreach (var openPanel in GameBoardPanels.Where(panel => !panel.IsMine)) { var nearbyPanels = GetNeighbors(openPanel.X, openPanel.Y); openPanel.AdjacentMines = nearbyPanels.Count(z => z.IsMine); } }
public List <Panel> GetNeighbors(int x, int y, int depth) { var nearbyPanels = GameBoardPanels.Where(panel => panel.X >= (x - depth) && panel.X <= (x + depth) && panel.Y >= (y - depth) && panel.Y <= (y + depth)); var currentPanel = GameBoardPanels.Where(panel => panel.X == x && panel.Y == y); return(nearbyPanels.Except(currentPanel).ToList()); }
public void RevealMines() { var minePanels = GameBoardPanels.Where(panel => panel.IsMine); foreach (var panel in minePanels) { panel.IsRevealed = true; } }
private void CompletionCheck(GameBoard gameBoard) { var hiddenPanels = GameBoardPanels.Where(x => !x.IsRevealed).Select(x => x.Id); var minePanels = GameBoardPanels.Where(x => x.IsMine).Select(x => x.Id); if (!hiddenPanels.Except(minePanels).Any()) { gameBoard.Status = GameStatus.Completed; } }
public Panel GetPanel(int x, int y) { try { return(GameBoardPanels.First(panel => panel.X == x && panel.Y == y)); } catch (Exception e) { Console.WriteLine($"X: {x}, Y: {y}"); Console.WriteLine(e); throw; } }