Question

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;
    }
                }
            }
    }
Was it helpful?

Solution

Messing around is no way to debug a program. When it doesn't behave as you expect your first step should be to try to understand why. By making what, at your level of experience, will basically be random changes to your code you will just make things worse. As Oli mentioned a debugger is the best tool for this. You should learn how to use yours.

Looking at the code there are probably lots of errors but one error jumps out at me which is this

Neighbors = (OriginalArray, CurrRow, CurrCol);

I've no idea why you think this will count the number of neighbours but trust me it doesn't. I suppose you took a guess and then when the compiler didn't complain you assumed the guess was right. I'm afraid not.

So that's the bit to concentrate on first. You need some new code to count the number of live neighbours. Have a think about that.

There's no code above to enter values into Board. Here some code to do that. It's correct as far as it goes, but that doesn't mean it will work with the rest of your code which needs a lot of work.

// 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
}

The two parts that were missing from your code were initially setting the whole Board to dead, and secondly once you have got the coordinates for a live cell from the user you did nothing to set that cell to live.

One other thing that's confusing me about your code is that you have different size boards

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

Why does Board have + 2 and the others don't. That makes no sense to me.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top