我正在完成康威生命遊戲的兩部分任務的後半部分。我創建了一個函數來生成1和0的隨機數組; 1代表活細胞,零代表空白空間。我創建了一個單獨的函數來檢查鄰域,並計算確定遊戲的進展情況。規則:如果一個細胞有2或3個鄰居,它就會生存下來,超過3個或少於2個就會死亡,如果一個空間有3個鄰居,它就是「天生的」。我甚至從你們那裏得到了使用模量來包裹屏幕的幫助,但是我無法導入一個.txt文件來完成第二部分。以下是第一部分的代碼:從.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是瘸子自動對焦,但它是由我的恐龍的-A-教授授權。
我的問題是,對於第二部分,他希望我們從他提供的名爲GliderGun.txt的文件中爲最初一代流傳輸座標。我正在使用Xcode和Im 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)定義。我不需要控制檯打印文件,我只需要它在列出的座標中寫入1即可。感謝您的任何建議!
謝謝! Number 1很容易,但是您對如何逐行讀取文件以及如何從文件的其餘部分分離第一行的輸入有什麼建議嗎?我應該使用什麼機制?我知道一些關於循環和函數的知識,但是從來沒有從文件中讀取數據。 –
只做了一個編輯來演示可用於此的邏輯。這與你所擁有的非常相似,在開始處理剩下的事情之前,你只需要用第一個數字做一些事情。 –