private void CreateGame() { allButtons = new ButtonExtended[sizeFieldInCells, sizeFieldInCells]; Random random = new Random(); for (int i = 0, index_y = 0; i < fieldHeight; i += changeLocation, index_y++) { for (int j = 0, index_x = 0; j < fieldWidth; j += changeLocation, index_x++) { ButtonExtended button = new ButtonExtended(); button.Location = new Point(start_x + j, start_y + i); button.Size = new Size(cellWidth, cellHeight); button.isFlag = true; if (random.Next(0, 100) < bombCreationProbability) //вероятность 20% появления бомбы на кнопке { button.isBomb = true; amountOfBombs++; } button.x_currectCell = index_x; button.y_currectCell = index_y; allButtons[index_x, index_y] = button; Controls.Add(button); button.MouseUp += new MouseEventHandler(ClickOnСell); button.Image = Image.FromFile("C:\\Users\\nikol\\source\\repos\\Sapper\\Images\\backGround.png"); } } }
void OpenRegion(int index_x, int index_y, ButtonExtended button) { Queue <ButtonExtended> queue = new Queue <ButtonExtended>(); queue.Enqueue(button); button.isOpen = true; while (queue.Count > 0) { ButtonExtended currectСell = queue.Dequeue(); OpenCell(currectСell.x_currectCell, currectСell.y_currectCell, currectСell); ShowImageInsteadOfNumbers(currectСell.x_currectCell, currectСell.y_currectCell, currectСell); if (CountBombsAround(currectСell.x_currectCell, currectСell.y_currectCell) == 0) { for (int x = currectСell.x_currectCell - 1; x <= currectСell.x_currectCell + 1; x++) { for (int y = currectСell.y_currectCell - 1; y <= currectСell.y_currectCell + 1; y++) { if (x == currectСell.x_currectCell && y == currectСell.y_currectCell) { continue; } if (x >= 0 && x < sizeFieldInCells && y >= 0 && y < sizeFieldInCells) { if (!allButtons[x, y].isOpen) { queue.Enqueue(allButtons[x, y]); allButtons[x, y].isOpen = true; } } } } } } }
void ClickOnСell(object sender, MouseEventArgs e) { gameTimer.Enabled = true; ButtonExtended button = (ButtonExtended)sender; LeftClick(e, button); RightClick(e, button); DisplayingBombs(); DisplayingAllEmptyCells(); RegistrationOfVictory(); }
void EmptyСell(ButtonExtended button) { for (int i = 0; i < sizeFieldInCells; i++) { for (int j = 0; j < sizeFieldInCells; j++) { if (allButtons[i, j] == button) { OpenRegion(i, j, button); } } } }
void OpenCell(int index_x, int index_y, ButtonExtended button) { if (button.wasСlicked == false) { amountOfOpenedСells++; DisplayingOpenedCells(); button.wasСlicked = true; } int bombcount = CountBombsAround(index_x, index_y); if (button.isOpen) { ShowImageInsteadOfNumbers(index_x, index_y, button); } }
void ShowImageInsteadOfNumbers(int index_x, int index_y, ButtonExtended button) { if (!button.isBomb) { if (CountBombsAround(index_x, index_y) == 0) { button.Image = Image.FromFile("C:\\Users\\nikol\\source\\repos\\Sapper\\Images\\whiteBackground2.png"); } if (CountBombsAround(index_x, index_y) == 1) { button.Image = Image.FromFile("C:\\Users\\nikol\\source\\repos\\Sapper\\Images\\1.png"); } if (CountBombsAround(index_x, index_y) == 2) { button.Image = Image.FromFile("C:\\Users\\nikol\\source\\repos\\Sapper\\Images\\2.png"); } if (CountBombsAround(index_x, index_y) == 3) { button.Image = Image.FromFile("C:\\Users\\nikol\\source\\repos\\Sapper\\Images\\3.png"); } if (CountBombsAround(index_x, index_y) == 4) { button.Image = Image.FromFile("C:\\Users\\nikol\\source\\repos\\Sapper\\Images\\4.png"); } if (CountBombsAround(index_x, index_y) == 5) { button.Image = Image.FromFile("C:\\Users\\nikol\\source\\repos\\Sapper\\Images\\5.png"); } if (CountBombsAround(index_x, index_y) == 6) { button.Image = Image.FromFile("C:\\Users\\nikol\\source\\repos\\Sapper\\Images\\6.png"); } if (CountBombsAround(index_x, index_y) == 7) { button.Image = Image.FromFile("C:\\Users\\nikol\\source\\repos\\Sapper\\Images\\7.png"); } if (CountBombsAround(index_x, index_y) == 8) { button.Image = Image.FromFile("C:\\Users\\nikol\\source\\repos\\Sapper\\Images\\8.png"); } } }
private void RightClick(MouseEventArgs e, ButtonExtended button) { if (e.Button == MouseButtons.Right && !button.isOpen) { button.isFlag = !button.isFlag; if (!button.isFlag) { button.Image = Image.FromFile("C:\\Users\\nikol\\source\\repos\\Sapper\\Images\\flag.png"); } else if (!button.isQuestion) { button.Image = Image.FromFile("C:\\Users\\nikol\\source\\repos\\Sapper\\Images\\question.png"); button.isQuestion = !button.isQuestion; button.isFlag = false; } else if (button.isQuestion) { button.Image = Image.FromFile("C:\\Users\\nikol\\source\\repos\\Sapper\\Images\\backGround.png"); button.isQuestion = !button.isQuestion; } } }
private void LeftClick(MouseEventArgs e, ButtonExtended button) { if (e.Button == MouseButtons.Left && button.isFlag) { if (firstclick) { if (button.isBomb) { button.isBomb = false; amountOfBombs--; } firstclick = false; } if (button.isBomb) { Explode(); } else { EmptyСell(button); } } }