被基于康威生活游戏的任务所淹没



我得到了文件gameOfLife.cpp,life.cpp和life.h。我只被允许编辑生活.cpp以使程序正常工作。我不知道该怎么编辑生活.cpp因为有很多事情是我不熟悉的。我得到了一个文件结帐生活.cpp来检查我的工作。

在过去的两天里,我一直在查看其他人的生活游戏文件,试图了解如何进行,但我很茫然。我不希望有人为我做我的工作,但我需要一些指导。

生命游戏.cpp

#include <iostream>
#include "life.cpp"
#include "life.h"
const int GENERATIONS=100;
using namespace std;
//make a random array of initial living cells
void gen(bool a[ROWS][COLS]){
    for(int i=0;i<ROWS;++i){
        for(int j=0;j<COLS;++j){
            if(rand()%100<10)a[i][j]=true;
            else a[i][j]=false;
        }
    }
    a[5][5]=true;
    a[5][6]=true;
    a[5][7]=true;
    return;
}
// check to see if two arrays are equal
bool equal(const bool a[ROWS][COLS], const bool b[ROWS][COLS]){
    int i,j;
    for(i=0;i<ROWS;++i)for(j=0;j<COLS;++j)if(a[i][j]!=b[i][j])return false;
    return true;
}
//copy the b array into the a array
void copy(bool a[ROWS][COLS], const bool b[ROWS][COLS]){
    for(int i=0;i<ROWS;++i){
        for(int j=0;j<COLS;++j){
            a[i][j]=b[i][j];
        }
    }
    return;
}
//print out the array
void print(const bool a[ROWS][COLS]){
    for(int i=0;i<ROWS;++i){
        for(int j=0;j<COLS;++j){
            if(a[i][j])cout << 'X';
            else       cout << ' ';
        }
        cout << endl;
    }
    return;
}

int main(){
    bool current[ROWS][COLS];
    bool next[ROWS][COLS];
    int i=0;
    //initialze the cell array and print it out
    gen(current);
    print(current);
    while(i<GENERATIONS){
        //get a carriage return before the next generation
        cin.get();
        //give the current generation to life()
        //it puts the next generation into next
        life(current,next);
        //copy the next generation into the current
        copy(current,next);
        //print it out
        print(current);
        i++;
    }
    return 0;
}

生活.cpp

/*1. You need to write a file life.cpp that implements the function prototyped in life.h.  You can and should write other functions
     and tuck them into the same file; whatever you need to get your function working in an elegant manner.
  2. Compile your file with checkoutLife.cpp and run the resulting executable to see if it passes all the tests.
  3. Compile yourfile with gameOfLife.cpp and run the resulting executable to see if it makes pretty pictures.
  4. If you are convinced steps 2 and 3 are working, submit your life.cpp via canvas.
*/
#include <iostream>
#include <cstdlib>
#include "life.h"
using namespace std;
void life(const bool current[ROWS][COLS], bool next[ROWS][COLS]){
}

生活.h

#ifndef LIFE_H
#define LIFE_H
const int ROWS=25;
const int COLS=25;
// Simulate one generation of Conways Game of Life
//
// Given:
//        a constant 2D bool array called "current" where each true element
//        indicates a live cell and each false element indicates a dead cell.
//
//        an empty  2D bool array called "next"
void life(const bool current[ROWS][COLS], bool next[ROWS][COLS]);
#endif

life() 使用两个参数调用:一个板的当前状态数组(大概你不会碰)和一个板的下一个状态数组(你将填充)。

以下是您可以使用的算法:

  • 对于 ROW 中的每一行:
    • 对于 COL 中的每个列:
      • 将当前[][]中的所有周围邻居相加,存储在"邻居"中
      • 如果当前[行][col] 处于活动状态,并且邻居<2,则下一个[行][col] = 死
      • 否则,如果当前[行][col]处于活动状态并且邻居== 2或3,则下一个[行][col]=活动
      • 否则,如果当前[行][col]还活着并且邻居>4,则下一个[行][col] =死了
      • 否则,如果当前[行][col]已死并且邻居== 3,则下一个[行][col]=活动

你可以稍微清理一下逻辑,但我把它打印出来,就像维基百科生命游戏条目上的规则一样。复杂的部分(IMO)是"将所有周围的邻居相加" - 这里有很多边界检查。为了清楚起见,我建议调用您编写的不同方法。

最新更新