学生Java开发人员在这里寻求一些帮助,使用C ++程序以新语言开始。
问题是这段代码不能在gcc上编译,我不明白为什么。
预期行为是一个包含一系列规则的游戏:
游戏被表示为一个二维数组,其中包含随机填充的二进制单元格,每个单元格都是活着的或死的。
每个" round"在游戏中,细胞基于规则子集生存或死亡 一个。如果一个小区有4个或更多的活着的邻居,它就会因过度拥挤而死亡 湾如果一个细胞有一个或更少的活着的邻居,它就会死于寂寞 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阵列,然后填充周围的&#34 ;壳"用"鬼魂值"代表邻居的内部。
任何帮助将其从底层送出的人都将不胜感激。我去了更多阅读。
#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;
}
}
答案 0 :(得分:1)
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()
位于文件底部的位置。
void setGhosts(int** board, int rows)
void initBoard(int** board, int rows)
int neighbors(int** board, int i, int j)
void printBoard(int** board, int rows)
void playGame(int** board, int rows, int maxCycles)
int main()
您还有一些不必要的内容,例如#include "Life.h"
和#include <stdio.h>
。其他C包括应该使用C ++格式包括,即代替#include <stdlib.h>
它应该是#include <cstdlib>
而不是#include <math.h>
它应该是#include <cmath>
。
最后,在您int main()
未使用end
未使用的情况下,您遇到了问题。一旦你解决了所有其他问题,你就会看到它。