- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我在使用 C++ 为 Conway 的生命游戏编码时遇到了一些问题。我觉得我有一个好的开始,我只需要一些方向。首先,当我尝试运行该程序时,我总是得到一个空白的控制台。而且我也不太确定为板上没有完整八个邻居的地方编码的最佳方法。任何帮助将不胜感激。
规则:
You will "hard code" a starting configuration. The user need not be able to provide a different starting configuration (that would just complicate your program).
Suggestions: Look for stable configurations. That is, look for communities that repeat patterns continually. The number of configurations in the repetition is called the period. There are configurations that are fixed, which continue without change. A possible project is to find such configurations.
Hints: Define a void function named generation that takes the vector we call world (call-by-reference or using a pointer), which contains the current (or initial) configuration. The function scans the vector and modifies the cells, marking the cells with births and deaths in accord with the rules listed earlier. This involves examining each cell in turn, either killing the cell, letting it live, or if the cell is empty, deciding whether a cell should be born. Note that the game will not work if your code counts neighbors in the same vector it is modifying. A copy of the world vector must be created before it is modified, and this copy is used to count neighbors, while cells are turned on or off in the original vector.
There should be a function display that accepts the vector world and displays the grid on the screen. Some sort of time delay is appropriate between calls to generation and display. To do this, your program should generate and display the next generation when you press Return/Enter. You are at liberty to automate this (put in a real time delay, and not wait for the user to press a key), but automation is not necessary for the program.
If you want to "delay" the display of your grid, rather than wait for the user to type something and press enter before displaying the next grid, then you will need to pause or "sleep" your program somehow. If you are using Microsoft Windows, do this:
We define each cell to have eight neighbor cells. The neighbors of a cell are the cells directly above, below, to the right, to the left, diagonally above to the right and left, and diagonally below to the right and left. Be careful when checking for neighbors on the edges; you can decide whether an edge cell has alive or dead neighbors beyond the edge.
If an occupied cell has zero or one neighbors, it dies of loneliness. If an occupied cell has more than three neighbors, it dies of overcrowding.
If an empty cell has exactly three occupied neighbor cells, there is a birth of a new cell to replace the empty cell.
Births and deaths are instantaneous and occur at the changes of generation. A cell dying for whatever reason may help cause birth, but a new born cell cannot resurrect a cell that is dying, nor will a cell's death prevent the death of another, say, by reducing the local population.
到目前为止我的代码:
/*
Filename: main.cpp
Author:
Version: 20120920
Description:
*/
#include<iostream>
#include<vector>
#include<unistd.h>
#include<cstdlib>
using namespace std;
/*Change the values of your world matrix*/
#define ROWS 21
#define COLS 80
/*Change the values for dead or alive*/
#define DEAD ' '
#define ALIVE '*'
/*Function Prototype for generation*/
void generation (vector< vector<char> > &world, vector< vector<char> > &world_copy);
/*Function Prototype for display*/
void display(vector< vector<char> >);
int main()
{
vector< vector<char> > world(ROWS, vector<char>(COLS, DEAD));
vector< vector<char> > world_copy(ROWS, vector<char>(COLS, DEAD));
/*Set ALIVE cells*/
world[1][1] = world[1][2] = world[1][3] = ALIVE;
while(true);
{
/*Clear screen and display world*/
system("cls");
display(world);
/*Wait*/
usleep(8000);
/*Update World*/
generation(world, world_copy);
}
return 0;
}
/*Copy the contents of world into world_copy*/
void generation (vector< vector<char> > &world, vector< vector<char> > &world_copy)
{
int ALIVE_count = 0;
for (int i = 0; i < ROWS; i++)
{
for (int j = 0; j < COLS; j++)
{
/*Checks neighbors for life*/
if(world_copy[i-1][j+1] == ALIVE)
{
ALIVE_count++;
}
if(world_copy[i][j+1] == ALIVE)
{
ALIVE_count++;
}
if(world_copy[i+1][j+1] == ALIVE)
{
ALIVE_count++;
}
if(world_copy[i-1][j] == ALIVE)
{
ALIVE_count++;
}
if(world_copy[i+1][j] == ALIVE)
{
ALIVE_count++;
}
if(world_copy[i-1][j-1] == ALIVE)
{
ALIVE_count++;
}
if(world_copy[i][j-1] == ALIVE)
{
ALIVE_count++;
}
if(world_copy[i+1][j-1])
{
ALIVE_count++;
}
/*Rule Section*/
/*Death by loneliness. 0 or 1 neighbors.*/
if (world_copy[i][j] == ALIVE && (ALIVE_count == 0 || ALIVE_count == 1))
{
world[i][j] = world_copy[i][j];
world[i][j] == DEAD;
}
/*Live to next generation. 2 or 3 neighbors.*/
else if(world_copy[i][j] == ALIVE && (ALIVE_count == 2 || ALIVE_count == 3))
{
world[i][j] = world_copy[i][j];
world[i][j] == ALIVE;
}
/*Death by overcrowding. More than 3 neighbors.*/
else if (world_copy[i][j] == ALIVE && ALIVE_count > 3)
{
world[i][j] = world_copy[i][j];
world[i][j] == DEAD;
}
/*Birth. Exactly 3 neighbors.*/
else if (world_copy[i][j] == ALIVE && ALIVE_count ==3)
{
world[i][j] = world_copy[i][j];
world[i][j] == ALIVE;
}
}
}
}
/*Display the world*/
void display(vector< vector<char> > &world)
{
for (int i = 0; i < ROWS; i++)
{
for (int j = 0; i < COLS; j++)
{
cout << world[i][j];
}
cout << endl;
}
}
更新:
发现main的while语句有问题。我曾有一个 ;过了一会儿:
while(true);
{
/*Clear screen and display world*/
system("cls");
display(world);
/*Wait*/
Sleep(800);
/*Update World*/
generation(world, world_copy);
}
我继续将其删除,但现在我得到了对“在 while 循环中显示”的 undefined reference 。
最佳答案
void display(vector< vector<char> > &world)
{
for (int i = 0; i < ROWS; i++)
{
for (int j = 0; i < COLS; j++)
{
cout << world[i][j];
}
cout << endl;
}
}
应该是:
void display(vector< vector<char> > &world)
{
for (int i = 0; i < ROWS; i++)
{
for (int j = 0; j < COLS; j++)
{
cout << world[i][j];
}
cout << endl;
}
}
注意内部 FOR 循环的条件子句。
while(true);
应该是:
while(true)
此模式在代码中重复多次。第一个任务被保留。第二个根本没有完成。这是一个条件表达式评估。看起来像这样:
world[i][j] = world_copy[i][j];
world[i][j] == DEAD;
可能看起来像这样(请注意,我猜测拷贝是先前的状态。不确定您的意图是什么):
world_copy[i][j] = world[i][j];
world[i][j] = DEAD;
文件顶部函数的原型(prototype)是:
void display(vector< vector<char> > );
然而下面的实现是:
void display(vector< vector<char> >& )
注意第二个中的引用参数,第一个中的 by-val 参数。不同的参数列表 = 不同的签名。可能第二个是您的想法,因此更改原型(prototype)(第一个)以匹配实现(第二个)。
您的代码中有几个地方(特别是 generation() 函数)的索引超出了分配的 vector 的末尾。例如,
/*Checks neighbors for life*/
if(world_copy[i-1][j+1] == ALIVE)
{
ALIVE_count++;
}
if(world_copy[i][j+1] == ALIVE)
{
ALIVE_count++;
}
if(world_copy[i+1][j+1] == ALIVE)
那些 [j+1]
和 [i+1]
索引一旦满足 (i==(ROWS-1))
或 (j==(COLS-1))
,因为 ROWS 和 COLS 是战场的分配大小。对于 [i-1]
和 [j-1]
同样,当 i
和 j
位于他们的循环(i=0 或 j=0)
有很多方法可以解决这个问题,从不检查会超限的逻辑(容易检测到)到用永远不会触及的死区来构建您的字段(涉及更多,但编码更有趣) .
底线:您必须学会有效地调试您的程序。如果您认为专业工程师只是吐出正确的代码,请三思。平均而言,我们花费一半的时间(字面上)调试编写的代码。如果您发现自己花费更少,很可能是您没有对您的代码进行必要的尽职调查(坦率地说,这是应有的)。
关于c++ - Conway 的 C++ 生命游戏问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12548545/
我正在开发一个使用多个 turtle 的滚动游戏。玩家 turtle 根据按键命令在 Y 轴上移动。当危害和好处在 X 轴上移动时,然后循环并改变 Y 轴位置。我尝试定义一个名为 colliding(
我不明白为什么他们不接受这个作为解决方案,他们说这是一个错误的答案:- #include int main(void) { int val=0; printf("Input:- \n
我正在使用基于表单的身份验证。 我有一个注销链接,如下所示: 以及对应的注销方法: public String logout() { FacesContext.getCurren
在 IIS7 应用程序池中有一个设置 Idle-time out 默认是 20 分钟,其中说: Amount of time(in minutes) a worker process will rem
我是一名优秀的程序员,十分优秀!