wrong output
Mar 18, 2015 at 8:26am UTC
so im writing a game of life code that will make a glider gun pattern but i have to use the fstream to get the values from another file. this is what i have so far but its not working is my fstream code wrong or maybe i dont have the file in the right location.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
#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[][COLS], int h, int w)
{
ofstream outFile("GliderGun.txt" , ios::out);
int total = 0;
for (int i = 0; i < h; i++)
{
for (int j = 0; j < w; 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 < h; i++)
{
for (int j = 0; j < w; 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 << "\n" << fillPercentage << " cycle #" << i << " Author: " << endl;
cout << endl;
Display(grid);
liveOrDie(grid);
Sleep(1000);
}
system("PAUSE" );
return 0;
}
Mar 18, 2015 at 10:33am UTC
I don't see your program read from a file anywhere. In savePattern you have code to write to a file but nothing that reads.
Topic archived. No new replies allowed.