i need to use fstream to read the file glidergunt.txt and make a glider gun game
ID: 645217 • Letter: I
Question
i need to use fstream to read the file glidergunt.txt and make a glider gun game of life. the values for the glidergun.txt are
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
the code for the game of life is
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <fstream>
#include <windows.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#define ALIVE true
#define DEAD false
using namespace std;
const int ROWS = 24;
const int COLS = 79;
void Display(bool grid[ROWS][COLS])
{
for(int a = 1; a < ROWS; a++){
for(int b = 1; b < COLS; b++){
if(grid[a][b] == true){
cout << "Q";
}
else{
cout << " ";
}
}
}
}
void CopyGrid (bool grid[ROWS][COLS],bool grid2[ROWS][COLS]){
for(int a =0; a < ROWS; a++){
for(int b = 0; b < COLS; b++){grid2[a][b] = grid[a][b];}
}
}
void savePattern( int array1[ROWS][COLS], int ROWS, int COLS)
{
ofstream outFile("GliderGun.txt", ios::out);
int total = 0;
for (int i = 0; i < ROWS; i++)
{
for (int j = 0; j < COLS; j++)
{
total += array1[i][j]; // count the number of live cells
}
}
outFile << total << endl; // output the count of point coordinates
for (int i = 0; i < ROWS; i++)
{
for (int j = 0; j < COLS; j++)
{
if (array1[i][j]) outFile << i << ' ' << j << endl; // output the coordinates in pairs
}
}
}
void liveOrDie(bool grid[ROWS][COLS])
{
bool grid2[ROWS][COLS] = {};
CopyGrid(grid, grid2);
for(int a = 1; a < ROWS-1; a++)
{
for(int b = 1; b < COLS-1; b++)
{
int neighbors = 0;
for(int c = -1; c < 2; c++)
{
for(int d = -1; d < 2; d++)
{
if(!(c == 0 && d == 0))
{
if(grid2[a+c][b+d]) {++neighbors;}
}
}
}
if(neighbors < 2) {grid[a][b] = DEAD;}
else if(neighbors == 3) {grid[a][b] = ALIVE;}
else if(neighbors > 3) {grid[a][b] = DEAD;}
}
}
}
int main()
{
int array [ROWS][COLS];
int n = sizeof(array) / sizeof(array[0]);
double fillPercentage;
fillPercentage = n;
bool grid[ROWS][COLS] = {};
grid[ROWS/2][COLS/2] = true;
grid[ROWS/2-1][COLS/2] = true;
grid[ROWS/2][COLS/2+1] = true;
grid[ROWS/2][COLS/2-1] = true;
grid[ROWS/2+1][COLS/2+1] = true;
for (int i = 1; i <= 50; ++i)
{
cout << " " << fillPercentage << " cycle #" << i << " Author: " << endl;
cout << endl;
Display(grid);
liveOrDie(grid);
Sleep(1000);
}
system("PAUSE");
return 0;
}
Explanation / Answer
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <fstream>
#include <windows.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#define ALIVE true
#define DEAD false
using namespace std;
const int ROWS = 24;
const int COLS = 79;
void Display(bool grid[ROWS][COLS])
{
for(int a = 1; a < ROWS; a++){
for(int b = 1; b < COLS; b++){
if(grid[a][b] == true){
cout << "Q";
}
else{
cout << " ";
}
}
}
}
void CopyGrid (bool grid[ROWS][COLS],bool grid2[ROWS][COLS]){
for(int a =0; a < ROWS; a++){
for(int b = 0; b < COLS; b++){grid2[a][b] = grid[a][b];}
}
}
void savePattern( int array1[ROWS][COLS], int ROWS, int COLS)
{
ofstream outFile("GliderGun.txt", ios::out);
int total = 0;
for (int i = 0; i < ROWS; i++)
{
for (int j = 0; j < COLS; j++)
{
total += array1[i][j]; // count the number of live cells
}
}
outFile << total << endl; // output the count of point coordinates
for (int i = 0; i < ROWS; i++)
{
for (int j = 0; j < COLS; j++)
{
if (array1[i][j]) outFile << i << ' ' << j << endl; // output the coordinates in pairs
}
}
}
void liveOrDie(bool grid[ROWS][COLS])
{
bool grid2[ROWS][COLS] = {};
CopyGrid(grid, grid2);
for(int a = 1; a < ROWS-1; a++)
{
for(int b = 1; b < COLS-1; b++)
{
int neighbors = 0;
for(int c = -1; c < 2; c++)
{
for(int d = -1; d < 2; d++)
{
if(!(c == 0 && d == 0))
{
if(grid2[a+c][b+d]) {++neighbors;}
}
}
}
if(neighbors < 2) {grid[a][b] = DEAD;}
else if(neighbors == 3) {grid[a][b] = ALIVE;}
else if(neighbors > 3) {grid[a][b] = DEAD;}
}
}
}
int main()
{
int array [ROWS][COLS];
int n = sizeof(array) / sizeof(array[0]);
double fillPercentage;
fillPercentage = n;
bool grid[ROWS][COLS] = {};
ifstream in;
in.open("glidergun.txt");
int num, i1, i2;
if(in.is_open()){
in >> num;
while(in >> i1){
in >> i2;
grid[i1][i2] = true;
}
}
else{
cout << "Can not find glidergun.txt in your directory" << endl;
return 0;
}
grid[ROWS/2][COLS/2] = true;
grid[ROWS/2-1][COLS/2] = true;
grid[ROWS/2][COLS/2+1] = true;
grid[ROWS/2][COLS/2-1] = true;
grid[ROWS/2+1][COLS/2+1] = true;
for (int i = 1; i <= 50; ++i)
{
cout << " " << fillPercentage << " cycle #" << i << " Author: " << endl;
cout << endl;
Display(grid);
liveOrDie(grid);
Sleep(1000);
}
system("PAUSE");
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.