我正在完成Conway的生命游戏的两部分任务的后半部分。我创建了一个函数来生成1和0的随机数组; 1代表一个活细胞,零代表一个空的空间。我创建了一个单独的功能来检查邻域并进行计数以确定游戏的进展情况。规则:如果一个单元有2个或3个邻居它存活,超过3个或少于2它死亡,如果一个空的空间有3个邻居则它是“天生的”。我甚至得到了你们的帮助,使用模数包装屏幕,但我无法导入.txt文件来完成第二部分。以下是第一部分的代码:
#include <iostream> //includes input-output stream
#include <time.h> //includes time function
#include <iomanip> //includes setprecision function
#include <unistd.h> //includes sleep function
#include <fstream> //includes ifstream function
using namespace std; //using standard library
int master[24][79]; //initializes primary data array
int h = 24; // initializes height variable
int w = 79; // initialises width variable
int noOfCycles; //initialize cycles variable
void gen0 (int master[24][79]); // creates initial generation
void life(int master[24][79]); //initializes life function
void copy(int arrayX[24][79], int arrayY[24][79]); //initializes cycle update function
void print(int master[24][79]); //initializes print function
void fillPercent (int master[24][79]); //initializes percentage calculating function
int main() //initialize main function
{
cout << "How many cycles would you like to run?"; //prompt user to input cycles
cin >> noOfCycles; //user inputs cycles
srand (time(0)); //creates initial randomness
gen0(master); //creates initial generation
for (int k = 0; k <= noOfCycles; k++) //prints gen0 and cycles 50 times
{
print(master); //prints current array
fillPercent(master); //calculates/prints fill percentage
cout << " Cycle #" << k << " Author: Mikhail Morgan" << endl << endl;
//prints cycle number and signature
life(master); //calls life function
sleep(1); //delays output by 1 second
} //end width loop
} //end main function
void gen0 (int master[24][79])
{
for(int j = 0; j < h; j++) //height loop
{
for (int i = 0; i < w; i++) //width loop
master[j][i] = rand() % 2; //creates random generation 0
} //end height loop
}
void print(int master[24][79]) //Prints array
{
for(int j = 0; j < h; j++) //height loop
{
for(int i = 0; i < w; i++) //width loop
{
if(master[j][i] == 1)
cout << '0'; //print living cells as zeros
else
cout << ' '; //print dead cells as spaces
} // end width loop
cout << endl;
} // end height loop
} //end print function
void fillPercent (int master[24][79]) // calculates fill percentage
{
double fillNumber = 0; //resets every cycle
for (int i = 0; i < h; i++ ) //width loop
{
for (int j = 0; j < w; j++ ) //height loop
{
fillNumber += master[i][j]; //increments fill number
} //end height loop
} //end width loop
cout << endl << fixed << setprecision(2) << (fillNumber/(w*h))*100; //print percentage
} //end fillPercent function
void life (int master[24][79]) //generates/kills cells based on neighborhood
{
int temp[24][79]; //temporary array for manipulating data
copy (master, temp); //copy array onto temp
for(int j = 0; j < h; j++) //height loop
{
for (int i = 0; i < w; i++) //width loop
{
int count = 0; //intialize neighbor count variable
count = master[(j-1+h) % h][i % w] + //searches down
master[(j-1+h) % h][(i-1+w) % w] + //down left
master[j % h][(i-1+w) % w] + //left
master[(j+1+h) % h][(i-1+w) % w] + //up left
master[(j+1+h) % h][i % w] + //up
master[(j+1+h) % h][(i+1+w) % w] + //up right
master[j % h][(i+1+w) % w] + //right
master[(j-1+h) % h][(i+1+w) % w]; //down right
//cell dies if count falls below 2 or rises above 3
if(count < 2 || count > 3)
temp[j][i] = 0;
//cell stays alive if it has two neighbors
if(count == 2)
temp[j][i] = master[j][i];
//cell either stays alive or gets born if three neighbors
if(count == 3)
temp[j][i] = 1;
} //end width loop
}//end height loop
copy(temp, master); //copy temp back to main array
} //end life function
void copy(int arrayX[24][79], int arrayY[24][79]) //Copy machine
{
for(int j = 0; j < h; j++) //height loop
{
for(int i = 0; i < w; i++) //width loop
arrayY[j][i] = arrayX[j][i]; //temporary arrays used for copying
} //end height loop
} //end copy function
我知道,使用命名空间std是蹩脚的af,但它是由我的恐龙教授强制执行的。
我的问题是,对于第二部分,他希望我们从他提供的名为GliderGun.txt的文件中流式传输初始生成的坐标。我正在使用Xcode和我99%确定我的文件位于正确的位置:我可以在与main.cpp相同的文件夹内的右侧菜单中看到它,我可以在Finder菜单旁边看到原始副本main.cpp中。第一行不是一个坐标对,它是文件中坐标的总数......我不确定它的目的是什么,我怀疑它是什么搞砸了我。这是文件本身的文本:
36
1 25
2 23
2 25
3 13
3 14
3 21
3 22
3 35
3 36
4 12
4 16
4 21
4 22
4 35
4 36
5 1
5 2
5 11
5 17
5 21
5 22
6 1
6 2
6 11
6 15
6 17
6 18
6 23
6 25
7 11
7 17
7 25
8 12
8 16
9 13
9 14
以下是替换gen0的函数的代码。我所做的就是用getPattern替换gen0的调用,并将定义改为:
void getPattern (int master[24][79]) //generates Glider Gun
{
ifstream infile("GliderGun.txt", ios::in);
infile.open("GliderGun.txt", ios::in);//opens .txt file
int numOfCoordinates; // number of coordinate pairs
int i, j; // x, y coordinates
infile >> numOfCoordinates;
for (int a = 0; a < numOfCoordinates; a++)
{
infile >> i >> j;
master[j][i] = 1;
}
infile.close(); // closes .txt file
}
控制台生成一个空白的24x79阵列。我觉得我有一个循环问题,但我不知道ifstream如何解决它。坐标列为(x y)或由我的其他循环(j i)定义。我不需要控制台来打印文件我只需要在列出的坐标中写1s。谢谢你的建议!
答案 0 :(得分:0)
仅仅是为了尝试推理您的代码,有两个问题是由getPattern
替代gen0
所引起的:{/ 1}}:
您可以通过两个简单的更改来解决此问题。
与您的代码无关,文本文件不需要与源文件位于同一目录中,它需要位于执行程序的同一目录中。我不知道在XCode中执行的位置,但如果您知道如何,可能需要直接从您的计算机外壳进行测试。
修改强>
从文件中读取的getPattern部分可能如下所示:
int num_coordinates; // This will be the number of coordinate pairs your file says it has.
int i, j; // These will be your x, y coordinates.
infile >> num_coordinates;
for (int loop_ct = 0; loop_ct < num_coordinates; loop_ct++) {
infile >> i >> j;
master[j][i] = 1;
}