//////////////////////////////////////////////////////////////////////
//
// life.cs
//
// for Life game
//
//
// XIII
//////////////////////////////////////////////////////////////////////
using System;
using System.IO;
namespace MyLife
{
class MyLifeMainClass
{
static void Main(string[] args)
{
//----------------------//-------------------------------------------//
MyLife life = new MyLife();
if(args.Length<1)
{
Console.WriteLine("life v0.1");
Console.WriteLine("usage: life <file_with_configuration>");
life.Load();
}
else
life.Load(args[0]);
Console.WriteLine("press eny Enter to play; for Exit press q");
while(Console.ReadLine()!="q")
life.Show();
//----------------------//-------------------------------------------//
}
}
public class MyLife
{
protected int age=0;
protected int table_size_x=60;
protected int table_size_y=20;
// for store life-array
char[,] itable;
public MyLife()
{
itable = new char [table_size_y+2 , table_size_x+2];
//
// set border
//
int i,j;
for(i=0;i<table_size_x+2;i++)
{
itable[0,i]='-';
itable[table_size_y+1,i]='-';
}
for(j=0;j<table_size_y+2;j++)
{
itable[j,0]='|';
itable[j,table_size_x+1]='|';
}
itable[0,table_size_x+1]='+';
itable[table_size_y+1,0]='+';
itable[0,0]='+';
itable[table_size_y+1,table_size_x+1]='+';
}
public void Load()
{
//
// generate random configuration
//
int seed = DateTime.Now.Hour;
seed+=DateTime.Now.Minute;
seed+=DateTime.Now.Millisecond;
System.Random rnd = new System.Random(seed);
for(int j=1; j<table_size_y+1; j++)
{
for(int i=1; i<table_size_x+1; i++)
{
if (rnd.Next(5)<2)
{
itable[j,i]='*';
}
else
{
itable[j,i]='0';
}
}
}
}
public void Load(string file)
{
//
// read configuration from file
//
if(file==null)
return;
// counters
int i,j;
StreamReader fs = null;
try
{
fs = new StreamReader(file);
j=0;
for(string line = fs.ReadLine(); line != null; line = fs. ReadLine())
{
//Console.WriteLine(line);
j++;
if(line.Length>table_size_x || j>table_size_y+1) // ignor '\n'
{
Console.WriteLine("Line too long!");
Console.WriteLine("need:");
Console.WriteLine("x: {0}, y: {1}",table_size_x, table_size_y);
Console.WriteLine("your x: {0}, y: {1}", line.Length, j);
//
// fill table with zero
//
for(j=1; j<table_size_y+1; j++)
{
for(i=1; i<table_size_x+1; i++)
{
itable[j,i]='0';
}
}
}
//
// put data into array
//
if(j<table_size_x)
{
for(i=1; i<table_size_x+1; i++)
{
itable[j,i]=line[i-1];
}
}
}
}
catch
{
}
finally
{
if(fs!=null)
fs.Close();
}
}
public void Show()
{
Console.WriteLine("Age: {0}", age);
age++;
//
// show configuration
//
for(int j=0; j<table_size_y+2; j++)
{
for(int i=0; i<table_size_x+2; i++)
{
if(itable[j,i]=='d')
itable[j,i]='0';
if(itable[j,i]=='a')
itable[j,i]='*';
if(itable[j,i]=='0')
Console.Write(" ");
else
Console.Write(itable[j,i]);
}
Console.Write("\n");
}
Next();
}
public void Next()
{
//
// calculate next configuration
//
int i,j, neibor;
//----
for(j=1; j<table_size_y+1; j++)
{
for(i=1; i<table_size_x+1; i++)
{
//-----------------------------
switch(itable[j,i])
{
case 'a':
case 'd':
case '+':
case '|':
case '-':
break;
case '0':
case '*':
//-------------------------------------//
neibor=0;
if(itable[j-1,i]=='*'|| itable[j-1,i]=='d') // west
neibor++;
if(itable[j-1,i-1]=='*'|| itable[j-1,i-1]=='d')// north-west
neibor++;
if(itable[j,i-1]=='*'||itable[j,i-1]=='d')// north
neibor++;
if(itable[j+1,i-1]=='*'||itable[j+1,i-1]=='d')// north-east
neibor++;
if(itable[j+1,i]=='*'||itable[j+1,i]=='d')// east
neibor++;
if(itable[j+1,i+1]=='*'||itable[j+1,i+1]=='d')// south-east
neibor++;
if(itable[j,i+1]=='*'||itable[j,i+1]=='d')// south
neibor++;
if(itable[j-1,i+1]=='*'||itable[j-1,i+1]=='d')// south-west
neibor++;
switch(neibor)
{
case 0:
case 1: // will die
if(itable[j,i]=='*')
itable[j,i]='d';
break;
// case 2:// stable =)
// if(itable[x][y]=='*')
// itable[x][y]='*';
// break;
case 3: // will born
if(itable[j,i]=='0')
itable[j,i]='a';
break;
case 4: // will die
case 5:
case 6:
case 7:
case 8:
if(itable[j,i]=='*')
itable[j,i]='d';
break;
default:
break;
}
//-------------------------------------//
break;
default:
break;
}
//-----------------------------
}
}
//----
}
}
}
|