0% found this document useful (0 votes)
27 views

Source Code

This document contains code for a minesweeper game implemented using C# and Windows Forms. It includes classes for the start form, game board form, mine grid, and shared data. The start form initializes the game settings and passes control to the game board form. The mine grid class handles random mine placement and counting adjacent mines. Shared data stores game state like the grid, flag count, and revealed tiles.

Uploaded by

lloyddagoc
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Source Code

This document contains code for a minesweeper game implemented using C# and Windows Forms. It includes classes for the start form, game board form, mine grid, and shared data. The start form initializes the game settings and passes control to the game board form. The mine grid class handles random mine placement and counting adjacent mines. Shared data stores game state like the grid, flag count, and revealed tiles.

Uploaded by

lloyddagoc
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Start Form

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 Minesweeper_WindowsForms
{

public partial class startForm : Form


{
public startForm()
{
InitializeComponent();

private void Form1_Load(object sender, EventArgs e)


{

private void playButton_Click(object sender, EventArgs e)


{
char level = 'e'; // this is the default level which is the easy mode
if (hardRB.Checked == true) level = 'h'; // if hard mode is clicked
else if (mediumRB.Checked == true) level = 'm'; // if medium mode is clicked

SharedData.mGrid = new MinesweeperGrid(level); // depending level which is clicked


SharedData.startFlag = false; // the first click hasn't happened yet
SharedData.numberOfRevealedTiles = 0; // number of tiles per level
SharedData.numberOfRemainingFlags = SharedData.mGrid.NumberOfMines; // in how many flags
is remaining after marking the tiles

Form window = new GameBoard();


window.Owner = this;
window.Show();
this.Hide();
}

private void label1_Click(object sender, EventArgs e)


{

private void easyRB_CheckedChanged(object sender, EventArgs e)


{

private void label2_Click(object sender, EventArgs e)


{

private void mediumRB_CheckedChanged(object sender, EventArgs e)


{

}
}
}

GameBoard Form
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 Minesweeper_WindowsForms
{

public partial class startForm : Form


{
public startForm()
{
InitializeComponent();

private void Form1_Load(object sender, EventArgs e)


{

}
private void playButton_Click(object sender, EventArgs e)
{
char level = 'e'; // this is the default level which is the easy mode
if (hardRB.Checked == true) level = 'h'; // if hard mode is clicked
else if (mediumRB.Checked == true) level = 'm'; // if medium mode is clicked

SharedData.mGrid = new MinesweeperGrid(level); // depending level which is clicked


SharedData.startFlag = false; // the first click hasn't happened yet
SharedData.numberOfRevealedTiles = 0; // number of tiles per level
SharedData.numberOfRemainingFlags = SharedData.mGrid.NumberOfMines; // in how many flags
is remaining after marking the tiles

Form window = new GameBoard();


window.Owner = this;
window.Show();
this.Hide();
}

private void label1_Click(object sender, EventArgs e)


{

private void easyRB_CheckedChanged(object sender, EventArgs e)


{

private void label2_Click(object sender, EventArgs e)


{

private void mediumRB_CheckedChanged(object sender, EventArgs e)


{

}
}
}

Minegrid.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Minesweeper_WindowsForms
{
class MinesweeperGrid
{
int numberOfMines;
int[,] grid;
Tuple<int, int>[] locationsOfMines;

#region constructors

public MinesweeperGrid()
{
// the default values
this.numberOfMines = 10;
grid = new int[9, 9];
locationsOfMines = new Tuple<int, int>[10];
}

/// <summary>
///
/// </summary>
/// <param name="level"> 'e' for easy, 'm' for medium and 'h' for hard </param>
public MinesweeperGrid(char level )
{
if (level == 'h' || level == 'H')
{
this.numberOfMines = 99;
this.grid = new int[16, 30];
locationsOfMines = new Tuple<int, int>[99];
}
else if (level == 'm' || level == 'M')
{
this.numberOfMines = 40;
this.grid = new int[16, 16];
locationsOfMines = new Tuple<int, int>[40];
}
else
{
this.numberOfMines = 10;
this.grid = new int[9, 9];
locationsOfMines = new Tuple<int, int>[10];
}
}

#endregion

#region Properties

public int NumberOfMines


{
get { return numberOfMines; }

}
public int[,] Grid
{
get { return grid ; }

public Tuple<int, int>[] LocationsOfMines


{
get { return locationsOfMines; }
}

#endregion

/// <summary>
/// this method distributes the mines randomly over the grid with -1 value
/// and making sure the grid[startX, startY] is not one of them.
/// It also calcuates the number of adjacent mines for every
/// tile in the grid to set its value to.
/// </summary>
/// <param name="startX">the row index of first tile to be clicked </param>
/// <param name="startY">the coulmn index of first tile to be clicked </param>
/// <returns>the return is 2D matrix with values of th grid</returns>
public int [,] distributeMines(int startX, int startY)
{
int mines = numberOfMines;
Random rand = new Random();
int r = grid.GetLength(0);
int c = grid.GetLength(1);
int i, j;

///distributing Mines randomly///


while (mines>0)
{
i = rand.Next(0,r); // random row
j = rand.Next(0,c); //random coulmn

// check if it is not a mine already


if ( !(startX ==i && startY ==j) && grid[i,j] !=-1)
{
grid[i, j] = -1; // this tile represents a mine now
mines--;
locationsOfMines[mines] = new Tuple<int, int>(i, j);
}

/// counting the adjacent mines ///


int sum;
for (i=0; i<r; i++)
{
for(j=0; j<c; j++)
{
//if it is a mine skip
if (grid[i, j] == -1)
continue;

sum = 0;
// making a 3x3 window to calculate
for(int a= -1; a < 2; a++)
for (int b = -1; b < 2; b++)
{
if (i + a >= 0 && i + a < r //to check boundaries
&& j + b >= 0 && j + b < c
&& grid[i + a, j + b] == -1)

sum++;

grid[i, j] = sum;

}
return grid;
}

}
}

Shareddata.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Minesweeper_WindowsForms
{
class SharedData
{
public static MinesweeperGrid mGrid;
public static bool startFlag;
public static int numberOfRevealedTiles;
public static int numberOfRemainingFlags;
}
}

Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Minesweeper_WindowsForms
{
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new startForm());
}
}
}

You might also like