我的人生游戏任务有问题。有两件事不能正常工作:
- 游戏运行了太多代(输入数字的两倍)
- 它没有正确更新世代:预定状态 2 是静态状态。然而,这也会消亡。
我检查并重新检查了我的生死状况,并将它们与互联网上的大量示例进行了比较,但我找不到任何东西。任何意见或指针都非常感谢!:)
这是代码:
函数.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;
}
函数.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,“主要”
#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;
}