功能
game of life
用到的觀念
switch function
CODE
using namespace std;using namespace std;/*PROGRAMMER :楊翔斌*/
#include "stdafx.h" /*DATE :2009-12-10*/
#include<iomanip> /*FILENAME :HW05002.cpp*/
#include <iostream> /*DESCRIPTION:game of life*/
#include "stdafx.h"
#include<iomanip>
#include <iostream>
using namespace std;
#include "consola.h"
const int row = 24, column = 24;//set constant
void rule_set(int [row][column] , int [row][column]);
void output(int a[row][column]);
int main()
{
int
init[row][column]={0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,1,0,0,1,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,0,1,1,0,0,0,0,
0,0,1,1,1,0,0,1,0,1,0,0,0,1,1,1,1,0,1,1,1,1,0,1,0,0,1,0,0,0,
0,0,1,0,0,1,0,1,1,0,0,1,0,1,0,0,1,0,1,0,0,1,0,1,1,1,1,0,0,0,
0,0,1,0,0,1,0,1,0,0,0,1,0,1,0,0,1,0,1,0,0,1,0,1,0,0,0,0,0,0,
0,0,1,1,1,0,0,1,0,0,0,1,0,1,1,1,1,0,1,1,1,1,0,0,1,1,1,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,
0,0,1,0,0,0,0,0,0,1,1,1,0,0,1,0,1,0,1,1,0,0,1,0,0,1,0,0,0,0,
0,0,1,0,0,1,1,0,1,0,0,1,0,0,1,1,0,1,0,0,1,0,1,1,1,1,0,0,0,0,
0,0,1,0,0,0,1,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,1,0,0,0,0,0,0,0,
0,0,0,1,1,1,0,0,0,1,1,0,1,0,1,0,0,1,0,0,1,0,0,1,1,1,0,0,0,0,
0,0,1,0,1,0,1,0,1,0,0,1,0,0,1,0,1,0,1,1,1,1,0,1,0,1,0,1,0,1,
1,0,0,0,1,0,1,0,0,1,0,1,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1,0,0,1,0,1 };//initial value
int next[row][column],m,generation,ch,c;//array "next" is stored with
the next generation, "ch" saves the choice
int i1, j1;
textcolor(LIGHTGREEN);//give following word color lightgreen
cout<<"this is game of life"<<endl;//show description
cout<<"if there are more than three or less than one cells
surrounding a cell, the cell dies"<<endl;//show description
cout<<"if there are two or three cells surrounding a cell, the cell
lives"<<endl;//show description
cout<<"if there are three cells surrounding a cell, the cell is
born"<<endl;//show description
system("pause");//stop for while
system("cls");//clean the scren
tag1:
cout<<"there are two function for this program"<<endl;
cout<<"if you don't want to creat your own matrix, type 1"<<endl;
cout<<"if you want to create a matrix enter 2"<<endl;
cin>>ch;
system("cls");
if(ch==1)
{
cout << "enter the generation you want:";
cin >> generation;
system("cls");
}
else if(ch==2)
{
tag2:
cout<<"you'll have to type 576 numbers"<<endl;
cout<<"if you want to give up, enter 0"<<endl;
cin>>c;
if(c==0)
{
goto tag1;//if c is equal to 0 it'll retrun to tag1
}
cout <<"pleasee enter "<<row<< " × " << column << "
matrix"<< endl<< "0 means no cell, 1 maens a live cell"<<endl;
for(i1 = 0; i1 < row; i1++)//enter the initial array
{
for(j1 = 0; j1 < column; j1++)
//run the loop to let people enter number
{
cin >> init[i1][j1];//enter number
}
}
cout << "enter the generation you want:";
//enter generation you want
cin >> generation;//appoint generation to "generation"
}
else
{
cout<<"you enter wrong"<<endl;
goto tag1;//if people don't type 1 or 2, go to tag1
}
tag3:
cout<<"(0)";
output(init);
system("cls");
for(int i=0; i<generation; i++)
//you enter the generation and it'll determine how many times it runs
{
cout<<"("<<i+1<<")";//the generation
rule_set(init, next);
//call the function and it will follow the rule to make another generation
output(next);//print the result
system("cls");//clean the screen
for(int x = 0; x < row; x++)//run the loop
{
for(int y = 0; y < column; y++)//run the loop
{
init[x][y] = next[x][y];
//the new one replace the old one
}
}
}
textcolor(LIGHTGREEN);//color it!
cout<<"when the generation is 198, the group is stable"<<endl;
//show description
cout<<"want to try 198th generation? type 1 or exit(enter any number)
"<<endl;
//show description
cin>>ch;//give you choice
if(ch==1)//determine whether it's 1
{
goto tag1;
}
system("pause");
return 0;
}
void rule_set(int init[row][column], int next[row][column])
{
int x,y,c,r,count=0;
for(x=0;x<row;x++) //calculate its neighbors. check the row
{
for(y=0;y<column;y++)//check the column
{
for(r=(x-1); r<=(x+1); r++)
//if it's x, check the right and left neighbor
{
for(c=(y-1);c<=(y+1);c++)
//check the up and down neighbor
{
if((r<0)||(r>=row)||(c<0)||(c>=column))
//check if it's a boundary, and ignore it
continue;
else if(init[r][c]==1)
//calculate how many "1" near
count++;
}
}
if(init[x][y] == 1)//if itself is 1, minus 1
count--;
switch (count)//check every occasion
{
case 0:
case 1:
case 4:
case 5:
case 6:
case 7:
case 8:
next[x][y]=0;
break;//if it's 0,1,4,5,6,7,8 it dies and
it's 0
case 2:
next[x][y]=init[x][y];//if it's 2, it lives
break;
case 3:
next[x][y]= 1;//if it's 3, it borns
break;
}
count=0;//appoint 0 to count
}
}
}
void output(int a[row][column])//print the result in order
{
int i,j;
for(i = 0; i < row; i++)
{
for(j = 0; j < column; j++)
{
gotoxy(12+2*j,1+i);//arrange with "gotoxy"
if(a[i][j]==1)
{
textcolor(RED);//color it
cout << a[i][j];
}
else if(a[i][j]==0)
{
textcolor(YELLOW);//color it
cout << a[i][j];
}
}
cout << endl;
}
delay(3000);//stop for 3s
system("pause");
cout<<endl;
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.112.7.59