Advertisement

C#控制台版扫雷游戏

阅读量:

使用System库;
使用System.Collections.Generic库;
使用System.Linq功能集;
使用System.Text API;

在此处放置你的文本

//设置地雷标记阵列
int[,] a = new int[n, n]; //通过二维整型数组a记录显示区域b中各坐标的地雷分布情况
Random r = new Random(); //引入并配置随机数发生器r
Program c = new Program(); //创建并返回实例c用于执行特定功能

do
随机生成x1和y1坐标
如果当前格子不是雷区,则将该位置设置为地雷(用数字10表示)
然后调用BJ函数处理周围的所有格子
减少雷区数量q
当雷区布置完毕时(即q等于零),停止循环

}

do//上下左右移动光标
{

if (Console.KeyAvailable)
{
#region 接受键盘,和上下左右移动
ConsoleKeyInfo cki = Console.ReadKey(true);
ConsoleKey ck = cki.Key;

当输入键值等于(ConsoleKey)37且x不为零时//左边
{
执行x减一操作
}
当输入键值等于(ConsoleKey)38且y不为零时//上方
{
执行y减一操作
}
当输入键值等于(ConsoleKey)39且x不等于n减一时//右边
{
执行x加一操作
}

if (ck == (ConsoleKey)40 && y != n - 1)//下
{
y++;
}

#endregion

if (ck equals the key for carriage return)
{
if the cell at position (y,x) is zero, indicating no mine:
{
call the QK function to clear the surrounding cells:
Console.Clear();
then display the grid row by row:
for each row i from 0 to n-1:
for each column j from 0 to n-1:
write the value of b[i,j];
if j is the last column, append a boundary character;
after printing a row, add a newline;
add boundary markers below each row:
for i2 from 0 to n:
write a boundary character;
} else if the cell at position (y,x) is ten, indicating a mine:
{
clear the console output and mark the mine with " *";
b[y,x] = " *";
then display all cells in the grid:
for each row i from 0 to n-1:
for each column j from 0 to n-1:
if b[i,j] is marked as " *" replace it with ■;
if a[i,j] is ten replace it with " *";
write b[i,j];
add a boundary character if j is last column;
after printing rows, add boundary markers below:
for i2 from 0 to n:
write a boundary character;
}

Console.WriteLine("\n踩到地雷,GameOver.....再接再厉哦");
Console.ReadKey();
break;
}
else if (a[y, x] >= 0 && a[y, x] <= 9)//如果是雷的周围数字,就将数字显示出来
{
Console.Clear();
b[y, x] = " " + a[y, x].ToString();
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
Console.Write(b[i, j]);
if (j == n - 1)//标记右边边界
{
Console.Write(bianjie);
}
}
Console.WriteLine();
}
for (int i2 = 0; i2 < n + 1; i2++)//标记下面边界
{
Console.Write(bianjie);
}
}

}
if (ck == (ConsoleKey)81)//这是标记雷区的按键
{
Console.Clear();
if (b[y, x] == "⊙")
{
b[y, x] = "■";
}
else if (b[y, x] == "■")
{
b[y, x] = "⊙";
}
int sum = 0;
int sumx = 0;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
//sum统计雷的位置和标记的对应的个数
if (b[i, j] == "⊙" && a[i, j] == 10)
{
sum++;
}
//sumx统计标记了多少个
if (b[i, j] == "⊙")
{
sumx++;
}
Console.Write(b[i, j]);
if (j == n - 1)//标记右边边界
{
Console.Write(bianjie);
}
}
Console.WriteLine();
}
for (int i2 = 0; i2 < n + 1; i2++)//标记下面边界
{
Console.Write(bianjie);
}
//sum=sumx=雷 的个数的时候,就胜利
if (sum == q1 && sumx == q1)
{
Console.WriteLine();
Console.WriteLine("恭喜您游戏胜利哦!!");

Console.ReadKey();
break;
}
}

}
Console.SetCursorPosition(2 * x, y);
} while (true);

public void Qk(int[,] a, int x, int y, string[,] b)//当被选中的单元格值为零时,清空
{
将b[x,y]设置为空字符串;
如果y大于等于1
{
如果a[x,y-1]等于0
{
将a[x,y-1]标记为-1;//防止递归过程中出现死循环
Qk(a, x, y-1, b);
将b[x,y-1]设置为空字符串;
}
}
}

}
else
{
if (b[x, y - 1] != " ")
{
b[x, y - 1] = " " + a[x, y - 1].ToString();
}
}
}
if (x - 1 >= 0)//左
{
if (a[x - 1, y] == 0)
{
a[x - 1, y] = -1;
Qk(a, x - 1, y, b);
b[x - 1, y] = " ";
}
else
{
if (b[x - 1, y] != " ")
{
b[x - 1, y] = " " + a[x - 1, y].ToString();
}
}
}
if (x + 1 <= a.GetUpperBound(0))//右
{
if (a[x + 1, y] == 0)
{

a[x + 1, y] = -1;
Qk(a, x + 1, y, b);
b[x + 1, y] = " ";

}
else
{
if (b[x + 1, y] != " ")
{
b[x + 1, y] = " " + a[x + 1, y].ToString();
}
}

}
if (y + 1 <= a.GetUpperBound(1))//下
{
if (a[x, y + 1] == 0)
{
a[x, y + 1] = -1;
Qk(a, x, y + 1, b);
b[x, y + 1] = " ";

}
else
{
if (b[x, y + 1] != " ")
{
b[x, y + 1] = " " + a[x, y + 1].ToString();
}
}

}

}

public void Bj(int[,] a, int x, int y) //对雷区周围的数字进行加1
{
if (x - 1 >= 0 && y - 1 >= 0)
{
a[x - 1, y - 1] = (a[x - 1, y - 1] == 10) ? 10 : ++a[x - 1, y - 1];//左上角
}
if (y - 1 >= 0)
{
a[x, y - 1] = (a[x, y - 1] == 10) ? 10 : ++a[x, y - 1];//上
}
if (x + 1 <= a.GetUpperBound(0) && y + 1 <= a.GetUpperBound(1))
{
a[x + 1, y + 1] = (a[x + 1, y + 1] == 10) ? 10 : ++a[x + 1, y + 1];//右上角
}
if (x - 1 >= 0)
{
a[x - 1, y] = (a[x - 1, y] == 10) ? 10 : ++a[x - 1, y];//左
}
if (x + 1 <= a.GetUpperBound(0))
{
a[x + 1, y] = (a[x + 1, y] == 10) ? 10 : ++a[x + 1, y];//右
}
if (x - 1 >= 0 && y + 1 <= a.GetUpperBound(1))
{
a[x - 1, y + 1] = (a[x - 1, y + 1] == 10) ? 10 : ++a[x - 1, y + 1];//左下角
}
if (y + 1 <= a.GetUpperBound(1))
{
a[x, y + 1] = (a[x, y + 1] == 10) ? 10 : ++a[x, y + 1];//下
}
if (x + 1 <= a.GetUpperBound(0) && y - 1 >= 0)
{
a[x + 1, y - 1] = (a[x + 1, y - 1] == 10) ? 10 : ++a[x + 1, y - 1];//右下角
}

}

}
}

全部评论 (0)

还没有任何评论哟~