扫雷游戏 思路 与 源代码 (一)

格子类 这个格子是否有地雷?格子周边的地雷数?格子的状态 格子初始状态为关闭 (用枚举类型进行三种选项列举 恢复关闭的状态、标记、打开)

雷区类
构造化:
构造雷区的属性 长宽 地雷数 一行的格子数 所有的格子数字的引用
构造方法的属性方法:将长宽一行格子数 地雷数构造出来,将所有格子的方法进行构造调用
初始化雷区的方法 用行的格子数*行的格子数 在对内存创建 用循环进行将每个格子new 出来
将所有格子进行排列的方法调用
创建一个格子排开
先求出单个格子的长宽
在定义k 与左边和上边都为0
外层循环 为行 里层循环为列
一个格子的长宽、左右负值
每次的格子加上上一个的高度(里层循环)
(外层循环)
中间的加上一列上的高度

选项:

先定义个雷区的mf 属性
再封装一个方法将每个格子的属性封装刷新
在初级中级高级中风别构造参数
并且进行调用

具体代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; //格子的状态与属性

namespace WindowsFormsApplication1
{

public class Cell:Button
{
//是否存在地雷
private bool hasMine;

public bool HasMine
{
get { return hasMine; }
set { hasMine = value; }
}
//格子四周地雷数
private int aroundMineCount;

public int AroundMineCount
{
get { return aroundMineCount; }
set { aroundMineCount = value; }
}

private Cellstate state;

public Cellstate State
{ //格子的状态
get { return state; }
set { state = value; }
}


public enum Cellstate
{
//枚举类型 四种状态 翻开,关闭,标记、还原
Open,
Closed,
Mark,
Reset


}
public Cell()
{
this.state = Cellstate.Closed; //初始化翻开的时候是关闭的

}

public void Open() //打开时的状态
{


}

public void Mark()
{
//标记

}

public void Reset()
{

//重置恢复到关闭的状态
}

}
}

雷区的代码:

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

namespace WindowsFormsApplication1
{
public class MineField
{


private int width;

public int Width //宽度
{
get { return width; }
set { width = value; }
}

private int height;

public int Height //高度
{
get { return height; }
set { height = value; }
}

private Cell[] cells; //根据用户的选择来初始化多个少个格子

public Cell[] Cells
{
get { return cells; }
set { cells = value; }
}

private int minecout;

public int Minecout //地雷数
{
get { return minecout; }
set { minecout = value; }
}

private int linecout;

public int Linecout //一行的格子数
{
get { return linecout; }
set { linecout = value; }
}


public MineField() { }
public MineField(int width, int height, int linecout, int minecout) //构造雷区的方法
{

this.width = width;
this.height = height;
this.linecout = linecout;
this.minecout = minecout;
this.Init();
}
//初始化雷区的方法
public void Init()
{

cells = new Cell[linecout * linecout]; //按照每边的数量创建所有的格子
for (int i = 0; i < linecout * linecout; i++)
{

cells[i] = new Cell();


}

this.Layyout();
}
public void Layyout()
{

int cellWidth = this.width / Linecout;
int cellHeihth = this.height / Linecout;
int k = 0;
int left, top;
left = 0;
top = 0;
for (int i = 0; i < linecout; i++)
{
left = 0;
for (int j = 0; j < linecout; j++)
{
cells[k].Width = cellWidth;
cells[k].Height = cellHeihth;
cells[k].Left = left;
cells[k].Top = top;
left += cellWidth;
k++;

}
top += cellHeihth;
}



}
}
}
//中级高级初级的选项
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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{

}
private MineField mf;

private void 开始游戏ToolStripMenuItem_Click(object sender, EventArgs e)
{


}

private void 初级ToolStripMenuItem_Click(object sender, EventArgs e)
{
mf = new MineField(this.panel1.Width, this.panel1.Height, 9, 10);
this.ShowCells();
}



public void ShowCells()
{
this.panel1.Controls.Clear();
foreach (Cell cell in mf.Cells)
{

this.panel1.Controls.Add(cell);


}


}

private void 中级ToolStripMenuItem_Click(object sender, EventArgs e)
{
mf = new MineField(this.panel1.Width, this.panel1.Height, 15, 40);
this.ShowCells();
}

private void 高级ToolStripMenuItem_Click(object sender, EventArgs e)
{
mf = new MineField(this.panel1.Width, this.panel1.Height, 20, 80);
this.ShowCells();
}

private void panel1_Resize(object sender, EventArgs e)
{

mf.Width = this.panel1.Width;
mf.Height = this.panel1.Height;
mf.Layyout();
}

private void Form1_Resize(object sender, EventArgs e)
{

}
}
}

原文地址:https://www.cnblogs.com/liyiyong/p/5110699.html