生命游戏C ++

时间:2016-10-24 08:16:09

标签: c++ arrays vector conways-game-of-life

您好,我目前正在制作一个程序,可以找到“直播”的电话号码。经过一定代之后的细胞。输入是:

•第一行,如上所述的游戏规则。即:B3 / S23 =一个细胞是" Born"如果它只有3个邻居," Survives"如果它有2或3个活着的邻居;否则就会死掉。

•第二行,一个数字i,即游戏应该进行的迭代次数。

•第三行,2个数字,即电路板中的行数(n)和列数(m)。

•n行(行),每行包含m(colums)字符。所有角色都是'#'或者'。'

最后,电路板是环形的。即,左侧和右侧环绕并且底侧和顶侧环绕。 有了我的代码,我得到了许多我甚至不知道的错误......加上我甚至不知道我做得对......你们能帮助我吗。

#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

0 个答案:

没有答案