生活游戏无法正确打印C++
Game of life does not print correctly C++
我有一些生命游戏的代码,它接受用户的输入并显示他们输入的单元格是活动的,但它总是打印一行四个*无论如何。我已经搞砸了很多不同的方式,但它要么仍然打印相同的东西,要么根本不显示任何内容。我已经在多个论坛和网站上进行了搜索,但是我找到的每个代码都完全不同,这是我所期望的(包括来自stackoverflow的代码(。
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
using namespace std;
#include <memory.h>
int main ()
{
const long MaxCols (10);
const long MaxRows (10);
const time_t WaitTime (3);
bool Board [MaxRows + 2] [MaxCols + 2];
long CurrCol;
long CurrRow;
time_t StopTime;
long Generation;
long OriginalArray[MaxRows][MaxCols];
long NewArray[MaxRows][MaxCols];
long Neighbors;
do
{
cout << "Enter a row and col for the living cell: ";
cin >> CurrRow >> CurrCol;
} while (CurrRow != 0 && CurrCol != 0);
for (Generation = 1; ; Generation++)
{
//Display the current generation on the board
system("cls");
cout << "tCurrent Generation: " << Generation << endl;
for (CurrRow = 1; CurrRow <= MaxRows; CurrRow++)
{
for (CurrCol = 1; CurrCol <= MaxCols; CurrCol++)
cout << (Board [CurrRow + 2] [CurrCol + 2] ? ' ' : '*');
cout << endl;
}
//Loop to determine nieghbors
for(CurrRow=1; CurrRow <= MaxRows + 2; CurrRow++)
{
cout << endl;
for (CurrCol = 1; CurrCol <= MaxCols + 2; CurrCol++)
{
if (OriginalArray[CurrRow][CurrCol] == '*')
{
Neighbors = (OriginalArray, CurrRow, CurrCol);
if (Neighbors != 3 || Neighbors != 4 )
NewArray[CurrRow][CurrCol] = ' ';
else
NewArray[CurrRow][CurrCol] = '*';
}
else
{
Neighbors = (OriginalArray, CurrRow, CurrCol);
if (Neighbors != 2 || Neighbors != 3 )
NewArray[CurrRow][CurrCol] = ' ';
else
NewArray[CurrRow][CurrCol] = '*';
}
cout << "Touch any key to halt the program";
StopTime = time(0) + WaitTime; // time(0) gives the number of seconds since Jan 1, 1970
while (time (0) < StopTime) // keep looping until the current time catches the stop time
if (_kbhit()) //true if the user has hit a key, false if not hit a key
{
cout << "nBye" << endl;
exit(0);
}
else;
}
}
}
}
乱
七八糟是无法调试程序的。当它没有按照您的预期运行时,您的第一步应该是尝试理解原因。通过对你的经验水平进行基本上是对代码的随机更改,你只会让事情变得更糟。正如 Oli 提到的,调试器是最好的工具。你应该学习如何使用你的。
查看代码可能有很多错误,但有一个错误跳出来了,那就是这个
Neighbors = (OriginalArray, CurrRow, CurrCol);
我不知道你为什么认为这会计算邻居的数量,但相信我,它不会。我想你做了一个猜测,然后当编译器没有抱怨时,你认为猜测是正确的。恐怕不是。
所以这是首先要关注的一点。您需要一些新代码来计算实时邻居的数量。想一想。
上面没有用于在 Board 中输入值的代码。这里有一些代码可以做到这一点。就目前而言,这是正确的,但这并不意味着它将与需要大量工作的其余代码一起使用。
// baord uses false for dead cells and true for living cells
bool Board [MaxRows + 2] [MaxCols + 2];
// initially make the whole board dead
for (int i = 0; i < MaxRows + 2; ++i)
for (int j = 0; j < MaxCols + 2; ++j)
Baord[i][j] = false;
// now ask the user for some live cells
for (;;)
{
cout << "Enter a row and col for the living cell (0 0 to quit): ";
int i, j;
cin >> i >> j;
if (i == 0 && j == 0)
break; // user entered 0 0 so quit loop
Board[i][j] = true; // set position i,j to live
}
代码中缺少的两个部分最初是将整个板设置为死,其次,一旦您从用户那里获得了活动单元的坐标,您就没有执行任何操作来将该单元设置为活动。
另一件让我对你的代码感到困惑的事情是你有不同尺寸的板
bool Board [MaxRows + 2] [MaxCols + 2];
long OriginalArray[MaxRows][MaxCols];
long NewArray[MaxRows][MaxCols];
为什么董事会有+ 2
而其他人没有。这对我来说毫无意义。
相关文章:
- 如何循环打印顶点结构
- 为什么在popback()操作之后,它仍然打印完整的矢量
- 如何在c++中打印目录
- 有一个打印语句的函数是一种糟糕的编程实践吗
- 在线编译器中的分段C++没有打印消息
- 在C++中打印指向不同基元数据类型的指针的内存地址
- 这个指针和内存代码打印是什么?我不知道是打印垃圾还是如何打印我需要的值
- 如何将结构插入到集合中并打印集合的成员
- 在循环C++中指定字符串之后,不会打印该字符串
- 以螺旋方式打印矩阵的程序.(工作不好)
- 从控制台中删除最后打印的元素
- 如何使用Crypto++并为RSA返回可打印的字节/字符数组
- 如何仅使用对象名称打印特定于对象的成员
- 回溯C++不打印函数,因此文件
- 在一定长度后从数组中打印时缺少整数
- 为什么这个 c++ 代码打印出长度 5,当我打印出字符串时,程序会自动终止?
- 在gem5中打印文件中的所有cache_blocks
- 打印数字图案
- Log4cpp:以UTC/GMT时区打印日期
- 如何使用gdb制作一个可以漂亮地打印每个对象的C++函数