Home > Net >  Life game algorithm
Life game algorithm

Time:03-08

My train of thought is: each Cell as a class, the judgeAlive () method can judge to accept Cell two-dimensional isAlive group eight adjacent cells, then change their isAlive specific implementation with a double circulation, but the result either all, either completely dead, or keep the original state of life and death invariable, excuse me everybody can help me have a look at which step to write wrong?
Class defined Cell Cell. Cs:
 using System; 
The namespace LifeGame
{
The class Cell
{
Private const int BOARD_LENGTH_X=44, BOARD_LENGTH_Y=76;
Public int pos_x=0;
Public int pos_y=0;
Public bool isAlive;
Public Cell ()
{
Enclosing isAlive=false;
}
Public void set (int x, int y)
{
Enclosing pos_x=x;
Enclosing pos_y=y;
}
Public void judgeAlive (Cell [and] board)
{
Int number_alive=0;
Int current_pos_x current_pos_y;
For (int x=1; X & lt; 2; X++)
{
For (int y=1; Y & lt; 2; Y++)
{
If ((x==0) & amp; & (y==0))
{
continue;
}
The else
{
If (x + enclosing pos_x & lt; 0)
{
Current_pos_x=BOARD_LENGTH_X - 1;
}
Else if (x + enclosing pos_x & gt;=BOARD_LENGTH_X)
{
Current_pos_x=0;
}
The else
{
Current_pos_x=x + enclosing pos_x;
}
If (y + enclosing pos_y & lt; 0)
{
Current_pos_y=BOARD_LENGTH_Y - 1;
}
Else if (y + enclosing pos_y & gt;=BOARD_LENGTH_Y)
{
Current_pos_y=0;
}
The else
{
Current_pos_y=y + enclosing pos_y;
}
If (board [current_pos_x, current_pos_y] isAlive)
{
Number_alive + +;
}
}

}
}
The switch (number_alive)
{
Case 0:
Case 1:
Enclosing isAlive=false;
break;
Case 2:
break;
Case 3:
Enclosing isAlive=true;
break;
Case 4:
Case 5:
Case 6:
Case 7:
Case 8:
Enclosing isAlive=false;
break;
Default:
break;
}
}
}
}

Program. Cs: main Program
 using System; 
using System.Threading;
The namespace LifeGame
{
Class Program
{
Public static void copyBoard (Cell [and] board_to_copy, Cell [and] board_being_copied)
{
Const int BOARD_LENGTH_X=44, BOARD_LENGTH_Y=76;
For (int x=0; X & lt; BOARD_LENGTH_X; X++)
{
For (int y=0; Y & lt; BOARD_LENGTH_Y; Y++)
{
Board_to_copy [x, y] isAlive=board_being_copied [x, y] isAlive;
}
}
}
Public static void consoleBoard (Cell [and] board)
{
Int BOARD_LENGTH_X=44, BOARD_LENGTH_Y=76;
The Console. Write (" ");
for (int i=0; I & lt; BOARD_LENGTH_Y; I++)
{
If (I & lt; 10)
{
The Console. Write (" {0} ", I);
}
The else
{
The Console. Write (" {0} ", I);
}
}
The Console. Write (" \ n ");
For (int x=0; X & lt; BOARD_LENGTH_X; X++)
{
If (x & lt; 10)
{
The Console. Write (" {0} ", x);
}
The else
{
The Console. Write (" {0} ", x);
}
For (int y=0; Y & lt; BOARD_LENGTH_Y; Y++)
{
If (board (x, y). The isAlive)
{
The Console. Write (" O ");//is a black square, CSDN don't let send
}
The else
{
The Console. Write (" ");
}
If (y==BOARD_LENGTH_Y - 1)
{
The Console. Write (" \ n ");
}
}
}
}
Public static int userSurface (Cell [and] board)
{

While (true)
{
Int BOARD_LENGTH_X=44, BOARD_LENGTH_Y=76;
The Console. Write (" LifeGame>" );
String userInput=Console. ReadLine ();
The switch (userInput)
{
A case of "live" :
The Console. Write (@ "LifeGame \ live \ x>" );
Int inX_live=the Convert. ToInt32 (Console. ReadLine ());
The Console. Write (@ "LifeGame \ live \ y>" );
Int inY_live=the Convert. ToInt32 (Console. ReadLine ());
The board [inX_live, inY_live] isAlive=true;
The Console. The Clear ();
Program. ConsoleBoard (board);
break;

Case "die" :
The Console. Write (@ "LifeGame \ die \ x>" );
Int inX_die=the Convert. ToInt32 (Console. ReadLine ());
The Console. Write (@ "LifeGame \ die \ y>" );
Int inY_die=the Convert. ToInt32 (Console. ReadLine ());
The board [inX_die, inY_die] isAlive=false;
The Console. The Clear ();
Program. ConsoleBoard (board);
break;

A case of "random" :
Random rd=new Random();
For (int x=0; X & lt; BOARD_LENGTH_X; X++)
{
For (int y=0; Y & lt; BOARD_LENGTH_Y; Y++)
{
Int key=rd. Next (0, 100);
The switch (key % 2)
{
Case 0:
Board (x, y). The isAlive=true;
break;
Case 1:
Board (x, y). The isAlive=false;
break;
}
}
}
The Console. The Clear ();
Program. ConsoleBoard (board);
break;
Case "start" :
The Console. The Clear ();
return 0;
}
}
}
The static void Main (string [] args)
{
The Console. The Title="LifeGame";
Const int BOARD_LENGTH_X=44, BOARD_LENGTH_Y=76, DURATION=100;
The Cell [and] board=new Cell [BOARD_LENGTH_X BOARD_LENGTH_Y];
For (int x=0; X & lt; BOARD_LENGTH_X; X++)
{
For (int y=0; Y & lt; BOARD_LENGTH_Y; Y++)
{
Board (x, y)=new Cell ();
}
}
ConsoleBoard (board);
UserSurface (board);
The Cell [and] boardCopy=new Cell [BOARD_LENGTH_X BOARD_LENGTH_Y];
For (int x=0; X & lt; BOARD_LENGTH_X; X++)
{
For (int y=0; Y & lt; BOARD_LENGTH_Y; nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  •  Tags:  
  • C#
  • Related