⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.cs

📁 演示遗传算法的C#程序LifeOFGame
💻 CS
字号:
/*
 * Created by SharpDevelop.
 * User: Rondge
 * Date: 2007-10-20
 * Time: 20:28
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections.Generic;
namespace GameOfLife
{
	class MainClass
	{
		/// <summary>
		/// 数据定义区////////
		/// </summary>
		const int size=20;
		
		/// <summary>
		/// 功能函数定义区
		/// </summary>
		/// <param name="args"></param>
		/// 
		//格式化
		public static void Init(char [,] data)
		{
			for(int i=0;i<data.GetLength(0);++i)
				for(int j=0;j<data.GetLength(1);++j)
				data[i,j]='.';				
		}
		//输出矩阵结果
		public static void OutData(char[,] data)
		{
			Console.Clear();
			Console.WriteLine("当前的状态是:");
			Console.Write("   ");
			for(int i=0;i<data.GetLength(0);++i)
				Console.Write((i+1).ToString()+" ");
			Console.WriteLine("");
			//输出数据
			for(int i=0;i<data.GetLength(0);++i)
			{
				if(i<9)
					Console.Write((i+1).ToString()+"  ");
				else
					Console.Write((i+1).ToString()+" ");
				for(int j=0;j<data.GetLength(1);++j)
				{
					if(j<9)
						Console.Write(data[i,j].ToString()+" ");
					else
						Console.Write(data[i,j].ToString()+"  ");
				}
				       Console.WriteLine("");
			}
		}
		//将posX,posY处的值置为X
		public static bool SetX(char[,] data,int posX,int posY)
		{
			if(posX>(data.GetLength(0)-1)||posY>(data.GetLength(1)-1))
				return false;
			data[posX,posY]='X';
			return true;
		}
		//将posX,posY处的值置为.
		public static bool SetDot(char[,] data,int posX,int posY)
		{
			if(posX>(data.GetLength(0)-1)||posY>(data.GetLength(1)-1))
				return false;
			data[posX,posY]='.';
			return true;
		}	
		public static bool IsX(char[,] data,int posX,int posY)
		{
			if(posX<0||posX>=data.GetLength(0)||posY<0||posY>=data.GetLength(1))
				return false;
			else if(data[posX,posY]=='X'||data[posX,posY]=='x')
				return true;
			else return false;
		}
		public static bool IsDot(char[,] data,int posX,int posY)
		{
			if(posX<0||posX>=data.GetLength(0)||posY<0||posY>=data.GetLength(1))
				return false;
			else if(data[posX,posY]=='.')
				return true;
			else return false;
		}
		public static int HowManyLive(char[,] data,int posX,int posY)
		{
			int sum=0;
			if(IsX(data,posX-1,posY-1))
			   ++sum;
			if(IsX(data,posX,posY-1))
				++sum;
			if(IsX(data,posX+1,posY-1))
				++sum;
			if(IsX(data,posX-1,posY))
				++sum;
			if(IsX(data,posX+1,posY))
				++sum;
			if(IsX(data,posX-1,posY+1))
				++sum;
			if(IsX(data,posX,posY+1))
				++sum;
			if(IsX(data,posX+1,posY+1))
				++sum;
			return sum;
			
		}
		
		//判断posX,posY处的点是否Live
		public static bool IsLive(char[,] data,int posX,int posY)
		{
			if(IsDot(data,posX,posY))
			{
				
				if(HowManyLive(data,posX,posY)==3)
					return true;
				else
					return false;		   	
			}
			else
			if(IsX(data,posX,posY))
			{
				if(HowManyLive(data,posX,posY)==2
				   ||HowManyLive(data,posX,posY)==3)
					return true;
				else
					return false;
			   	
			}
			else return true;
			       
		}
		//一次生命周期
		public static void LifeCircle(char[,] data)
		{
			for(int i=0;i<data.GetLength(0);++i)
				for(int j=0;j<data.GetLength(1);++j)
				if(IsLive(data,i,j))
				   SetX(data,i,j);
				   else
				   	SetDot(data,i,j);
		}
		public static void Summary()
		{
			Console.WriteLine("模拟 Game of Life");
			Console.WriteLine("Game of Live 规则:");
			Console.WriteLine("1.一个元细胞要么死亡(用.表示),要么活着(用X表示)");
			Console.WriteLine("2.每个元细胞位于一个3*3的单元格的中心,它四周有8个" +
			                  "相邻的元细胞");
			Console.WriteLine("3.t时刻的空元细胞将在t+1时刻成活,但前提是t时间时," +
			                  "正好有3个相邻的元细胞是活着的.");
			Console.WriteLine("4.t时刻的元胞剿 活着的,那么在t+1时候保持成活状态," +
			                  "但前提是而且只能是t时刻有两个或三个相邻的元细胞是成" +
			                  "活的。否则,它将因为过于孤单(邻近的元细胞<2)或过于拥挤" +
			                  "(邻近的元细胞>3)而无法生存");
		}
		public static void Main(string[] args)
		{
			string next;
			char[,] data=new char[size,size];
			
			Console.WriteLine("Hello World!");
				
			Summary();
			
			Init(data);
			
			string posX,posY,input;
			int tmp;
			
			Console.Write("请输入初始化位置(x,y):");
			input=Console.ReadLine();
			while(!input.Equals("end"))
			{
				tmp=input.IndexOf(",");
				if(tmp==0) continue;
				posX=input.Substring(0,tmp);
				tmp=input.LastIndexOf(",");
				if(tmp==0) continue;
				posY=input.Substring(tmp+1);
				if(int.Parse(posX)<1||int.Parse(posY)<1) continue;
				SetX(data,int.Parse(posX)-1,int.Parse(posY)-1);
				
				OutData(data);
				
			    Console.Write("请输入初始化位置(x,y):");
			    input=Console.ReadLine();
				
			//	Console.WriteLine(posX+"   "+posY);
			
			}			
			OutData(data);
			//Console.WriteLine(data.GetLength(0));
			//Console.WriteLine(data.GetLength(1));
			Console.WriteLine("下面进行生命演示(输入end结束,其他继续):");
			input=Console.ReadLine();
			int count=0;
			try{
				while(!(input.Equals("end")))
			{
				LifeCircle(data);
				++count;
				OutData(data);
				Console.WriteLine("当前第"+count.ToString()+"代.");
				input=Console.ReadLine();
							
			}
			}
			catch(Exception e)
			{
				Console.WriteLine(e.ToString());
				Console.ReadLine();
			}
			next=Console.ReadLine();
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -