首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >学习C++:生命的游戏

学习C++:生命的游戏
EN

Stack Overflow用户
提问于 2018-02-12 21:34:36
回答 1查看 101关注 0票数 0

这里的学生Java开发人员正在寻找一些帮助,用一个C++程序开发一种新的语言。

问题是,这段代码不会在gcc上编译,我完全不知道为什么。

意图的行为作为一种游戏遵循一组规则:

游戏被表示为一个二维数组,随机填充的二进制单元,每个活的或死的。

每个“回合”的游戏,细胞生存或死亡的基础上的规则子集。

如果一个牢房有4个或4个以上的活邻居,它就会因过度拥挤而死亡。

如果一个牢房有一个或更少的活邻居,它就会孤独而死。

如果一个死掉的细胞正好有三个邻居,它就会像殖民地一样活过来。

为了确定邻居,板的每一面都被认为是相邻的。因此,右侧与左侧相邻,反之亦然。同样适用于顶部和底部,也适用于角落。

例如:有8个邻居,上面是n- 1,在同一行是,下面是1,1和1。

运行,直到板中没有任何更改或用户指定的最大周期已经完成。

其中最令人困惑的元素可能是,我选择以最简单的方式表示它,方法是通过N+2数组将N×N数组镜像到一个N+2数组中,并在周围的"shell“中填充表示内部相对邻居的”幽灵值“。

如果你能帮我把这个从一楼弄出去,我将不胜感激。去看更多的书我走了。

代码语言:javascript
复制
#include "Life.h"
#include <stdio.h>      /* printf, scanf, puts, NULL */
#include <stdlib.h>     /* rand */
#include <time.h>       /* time */
#include<iostream>
#include<fstream>
using namespace std;
/*
 * Main handles the first steps, collecting user input. It creates the initial board, then passes it for further manipulation.
 */
int main(){
    time_t start;
    time(&start);
    int rows;
    cout << "Please input the row number \n";
    cin >> rows;
    rows += 2;
    int maxCycles;
    cout << "Please input Maximum cycles \n";
    cin >> maxCycles;
    int** board = new int*[rows];
    for(int i = 0; i < rows; ++i){
        board[i] = new int[rows];
    }
    initBoard(board, rows);
    playGame(board, rows, maxCycles);
    time_t end;
    cout << "Runtime:" << end-start << "\n";

    return 0;
}

/*
 * Randomizes the chance of a cell to be alive in the initial cycle. After values are initialized, passes board to set ghost values, i.e., the surrounding opposite side neighbors.
 */
void initBoard(int** board, int rows){
    for(int i = 1; i < rows-1; ++i){
        for(int j = 1; j < rows-1; ++j){
            if(rand()%4 == 0){
                board[i][j] = 1;
            }else{
                board[i][j] = 0;
            }
        }
    }
    setGhosts(board, rows);
}

/*
 * Sets the ghost values framing the functioning game board
 */
void setGhosts(int** board, int rows){
    for(int i = 1; i < rows-1; ++i){
        board[0][i] = board[rows-2][i];
        board[rows-1][i] = board[1][i];
        board[i][0] = board[i][rows-2];
        board[i][rows-1] = board[i][1];
    }

    //Sets corner values
    board[0][0] = board[rows-2][rows-2];
    board[rows-1][rows-1] = board[1][1];
    board[0][rows-1] = board[rows-2][1];
    board[rows-1][0] = board[1][rows-2];
}

//Runs up to maxCycles cycles of the game, with each cycle altering the value of board.
void playGame(int** board, int rows, int maxCycles){
    int boolean = 1;
    for(int k = 0; k < maxCycles; ++k){
        //initialize temp array
        int** temp = new int*[rows];
        for(int i = 0; i < rows; ++i){
            temp[i] = new int[rows];
        }
        //Begin game
        for(int i = 1; i < rows-1;++i){
            for(int j = 1; j < rows; ++j){
                //check live neighbors
                int count = neighbors(board, i, j);
                if(board[i][j] == 1){//If alive, check if it stays alive
                    if(count < 4 || count > 1){
                        temp[i][j] = 1;
                    }else{
                        temp[i][j] = 0;
                        boolean = 0;
                    }
                }else if(board[i][j] == 0){//If dead, check if it stays dead
                    if(count == 3){
                        temp[i][j] = 1;
                        boolean = 0;
                    }else{
                        temp[i][j] = 0;
                    }
                }
            }
        }
        setGhosts(temp, rows);
        board = temp;
        if(boolean == 1) break;//If there is no change in the board across a cycle, the game is over
    }
    printBoard(board, rows);
}

//Returns the number of living neighbors to the given cell[i][j]
int neighbors(int** board, int i, int j){
    int count = 0;
    if(board[i-1][j-1] == 1){ ++count;}
    if(board[i-1][j] == 1){ ++count;}
    if(board[i-1][j+1] == 1){ ++count;}
    if(board[i][j-1] == 1){ ++count;}
    if(board[i][j+1] == 1){ ++count;}
    if(board[i+1][j-1] == 1){ ++count;}
    if(board[i+1][j] == 1){ ++count;}
    if(board[i+1][j+1] == 1){ ++count;}
    return count;
}

void printBoard(int** board, int rows){
    for(int i=1; i< rows-1; i++){
        for(int j=1; j< rows-1; j++){
            cout << board[i][j]  << "  ";
        }
        cout << endl;
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-02-12 21:57:52

从g++输出的观测误差

代码语言:javascript
复制
g++ -c -Wall -ansi -O3 -std=c++11 foo.cc
foo.cc: In function 'int main()':
foo.cc:51:26: error: 'initBoard' was not declared in this scope
     initBoard(board, rows);
                          ^
foo.cc:52:36: error: 'playGame' was not declared in this scope
     playGame(board, rows, maxCycles);
                                    ^
foo.cc: In function 'void initBoard(int**, int)':
foo.cc:72:26: error: 'setGhosts' was not declared in this scope
     setGhosts(board, rows);
                          ^
foo.cc: In function 'void playGame(int**, int, int)':
foo.cc:106:50: error: 'neighbors' was not declared in this scope
                 int count = neighbors(board, i, j);
                                                  ^
foo.cc:128:27: error: 'printBoard' was not declared in this scope
     printBoard(board, rows);
                           ^
make: *** [foo.o] Error 1

如何修复它

您必须转发声明所有函数,以便编译器在调用它们时了解它们。在调用这些函数的方式上,您有一个顺序依赖关系。将函数的顺序更改为类似于int main()位于文件底部的顺序。

  1. void setGhosts(int** board, int rows)
  2. void initBoard(int** board, int rows)
  3. int neighbors(int** board, int i, int j)
  4. void printBoard(int** board, int rows)
  5. void playGame(int** board, int rows, int maxCycles)
  6. int main()

您也有一些不必要的包含,如#include "Life.h"#include <stdio.h>。另一个C包括应该使用C++格式,即不是#include <stdlib.h>,它应该是#include <cstdlib>,而不是#include <math.h>,它应该是#include <cmath>

最后,在未初始化end的情况下,您在end的末尾遇到了一个问题。一旦你把所有其他问题都解决了,你就会看到它。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48755863

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档