人生游戏编程问题C ++

时间:2019-10-07 01:03:46

标签: c++ project

我是新来的,所以如果格式化不正确,我深表歉意。我似乎无法获得人生游戏的成果。输出应为2网格中的0和1,并且似乎没有输出任何内容。有人可以帮我吗?

编辑:我更改了代码,并收到此错误:

#include <vector>
using namespace std;
#include <iostream>

vector<vector<bool> > world = {
{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,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,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,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,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}
};
vector<vector<bool> > world2 = world;
const int ROW_NUMBER = world.size();
const int COL_NUMBER = world[0].size();
unsigned int i;
unsigned int j;
int countLivingNeightbors(vector<vector<bool> > world,unsigned int i,unsigned int j) {
    int livingNeighbors = 0;
    for (int row = i - 1; row <= i + 1; row++) {
        for (int col = j - 1; col <= j + 1; col++) {
            if (row == i && col == j) {
                continue; // skipping this iteration
            } else {
                int optimizedRow = (row + ROW_NUMBER) % ROW_NUMBER;
                int optimizedCol = (col + COL_NUMBER) % COL_NUMBER;
                if (world[optimizedRow][optimizedCol] == true) {
                    livingNeighbors++;
                } else {
                    continue;
                }
            }
        }
    }
    return livingNeighbors;
}

int main()
{
for (int i = 0; i < world.size(); i++) {
        for (int j = 0; j < world[i].size(); j++) {
            int livingNeighbors = countLivingNeightbors(world, i, j);
            if((world[i][j]==1)&&(livingNeighbors>3)){
                world2 [i][j]=='.';
            }
            if((world[i][j]==1)&&(livingNeighbors<2)){
                world2 [i][j]=='.';
            }
            if((world[i][j]==0)&&(livingNeighbors==3)){
                world2 [i][j]=='O';
            }
            if((world[i][j]==1)&&(livingNeighbors==2||livingNeighbors==3)){
                world2 [i][j]=='O';
            }

        cout << world2;

        }
        cout << endl;
    }
    return 0;
}

0 个答案:

没有答案