生活游戏C ++

问题描述 投票:0回答:1

嗨,我目前正在编写一个程序,以在经过某些世代后查找“活”细胞的数量。输入为:

•第一行,如上所述的游戏规则。即:B3 / S23 =如果一个单元正好有3个邻居,则该单元为“出生”;如果具有2个或3个活体邻居,则该单元为“存活”;否则死亡。

•第二行,数字i是游戏应进行的迭代次数。

•第三行,两个数字,即面板中的行数(n)和列数(m)。

•n行(行),每行m个(列)字符。所有字符均为“#”或“。”

最后板子是环形的。即左侧和右侧环绕,底部和顶部环绕。借助我的代码,我收到了很多我什至不知道的错误...另外,我什至不知道我是否做得正确...你们能帮我吗?

#include<iostream>
#include<string>
#include <vector>
using namespace std;
// other includes? Probably yes.
void copy(int m, int n, int array1[m][n], int array2[m][n]){
    for(int j = 0; j < m; j++;){
        for(int i = 0; i < n; i++)
            array2[j][i] = array1[j][i]; 
    }
}
void life(int array[m][n], m, n, string born, survive){
    //Copies the main array to a temp array so changes can be entered into a grid
    //without effecting the other cells and the calculations being performed on them.
    int temp[m][n];
    copy(array, temp);
    for(int j = 1; j <= m; j++){
        for(int i = 1; i <= n; i++) {
            //The Moore neighborhood checks all 8 cells surrounding the current cell in the array.
            int count = 0;
            count = array[j-1][i] + 
                array[j-1][i-1] +
                array[j][i-1] +
                array[j+1][i-1] +
                array[j+1][i] +
                array[j+1][i+1] +
                array[j][i+1] +
                array[j-1][i+1];

            //The cell dies.
            for(int k = 0; k < born.length(); k++;){
                if(count == born[k])
                    temp[j][i] = 1;
            }
            for(int x = 0; x < survive.length(); x++;){
                if(count != survive[k])
                    temp[j][i] = 0;
                if(count == x)
                    temp[j][i] = array[j][i];
            }
        }
    }
    copy(temp, array);
}
bool compare(int array1[m][n], int array2[m][n])
{
    int count = 0;
    for(int j = 0; j < m; j++)
    {
        for(int i = 0; i < n; i++)
        {
            if(array1[j][i]==array2[j][i])
                count++;    
        }
    }
    //Since the count gets incremented every time the cells are exactly the same,
    //an easy way to check if the two arrays are equal is to compare the count to 
    //the dimensions of the array multiplied together.
    if(count == m*n)
        return true;
    else
        return false;
}
// functions? Yep, there should be some.

int main(){
    string variable;
    string i;
    string grid;
    string board;
    getline(cin,variable);
    getline(cin,i);
    getline(cin,grid);
    getline(cin,board);
    auto pos_b= variable.find_first_of("B");
    auto pos_s= variable.find_first_of("S");
    pos_s -= pos_b;//now it's the length of size
    auto b = variable.substr(pos_b+1,pos_s-2);
    auto s = variable.substr(pos_s+1);
    int int_b = stoi(b);
    int int_s = stoi(s);

    cout << int_b << ' ' << int_s << endl;
    auto pos_spc= grid.find_first_of(' ');
    int row = stoi(grid.substr(0,pos_spc));
    int column = stoi(grid.substr(pos_spc+1));
    cout << row << ' ' << column << endl;

    int cnt = 0;
    int gen0[column][row];
    int todo[column][row];
    int backup[column][row];
    for(int j = 1; j < m; j++){
        for (int i = 1; i < n; i++){
            if(board[j][i] == '.')
                board[j][i] = 0;
            if(board[j][i] == '#')
                board[j][i] = 1;
        }
        for(int k = 0; k < i; i++){
            if(i == 0)
                copy(gen0, todo, column,row);  
            copy(todo, backup,column,row);          
            print(todo);        
            life(int todo[column][row], column, row, string int_b, int_s);
            cnt++;
        }
    }
}

[Test Cases [测试用例] [2] test cases

c++ arrays vector conways-game-of-life
1个回答
0
投票

merhaba bunu tamamladiysan bana atarmisin。

© www.soinside.com 2019 - 2024. All rights reserved.