using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace 生命游戏
{
class Program
{
static List<Cell> cells = new List<Cell>();
static List<Cell> GaiBian = new List<Cell>();
static void Main(string[] args)
{
Cell[,] CellList = new Cell[10, 10];
Init(CellList);
int n = 1;
Console.WriteLine("num:"+ n++);
Show(CellList);
while (true)
{
Thread.Sleep(1000);
for (int i = cells.Count - 1; i >= 0; i--)
{
ruleH(cells[i], CellList);
}
for (int i = 0; i < GaiBian.Count; i++)
{
GaiBian[i].Bl = GaiBian[i].BlEnd;
}
Console.Clear();
Console.WriteLine("num:" + n++);
Show(CellList);
}
}
public static void ruleH(Cell cell,Cell[,] CellList)
{
//细胞周围有两个活着的则不改变
//细胞周围有三个则转为生
//其他则转为死
int n = 0;//活细胞数量
for (int i = -1; i <= 1; i++)
{
for (int j = -1; j <= 1; j++)
{
if (cell.X+i < 0 || cell.X+i > 9)
{
break;
}
if (cell.Y+j < 0 || cell.Y+j > 9)
{
continue;
}
if (!(0 == i && j == 0))
{
if (CellList[cell.X + i, cell.Y + j].Bl == true)
{
n++;
}
else
{
ruleS(CellList[cell.X + i, cell.Y + j], CellList);
}
}
}
}
if (n == 3)
{
cell.BlEnd = true;
if (!cells.Contains(cell))
{
cells.Add(cell);
}
}
else if (n == 2)
{
}
else
{
cell.BlEnd = false;
if (cells.Contains(cell))
{
cells.Remove(cell);
}
GaiBian.Add(cell);
}
}
public static void ruleS(Cell cell, Cell[,] CellList)
{
int n = 0;
for (int i = -1; i <= 1; i++)
{
for (int j = -1; j <= 1; j++)
{
if (cell.X + i < 0 || cell.X + i > 9)
{
break;
}
if (cell.Y + j < 0 || cell.Y + j > 9)
{
continue;
}
if (!(0 == i && j == 0))
{
if (CellList[cell.X + i, cell.Y + j].Bl == true)
{
n++;
}
}
}
}
if (n == 3)
{
cell.BlEnd = true;
if (!cells.Contains(cell))
{
cells.Add(cell);
}
GaiBian.Add(cell);
}
else if (n == 2)
{
}
else
{
cell.BlEnd = false;
if (cells.Contains(cell))
{
cells.Remove(cell);
}
}
}
public static void Init(Cell[,] CellList)
{
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
Cell cell = new Cell();
cell.X = i;
cell.Y = j;
CellList[i,j] = cell;
}
}
int x, y;
int n = 1;
Console.WriteLine("请输入第" + n + "个活细胞细胞的位置");
Console.WriteLine("X轴坐标:");
x = Convert.ToInt32(Console.ReadLine()) - 1;
Console.WriteLine("Y轴坐标:");
y = Convert.ToInt32(Console.ReadLine()) - 1;
while(!(x>9||x<0||y>9||y<0||n>100))
{
CellList[x,y].Bl = true;
cells.Add(CellList[x, y]);
Console.WriteLine("请输入第" + n + "个活细胞细胞的位置");
Console.WriteLine("X轴坐标:");
x = Convert.ToInt32(Console.ReadLine()) - 1;
Console.WriteLine("Y轴坐标:");
y = Convert.ToInt32(Console.ReadLine()) - 1;
n++;
}
}
public static void Show(Cell[,] CellList)
{
for(int i = 0;i < 10;i ++)
{
for(int j =0; j < 10;j ++)
{
Console.Write(CellList[j,i].Bl?"#":"*");
}
Console.Write("\n");
}
}
}
class Cell
{
private int x; //细胞位置
public int X
{
get { return x; }
set { x = value; }
}
private int y; //细胞位置
public int Y
{
get { return y; }
set { y = value; }
}
private Boolean bl = false;//当前生死状态
public Boolean Bl
{
get { return bl; }
set { bl = value; }
}
private Boolean blEnd; //之后生死状态
public Boolean BlEnd
{
get { return blEnd; }
set { blEnd = value; }
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace 生命游戏
{
class Program
{
static List<Cell> cells = new List<Cell>();
static List<Cell> GaiBian = new List<Cell>();
static void Main(string[] args)
{
Cell[,] CellList = new Cell[10, 10];
Init(CellList);
int n = 1;
Console.WriteLine("num:"+ n++);
Show(CellList);
while (true)
{
Thread.Sleep(1000);
for (int i = cells.Count - 1; i >= 0; i--)
{
ruleH(cells[i], CellList);
}
for (int i = 0; i < GaiBian.Count; i++)
{
GaiBian[i].Bl = GaiBian[i].BlEnd;
}
Console.Clear();
Console.WriteLine("num:" + n++);
Show(CellList);
}
}
public static void ruleH(Cell cell,Cell[,] CellList)
{
//细胞周围有两个活着的则不改变
//细胞周围有三个则转为生
//其他则转为死
int n = 0;//活细胞数量
for (int i = -1; i <= 1; i++)
{
for (int j = -1; j <= 1; j++)
{
if (cell.X+i < 0 || cell.X+i > 9)
{
break;
}
if (cell.Y+j < 0 || cell.Y+j > 9)
{
continue;
}
if (!(0 == i && j == 0))
{
if (CellList[cell.X + i, cell.Y + j].Bl == true)
{
n++;
}
else
{
ruleS(CellList[cell.X + i, cell.Y + j], CellList);
}
}
}
}
if (n == 3)
{
cell.BlEnd = true;
if (!cells.Contains(cell))
{
cells.Add(cell);
}
}
else if (n == 2)
{
}
else
{
cell.BlEnd = false;
if (cells.Contains(cell))
{
cells.Remove(cell);
}
GaiBian.Add(cell);
}
}
public static void ruleS(Cell cell, Cell[,] CellList)
{
int n = 0;
for (int i = -1; i <= 1; i++)
{
for (int j = -1; j <= 1; j++)
{
if (cell.X + i < 0 || cell.X + i > 9)
{
break;
}
if (cell.Y + j < 0 || cell.Y + j > 9)
{
continue;
}
if (!(0 == i && j == 0))
{
if (CellList[cell.X + i, cell.Y + j].Bl == true)
{
n++;
}
}
}
}
if (n == 3)
{
cell.BlEnd = true;
if (!cells.Contains(cell))
{
cells.Add(cell);
}
GaiBian.Add(cell);
}
else if (n == 2)
{
}
else
{
cell.BlEnd = false;
if (cells.Contains(cell))
{
cells.Remove(cell);
}
}
}
public static void Init(Cell[,] CellList)
{
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
Cell cell = new Cell();
cell.X = i;
cell.Y = j;
CellList[i,j] = cell;
}
}
int x, y;
int n = 1;
Console.WriteLine("请输入第" + n + "个活细胞细胞的位置");
Console.WriteLine("X轴坐标:");
x = Convert.ToInt32(Console.ReadLine()) - 1;
Console.WriteLine("Y轴坐标:");
y = Convert.ToInt32(Console.ReadLine()) - 1;
while(!(x>9||x<0||y>9||y<0||n>100))
{
CellList[x,y].Bl = true;
cells.Add(CellList[x, y]);
Console.WriteLine("请输入第" + n + "个活细胞细胞的位置");
Console.WriteLine("X轴坐标:");
x = Convert.ToInt32(Console.ReadLine()) - 1;
Console.WriteLine("Y轴坐标:");
y = Convert.ToInt32(Console.ReadLine()) - 1;
n++;
}
}
public static void Show(Cell[,] CellList)
{
for(int i = 0;i < 10;i ++)
{
for(int j =0; j < 10;j ++)
{
Console.Write(CellList[j,i].Bl?"#":"*");
}
Console.Write("\n");
}
}
}
class Cell
{
private int x; //细胞位置
public int X
{
get { return x; }
set { x = value; }
}
private int y; //细胞位置
public int Y
{
get { return y; }
set { y = value; }
}
private Boolean bl = false;//当前生死状态
public Boolean Bl
{
get { return bl; }
set { bl = value; }
}
private Boolean blEnd; //之后生死状态
public Boolean BlEnd
{
get { return blEnd; }
set { blEnd = value; }
}
}
}