public void checkForEmpty(Mine tMine) { if (!tMine.open && !tMine.bomb) { tMine.open = true; tMine.Text = tMine.count.ToString(); ushort x = tMine.x, y = tMine.y; MineInfo tMineInfo; tMineInfo.player = Players.None; tMineInfo.bomb = false; tMineInfo.count = tMine.count; tMineInfo.x = x; tMineInfo.y = y; try { MineCheckEvent(new MineEventArgs(tMineInfo)); } catch (Exception) { } if (tMine.count == 0) { if (x - 1 >= 0 && y - 1 >= 0 && !_mines[x - 1, y - 1].bomb && !_mines[x - 1, y - 1].open) checkForEmpty(_mines[x - 1, y - 1]); if (y - 1 >= 0 && !_mines[x, y - 1].bomb && !_mines[x, y - 1].open) checkForEmpty(_mines[x, y - 1]); if (x + 1 < Horizontal && y - 1 >= 0 && !_mines[x + 1, y - 1].bomb && !_mines[x + 1, y - 1].open) checkForEmpty(_mines[x + 1, y - 1]); if (x + 1 < Horizontal && !_mines[x + 1, y].bomb && !_mines[x + 1, y].open) checkForEmpty(_mines[x + 1, y]); if (x + 1 < Horizontal && y + 1 < Vertical && !_mines[x + 1, y + 1].bomb && !_mines[x + 1, y + 1].open) checkForEmpty(_mines[x + 1, y + 1]); if (y + 1 < Vertical && !_mines[x, y + 1].bomb && !_mines[x, y + 1].open) checkForEmpty(_mines[x, y + 1]); if (x - 1 >= 0 && y + 1 < Vertical && !_mines[x - 1, y + 1].bomb && !_mines[x - 1, y + 1].open) checkForEmpty(_mines[x - 1, y + 1]); if (x - 1 >= 0 && !_mines[x - 1, y].bomb && !_mines[x - 1, y].open) checkForEmpty(_mines[x - 1, y]); } } }
private void DrawMinefield() { field_group.Controls.Clear(); this.Size = new System.Drawing.Size(settings.Horizontal<14?350:settings.Horizontal * 24 + 15, settings.Vertical * 24 + (chat_group.Visible?260:125)); for (ushort i = 0; i < settings.Horizontal; i++) { for (ushort j = 0; j < settings.Vertical; j++) { Mine newMine = new Mine(i, j); newMine.Name = "Mine_" + i + "_" + j; newMine.Size = new Size(24, 24); newMine.Text = null; newMine.Font = new System.Drawing.Font(newMine.Font.FontFamily, 10f, FontStyle.Bold); newMine.Location = new Point(i * 24+5, j * 24+10); newMine.Enabled = false; // newMine.FlatStyle = FlatStyle.Flat; newMine.MouseDown += new MouseEventHandler(newMine_MouseDown); field_group.Controls.Add(newMine); } } }
private void addToNeigbours(Mine tMine) { int x = tMine.x, y = tMine.y; if (x - 1 >= 0 && y - 1 >= 0) _mines[x - 1, y - 1].count++; if (y - 1 >= 0) _mines[x, y - 1].count++; if (x + 1 < _horizontal && y - 1 >= 0) _mines[x + 1, y - 1].count++; if (x + 1 < _horizontal) _mines[x + 1, y].count++; if (x + 1 < _horizontal && y + 1 < _vertical) _mines[x + 1, y + 1].count++; if (y + 1 < _vertical) _mines[x, y + 1].count++; if (x - 1 >= 0 && y + 1 < _vertical) _mines[x - 1, y + 1].count++; if (x - 1 >= 0) _mines[x - 1, y].count++; }
public void Setup(ushort hor, ushort ver, ushort bombs) { _horizontal = hor; _vertical = ver; _bombs = bombs; _mines = new Mine[_horizontal, _vertical]; for (ushort i = 0; i < _horizontal; i++) { for (ushort j = 0; j < _vertical; j++) { Mines[i, j] = new Mine(i, j); Mines[i, j].Size = new System.Drawing.Size(25, 25); Mines[i, j].Location = new System.Drawing.Point(i * 25, j * 25); //Mines[i, j].Text = null; //Mines[i, j].MouseDown += new MouseEventHandler(MouseClickHandler); } } ushort t = 0; Random g = new Random(); while (t < _bombs) { ushort rX = (ushort)g.Next(0, _horizontal), rY = (ushort)g.Next(0, _vertical); if (!Mines[rX, rY].bomb) { Mines[rX, rY].bomb = true; addToNeigbours(Mines[rX, rY]); t++; } } }