C language with the help of EASYX implementation of the life game source _c language

Source: Internet
Author: User
Tags function definition random seed

This article describes the C language with the help of easyx to achieve the life game, the specific operating procedures are as follows:

1. Life Game content:

The game consists of a two-dimensional rectangular world in which every square in the world lives a living or dead cell. The life of a cell at the next moment depends on the number of living cells in the adjacent eight squares. If the number of living cells around a cell is more than 3, this cell will die at the next moment because of the scarcity of resources; if there are 3 living cells around a position, the position will be born a new cell at the next moment; if there are 2 living cells around a position, the cell life and death status in that position remains unchanged. If there are fewer than 2 living cells around a cell, the cell will die of loneliness. So that the whole world of life is not too desolate or crowded, but a dynamic balance.

2. The implementation code is as follows:

Program Name: Life Game//compilation environment: Visual C + + 6.0,easyx//#include #include #inc Lude//define global variable __int8 world[102][102] = {0}; Define two-dimensional World IMAGE imglive, Imgempty;   Defines the living cells and the cell-free region of the pattern//function declaration void Init ();  initialize void Squareworld ();  Create a cell with a square distribution of the World Void Randworld ();  Create a randomly distributed world void Paintworld ();  Draw world void Evolution ();
 Evolution//Main function int main () {Init ();  int Speed = 500; Game Speed (ms) while (true) {if (Kbhit) | |
  Speed = = 900) {Char c = getch ();
  if (c = = ' && Speed!= 900) c = Getch ();
  if (c >= ' 0 ' && C <= ' 9 ') Speed = (' 9 '-c) * 100;
  Switch (c) {case ' s ': Squareworld ();////////////////////////////////// Case ' R ': Case ' R ': Randworld ();
  Produces a default cell with a square distribution of the world break;
  Case Vk_escape:goto End;  } Evolution ();  Evolutionary Paintworld ();
 When drawing the world if (Speed!= 900)//speed is 900, to press any key to step through sleep (Speed);
 } end:closegraph ();
return 0; }
///////////////////////////////////function definition//initialization void Init () {//Create drawing window Initgraph (640,480);
 Set up random seed srand ((unsigned) time (NULL));
 Adjusts the size of the world pattern Resize (&imglive, 4, 4);
 Resize (&imgempty, 4, 4);
 Drawing Setworkingimage (&imglive) with a life-world pattern;
 SetColor (GREEN);
 Setfillstyle (GREEN);
 FillEllipse (0, 0, 3, 3);
 Drawing the Pattern setworkingimage (&imgempty) of inanimate worlds;
 SetColor (Darkgray);
 Rectangle (1, 1, 2, 2);
 Restores the drawing setworkingimage (NULL) to the default window;
 Output Simple Description SetFont (24, 0, "blackbody");
 Outtextxy (254, 18, "Life Game");
 RECT r = {440, 60, 620, 460};
 SetFont (12, 0, "song Body"); DrawText ("Life game introduction: \ n Life game includes a two-dimensional rectangular world in which every square of the world lives in a living or dead cell.") A cell's life and death depends on the number of living cells in the adjacent eight squares at the next moment. If the number of living cells around a cell is more than 3, this cell will die at the next time because of the scarcity of resources; if there are 3 living cells around a position, the position will be born in a new cell at the next moment; if there are 2 living cells around one position, the cell life and death status in that position remains unchanged. If there are less than 2 cells living around a cell, the cell will die of loneliness. So that the whole world of life will not be too desolate or crowded, but a dynamic balance.
 \ n \ nthe game control: \ n 0-9: Adjust speed (slow-fast) \ n ESC: Exit \ n space: \ n Pause | Continue \ S: Create cells in a square-shaped world \ R: Create a randomly distributed world of cells, &r, dt_wordbreak;
Produces the default cell with a square distribution of the world Squareworld (); //Create a cell with a squareDistributed world void Squareworld () {memset (worlds, 0, 102 * 102 * sizeof (__INT8));
 for (int x = 1; x <= + + +) world[x][1] = world[x][100] = 1;
for (int y = 1; y <= y++) world[1][y] = world[100][y] = 1; //Create a cell randomly distributed world void Randworld () {for (int x = 1; x <=-X + +) for (int y = 1; y <= y++) world[x][y] =
RAND ()% 2; }//Draw world void Paintworld () {for (int x = 1; x <= + + +) for (int y = 1; y <= y++) putimage (+ x * 4, 5 6 + y * 4, world[x][y]?
&imglive: &imgempty);
 }//Evolution void Evolution () {__int8 tmp[102][102] = {0};//temporary array int sum; for (int x = 1; x <= + x) {for (int y = 1; y <= y++) {//Calculate the number of lives around sum = world[x+1][y] + world[x+
  1][Y-1] + world[x][y-1] + world[x-1][y-1] + world[x-1][y] + world[x-1][y+1] + world[x][y+1] + world[x+1][y+1];  Calculates the current position of the life State switch (sum) {case 3:tmp[x][y] = 1;
  Break Case 2:tmp[x][y] = world[x][y];
  Break  Default:tmp[x][y] = 0;
  Break Return the temporary array to the world
 memcpy (World, TMP, 102 * 102 * sizeof (__INT8)); }

3. The effect is as shown in the following picture:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.