学习C :生活游戏
Learning C++: The game of Life
学生java开发人员在这里寻找一些用C 程序以新语言为基础的帮助。
问题是,此代码不会在GCC上编译,我的想法为零。
预期的行为作为一组规则的游戏:
游戏表示为2D阵列,带有随机填充的二进制单元格,每个细胞都活着或死亡。
游戏的每个"圆",单元格根据规则子集生命或死亡。
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 raray反映为n 2的n 2阵列,并填充周围的" shell"用代表内部对面邻居的"幽灵价值"。
任何帮助将其从底楼中取出的任何帮助将不胜感激。我去更多阅读。
#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()
在文件底部的情况。
-
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
未经初始化。一旦解决了所有其他问题,您就会看到它。
- 在C++游戏中与库存系统作斗争
- 换位表导致测试失败(但在游戏中运行良好)
- Craps游戏问题,忽略if语句
- 矢量下标超出SFML游戏中的范围
- 我是 c++ 的新手.学习基本知识后,我想做井字游戏.对于印刷板,我在下面写代码,但它显示错误
- 我在贪吃蛇游戏中收到了错误代码 -1073741571
- 我应该在简单的策略游戏中为各个派系使用类吗 - C++
- 游戏体验和升级
- TCS 模拟生活 2019 第 2 轮问题:跳跃游戏
- 使用类C 设计生活游戏
- 学习C :生活游戏
- 多线程康威的生活游戏在C++偶尔搭便车?
- 生活的游戏邻居检查(更简单的方法可能吗?)
- 生活游戏哈希表 .h 文件模板问题
- 生活中的游戏问题,C++
- 生活游戏程序,需要用户输入的帮助
- 生活游戏无法正确打印C++
- 被基于康威生活游戏的任务所淹没
- 分区是一个类似生活游戏的程序,用于负载平衡的并行计算
- 在生活游戏中分配迭代时间