生活游戏无法正确打印C++

Game of life does not print correctly C++

本文关键字:打印 C++ 游戏 生活      更新时间:2023-10-16

我有一些生命游戏的代码,它接受用户的输入并显示他们输入的单元格是活动的,但它总是打印一行四个*无论如何。我已经搞砸了很多不同的方式,但它要么仍然打印相同的东西,要么根本不显示任何内容。我已经在多个论坛和网站上进行了搜索,但是我找到的每个代码都完全不同,这是我所期望的(包括来自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而其他人没有。这对我来说毫无意义。