/// <summary> /// Creates a new minesweeper field and populates it with mines. /// </summary> /// <returns>An instance of IField</returns> public override IField CreateField() { IField field = new Field(FieldRows, FieldCols); IGenerator mineGenerator = new MineGenerator(field); IGeneratable mine = new Mine('*'); IAdjacencyMap mineMap = new MinesweeperAdjacencyMap(field, mine); mineGenerator.Generate(mine, FieldRows + FieldCols); mineMap.CreateNeighboursMap(); return field; }
private void Minesweeper_MouseDown(object sender, MouseEventArgs e) { Mine addMine = board.SweepMine(e.X, e.Y); if (addMine != null) //判斷是否有踩到地雷 { this.Controls.Add(addMine); if (addMine.whetherTheMine == true) { MessageBox.Show("game over"); for (int i = 0; i < 8; i++) //若有踩到地雷,最後顯示所有地雷位置 { for (int j = 0; j < 8; j++) { if (board.ArrayClass[i, j].whetherMine == true) { this.Controls.Add(board.whereTheMine(i, j)); } } } } else { int noMineCount = 0; for (int i = 0; i < 8; i++) //計算目前踩了多少沒地雷的位置 { for (int j = 0; j < 8; j++) { if (board.ArrayClass[i, j].whetherMine == false && board.ArrayClass[i, j].whetherClick == true) { noMineCount++; } } } this.Text = noMineCount.ToString(); if (noMineCount == 64 - board.MineCount) { MessageBox.Show("You Win!!!!!!"); } } } }
/// <summary> /// Generates new board. /// </summary> /// <param name="xSize">Width</param> /// <param name="ySize">Length</param> /// <param name="mineCount">Number of mines</param> public override void GenerateBoard(int xSize, int ySize, int mineCount) { this.xSize = xSize; this.ySize = ySize; this.mineCount = lastMineCount = mineCount; flagCounter.SetFlagCount(mineCount); ResizePanel(xSize, ySize); if (grid != null) ClearTiles(); grid = new Tile[xSize, ySize]; int x, y; for (int i = 0; i < mineCount; i++) { do { x = rand.Next(xSize); y = rand.Next(ySize); } while (grid[x, y] != null); grid[x, y] = new Mine(x, y); AddElement(grid[x, y]); AddTileButtonMouseActions(grid[x, y]); } for (int i = 0; i < xSize; i++) { for (int j = 0; j < ySize; j++) { if (grid[i, j] == null) { int minesAround = CountAround(i, j); if (minesAround == 0) { grid[i, j] = new Empty(i, j); } else { grid[i, j] = new Number(i, j, minesAround); } AddElement(grid[i, j]); AddTileButtonMouseActions(grid[i, j]); } } } }
private void placeMines(int numOfMines) { Random rnd = new Random(); for (int i = 0; i < numOfMines; i++) { int randomLocationX = rnd.Next(0, graph.Count - 1); int randomLocationY = rnd.Next(0, graph.Count - 1); if (graph[randomLocationY][randomLocationX].GetType().Equals(typeof(Mine))) { continue; } else { graph[randomLocationY][randomLocationX] = new Mine(); } } }
static void Main(string[] args) { UserInterface.PrintStartMenu(); String input = Console.ReadLine(); UserInterface.UserInputStart(input); Mine Mine = new Mine(Mine.demensions, Mine.bombs); while (!Mine.GameOver) { Console.Clear(); Mine.printMatrix(); if (Mine.GameOver == true) { break; } Console.WriteLine("Please enter a comand: open or mark!"); Console.Write("Comand:"); string comands = Console.ReadLine(); if (UserInterface.ReciveComand(comands) == 1) { Mine.Open(UserInterface.ActionXcordinates, UserInterface.ActionYcordinates); } else if(UserInterface.ReciveComand(comands)==0) { Mine.Mark(UserInterface.ActionXcordinates, UserInterface.ActionYcordinates); } else if(UserInterface.ReciveComand(comands)==2) { Mine.printMatrixAdmin(); string a = Console.ReadLine(); } } Console.Clear(); Mine.printMatrix(); Console.WriteLine("0 - restart"); Console.WriteLine("1 - exit"); string finalInput = Console.ReadLine(); while (!finalInput.Equals("1")&&(!finalInput.Equals("0"))) { Console.WriteLine("Invalid input!"); finalInput = Console.ReadLine(); } if (finalInput.Equals("1")) { Environment.Exit(0); } else { System.Diagnostics.Process.Start(@"Minesweeper.exe"); Environment.Exit(0); } }