Conway's Game of Life, using file stream/arrays--not working properly?

EDIT: I fixed it. Nevermind.

So, I have an assignment to do that involves arrays and the Conway's Game of Life. We were advised to use file streams for this assignment. However, something in my program isn't working right...I have a section in my code in one function where it checks the cells surrounding the array and determines whether the cell lives or dies.

The giant underscore in my code is where it seems to have a problem. The couts in the next few if-statements are just there to see which one is causing the problem.

An image to the output can be found below. The first block is just the text from my assignment7.txt file, and the second block is the result after going through. (Asterisks are living cells, dashes are dead cells.)

http://i34.tinypic.com/n478r4.png


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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include <iostream>
#include <fstream>
#include <graphics.h>

using namespace std;

// 22 rows in the program
const int ROW = 22;
// Added one to COLUMN to include '\n'
const int COLUMN = 51;
char world[ROW][COLUMN];
char world2[ROW][COLUMN];


// Function prototypes
void nextGeneration();
void display();

int main()
{
    // Initializes the ifstream object (fin for "file input")
    ifstream fin;
    fin.open("assignment7.txt");

    // Initializes 2-D arrays
    char reading;
    int one, two;
    char dummy;

    dummy = 'n';

    // If the file fails to open
    if (fin.fail())
    {
        cout << "Failed to open assignment7.txt. The program will end." << "\n";
        exit (1);
    }

    cout << "Press 'n' after every generation is displayed for the next generation." << "\n";
    cout << "Press any other key to quit." << "\n";

    // Loops through .get to read the characters into arrays
    for (one = 0; one < ROW; one++)
    {
        for (two = 0; two < COLUMN; two++)
        {
            fin.get(reading);
            if (reading == '\n')
            {
                // Makes it so it prints out a new line and reading does not set it as an array element
                cout << "\n";
            }
            else
            {
                world[one][two] = reading;
                cout << world[one][two];
            }
        }
    }

    fin.close();
    cin >> dummy;

    while ((dummy == 'n') || (dummy == 'N'))
    {
        nextGeneration();
        display();
        cin >> dummy;
    }
    return 0;
}



// Displays the next generation
void display()
{
    int onedisp, twodisp;
    for (onedisp = 0; onedisp < ROW; onedisp++)
    {
        for (twodisp = 0; twodisp < COLUMN; twodisp++)
        {
            cout << world[onedisp][twodisp];
            // Prints out a new line when twodisp has increased to COLUMN - 1
            if (twodisp == (COLUMN - 1))
            {
                cout << "\n";
            }
        }
    }
}



// Decides which cells live or die
void nextGeneration()
{
    int onefunc, twofunc;
    int count;
    int test;
    for (onefunc = 0; onefunc < ROW; onefunc++)
    {
        for (twofunc = 0; twofunc < COLUMN; twofunc++)
        {
            count = 0;
            // Counts to see if there are any other asterisks around the cell
            if ((world[onefunc - 1][twofunc]) == '*')
            {
                count = count + 1;
            }
            else if ((world[onefunc + 1][twofunc]) == '*')
            {
                count = count + 1;
            }
            else if ((world[onefunc - 1][twofunc - 1]) == '*')
            {
                count = count + 1;
            }
            else if ((world[onefunc + 1][twofunc + 1]) == '*')
            {
                count = count + 1;
            }
            else if ((world[onefunc][twofunc - 1]) == '*')
            {
                count = count + 1;
            }
            else if ((world[onefunc][twofunc + 1]) == '*')
            {
                count = count + 1;
            }
            else if ((world[onefunc - 1][twofunc + 1]) == '*')
            {
                count = count + 1;
            }
            else if ((world[onefunc + 1][twofunc - 1]) == '*')
            {
                count = count + 1;
            }


            // Dies due to underpopulation
            // ______________________________________________________________
            if ((world[onefunc][twofunc]) == '*' && (count < 2))
            {
                world[onefunc][twofunc] = '-';
                cout << "h" << "\n";
            }
            // Dies due to overcrowding
             else if ((world[onefunc][twofunc]) == '*' && (count > 3))
             {
                world[onefunc][twofunc] = '-';
                cout << "i" << "\n";
             }
             // If the cell is alive and has two or three neighbors, it remains living
             else if ((world[onefunc][twofunc]) == '*' && count == 2 || count == 3)
             {
                world[onefunc][twofunc] = '*';
                cout << "j" << "\n";
             }
             // If the cell is dead and if there are EXACTLY three cells surrounding it, it is born
             else if ((world[onefunc][twofunc]) == '-' && count == 3)
             {
                world[onefunc][twofunc] = '*';
                cout << "j" << "\n";
             }
        }
    }
}
Last edited on
Topic archived. No new replies allowed.