0% found this document useful (0 votes)
40 views10 pages

Minesweeperr

The document contains code for a minesweeper game implemented with C# and Windows Forms. It initializes a 25x25 grid of buttons to represent the game board and randomly places 150 mines. It then calculates the number of adjacent mines for each non-mine square and stores the count in a 2D array. When a button is clicked, if it is a mine the game ends, otherwise it will recursively reveal any adjacent squares with a count of 0.

Uploaded by

itsbigbraintime6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views10 pages

Minesweeperr

The document contains code for a minesweeper game implemented with C# and Windows Forms. It initializes a 25x25 grid of buttons to represent the game board and randomly places 150 mines. It then calculates the number of adjacent mines for each non-mine square and stores the count in a 2D array. When a button is clicked, if it is a mine the game ends, otherwise it will recursively reveal any adjacent squares with a count of 0.

Uploaded by

itsbigbraintime6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 10

using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp19
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Button[,] matrica = new Button[25, 25];
int[,] mine = new int[25, 25];
Random random = new Random();
bool zas = false;
private void Form1_Load(object sender, EventArgs e)
{
int m = 0;
for (int i = 0; i < 25; i++)
{
for (int j = 0; j < 25; j++)
{
m++;
string ime = $"Button{m}";
matrica[i, j] = (Button)Controls.Find(ime, true)[0];
}
}
for (int i = 0; i < 150; i++)
{
int i1 = random.Next(0, 24);
int j1 = random.Next(0, 24);
mine[i1, j1] = 100;
}
int br = 0;
for (int i = 0; i < 25; i++)
{

for (int j = 0; j < 25; j++)


{
br = 0;
if (mine[i, j] == 100)
{

}
#region gornji levi cosak
else if (i == 0 && j == 0)
{
if (mine[i + 1, j + 1] == 100)
{
br++;
}
if (mine[i + 1, j] == 100)
{
br++;
}
if (mine[i, j + 1] == 100)
{
br++;
}
mine[i, j] = br;
}
#endregion
#region gornji desni cosak
else if (i == 0 && j == 24)
{
if (mine[i + 1, j] == 100)
{
br++;
}
if (mine[i + 1, j - 1] == 100)
{
br++;
}
if (mine[i, j - 1] == 100)
{
br++;
}
mine[i, j] = br;
}
#endregion
#region dolelevo
else if (i == 24 && j == 0)
{
if (mine[i, j + 1] == 100)
{
br++;
}
if (mine[i - 1, j + 1] == 100)
{
br++;
}
if (mine[i - 1, j] == 100)
{
br++;
}
mine[i, j] = br;
}
#endregion
#region doledesno
else if (i == 24 && j == 24)
{
if (mine[i, j - 1] == 100)
{
br++;
}
if (mine[i - 1, j - 1] == 100)
{
br++;
}
if (mine[i - 1, j] == 100)
{
br++;
}
mine[i, j] = br;
}
#endregion
#region gore
else if (i == 0)
{
if (mine[i, j - 1] == 100)
{
br++;
}
if (mine[i, j + 1] == 100)
{
br++;
}
if (mine[i + 1, j] == 100)
{
br++;
}
if (mine[i + 1, j + 1] == 100)
{
br++;
}
if (mine[i + 1, j - 1] == 100)
{
br++;
}
mine[i, j] = br;
}
#endregion
#region levo
else if (j == 0)
{
if (mine[i, j + 1] == 100)
{
br++;
}
if (mine[i - 1, j] == 100)
{
br++;
}
if (mine[i + 1, j] == 100)
{
br++;
}
if (mine[i - 1, j + 1] == 100)
{
br++;
}
if (mine[i + 1, j + 1] == 100)
{
br++;
}
mine[i, j] = br;
}
#endregion
#region desno
else if (j == 24)
{
if (mine[i, j - 1] == 100)
{
br++;
}
if (mine[i - 1, j] == 100)
{
br++;
}
if (mine[i + 1, j] == 100)
{
br++;
}
if (mine[i - 1, j - 1] == 100)
{
br++;
}
if (mine[i + 1, j - 1] == 100)
{
br++;
}
mine[i, j] = br;
}
#endregion
#region dole
else if (i == 24)
{
if (mine[i, j + 1] == 100)
{
br++;
}
if (mine[i, j - 1] == 100)
{
br++;
}
if (mine[i - 1, j] == 100)
{
br++;
}
if (mine[i - 1, j - 1] == 100)
{
br++;
}
if (mine[i - 1, j + 1] == 100)
{
br++;
}
mine[i, j] = br;
}
#endregion
#region srednje
else
{
if (mine[i, j + 1] == 100)
{
br++;
}
if (mine[i, j - 1] == 100)
{
br++;
}
if (mine[i - 1, j] == 100)
{
br++;
}
if (mine[i - 1, j - 1] == 100)
{
br++;
}
if (mine[i - 1, j + 1] == 100)
{
br++;
}
if (mine[i + 1, j + 1] == 100)
{
br++;
}
if (mine[i + 1, j] == 100)
{
br++;
}
if (mine[i + 1, j - 1] == 100)
{
br++;
}
mine[i, j] = br;
}
#endregion

}
}
}
private void button_Click(object sender, EventArgs e)
{

}
public void Okolo(Button[,] matrica, int[,] mine, int i1, int j1)
{
try
{
matrica[i1 + 1, j1].Text = mine[i1 + 1, j1].ToString();
}
catch
{
}
try
{
matrica[i1 - 1, j1].Text = mine[i1 - 1, j1].ToString();
}
catch
{

}
try
{
matrica[i1 - 1, j1 - 1].Text = mine[i1 - 1, j1 - 1].ToString();

}
catch
{

}
try
{
matrica[i1 - 1, j1 + 1].Text = mine[i1 - 1, j1 + 1].ToString();

}
catch
{

}
try
{
matrica[i1, j1 + 1].Text = mine[i1, j1 + 1].ToString();
}
catch { }
try
{
matrica[i1, j1 - 1].Text = mine[i1, j1 - 1].ToString();
}
catch { }
try
{
matrica[i1 + 1, j1 + 1].Text = mine[i1 + 1, j1 + 1].ToString();
}
catch
{

}
try
{
matrica[i1 + 1, j1 - 1].Text = mine[i1 + 1, j1 - 1].ToString();
}
catch
{

}
}
private void universalButtonClick(object sender, MouseEventArgs e)
{
Button clickedButton = sender as Button;
if (!zas)
{
for (int i = 0; i < 25; i++)
{
for (int j = 0; j < 25; j++)
{
int i1 = i;
int j1 = j;
if (clickedButton == matrica[i, j])
{
if (mine[i, j] == 100)
{

clickedButton.Text = " ";


for (int k = 0; k < 25; k++)
{
for (int l = 0; l < 25; l++)
{
matrica[k, l].BackColor = Color.RosyBrown;
}
}
}
else if (e.Button == MouseButtons.Left)
{
matrica[i, j].Text = mine[i, j].ToString();
if (mine[i, j] == 0)
{
try
{
while (mine[i1, j1 + 1] == 0 && j1 + 1 <
24)
{
matrica[i1, j1 + 1].Text = mine[i1, j1
+ 1].ToString();
j1++;

}
catch { }
Okolo(matrica, mine, i1, j1);
j1 = j;
try
{
while (mine[i1, j1 - 1] == 0 && j1 - 1 > -
1)
{

matrica[i1, j1 - 1].Text = mine[i1, j1


- 1].ToString();
j1--;

}
}
catch { }
j1 = j;
try
{
while (mine[i1, j1 - 1] == 0 && j1 - 1 > -
1)
{

matrica[i1, j1 - 1].Text = mine[i1, j1


- 1].ToString();
j1--;

}
}
catch { }
j1 = j;
try
{
while (mine[i1, j1 - 1] == 0 && j1 - 1 > -
1)
{
matrica[i1, j1 - 1].Text = mine[i1, j1
- 1].ToString();
j1--;
}
}
catch { };

Okolo(matrica, mine, i1, j1);


j1 = j;
try
{
while (mine[i1 + 1, j1] == 0 && i1 + 1 <
25)
{

matrica[i1 + 1, j1].Text = mine[i1 + 1,


j1].ToString();
i1++;
}
}
catch { }
Okolo(matrica, mine, i1, j1);
i1 = i;
try {
while (mine[i1 - 1, j1] == 0 && j1 - 1 > -
1)

{
matrica[i1 - 1, j1].Text = mine[i1 - 1,
j1].ToString();
i1--;
}

}
catch { }
Okolo(matrica, mine, i1, j1);
i1 = i;
j1 = j;
try {
while (mine[i1 + 1, j1 + 1] == 0 && j1 + 1
< 25 && i1 + 1 < 25)

{
matrica[i1 + 1, j1 + 1].Text = mine[i1
+ 1, j1 + 1].ToString();
j1++;
i1++;
}

}
catch { }
Okolo(matrica, mine, i1, j1);
i1 = i;
j1 = j;
try {
while (mine[i1 - 1, j1 - 1] == 0 && j1 - 1
> -1 && j1 - 1 > -1)

{
matrica[i1 - 1, j1 - 1].Text = mine[i1
- 1, j1 - 1].ToString();
j1--;
i1--;
}

}
catch { }
Okolo(matrica, mine, i1, j1);
i1 = i;
j1 = j;
try {
while (mine[i1 - 1, j1 + 1] == 0 && j1 - 1
> -1 && j1 + 1 < 25)
{

matrica[i1 - 1, j1 + 1].Text = mine[i1


- 1, j1 + 1].ToString();
j1++;
i1--;

}
}
catch { }
Okolo(matrica, mine, i1, j1);
i1 = i;
j1 = j;
try
{ while (mine[i1 + 1, j1 - 1] == 0 && i1 + 1 <
25 && j1 - 1 > -1)
{

matrica[i1 + 1, j1 - 1].Text = mine[i1


+ 1, j1 - 1].ToString();
j1--;
i1++;
}

}
catch { }
Okolo(matrica, mine, i1, j1);
i1 = i;
j1 = j;
}
}
}

}
}
}
else
{
int zb = 0;
if (clickedButton.BackColor == Color.Yellow)
{
clickedButton.BackColor = Color.White;
}
else
{
clickedButton.BackColor = Color.Yellow;
}
for(int i = 0; i < 25; i++)
{
for(int j =0; j<25; j++)
{
if(matrica[i,j].BackColor == Color.Yellow)
{
zb++;
}
}
}
label1.Text = "oznacena polja: "+ zb.ToString();

}
}

private void button626_Click(object sender, EventArgs e)


{
this.Close();

// Restart the application


System.Diagnostics.Process.Start(Application.ExecutablePath);
}

private void button627_Click(object sender, EventArgs e)


{
if (zas)
{
zas = false;
}
else
{
zas = true;
}
}
}
}

You might also like