码迷,mamicode.com
首页 > 其他好文 > 详细

lifegame

时间:2019-09-08 22:42:29      阅读:112      评论:0      收藏:0      [点我收藏+]

标签: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

lifegame

标签:start   date   game   bre   ati   clu   temp   while   for   

原文地址:https://www.cnblogs.com/wwqdata/p/11488570.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!