C_扫雷游戏程序源代码
using System;
using System.Drawing;
public struct Miner
{
public bool[,] buriedMine;
public int[,] roundMineNum;
public char[,] status;
public int totalMine;
public static char Ô,Ê? = 'u', ?ÈÀ× = 'e', ÓÐÀ× = 'F', ÎÞÀ× = '_',
??ÒÉ = '?';
private static Size[] neighbouArray = { new Size (-1,-1), new Size
(-1,0), new Size (-1,1), new Size (0,-1), new Size (0,1), new Size
(1,-1), new Size (1,0), new Size (1,1)};
public void FindMine(int i, int j)
{
if (status[i, j] == Ô,Ê?)
{
status[i, j] = ÓÐÀ×;
}
else if (status[i, j] == ÓÐÀ×)
{
status[i, j] = Ô,Ê?;
}
}
public void MaybeMine(int i, int j)
{
if (status[i, j] == Ô,Ê?)
{
status[i, j] = ??ÒÉ;
}
else if (status[i, j] == ??ÒÉ)
{
status[i, j] = Ô,Ê?;
}
}
public bool AutoOpen(int i, int j)
{
int sum = 0;
if (status[i, j] == ÎÞÀ×) return true;
Point[] eight = EightNeighbours(new Point(i, j), neighbouArray,
buriedMine.GetLength(0), buriedMine.GetLength(1));
foreach (Point any in eight)
{
if (any == new Point(-10, -100))
{
continue;
}
if (status[any.X, any.Y] == ÓÐÀ×) sum++;
}
if (roundMineNum[i, j] == sum)
{
Point[] otherEight = EightNeighbours(new Point(i, j),
neighbouArray, buriedMine.GetLength(0), buriedMine.GetLength(1));
foreach (Point any in otherEight)
{
if (any == new Point(-10, -100))
{
continue;
}
if (status[any.X, any.Y] == Ô,Ê?)
{
if (OpenMine(any.X, any.Y) == false)
return false;
}
}
}
return true;
}
public bool OpenMine(int i, int j)
{
if (status[i, j] == ÎÞÀ×)
{
return true; // ÎÞÀ×Ê??µ?Ø0
}
if (buriedMine[i, j])
{ // ÅöÉϵØÀ×ÁË,?ÍÒý??À×ÇøÈ???µØÀ×
for (int ii = 0; ii < buriedMine.GetLength(0); ii++)
{
for (int jj = 0; jj < buriedMine.GetLength(1); jj++)
{
if (buriedMine[ii, jj] && status[ii, jj] == Ô,Ê?)
{
status[ii, jj] = ?ÈÀ×;
}
}
}
return false;
}
else
{
status[i, j] = (char)(roundMineNum[i, j] + '0');
//~ status[i, j] = ÎÞÀ×;
if (roundMineNum[i, j] == 0)
{
// ÖÜÎ?8?ö?ñ×Ó??ÎÞÀ×Ê?µÄ×Ô??µÝ?é??Àí
status[i, j] = ÎÞÀ×;
Point[] eight = EightNeighbours(new Point(i, j),
neighbouArray, buriedMine.GetLength(0), buriedMine.GetLength(1));
foreach (Point any in eight)
{
if (any == new Point(-10, -100))
{
continue;
}
OpenMine(any.X, any.Y);
//~ roundMineNum[any.X, any.Y]++;
}
}
//~ status[i, j] = (char)(roundMineNum [i, j]+'0');
return true;
}
}
public static Point[] EightNeighbours
(Point p, Size[] neighbouArray, int m, int n)
{
Point[] eights = new Point[8];
for (int i = 0; i < eights.Length; i++)
{
eights[i] = p + neighbouArray[i];
}
for (int i = 0; i < eights.Length; i++)
{
if (eights[i].X < 0 || eights[i].X >= m
|| eights[i].Y < 0 || eights[i].Y >= n)
{
//~ Console.WriteLine (roundMineNum.GetLength(0));
//~ Console.WriteLine (roundMineNum.GetLength(1));
eights[i].X = -10;
eights[i].Y = -100;
}
//~ Console.WriteLine (eights[i] );
}
return eights;
}
public Miner(int m, int n, int totalMine)
{
while (n * n < totalMine)
{
Console.WriteLine
("µØÀ×ÊýÁ?Ì??à,ÎÞ??ÔÚÀ×ÇøÈ???ÂñÉè,?õ?ëºóÔÙÂñÀ×.");
totalMine = totalMine / 2;
}
this.buriedMine = new bool[m, n];
this.roundMineNum = new int[m, n];
this.status = new char[m, n];
this.totalMine = totalMine;
Random rm = new Random();
int i;
int j;
for (int k = 0; k < totalMine; k++)
{
do
{
i = rm.Next() % m;
j = rm.Next() % n;
} while (buriedMine[i, j]);
buriedMine[i, j] = true;
Point[] eight = EightNeighbours(new Point(i, j),
neighbouArray, m, n);
foreach (Point any in eight)
{
if (any == new Point(-10, -100))
{
continue;
}
//~ if (buriedMine[any.X, any.Y] == true) {
roundMineNum[any.X, any.Y]++;
}
}
for (int ii = 0; ii < m; ii++)
{
for (int jj = 0; jj < n; jj++)
{
status[ii, jj] = Ô,Ê?;
}
}
}
public static void HandleCommand(Miner m, string[] command, ref
bool flag)
{
int i = int.Parse(command[1]);
int j = int.Parse(command[2]);
if (command[0] == "o" || command[0] == "O")
{
flag = m.OpenMine(i, j);
PrintStatus(m);
}
else if (command[0] == "a" || command[0] == "A")
{
flag = m.AutoOpen(i, j);
PrintStatus(m);
}
else if (command[0] == "f" || command[0] == "F")
{
m.FindMine(i, j);
PrintStatus(m);
}
else if (command[0] == "?")
{
m.MaybeMine(i, j);
PrintStatus(m);
}
else if (command[0] == "x" || command[0] == "x")
{
flag = false;
}
else
{
Console.WriteLine("ÊäÈëÃüÁî???ûºÏÒªÇó,ÇëÖØÐÂÊäÈë");
}
}
public static void PrintStatus(Miner m)
{
int foundMiner = 0;
foreach (char any in m.status)
{
if (any == 'f' || any == 'F')
{
foundMiner++;
}
}
Console.WriteLine("*************************");
Console.WriteLine("\t??ÓÐ{0}?öµØÀ×Ã?ÓÐ?ê?ö!", m.totalMine -
foundMiner);
Console.Write(' ' + "\t");
for (int j = 0; j < m.status.GetLength(1); j++)
{
Console.Write(j + " ");
}
Console.WriteLine();
Console.WriteLine();
for (int i = 0; i < m.status.GetLength(0); i++)
{
Console.Write(i + "\t");
for (int j = 0; j < m.status.GetLength(1); j++)
{
Console.Write(m.status[i, j] + " ");
}
Console.WriteLine();
}
Console.WriteLine("*************************");
}
public static void Main(string[] args)
{
Miner m = new Miner(8, 10, 5);
bool flag = true;
do
{
//?ÓÊÜ?üÅÌÊäÈëÃüÁî;
Console.Write("ÇëÊäÈëÉ?À×ÃüÁî???ñÊ?Ϊ?º\nÃüÁî??µÚ??ÐÐ??µÚ??ÁÐ\n");
string s = Console.ReadLine();
string[] command = s.Split(new Char[]{ ','});
//??Àí?üÅÌÊäÈëÃüÁî;
HandleCommand(m, command, ref flag);
} while (flag);
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1 {
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}