标签:start date game bre ati clu temp while for
main.cpp
#include <iostream>
#include"Life.h"
using namespace std;
int maxrow=10,maxcol=10;
int grid[10][10];
char temp;
int row,col;
int main() {
Life life;
cout<<"please input a grid[10][10]"<<endl;
life.initialize();
life.print();
cout<<"please input the row and col to start"<<endl;
cin>>row>>col;
while(1){
life.update();
life.print();
cout << "\nContinue next Generation ? (y/n)" << endl;
cin>>temp;
if (temp != ‘y‘)
break;
else continue;
}
return 0;
}
Life.cpp
#include "Life.h"
#include <iostream>
using namespace std;
int count;
void Life::initialize(){
for(int r=0;r<10;r++)
{for(int c=0;c<10;c++)
{cout<<"grid["<<r<<"]"<<"["<<c<<"]"<<"="<<endl;
cin>>grid[r][c];
}
}
}
void Life::print(){
cin>>grid[10][10];
for(int r=0;r<10;r++){
for(int c=0;c<10;c++)
{
if (grid[r][c] == 1) {
cout << "*";
}
else {
cout << "%";
}
}
cout << endl;
}
cout << endl;
}
int neighbor_count(int row,int col){
int count=0,c,r;
int maxrow=10;
int maxcol=10;
int grid[10][10]={0};
int newgrid[10][10]={0};
for(int r=0;r<10;r++)
{for(int c=0;c<10;c++)
{
cin>>grid[r][c];
}
}
for(r=row-1;r<=row+1;r++)
{
for(c=col-1;c<=col+1;c++)
{
if(r<0||r>maxrow||c<0||c>maxcol)
continue;
if(grid[r][c]==1)
count++;
}
}
if(grid[row][col]==1)
count--;
return count;
}
void Life::update() {
cin >> count;
cin>>grid[10][10];
for (int row = 0; row < 10; row++) {
for (int col = 0; col < 10; col++) {
switch (count) {
case 0:
case 1:
case 4:
case 5:
case 6:
case 7:
case 8:
grid[row][col] = 0;
break;
case 2:
break;
case 3:
grid[row][col] = 1;
break;
}
}
}
}
Life.h
#ifndef LIFEGAME_LIFE_H
#define LIFEGAME_LIFE_H
class Life {
public:
void initialize();
void print();
void update();
private:
static const int maxrow=50;
static const int maxcol=50;
int grid[maxrow+2][maxcol+2];
int neighbor_count(int row,int col);
};
#endif //LIFEGAME_LIFE_H
标签:start date game bre ati clu temp while for
原文地址:https://www.cnblogs.com/wwqdata/p/11488570.html