5 Array of Life games

Source: Internet
Author: User

After learning the concept of the array, there is a simple mini-game is particularly suitable to get started---life game, fake with int cells[30][30], that is 30x30 a small lattice,
Every little lattice can have cell life, or cell death. By outputting these states, the corresponding patterns can be displayed.
Rules for the Evolution of Life games:

Each matrix box can contain an organism, and an organism that is not on the edge has 8 adjacent squares.
1. If there are 3 cells around a cell for a living, the cell will live (that is, if the cell is dead, it will live, and if it does, it will remain the same)
2. If there are 2 cells around a cell for a living, the cell's life and death status remains the same.
3. In other cases, the cell is dead (that is, if the cell was originally a living, then died, if it had died, it remained unchanged)

#include <stdio.h>#include<stdlib.h>#include<conio.h>#include<windows.h>#include<time.h>#defineHigh 25//Game Screen Size#defineWidth 50intCells[high][width];//1 born 0 deadvoidGotoxy (intXinty) {HANDLE HANDLE=GetStdHandle (Std_output_handle);    COORD POS; Pos. X=x; Pos. Y=y; SetConsoleCursorPosition (handle, POS);}voidstartup () {intI, J;  for(i =0; I < high; i++){         for(j =0; J < Width; J + +) {Cells[i][j]=1; }    }}voidShow () {Gotoxy (0,0); intI, J;  for(i =1; I <= high-1; i++){         for(j =1; J <= Width-1; J + +){            if(Cells[i][j] = =1) printf ("*");//living cells.            Elseprintf (" ");//Output Spaces} printf ("\ n"); } Sleep ( -);}voidUpdatewithoutinput () {intNewcells[high][width];//cell condition in the next frame    intNeibournumber; intI, J;  for(i =1; I <= high-1; i++){         for(j =1; J <= Width-1; J + +) {Neibournumber= cells[i-1][j-1] + cells[i-1][J] + cells[i-1][j+1] + cells[i][j-1] + cells[i][j+1] + cells[i+1][j-1] + cells[i+1][J] + cells[i+1][j+1]; if(Neibournumber = =3) Newcells[i][j]=1; Else if(Neibournumber = =2) Newcells[i][j]=Cells[i][j]; ElseNewcells[i][j]=0; }    }     for(i =1; I <= high-1; i++)         for(j =1; J <= Width-1; J + +) Cells[i][j]=newcells[i][j];}voidupdatewithinput () {}voidMain () {startup ();  while(1) {show ();        Updatewithoutinput ();    Updatewithinput (); }}

5 Array of Life games

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.