C# Mines(布雷) 代码
本文给出一个[b] [size=2][color=#000000]C#[/color][/size] Mines(布雷)的 代码[/b],新手研究一下吧。 [table=95%][tr][td][b]以下是引用片段:[/b]using System.Collections; o9vG+eObw J-M
using System.IO;
using System; 7hB {/VR.L
namespace com.Mines /_e)P8hP
{ XH:Zk4p
class SearchingMines
{ 6b` Z#CNN
public ArrayList list = new ArrayList();
public int[,] mines = new int[10, 10];
static void Main(string[] args)
{
SearchingMines sm = new SearchingMines(); 1R"r{*YU@+?V
sm.initMines(); }yTD:T
sm.HidenMines(); a-AD)h0a$D,]5Z7LSu,X
sm.FillInOtherNumber(); /W;F_g s
sm.display(); #mhTk`.C^7{
// sm.SaveTxt();
}
public void initMines()
{ .y:u#o Pm2P NU
for (int i = 0; i < this.mines.GetLength(0); i++)
{ 5PA!p;}[4{OK
for (int j = 0; j < this.mines.GetLength(1); j++)
{
this.mines[i, j] = 0;
list.Add(this.mines[i, j]); (A Fr yX
} *At/afU G)Yj"|
} *?KE.D-jA9P"}(n
} 2wU J%kV7f*c
public void HidenMines()
{ I3q5i%_6sA*Ik
Random r = new Random(); _]1_k*J$uw8L?
for (int i = 0; i < 9; i++)
{
int count = this.list.Count; C \n,o|W
int number = r.Next(count); 6Ho1o9[6}Qq
int row = number / 10;
int column = number % 10; 7W.E3\bg%l
this.mines[row, column] = 9;
this.list.RemoveAt(this.mines[row, column]);
}
} "t.lcTc%m
public void FillInOtherNumber()
{ .Q0_0dQ+}/k-rQm9rw
try 9em2ob;pvv4b#U
{ e*TGCP)j/ob
for (int i = 0; i < this.mines.GetLength(0); i++) Gh"[q4D9j*R |{
{ (MMi1L$yy:m
for (int j = 0; j < this.mines.GetLength(1); j++) T`"v(eYM&j\
{ ya!iA$c7G
int left = j - 1; Df;^j*H
int right = j + 1; RK rS9i"m}t
int top = i - 1; e]i+`2i
int bottom = i + 1; p V omm"R
if (this.mines[i, j] != 9)
{
if(top>=0 && left>=0)//左边和上边 7`[]E:VI3P
{