gpt4 book ai didi

c++ - 学习 C++ : The game of Life

转载 作者:行者123 更新时间:2023-11-28 04:48:18 25 4
gpt4 key购买 nike

学生 Java 开发人员在这里寻求一些帮助,通过 C++ 程序开始使用新语言。

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

预期行为遵循一组规则:

游戏表示为一个二维数组,其中包含随机填充的二进制单元格,每个单元格要么是活的,要么是死的。

游戏的每一“轮”,细胞根据规则子集生死。
A。如果一个单元格有 4 个或更多活着的邻居,它就会死于过度拥挤。
b.如果一个细胞有 1 个或更少的活邻居,它就会死于孤独。
C。如果一个死细胞恰好有 3 个邻居,它就会像被殖民一样复活。

为了确定邻居,棋盘的每一面都被认为与另一面相邻。因此右侧与左侧相邻,反之亦然。顶部和底部以及角落也是如此。

例如:[0][0]有8个邻居,在它上面是[n-1][n-1],[n-1][0],[n-1][1],在同一行是[0][n-1]、[0][1],下面是[1][n-1]、[1][0]、[1][1]。

运行直到电路板没有变化或用户指定的最大循环已经完成。

可能最令人困惑的元素是我选择了我认为可能的最简单的方式来表示它,通过将 N×N 数组镜像到 N+2×N+2 数组,并填充周围的“壳”具有代表内部相对邻居的“幽灵值”。

如果您能提供帮助,我们将不胜感激。我去读更多的书。

#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;
}
}

最佳答案

观察到 g++ 的错误输出

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> .

最后,您在 int main() 的末尾遇到了问题其中 end未初始化使用。一旦您解决了所有其他问题,您就会看到它。

关于c++ - 学习 C++ : The game of Life,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48755863/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com