1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 |
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication2 { public partial class Form1 : Form { # region Переменные private const int W = 40; private const int H = 40; private int[,] Fild = new int[Settings1.Default.MR + 2, Settings1.Default.MC + 2]; private int nMin; private int nFlag; private int status; # endregion # region Методы // Конструктор public Form1() { InitializeComponent(); newGame(); } // Новая игра private void newGame() { for (int row = 0; row <= Settings1.Default.MR + 1; row++) { Fild[row, 0] = -3; Fild[row, Settings1.Default.MC + 1] = -3; } for (int col = 0; col <= Settings1.Default.MC + 1; col++) { Fild[0, col] = -3; Fild[Settings1.Default.MR + 1, col] = -3; } this.ClientSize = new Size(W * Settings1.Default.MC + 1, H * Settings1.Default.MR + menuStrip1.Height + 1); int trow, tcol; int n = 0; int k; for (trow = 1; trow <= Settings1.Default.MR; trow++) for (tcol = 1; tcol <= Settings1.Default.MC; tcol++) Fild[trow, tcol] = 0; Random rnd = new Random(); do { trow = rnd.Next(Settings1.Default.MR) + 1; tcol = rnd.Next(Settings1.Default.MC) + 1; if (Fild[trow, tcol] != 9) { Fild[trow, tcol] = 9; n++; } } while (n != Settings1.Default.NM); for (trow = 1; trow <= Settings1.Default.MR; trow++) { for (tcol = 1; tcol <= Settings1.Default.MC; tcol++) { if (Fild[trow, tcol] != 9) { k = 0; if (Fild[trow - 1, tcol - 1] == 9) k++; if (Fild[trow - 1, tcol] == 9) k++; if (Fild[trow - 1, tcol + 1] == 9) k++; if (Fild[trow, tcol - 1] == 9) k++; if (Fild[trow, tcol + 1] == 9) k++; if (Fild[trow + 1, tcol - 1] == 9) k++; if (Fild[trow + 1, tcol] == 9) k++; if (Fild[trow + 1, tcol + 1] == 9) k++; Fild[trow, tcol] = k; } } } status = 0; nMin = 0; nFlag = 0; } // Отрисовка и расчет клеток поля private void showPole(Graphics g, int status) { for (int row = 1; row <= Settings1.Default.MR; row++) for (int col = 1; col <= Settings1.Default.MC; col++) this.kletka(g, row, col, status); } // Клетка поля private void kletka(Graphics g, int row, int col, int status) { int x; int y; x = (col - 1) * W + 1; y = (row - 1) * H + 1; if (Fild[row, col] < 100) { g.FillRectangle(Brushes.GreenYellow, x - 1, y - 1, W, H); } if (Fild[row, col] >= 100) { if (Fild[row, col] != 109) g.FillRectangle(Brushes.Khaki, x - 1, y - 1, W, H); else g.FillRectangle(Brushes.Red, x - 1, y - 1, W, H); if ((Fild[row, col] >= 101) && (Fild[row, col] <= 108)) g.DrawString((Fild[row, col] - 100).ToString(), new Font("Tahoma", 16, System.Drawing.FontStyle.Regular), Brushes.Indigo, x + 10, y + 7); } if (Fild[row, col] >= 200) { this.flag(g, x, y); } g.DrawRectangle(Pens.Black, x - 1, y - 1, W, H); if ((status == 2) && ((Fild[row, col] % 10) == 9)) { this.mina(g, x, y); } } // Открыть private void open(int row, int col) { int x = (col - 1) * W + 1; int y = (row - 1) * H + 1; if (Fild[row, col] == 0) { Fild[row, col] = 100; this.kletka(g, row, col, status); this.open(row, col - 1); this.open(row - 1, col); this.open(row, col + 1); this.open(row + 1, col); this.open(row - 1, col - 1); this.open(row - 1, col + 1); this.open(row + 1, col - 1); this.open(row + 1, col + 1); } else if ((Fild[row, col] < 100) && (Fild[row, col] != -3)) { Fild[row, col] += 100; this.kletka(g, row, col, status); } } // Так проще? ) private void mina(Graphics g, int x, int y) { g.DrawImage(Pic.m, new Point(x, y)); } // Так проще? ) private void flag(Graphics g, int x, int y) { g.DrawImage(Pic.f, new Point(x, y)); } # endregion # region Обработчики // private void panel1_MouseClick(object sender, MouseEventArgs e) { if (status == 2) return; if (status == 0) status = 1; int row = (int)(e.Y / H) + 1; int col = (int)(e.X / W) + 1; int x = (col - 1) * W + 1; int y = (row - 1) * H + 1; if (e.Button == MouseButtons.Left) { if (Fild[row, col] == 9) { Fild[row, col] += 100; status = 2; this.panel1.Invalidate(); const string message = "Вы взорвались:)!!! \n \n \n Играть еще??? "; const string caption = "проигрешь"; var result = MessageBox.Show(message, caption, MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (result == DialogResult.Yes) { newGame(); showPole(g, status); } else { Close(); } } else if (Fild[row, col] < 9) { this.open(row, col); } } if (e.Button == MouseButtons.Right) { if (Fild[row, col] <= 9) { nFlag += 1; if (Fild[row, col] == 9) { nMin += 1; } Fild[row, col] += 200; if ((nMin == Settings1.Default.NM) && (nFlag == Settings1.Default.NM)) { this.Invalidate(); const string message = "Хотите сыграть ещё??"; const string caption = "Победа!!!:)"; var result = MessageBox.Show(message, caption, MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (result == DialogResult.Yes) { newGame(); showPole(g, status); } else { Close(); } } else { this.kletka(g, row, col, status); } } else if (Fild[row, col] >= 200) { nFlag -= 1; Fild[row, col] -= 200; this.kletka(g, row, col, status); } } } // private void новаяИграToolStripMenuItem_Click(object sender, EventArgs e) { newGame(); showPole(g, status); } // private void оПрограммеToolStripMenuItem_Click(object sender, EventArgs e) { Form2 aboutBox = new Form2(); aboutBox.ShowDialog(); } // private void профессToolStripMenuItem_Click(object sender, EventArgs e) { Settings1.Default.MR = 16; Settings1.Default.MC = 30; Settings1.Default.NM = 99; Settings1.Default.Save(); this.ClientSize = new System.Drawing.Size(1202, 666); newGame(); showPole(g, status); } // private void любительToolStripMenuItem_Click(object sender, EventArgs e) { Settings1.Default.MR = 16; Settings1.Default.MC = 16; Settings1.Default.NM = 40; Settings1.Default.Save(); this.ClientSize = new System.Drawing.Size(642, 665); newGame(); showPole(g, status); } // private void новToolStripMenuItem_Click(object sender, EventArgs e) { Settings1.Default.MR = 9; Settings1.Default.MC = 9; Settings1.Default.NM = 10; Settings1.Default.Save(); this.ClientSize = new System.Drawing.Size(362, 385); newGame(); showPole(g, status); } // Отрисовка protected override void OnPaint(PaintEventArgs e) { showPole(e.Graphics, status); } # endregion } } |
Skip to content
Sign up
Search code, repositories, users, issues, pull requests…
Provide feedback
We read every piece of feedback, and take your input very seriously.
Include my email address so I can be contacted
Saved searches
Use saved searches to filter your results more quickly
Sign in
Sign up
AlexSikilinda
/
Saper
Public
-
Notifications
-
Fork
2 -
Star
5
Игра «сапер» на WinForms.
it-student.org.ua
5
stars
2
forks
Activity
Star
Notifications
master
{{ refName }}
default
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
1
branch
0
tags
Code
-
Clone
Use Git or checkout with SVN using the web URL.
-
Open with GitHub Desktop
-
Download ZIP
Latest commit
Git stats
-
4
commits
Files
Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
saper
README.md
saper.sln
saper.suo
README.md
Saper
Игра, аналогичная встроеной в Windows.
Написана на C# во время прохождения курса ООП в ХНУРЭ.
Демонстрирует применения паттерна Model-View. Цель паттерна — вынесение логики приложения из обработчиков событий WinForms.
About
Игра «сапер» на WinForms.
it-student.org.ua
Resources
Readme
Activity
Stars
5
stars
Watchers
3
watching
Forks
2
forks
Report repository
Releases
No releases published
Packages
No packages published
Languages
-
C#
100.0%
Introduction
Github repo
Download Exe
I will show you how to develop a MineSweeper game using C#.
The goal of this project is to create a MineSweeper as close as the original using C# WindowsForm.
Rule of MineSweeper.
- Click a cell to open it.
- If the cell is a mine cell the game is over.
- The first cell you click is guaraneed that it will not be a mine cell.
- You are given the information from the open cell, the number you see is the number of mine cell around the open cell.
In order to win the game, you need to open all the cells that are not mine cells.
The structure of the program
UI
I use a panel called pnlGame to handle most of the UI in this game.
It contains labels to display cell type.
Board class
int[,] Matrix keep the cell information.
Boolean[,] IsOpenCell to keep if the open/close state of the cell
ConstCell
Cell has 3 value, Bomb, Blank, HasValue
Game class
Contain Board object and status of the game.
The logic behind the game class when the game is created is
- Initial Board.
- Genereate mines on the board randomly.
- Loop though all of the board.
- if the cell is mine, list all of neightbour cell (8 adjanct cells next to mine cell)
- Loop though all the neightbour cell
- if (the neightbour is not
mine and does not has
the value yet and
Random value > 3)
set neightbour to be
HasValue cell - Loop though all of the cell in the board
- if the cell is has HasValue type, count number of mine around the cell (8 adjanct cells next co HasValue cell)
These are the codes.
private void SetBomb(Board pBoard, int NumberofBomb, Position positionDoesnotAllow) {
int i;
int j;
List <Position> listPositionBomb = GetUniqueRandomPosition(pBoard.NoofRow, pBoard.NoofCol, NumberofBomb, positionDoesnotAllow);
int CellValue = 0;
for (i = 0; i < NumberofBomb; i++) {
pBoard.Matrix[listPositionBomb[i].Row, listPositionBomb[i].Col] = ConstCell.Bomb;
}
}
private void SetHasValueCell(Board pBoard) {
int i;
int j;
for (i = 0; i < pBoard.NoofRow; i++) {
for (j = 0; j < pBoard.NoofCol; j++) {
if (pBoard.Matrix[i, j] != ConstCell.Bomb) {
continue;
}
List <Position> listPositionhasValue = GetNeighbourCell(i, j, pBoard.NoofRow, pBoard.NoofCol);
foreach(Position posi in listPositionhasValue) {
int NieghbourCellValue = pBoard.Matrix[posi.Row, posi.Col];
if (NieghbourCellValue.In(ConstCell.Bomb, ConstCell.HasValue)) {
continue;
}
int RandomValue = Random(1, 9);
if (RandomValue > 3) {
pBoard.Matrix[posi.Row, posi.Col] = ConstCell.HasValue;
}
}
}
}
}
public void GenerateMine(Board pBoard) {
int i;
int j;
InitialBoard(pBoard);
SetBomb(pBoard, this.NumberofMines, this.PostionThatMineMustNotExist);
if (PostionThatMineMustNotExist.Row == -1) {
this.board.Matrix[0, 0] = ConstCell.Bomb;
}
SetHasValueCell(pBoard);
for (i = 0; i < pBoard.NoofRow; i++) {
for (j = 0; j < pBoard.NoofCol; j++) {
if (pBoard.Matrix[i, j].In(ConstCell.Bomb, ConstCell.Blank)) {
continue;
}
List <Position> listNeighbour = GetNeighbourCell(i, j, pBoard.NoofRow, pBoard.NoofCol);
int NumberBomb = 0;
foreach(Position p in listNeighbour) {
if (pBoard.Matrix[p.Row, p.Col] == ConstCell.Blank) {
continue;
}
if (pBoard.Matrix[p.Row, p.Col] == ConstCell.Bomb) {
NumberBomb++;
}
}
pBoard.Matrix[i, j] = NumberBomb;
}
}
}
Enter fullscreen mode
Exit fullscreen mode
The logic behind cell click event is
- If Cell is not opened yet, just open it.
- If it is blankcell, open neighbor blank cell.
- after the cell was opened the game object will caluculate the result
if the game state is finished
if the first click got a mine cell (first time bad luck) just restart a game without losing.
(Our game try to prevent a first time bad luck).
else show the result
private void UserClick(Game pGame, int Row, int Column) {
if (MineSweep.GameState == GameStateEnum.End) {
return;
}
bool IsThisCellAlreayOpen = pGame.board.IsOpenCell[Row, Column];
if (IsThisCellAlreayOpen) {
return;
}
pGame.OpenCell(Row, Column);
if (this.timer1.Enabled == false) {
StartTimer();
}
int CellValue = pGame.board.Matrix[Row, Column];
if (pGame.board.Matrix[Row, Column] == ConstCell.Blank) {
pGame.OpenNeighborBlankCell(Row, Column);
}
if (pGame.GameState == GameStateEnum.Running) {
ShowFaceWonder();
return;
}
if (pGame.GameState == GameStateEnum.End) {
if (!pGame.HasSuccessfulClickthefirstCellWithoutDie) {
Position ByPassPositionFortheNewGame = new Position(Row, Column);
NewGame(ByPassPositionFortheNewGame);
UserClick(MineSweep, Row, Column);
return;
}
StopTimer();
if (pGame.GameResult == GameResultEnum.Lost) {
ShowFaceLost();
} else {
ShowFaceWon();
if (MineSweep.GameDifficultLevel == 4) {
return;
}
String message = @ "You broke a record for " + arrDifficultLevel[MineSweep.GameDifficultLevel];
if (MineSweep.Seconds < score.GetSecond(MineSweep.GameDifficultLevel)) {
FormEnterNewTimeRecord f = new FormEnterNewTimeRecord();
f.Message = message;
f.PreviousRecordName = score.GetName(MineSweep.GameDifficultLevel);
f.ShowDialog();
if (f.DialogResult != DialogResult.OK) {
return;
}
score.SetSecond(MineSweep.GameDifficultLevel, MineSweep.Seconds);
score.SetName(MineSweep.GameDifficultLevel, f.NewName);
SaveScore();
}
}
}
}
public void OpenNeighborBlankCell(int Row, int Column) {
List < Position > listNeighborRecursive = new List < Position > ();
HashSet < int > hshNeighborRecursive = new HashSet < int > ();
GetNeighbourCellRecursive(listNeighborRecursive,
hshNeighborRecursive,
Row,
Column,
board.NoofRow,
board.NoofCol,
ConstCell.Blank);
listNeighborRecursive.ForEach(x => OpenCell(x.Row, x.Col));
int i;
foreach(Position po in listNeighborRecursive) {
List < Position > listNeighbourNotBlank = GetNeighbourCell(po.Row, po.Col, board.NoofRow, board.NoofCol);
foreach(Position poNotBlank in listNeighbourNotBlank) {
if (!board.Matrix[poNotBlank.Row, poNotBlank.Col].IsBetween(1, 8)) {
continue;
}
OpenCell(poNotBlank);
}
}
}
private List < Position > GetNeighbourCellRecursive(List < Position > lisNeighborAll, HashSet < int > HshNeighborAll, int Row, int Column, int NoofRow, int NoofCol, int CellValueCriteria) {
List < Position > listNeighbor = GetNeighbourCell(Row, Column, board.NoofRow, board.NoofCol);
foreach(Position pos in listNeighbor) {
if (board.Matrix[pos.Row, pos.Col] != CellValueCriteria) {
continue;
}
int HashValue = pos.Row * NoofRow + pos.Col;
if (HshNeighborAll.Contains(HashValue)) {
continue;
}
HshNeighborAll.Add(HashValue);
lisNeighborAll.Add(pos.Clone());
List < Position > listNeighborChild = GetNeighbourCellRecursive(lisNeighborAll,
}
return listNeighbor;
}
Enter fullscreen mode
Exit fullscreen mode
I made a minesweeper game in c# as a method of studying, and right now its fully operational.
I tried to use inheritance, recursion, and other fundamentals as clean as possible. Also I tried to use commenting as much as possible as I see many people do not do this, and it makes the code all the more confusing, both for the reader and the coder (at least for me).
I tried my best to handle exceptions and anticipate possible problems, and I fixed any I could find. There was one exception however, it was a stack overflow exception, but it happened only one time and i couldn’t reproduce it again to correct. If anyone can reproduce it and inform why and how, it will be much appreciated.
If some part of code is missing, just let me know and i will add them as an edit.
Thank you very much for your help.
Form1.cs :
using MineSweeper.Properties;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace MineSweeper
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
readonly string msgBoxTitle = "Warning";
List<int> panels = new List<int>();
int rowCount;
int columnCount;
private void ModifyMineField()
{
panels.Clear(); //clears the array in case user will make a new field
MineFieldTable.RowStyles.Clear(); //resets the table
MineFieldTable.ColumnStyles.Clear(); //resets the table
MineFieldTable.Controls.Clear(); //resets the table
MineFieldTable.Visible = false;
rowCount = Convert.ToInt32(TB_tableDimension1.Text); //take rowCount from user input
columnCount = Convert.ToInt32(TB_tableDimension2.Text); //take columnCount from user input
int mineCount = Convert.ToInt32(TB_mineCount.Text); //amount of mines, decided by user
MineFieldTable.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;
MineFieldTable.AutoSize = true;
MineFieldTable.RowCount = rowCount;
MineFieldTable.ColumnCount = columnCount;
Random rnd = new Random();
int counter = 0;
int[] panelArray = new int[rowCount * columnCount]; //track the panel layout, 0= empty, 1=mine, 2=opened
for (int i = 0; i < panelArray.Length; i++) //populate the array with empty land, to be re-designated later
{
panelArray[i] = 0;
}
while (counter < mineCount) //creating mine coordinates by array numbering
{
int a = rnd.Next(0, rowCount * columnCount - 1);
if (panelArray[a] != 1) { panelArray[a] = 1; counter++; }
}
panels.AddRange(panelArray);
for (int i = 0; i < MineFieldTable.RowCount; i++) //generating row styles
{
MineFieldTable.RowStyles.Add(new RowStyle(SizeType.Absolute, 30f));
}
for (int i = 0; i < MineFieldTable.ColumnCount; i++) //generating column styles
{
MineFieldTable.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 30f));
}
for (int i = 0; i < rowCount * columnCount; i++) //lay the field with mines or not
{
if (panels[i] != 1) //add empty land
{
NoFocusButton emptyLand = new NoFocusButton("", FlatStyle.Standard, 0) { };
emptyLand.MouseUp += Land_Click;
MineFieldTable.Controls.Add(emptyLand);
}
if (panels[i] == 1) //add mines
{
NoFocusButton mine = new NoFocusButton("", FlatStyle.Standard, 1) { };
mine.MouseUp += Land_Click;
MineFieldTable.Controls.Add(mine);
}
}
MineFieldTable.Visible = true; //to make field generation quicker, first invisible, then visible
}
private void BtnCreate_Click(object sender, EventArgs e)
{
if ( //check if user entered correct parameters
int.TryParse(TB_mineCount.Text, out _) == false
|| int.TryParse(TB_tableDimension1.Text, out _) == false
|| int.TryParse(TB_tableDimension2.Text, out _) == false
|| Convert.ToInt32(TB_mineCount.Text) < 1
|| Convert.ToInt32(TB_tableDimension1.Text) < 1
|| Convert.ToInt32(TB_tableDimension1.Text) > 30
|| Convert.ToInt32(TB_tableDimension2.Text) < 1
|| Convert.ToInt32(TB_tableDimension2.Text) > 30
|| Convert.ToInt32(TB_mineCount.Text) > Convert.ToInt32(TB_tableDimension1.Text) * Convert.ToInt32(TB_tableDimension2.Text))
{
MessageBox.Show("Please enter valid values, and consider the following: \n \n-Field cannot be bigger than 30x30.\n \n-Mines cannot be more than the field size.", msgBoxTitle);
}
else
{
try { MineFieldTable.Visible = false; ModifyMineField(); } catch (Exception ex) { MessageBox.Show(ex.Message, msgBoxTitle); }
}
}
private void Land_Click(object sender, MouseEventArgs e)
{
TableLayoutPanelCellPosition clickCoords = MineFieldTable.GetPositionFromControl((Control)sender); //obtaining coordinates of control
NoFocusButton currentControl = MineFieldTable.GetControlFromPosition(clickCoords.Column, clickCoords.Row) as NoFocusButton; //the current control, obtained from coords
if (e.Button == MouseButtons.Left & currentControl.Text != ".")
{
if (panels[clickCoords.Column + (clickCoords.Row) * columnCount] == 0) //if the clicked land is empty land
{
currentControl.LandOpening(clickCoords.Row, clickCoords.Column, MineFieldTable, panels);
}
if (panels[clickCoords.Column + (clickCoords.Row) * columnCount] == 1) //stepped on a mine!
{
NoFocusButton currentButton = MineFieldTable.GetControlFromPosition(clickCoords.Column, clickCoords.Row) as NoFocusButton;
currentButton.BackgroundImage = Resources.mineExplode;
currentButton.Text = "";
currentButton.FlatStyle = FlatStyle.Flat;
NoFocusButton currentButton2; //other panels on the land other than the stepped panel
for (int i = 0; i < panels.Count; i++)
{
if (panels[i] == 1)
{
int row = i / columnCount;
int column = i % columnCount;
currentButton2 = MineFieldTable.GetControlFromPosition(column, row) as NoFocusButton;
if (clickCoords.Row != row | clickCoords.Column != column) //in order to not replace the red mine
{
currentButton2.FlatStyle = FlatStyle.Flat;
currentButton2.BackgroundImage = Resources.mine;
}
if (currentButton2.Text == ".") //if mine was flagged, show it as green
{
currentButton2.FlatStyle = FlatStyle.Flat;
currentButton2.Image = null;
currentButton2.BackgroundImage = Resources.mineGreen;
}
}
if (panels[i] != 1)
{
int row = i / columnCount;
int column = i % columnCount;
currentButton2 = MineFieldTable.GetControlFromPosition(column, row) as NoFocusButton;
if (currentButton2.Text == ".") //if a flag was placed wrong, indicate that it was wrong
{
currentButton2.Image = null;
currentButton2.Text = "";
currentButton2.Image = Resources.flag2crossed;
}
}
}
MessageBox.Show("You lost the game!", "Game Over");
MineFieldTable.Visible = false;
MineFieldTable.Controls.Clear();
}
}
if (e.Button == MouseButtons.Right) //flagging land where suspected to be mines
{
if (currentControl.Text == "") //place flag
{
currentControl.Image = Resources.flag2;
currentControl.Text = ".";
currentControl.ForeColor = Color.Gray;
}
else if (currentControl.Text == ".") //remove flag
{
currentControl.Image = null;
currentControl.Text = "";
}
}
if (panels.Contains(0) == false) //if all mines are revealed, finish the game
{
for (int i = 0; i < panels.Count; i++)
{
if (panels[i] == 1)
{
int row = i / columnCount;
int column = i % columnCount;
NoFocusButton currentButton2 = MineFieldTable.GetControlFromPosition(column, row) as NoFocusButton;
if (currentButton2.Text == ".") //if mine was flagged, show it as green
{
currentButton2.FlatStyle = FlatStyle.Flat;
currentButton2.Image = null;
currentButton2.BackgroundImage = Resources.mineGreen;
}
if (currentButton2.Text != ".") //if mine was not flagged, show as usual
{
currentButton2.FlatStyle = FlatStyle.Flat;
currentButton2.BackgroundImage = Resources.mine;
}
}
}
MessageBox.Show("You won the game!", "Game Over");
MineFieldTable.Visible = false;
MineFieldTable.Controls.Clear();
}
}
}
}
NoFocusButton.cs (a custom class for buttons) :
using MineSweeper.Properties;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace MineSweeper
{
class NoFocusButton : Button
{
public NoFocusButton(string text, FlatStyle flatstyle, int indexnumber)
{
SetStyle(ControlStyles.Selectable, false);
Text = text;
TextAlign = ContentAlignment.MiddleCenter;
Margin = new Padding(0);
Padding = new Padding(0);
Dock = DockStyle.Fill;
FlatStyle = flatstyle;
FlatAppearance.BorderSize = 0;
}
public void LandOpening(int coordinateX, int coordinateY, TableLayoutPanel table, List<int> panelArray) //if the opened land is empty, do this
{
for (int i = -1; i < 2; i++)
{
for (int j = -1; j < 2; j++)
{
try
{
int a = coordinateY + j; int b = coordinateX + i; //coordinates of the current control
if (a > -1 && b > -1 && a < table.ColumnCount && b < table.RowCount) //to confirm if the control index is not out of bounds
{
NoFocusButton currentButton = table.GetControlFromPosition(a, b) as NoFocusButton;
if (panelArray[a + b * table.ColumnCount] == 0) //if the nearby land is empty, write the mine count on that land
{
int nearbyMines = 0;
for (int k = -1; k < 2; k++) //to determine minecount near opened land and write it
{
for (int l = -1; l < 2; l++)
{
int d = coordinateY + j + k; int e = coordinateX + i + l; //to shorten future code
if (d > -1 && e > -1 && d < table.ColumnCount && e < table.RowCount) //to confirm if the control index is not out of bounds
{
NoFocusButton openingButton = table.GetControlFromPosition(d, e) as NoFocusButton;
bool c = int.TryParse(openingButton.Text, out int n); //to shorten future code
if (panelArray[d + e * table.ColumnCount] == 1 && c == false) //to check if cell has number inside
{
nearbyMines++;
}
}
}
}
if (a >= 0 && b >= 0 && a <= table.ColumnCount && b <= table.RowCount) //to confirm if the control index is not out of bounds
{
NoFocusButton x = table.GetControlFromPosition(a, b) as NoFocusButton;
if (nearbyMines != 0 & x.Text !=".") //dont write 0 mines
{
x.Text = nearbyMines.ToString();
}
if (x.Text != ".")
{
x.FlatStyle = FlatStyle.Flat;
x.Enabled = false;
panelArray[a + b * table.ColumnCount] = 2; //mark this panel in the array as opened
}
}
if (nearbyMines == 0) //using recursion to open more area if there is a panel with zero at the border of opened area
{
LandOpening(b, a, table, panelArray);
}
}
}
}
catch (Exception ex) { MessageBox.Show(ex.Message); }
}
}
}
}
}
Form1.Designer.cs :
namespace MineSweeper
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
this.TB_tableDimension1 = new System.Windows.Forms.TextBox();
this.TB_tableDimension2 = new System.Windows.Forms.TextBox();
this.BtnCreate = new System.Windows.Forms.Button();
this.MineFieldTable = new System.Windows.Forms.TableLayoutPanel();
this.TB_mineCount = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// TB_tableDimension1
//
this.TB_tableDimension1.Location = new System.Drawing.Point(20, 25);
this.TB_tableDimension1.MaxLength = 2;
this.TB_tableDimension1.Name = "TB_tableDimension1";
this.TB_tableDimension1.Size = new System.Drawing.Size(20, 20);
this.TB_tableDimension1.TabIndex = 2;
this.TB_tableDimension1.Text = "10";
//
// TB_tableDimension2
//
this.TB_tableDimension2.Location = new System.Drawing.Point(50, 25);
this.TB_tableDimension2.MaxLength = 2;
this.TB_tableDimension2.Name = "TB_tableDimension2";
this.TB_tableDimension2.Size = new System.Drawing.Size(20, 20);
this.TB_tableDimension2.TabIndex = 3;
this.TB_tableDimension2.Text = "10";
//
// BtnCreate
//
this.BtnCreate.Location = new System.Drawing.Point(160, 25);
this.BtnCreate.Name = "BtnCreate";
this.BtnCreate.Size = new System.Drawing.Size(75, 20);
this.BtnCreate.TabIndex = 1;
this.BtnCreate.Text = "CREATE";
this.BtnCreate.UseVisualStyleBackColor = true;
this.BtnCreate.Click += new System.EventHandler(this.BtnCreate_Click);
//
// MineFieldTable
//
this.MineFieldTable.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
this.MineFieldTable.CellBorderStyle = System.Windows.Forms.TableLayoutPanelCellBorderStyle.Single;
this.MineFieldTable.ColumnCount = 10;
this.MineFieldTable.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 20F));
this.MineFieldTable.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 20F));
this.MineFieldTable.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 20F));
this.MineFieldTable.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 20F));
this.MineFieldTable.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 20F));
this.MineFieldTable.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 20F));
this.MineFieldTable.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 20F));
this.MineFieldTable.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 20F));
this.MineFieldTable.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 20F));
this.MineFieldTable.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 30F));
this.MineFieldTable.Location = new System.Drawing.Point(20, 50);
this.MineFieldTable.Margin = new System.Windows.Forms.Padding(0);
this.MineFieldTable.Name = "MineFieldTable";
this.MineFieldTable.RowCount = 10;
this.MineFieldTable.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 20F));
this.MineFieldTable.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 20F));
this.MineFieldTable.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 20F));
this.MineFieldTable.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 20F));
this.MineFieldTable.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 20F));
this.MineFieldTable.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 20F));
this.MineFieldTable.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 20F));
this.MineFieldTable.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 20F));
this.MineFieldTable.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 20F));
this.MineFieldTable.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 20F));
this.MineFieldTable.Size = new System.Drawing.Size(210, 210);
this.MineFieldTable.TabIndex = 4;
this.MineFieldTable.Visible = false;
//
// TB_mineCount
//
this.TB_mineCount.Location = new System.Drawing.Point(100, 25);
this.TB_mineCount.MaxLength = 3;
this.TB_mineCount.Name = "TB_mineCount";
this.TB_mineCount.Size = new System.Drawing.Size(25, 20);
this.TB_mineCount.TabIndex = 4;
this.TB_mineCount.Text = "10";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(70, 28);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(27, 13);
this.label2.TabIndex = 6;
this.label2.Text = "Size";
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(125, 28);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(35, 13);
this.label3.TabIndex = 7;
this.label3.Text = "Mines";
//
// label1
//
this.label1.AutoSize = true;
this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 6F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.label1.Location = new System.Drawing.Point(40, 31);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(10, 9);
this.label1.TabIndex = 8;
this.label1.Text = "X";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.AutoScroll = true;
this.AutoSize = true;
this.ClientSize = new System.Drawing.Size(284, 261);
this.Controls.Add(this.label1);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.TB_mineCount);
this.Controls.Add(this.MineFieldTable);
this.Controls.Add(this.BtnCreate);
this.Controls.Add(this.TB_tableDimension2);
this.Controls.Add(this.TB_tableDimension1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "Form1";
this.Padding = new System.Windows.Forms.Padding(0, 0, 20, 20);
this.Text = "MineSweeper by MHS";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.TextBox TB_tableDimension1;
private System.Windows.Forms.TextBox TB_tableDimension2;
private System.Windows.Forms.Button BtnCreate;
private System.Windows.Forms.TableLayoutPanel MineFieldTable;
private System.Windows.Forms.TextBox TB_mineCount;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label1;
}
}
Введение
Алгоритм Сапера – это написанный на языке программирования смешанной реальности, процедурный алгоритм, который используется для создания компьютерной игры, известной как «Сапер». Разработанная компания Microsoft Сапер была выпущена в 1990-х годах и с тех пор стала одной из самых популярных игр, поставляемых с операционной системой Microsoft Windows.
Цель Сапера состоит в том, чтобы открыть все клетки на игровом поле, кроме тех, которые содержат бомбы. Клетки на поле, которые не содержат бомб, могут содержать число, указывающее, сколько мин рядом, а клетки, содержащие бомбы, будут помечены специальным символом.
В этой статье мы рассмотрим различные аспекты алгоритма Сапера и опишем, как его можно реализовать на языке программирования C#.
Технические аспекты алгоритма Сапера
В игре Сапер используется квадратное поле, которое разделено на клетки. Каждая клетка может быть открыта или закрыта. Если клетка содержит бомбу, она будет помечена специальным символом.
Когда в игре была открыта каждая клетка, кроме тех, которые содержат бомбы, игра будет завершена, и игрок победит. Если же игрок откроет клетку, содержащую бомбу, игра завершится проигрышем.
В игре Сапер каждая закрытая клетка содержит определенное количество мин, которое может быть отображено или скрыто от игрока. При открытии клетки число, отображающее количество мин находит следующее выражение:
n = n1 + n2 + n3 + n4 + n5 + n6 + n7 + n8,
где n1 — количество мин в клетке сверху слева, n2 — количество мин в клетке сверху, n3 — количество мин в клетке сверху справа, n4 — количество мин в клетке слева, n5 — количество мин в клетке справа, n6 — количество мин в клетке снизу слева, n7 — количество мин в клетке снизу, n8 — количество мин в клетке снизу справа.
Реализация алгоритма Сапера на языке C#
Сначала мы должны создать модель данных для игры. В игре Сапер имеется следующий объект:
class Cell
{
public bool IsBomb { get; set; } // содержит ли клетка бомбу
public int Number { get; set; } // количество клеток рядом, содержащих мин
public bool IsOpen { get; set; } // открыта ли клетка
public bool IsFlagged { get; set; } // помечена ли клетка флагом
}
Затем мы создаем класс игрового поля, который хранит массив клеток:
class Field
{
private Cell[,] _cells;
}
Далее мы выполняем инициализацию массива ячеек при помощи следующего метода:
private void InitializeCells(int bombsCount)
{
var random = new Random(DateTime.Now.Millisecond);
for (var i = 0; i < _cells.GetLength(0); i++)
{
for (var j = 0; j < _cells.GetLength(1); j++)
{
_cells[i, j] = new Cell();
}
}
for (var i = 0; i < bombsCount; i++)
{
var x = random.Next(_cells.GetLength(0));
var y = random.Next(_cells.GetLength(1));
while (_cells[x, y].IsBomb)
{
x = random.Next(_cells.GetLength(0));
y = random.Next(_cells.GetLength(1));
}
_cells[x, y].IsBomb = true;
}
}
Метод InitializeCells случайным образом перемещает на поле заданное количество бомб.
Далее опишем метод для вычисления количества мин рядом с определенной клеткой:
private int GetBombsInNeighborhood(int x, int y)
{
var bombs = 0;
if (y > 0 && _cells[x, y — 1].IsBomb)
{
bombs++;
}
if (x > 0 && y > 0 && _cells[x — 1, y — 1].IsBomb)
{
bombs++;
}
if (x > 0 && _cells[x — 1, y].IsBomb)
{
bombs++;
}
if (x > 0 && y < _cells.GetLength(1) — 1 && _cells[x — 1, y + 1].IsBomb)
{
bombs++;
}
if (y < _cells.GetLength(1) — 1 && _cells[x, y + 1].IsBomb)
{
bombs++;
}
if (x < _cells.GetLength(0) — 1 && y < _cells.GetLength(1) — 1 &
_cells[x + 1, y + 1].IsBomb)
{
bombs++;
}
if (x < _cells.GetLength(0) — 1 && _cells[x + 1, y].IsBomb)
{
bombs++;
}
if (x < _cells.GetLength(0) — 1 && y > 0 && _cells[x + 1, y — 1].IsBomb)
{
bombs++;
}
return bombs;
}
Данный метод находит количество мин в 8 соседних клетках, находящихся вокруг заданной клетки на игровом поле.
Для отображения игрового поля и его изменения на форме нам понадобится класс, наследуемый от класса System.Windows.Forms.Panel:
class FieldPanel : Panel
{
private int _cellSize;
private Field _field;
private readonly Dictionary<Keys, bool> _pressedKeys;
public FieldPanel(int width, int height, int cellSize, Field field)
{
_cellSize = cellSize;
_field = field;
_pressedKeys = new Dictionary<Keys, bool>();
Width = width * _cellSize;
Height = height * _cellSize;
MouseDown += FieldPanel_MouseDown;
MouseMove += FieldPanel_MouseMove;
MouseUp += FieldPanel_MouseUp;
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
for (var i = 0; i < _field.Width; i++)
{
for (var j = 0; j < _field.Height; j++)
{
var cell = _field[i, j];
var x = i * _cellSize;
var y = j * _cellSize;
e.Graphics.FillRectangle(Brushes.Silver, x, y, _cellSize, _cellSize);
if (cell.IsOpen)
{
if (cell.IsBomb)
{
e.Graphics.DrawString(«*», new Font(«Arial», _cellSize — 4), Brushes.Black, x + 2, y);
}
else if (cell.Number > 0)
{
e.Graphics.DrawString(cell.Number.ToString(), new Font(«Arial», _cellSize — 4), Brushes.Black, x + 2, y);
}
}
if (cell.IsFlagged)
{
e.Graphics.DrawString(«F», new Font(«Arial», _cellSize — 4), Brushes.Black, x + 2, y);
}
}
}
}
}
В классе FieldPanel для каждой клетки игрового поля определим вывод текстовой символики и изображений.
И последним шагом в разработке алгоритма Сапера на языке C# будет создание основной формы приложения:
public partial class MainForm : Form
{
private readonly Field _field;
private FieldPanel _fieldPanel;
private bool _gameIsOver;
public MainForm()
{
InitializeComponent();
_field = new Field(10, 10);
_field.Initialize(10);
_fieldPanel = new FieldPanel(10, 10, 20, _field);
Controls.Add(_fieldPanel);
}
}
Обратите внимание на то, что в данной реализации мы создали поле фиксированной размерности 10*10, и в метод Initialize было добавлено 10 случайных бомб. Также было установлено соответствие между размером клетки и размером панели.
Заключение
В этой статье мы рассмотрели алгоритм Сапера и показали, как его можно реализовать на языке программирования C#. Создана модель данных для игры и класс игрового поля, который содержит массив с клетками. Для отображения игрового поля на экране разработан класс FieldPanel. И наконец, мы создали основную форму приложения, где добавили нужный элемент управления на котором реализовано отображение всего игрового поля.
Хотя это небольшой проект, реализация алгоритма Сапера может улучшить ваши навыки программирования, так как в нем присутствуют основные элементы любой игры — генерация случайных объектов, обработка пользовательского ввода, рисование на экране, управление памятью и т. д.