学习C++:生命的游戏 爪哇开发人员在这里寻找一些帮助,用C++语言在新的语言中脱颖而出。
问题是这段代码不会在gcc上编译,我不知道为什么 预期行为如下所示,是一个有一套规则的游戏: 游戏被表示为一个二维数组,随机填充二进制单元格,每个单元格要么活着,要么死去 在游戏的每一轮中,细胞根据一个规则子集生存或死亡。 A.如果一个细胞有4个或更多的活邻居,它就会因过度拥挤而死亡。 B如果一个细胞有一个或更少的活邻居,它就会孤独而死。 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数组中,并用表示内部相反邻居的重影值填充周围的外壳 如果您能帮我把这个从一楼拿下来,我们将不胜感激。我去多读书学习C++:生命的游戏 爪哇开发人员在这里寻找一些帮助,用C++语言在新的语言中脱颖而出。,c++,C++,问题是这段代码不会在gcc上编译,我不知道为什么 预期行为如下所示,是一个有一套规则的游戏: 游戏被表示为一个二维数组,随机填充二进制单元格,每个单元格要么活着,要么死去 在游戏的每一轮中,细胞根据一个规则子集生存或死亡。 A.如果一个细胞有4个或更多的活邻居,它就会因过度拥挤而死亡。 B如果一个细胞有一个或更少的活邻居,它就会孤独而死。 C如果一个死亡的细胞正好有3个邻居,它就会像殖民地一样重生 为了确定相邻,电路板的每一侧被视为与相对侧相邻。因此,右侧与左侧相邻,反之亦然。顶部和底部以及角落
#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的观测误差输出++
如何修复它
您必须向前声明所有函数,以便编译器在调用它们时知道它们。这些函数相互调用的方式存在顺序依赖性。将函数的顺序更改为这样,int main位于文件的底部
void setghost在**板上,整数行
void initBoardint**board,int行
int邻接**板,int i,int j
无效打印板int**板,int行
void playGameint**board,int行,int maxCycles
int main
你也有一些不必要的包含,比如include Life.h和include。其他C包括应该使用C++格式包括,而不是包括它应该包括,而不是包括它应该包括。
最后,在int-main的末尾出现了一个问题,其中未初始化地使用了end。一旦你解决了所有其他问题,你就会看到它。你已经到了成为程序员的时候,你需要学习如何使用调试器。编译器需要能够解析你的函数,如initBoardboard、rows;,playGameboard,行,最大循环;,设置重影板,行;,邻居委员会,i,j;,最后是印刷板,行;。您必须将这些declarions放在int main之前。请确保我理解此修复:此正向声明允许后续函数访问它?例如,当initBoard调用setghost时,您将转发声明setghost,以便在initBoard调用它时声明它?为此,由于main使用playGame,playGame使用列表中前面的函数,这两个函数需要按相反的顺序在声明顺序的底部播放main?另外,感谢您指出结束错误,忘记包含时间和结束;after@WoodGlass没错,只需重新排序.cpp文件中的函数,使它们在调用之前都显示在编译器中。@WoodGlass我很高兴它对您有所帮助!请接受我的回答,如果它让你离开一楼-刚刚尝试了这些改变,一切都很好!现在来格式化tweeks。。。非常感谢。
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