Conway의 Game of Life에서 두 부분으로 나눠서 과제의 후반부를 끝내고 있습니다. 나는 1과 0의 무작위 배열을 생성하는 함수를 만들었습니다. 1은 살아있는 세포를 나타내고 0은 빈 공간을 나타냅니다. 이웃을 조사하고 게임이 어떻게 진행되는지 결정하기 위해 별도의 기능을 만들었습니다. 규칙 : 세포가 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
내가 알고, 네임 스페이스 표준을 사용하는 절름발이 AF이지만이 내 공룡 - 중 - - 교수에 의해 위임합니다.
내 문제는 두 번째 부분에 대해 그는 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은 쉽지만 한 줄씩 파일을 읽는 방법에 대한 충고와 나머지 파일의 첫 줄에서 입력을 분리하는 방법에 대한 조언이 있습니까? 어떤 메커니즘을 사용해야합니까? 나는 루프와 함수에 관해 좋은 점을 알고 있지만, 파일에서 데이터를 읽은 적이 한번도 없다. –
그냥 이것을 사용하여 당신이 사용할 수있는 논리를 보여줄 편집을했다. 그것은 당신이 가진 것과 매우 흡사합니다. 나머지를 처리하기 전에 첫 번째 숫자로 무언가를해야합니다. –