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