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