-1

I have some code for the game of life that takes the users input and displays that input as to which cells they entered as alive, but it always prints a row of four * no matter what. I've messed around with changing things a lot of different ways, but it either still prints the same thing, or doesn't display anything at all. I've done searching around multiple forums and websites, but every code i find is done completely different, which is something i would expect (including code from 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;
    }
                }
            }
    }
4

1 回答 1

1

乱搞是没有办法调试程序的。当它的行为不像您期望的那样时,您的第一步应该是尝试了解原因。以你的经验水平,基本上是随机更改你的代码,你只会让事情变得更糟。正如 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
}

您的代码中缺少的两个部分最初将整个 Board 设置为死,其次,一旦您从用户那里获得了活细胞的坐标,您就没有做任何事情来设置该细胞。

让我对您的代码感到困惑的另一件事是您有不同尺寸的板

    bool Board [MaxRows + 2] [MaxCols + 2];
    long OriginalArray[MaxRows][MaxCols];
    long NewArray[MaxRows][MaxCols];

为什么董事会有+ 2而其他人没有。这对我来说毫无意义。

于 2013-09-28T19:46:27.790 回答