简体   繁体   中英

Game of life problems, C++

I am having problems with my Game Of Life assignment. There are two things not working correctly:

  1. The game runs too many generations (twice the number entered)
  2. It does not update the generations properly: Predetermined state 2 is a static state. This, however, dies as well.

I have checked and re-checked my alive-or-dead conditions and compared them to numerous examples found on the Internet, but I can not find anything. Any comments or pointers are greatly appreciated! :)

Here's the code:

functions.cpp:

#include <iostream>
#include <ctime>
#include "functions.h"

using namespace std;

void dspIntroMenu()
{
    cout << "Welcome to the Game Of Life! Please make a choice below" << endl << endl;
    cout << "Press \"P\" to play!" << endl;
    cout << "Press \"R\" to read more about the Game Of Life." << endl;
    cout << "Press \"Q\" to quit." << endl;
    cout << endl << "Choice: ";
}


void whatIs()
{
    cout << "The game of life ........";
}

void playMenu()
{
    cout << "Press \"P\" to choose from predetermined initial states." << endl;
    cout << "Press \"R\" to randomize." << endl;
    cout << endl << "Choice: ";
}

void dispPredStates()
{
    cout << "Please choose between the following states" << endl << endl;
    cout << "    State 1" << endl << "--------------" << endl;
    cout << "   1 0 1 1" << endl << "   0 0 0 0" << endl << "   1 0 1 0" << endl << "   0 1 1 1" << endl << endl;

    cout << "    State 2" << endl << "--------------" << endl;
    cout << "   1 1 1 0" << endl << "   0 1 1 1" << endl << endl;

    cout << "    State 3" << endl << "--------------" << endl;
    cout << "   0 1 0 1" << endl << "   1 1 0 0" << endl << "   0 1 0 1" << endl << "   1 1 1 0" << endl << endl;
}

function.h:

#ifndef HEADER_H
#define HEADER_H

void dspIntroMenu();
void whatIs();
void playMenu();
void dispPredStates();
int neighbours(int i, int j, int (*game)[21], int x, int y);


#endif

in4.cpp, the "main"

#include <iostream>
#include <ctime>
#include <cstdlib>
//#include <windows.h>
#include "functions.h"

using namespace std;

int main()
{
    static int size = 22;
    int neighAlive, go=0, gen;
    int game[size][size];
    int gameTemp[size][size];
    char mChoice;


    //Fyll matriserna med nollor:
    for (int i=0; i<=size-1; i++)
    {
        for (int j=0; j<=size-1; j++)
        {
            game[i][j] = 0;
            gameTemp[i][j] = 0;
        }
    }
    //%-----------------------------%

    dspIntroMenu();
    cin >> mChoice;

    while (go==0)
    {
        char pMenuChoice;
        int psChoice, help;

        switch (mChoice)
        {
            case 'p':
            case 'P':
                cout << "How many generations do you wish to view?: ";
                cin >> gen;
                cout << endl << endl;
                playMenu();
                cin >> pMenuChoice;

                switch (pMenuChoice)
                {
                    case 'p':
                    case 'P':
                        dispPredStates();
                        cout << "Choice: ";
                        cin >> psChoice;
                        if (psChoice == 1)
                        {
                            game[9][9] = 1; game[9][10] = 0; game[9][11] = 1; game[9][12] = 1;
                            game[10][9] = 0; game[10][10] = 0; game[10][11] = 0; game[10][12] = 0;
                            game[11][9] = 1; game[11][10] = 0; game[11][11] = 1; game[11][12] = 0;
                            game[12][9] = 0; game[12][10] = 1; game[12][11] = 1; game[12][12] = 1;
                        }
                        else if (psChoice == 2)
                        {
                            game[10][9] = 1; game[10][10] = 1; game[10][11] = 1; game[10][12] = 0;
                            game[11][9] = 0; game[11][10] = 1; game[11][11] = 1; game[11][12] = 1;
                        }
                        else
                        {
                            game[9][9] = 0; game[9][10] = 1; game[9][11] = 0; game[9][12] = 1;
                            game[10][9] = 1; game[10][10] = 1; game[10][11] = 0; game[10][12] = 0;
                            game[11][9] = 0; game[11][10] = 1; game[11][11] = 0; game[11][12] = 1;
                            game[12][9] = 1; game[12][10] = 1; game[12][11] = 0; game[12][12] = 0;;
                        }
                        break;
                        go=1;
                    case 'r':
                    case 'R':
                        srand(time(0));
                        for(int i=9; i<=12; i++)
                        {
                            cout << endl;
                            for (int j=9; j<=12; j++)
                            {
                                help = rand() % 3;
                                if (help == 2)
                                {
                                    game[i][j] = 0;
                                }
                                else {game[i][j] = 1;}

                                //cout << game[i][j] << " " << endl;
                            }
                        }
                        break;
                }
                go=1;
                break;

            case 'r':
            case 'R':
                whatIs();
                break;

            case 'q':
            case 'Q':
                return 0;

            default:
                cout << "Please press \"P\", \"R\" or \"Q\": ";
                break;
        }
        break;
    }

    //The real game starts here!
    for (int k=0; k<=size-1; k++)
        {
            for (int l=0; l<=size-1; l++)
            {
                cout << game[k][l] << " ";
            }
            cout << endl;
        }
        cin.ignore(1024, '\n');
        cout << "Press enter to continue...";
        cin.get();
        //system("pause");
        //system("cls");

    for (int tid=1; tid<=gen; tid++)
    {
        for (int i=1; i<=size-2; i++)
        {
            for (int j=1; j<=size-2; j++)
            {
                neighAlive = game[i-1][j] + game[i+1][j] + game[i][j-1] + game[i][j+1] + game[i-1][j-1] + game[i+1][j-1] + game[i-1][j+1] + game[i+1][j+1];
                if ((game[i][j] == 1 && neighAlive < 2) || (game[i][j] == 1 && neighAlive > 3))
                {
                    gameTemp[i][j] = 0;
                }
                else if (game[i][j] == 0 && neighAlive == 3)
                {
                    gameTemp[i][j] = 1;
                }

            }
            //cout << endl;
        }
        for (int k=0; k<=size-1; k++)
        {
            for (int l=0; l<=size-1; l++)
            {
                game[k][l] = gameTemp[k][l];
                cout << game[k][l] << " ";
            }
            cout << endl;
        }
        //Sleep(1000);
        //system("pause");
        //system("CLS");
        cin.ignore(1024, '\n');
        cout << "Press enter to continue...";
        cin.get();
    }



    return 0;
}
                    break;
                     go=1;

Your go is never reachable and it will loop forever. Place it before break.

I see a logic flaw here

        if ((game[i][j] == 1 && neighAlive < 2) || (game[i][j] == 1 && neighAlive > 3))
        {
            gameTemp[i][j] = 0;
        }
        else if (game[i][j] == 0 && neighAlive == 3)
        {
            gameTemp[i][j] = 1;
        }

What would gameTemp[i][j] be initialized to when none of conditions in if statements hold?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM