c#

位置:IT落伍者 >> c# >> 浏览文章

趣味编程:自创C#扫雷代码


发布日期:2019年06月01日
 
趣味编程:自创C#扫雷代码
虽然不很好但是还可以玩绝对是原版代码

using System;

using SystemDrawing;

using SystemWindowsForms;

namespace WindowsFormsApplication

{

public partial class Frm : Form

{

#region 变量

public int qishu;

public static int ls= hs=;

public int lgs = ; //列数行数

public int bl=; //是否重新布雷

public int time ;

public int[] bulei = new int[ls hs]; //bulei数组的值代表无雷代表雷的个数代表雷

public int ceshi = ;

public int[] click = new int[ls hs]; //表示要清除既是单击的 表示右击一次表示右击两次 表示以清除过了

public int sum = ;

public int timerEnable = ; //游戏所处的状态 是游戏中

#endregion

public Frm()

{

InitializeComponent()

}

///

/// 标识雷的个数 和可插的旗数

///

public void Qishu()

{

if (qishu <= lgs)

{

gamesourceText = + (lgs qishu)ToString()

}

else

{

gamesourceText = + (lgs qishu)ToString()

}

}

///

/// 画方格

///

/// <实例对象>

/// <要画图的位置>

/// <要画图的位置>

/// <画图使用的颜色>

public void Dline(Graphics dlineint mxint myPen mypen)

{

dlineDrawLine(mypen mx my mx + my

dlineDrawLine(mypen mx my mx my +

dlineDrawLine(new Pen(ColorBlack) mx + my mx + my +

dlineDrawLine(new Pen(ColorBlack) mx my + mx + my +

}

///

/// 看是否可以向周围移动

///

/// <移动的基本X坐标>

/// <移动的基本Y坐标>

public void Panjie(int xint y)

{

Pl(x y)

if (y >=

{

Pl(xy

if (x >=

{

Pl(xy

}

if (x +

{

Pl(x + y

}

}

if (y + < hs)

{

Pl(xy+

if (x >=

{

Pl(x y +

}

if (x + < ls)

{

Pl(x + y +

}

}

if (x >=

{

Pl(x y)

}

if (x + < ls)

{

Pl(x + y)

}

}

///

/// 判断是否要清清除

///

/// <要判断的基本X坐标>

/// <要判断的基本Y坐标>

public void Pl(int x int y)

{

if (bulei[x y] == && click[x y] != && click[x y] != && click[x y] !=

{

click[x y] = ;

Panjie(xy)

}

if (bulei[x y] > && bulei[x y] < && click[x y] != && click[x y] != && click[x y] !=

{

click[x y] = ;

}

}

///

/// 初始化数组布雷判断周围的雷数

///

public void Blpl()

{

sum = ls * hs;

qishu = ;

int mxmy;

//初始化数组 表示雷数

int mxmy;

int sum=;

for (mx = ; mx < ls; mx++)

{

for (my = ; my

{

bulei[mx my] = ;

click[mx my] = ;

}

}

while(sum

//决定雷的个数和雷德位置

{

sum= ;

Random r = new Random((int)DateTimeNowTicks)

mx = rNext(ls

my = rNext(hs

bulei[mxmy] = ;

for (mx = ; mx < ls; mx++)

{

for (my = ; my < hs; my++)

{

sum += bulei[mx my];

}

}

}

//判断周围雷的个数

for (mx = ; mx < ls; mx++)

{

for (my = ; my < hs; my++)

{

if (bulei[mxmy] !=

{

if (mx >=

{

if (bulei[mx my] ==

{

bulei[mx my]++;

}

if (my >= && bulei[mx my ] ==

{

bulei[mx my]++;

}

if ( my + < hs && bulei[mx my + ] ==

{

bulei[mx my]++;

}

}

if (mx +

{

if (bulei[mx + my] ==

{

bulei[mx my]++;

}

if (my >= && bulei[mx+ my ] ==

{

bulei[mx my]++;

}

if (my + < hs && bulei[mx + my + ] ==

{

bulei[mx my]++;

}

}

if(my>= && bulei[mxmy]==

{

bulei[mx my]++;

}

if (my +

{

bulei[mx my]++;

}

}

}

}

}

///

/// 右击的重绘

///

/// >

public void Rdraw(Graphics dline)

{

string text;

int mxmy;

int mx my;

for (mx = ;mx < ls; mx++)

{

for (my = ; my < hs; my++)

{

mx = mx * + mx * + ;

my = my * + my * + ;

if ( click[mx my] ==

{

//第一次右击画旗

dlineDrawLine(new Pen(ColorWhite) mx my mx + my +

dlineDrawLine(new Pen(ColorWhite) mx my mx my +

dlineDrawLine(new Pen(ColorBlack) mx + my mx + my +

dlineDrawLine(new Pen(ColorBlack) mx my + mx + my +

dlineFillRectangle(new SolidBrush(ColorFromArgb( )) mx + my +

text = P;

dlineDrawString(text new Font(宋体 new SolidBrush(ColorRed) mx + my +

}

if ( click[mx my] ==

{

//第二次右击画问号

dlineDrawLine(new Pen(ColorWhite) mx my mx + my +

dlineDrawLine(new Pen(ColorWhite) mx my mx my +

dlineDrawLine(new Pen(ColorBlack) mx + my mx + my +

dlineDrawLine(new Pen(ColorBlack) mx my + mx + my +

dlineFillRectangle(new SolidBrush(ColorFromArgb( )) mx + my +

text = ?;

dlineDrawString(text new Font(宋体 new SolidBrush(ColorBlack) mx + my +

}

if (click[mx my] ==

{

//第三次右击 恢复原样

click[mx my] = ;

dlineDrawLine(new Pen(ColorWhite) mx my mx + my +

dlineDrawLine(new Pen(ColorWhite) mx my mx my +

dlineDrawLine(new Pen(ColorBlack) mx + my mx + my +

dlineDrawLine(new Pen(ColorBlack) mx my + mx + my +

dlineFillRectangle(new SolidBrush(ColorFromArgb( )) mx + my +

}

}

}

}

///

/// 初始化屏幕 (包还所有的)

///

public void Startscr()

{

Graphics dline = labdrawCreateGraphics()

time = ;

Blpl()

timerEnabled = false;

Color c = ColorFromArgb(

labdrawCreateGraphics()Clear(c)

Drawscreen(dline)

timerEnable = ;

labtimeText = (

}

///

/// 画界面

///

/// >

public void Drawscreen(Graphics dline)

{

sum = ; //得到没清楚的格子数

int mx my;

int mx = my = ; //得到要画图的坐标

string text;

for (mx = ; mx < ls; mx++)

{

for (my = ; my < hs; my++)

{

//画图地位置 mxmy分别是屏幕xy轴

mx = mx * + mx * + ;

my = my * + my * + ;

if (click[mx my] != && click[mx my] != && click[mx my] != && click[mx my] !=

{

//下面用于画立体形矩形

sum++;

Pen mypen = new Pen(ColorGray)

Dline(dline mx my mypen)

dlineFillRectangle(new SolidBrush(ColorFromArgb( )) mx + my +

}

if ((click[mx my] == || click[mx my] == ))

{

click[mx my] = ; //清除的格子

//提示周围的雷数

if (bulei[mx my] <= && bulei[mx my] >

{

text = bulei[mx my]ToString()

dlineDrawString(text new Font(宋体 new SolidBrush(ColorBlack) mx + my +

}

//显示雷

if ((bulei[mx my] == || bulei[mx my] == ))

{

text = *;

dlineDrawString(text new Font(宋体 new SolidBrush(ColorBlack) mx + my +

if (bulei[mx my] ==

{

//被单击的雷上画上叉号

dlineDrawLine(new Pen(ColorRed) mx my mx + my +

dlineDrawLine(new Pen(ColorRed) mx my + mx + my

}

}

}

}

//用于画旗 和问号

Rdraw(dline)

}

}

///

/// 当为雷时的算法

///

/// >

/// >

public void Panlei(int x int y)

{

if (bulei[x y] !=

{

if (bulei[x y] ==

{

timerEnabled = false; //使计时器停止

timerEnable = ; //使游戏出游结束状态

bulei[x y] = ;

int i j;

for (i = ; i < ls; i++)

{

for (j = ; j < hs; j++)

{

if (bulei[i j] == || bulei[i j] ==

{

click[i j] = ; //显示所有的雷

}

}

}

}

if (bulei[x y] > && bulei[x y] <

{

click[x y] = ;

}

}

else

{

Panjie(x y)

}

}

///

/// 回到游戏开始的界面

///

///

///

private void btnreplay_Click(object sender EventArgs e)

{

//初始化一切 包括布雷 界面 click数组

Startscr()

}

///

/// 计算游戏所用的时间

///

///

///

private void timer_Tick(object sender EventArgs e)

{

time++;

if (time <=

{

if (time <

{

labtimeText = ( + timeToString())

}

else

{

labtimeText = ( + timeToString())

}

}

else

{

if (time <

{

labtimeText = (timeToString())

}

else

{

timerEnabled = false;

MessageBoxShow(RIME OVER

time = ;

timerEnable = ;

}

}

}

///

/// 在启动的游戏时初始化界面

///

///

///

private void labdraw_Paint(object sender PaintEventArgs e)

{

Graphics dline = eGraphics;

if (bl ==

{

Blpl()

Drawscreen(dline)

}

else

{

Drawscreen(dline)

}

}

///

/// //单击时触发的事件

///

///

///

private void labdraw_MouseClick(object sender MouseEventArgs e)

{

bl = ;

int i= j=;

if (timerEnable == ) //判断游戏是否处于结束状态

{

timerEnabled = true;

Graphics dline = labdrawCreateGraphics() //将LAB建立成图形对象

int mx = my = ;

int mx = my = ;

int mx = my = ;

//判断鼠标所点位置

mx = eX;

my = eY;

for (i = ; i < ls; i++)

{

for (j = ; j < hs; j++)

{

mx = + i * + i * ;

my = + j * + j * ;

//找到单击位置代表的数组

if (mx < mx && mx + + > mx && my < my && my < my + +

{

mx = i;

my = j;

break;

}

}

if (j <

{

break;

}

}

//判断是左击还是右击

if (eButton == MouseButtonsRight && click[mx my] != && mx >= && my >= && click[mx my] !=

{

//第次右击

if (click[mx my] == && click[mx my] !=

{

click[mx my] = ;

qishu;

}

else

{

//第次右击

if (click[mx my] != && click[mx my] !=

{

if (qishu

{

click[mx my] = ;

qishu++;

}

}

if (click[mx my] == && click[mx my] !=

{

click[mx my] = ;

}

}

//右击的绘图事件

Rdraw(dline)

Qishu()

}

else

{

//左击

if (click[mx my] != && click[mx my] !=

{

click[mx my] = ;

Color c = ColorFromArgb(

//清屏 用背景颜色

labdrawCreateGraphics()Clear(c)

Panlei (mx my

Drawscreen(dline)

}

if (bulei[mx my] ==

{

MessageBoxShow(GAME OVER

timerEnabled = false;

timerEnable = ;

}

}

if (sum == lgs)

{

MessageBoxShow(GAME WIN

timerEnabled = false;

timerEnable = ;

}

}

}

private void Frm_Load(object sender EventArgs e)

{

Qishu() //输出雷的个数

// thisHeight = hs * + ;

// thisWidth = ls * + ;

}

}

}

               

上一篇:C#线程从陌生到熟悉:突飞猛进

下一篇:C#实现在Excel文档中搜索文本