生活中的游戏问题,C++

Game of life problems, C++

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

我的人生游戏任务有问题。有两件事不能正常工作:

  1. 游戏运行了太多代(是输入数字的两倍)
  2. 它没有正确更新生成:预定义状态2是一个静态状态。然而,这也会消亡

我反复检查了我的生死状况,并将其与互联网上的许多例子进行了比较,但我什么都找不到。非常感谢您的任何评论或意见!:)

这是代码:

functions.cp:

#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;
}

函数.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

在4.cpp中,"主要"

#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;

你的围棋永远无法到达,它将永远循环。把它放在休息前。

我在中看到一个逻辑缺陷

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

if语句中的任何条件都不成立时,gameTemp[i][j]将被初始化为什么?