John Conway’s Game of Life – Windows/C++ (ASCII)

This is a simple version of John Conway’s Game of Life written in C/C++ and runs on Windows using ASCII graphics.

For a more interesting version using SDL to draw pixel graphics, check out this link: John Conway’s Game of Life – C++/SDL
John Conway's Game of Life
Here is the code for the ASCII Version:

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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#include "math.h"
#include <iostream>
#include <stdlib.h>
#include <windows.h>
#include <time.h>
 
void init();
void draw();
void process();
void processMutate(int x, int y);
int getNumNeigbors(int x, int y);
void clrscr();
void flip();
char getLeft(int x, int y);
char getRight(int x, int y);
char getUp(int x, int y);
char getDown(int x, int y);
char getUpLeft(int x, int y);
char getUpRight(int x, int y);
char getDownLeft(int x, int y);
char getDownRight(int x, int y);
void sleep(unsigned int mseconds);
 
char BLOCKS[] = {' ', '*'};
int NUM_BLOCKS = 2;
char LIVE = BLOCKS[1];
char DEAD = BLOCKS[0];
 
int GRID_WIDTH = 70;
int GRID_HEIGHT = 22;
char** grid;
char** gridTmp;
 
int main() {
    init();
    bool running = true;
    while(running) {
        draw();
        sleep(500);
        process();
    }
}
 
 
void init() {
    srand(time(0));
    grid = new char*[GRID_WIDTH];
    gridTmp = new char*[GRID_WIDTH];
    for(int x = 0; x < GRID_WIDTH; ++x) {
         grid[x] = new char[GRID_HEIGHT];
         gridTmp[x] = new char[GRID_HEIGHT];
         for(int y = 0; y < GRID_HEIGHT; ++y) {
              grid[x][y] = BLOCKS[rand() % NUM_BLOCKS];
              gridTmp[x][y] = ' ';
         }
    }
}
 
void draw() {
    clrscr();
    for(int y = 0; y < GRID_HEIGHT; ++y) {
        for(int x = 0; x < GRID_WIDTH; ++x) {
            std::cout << grid[x][y];
        }
        std::cout << std::endl;
    }
}
 
void process() {
    for(int y = 0; y < GRID_HEIGHT; ++y) {
        for(int x = 0; x < GRID_WIDTH; ++x) {
            int neighbors = getNumNeigbors(x, y);
            if(grid[x][y] == LIVE) {
                //1. Any live cell with fewer than two live neighbors dies, as if caused by underpopulation.
                //2. Any live cell with more than three live neighbors dies, as if by overcrowding.
                if(neighbors < 2 || neighbors > 3) {
                    gridTmp[x][y] = DEAD;
                } else {
                    gridTmp[x][y] = LIVE;
                }
                //3. Any live cell with two or three live neighbors lives on to the next generation.
 
            } else {
                //4. Any dead cell with exactly three live neighbors becomes a live cell.
                if(neighbors == 3) {
                    gridTmp[x][y] = LIVE;
                } else {
                    gridTmp[x][y] = DEAD;
                }
            }
          //  processMutate(x, y);
        }
    }
    flip();
}
 
 
int getNumNeigbors(int x, int y) {
    int i = 0;
    if(getLeft(x, y) == LIVE) {
        i++;
    }
    if(getRight(x, y) == LIVE) {
        i++;
    }
    if(getUp(x, y) == LIVE) {
        i++;
    }
    if(getDown(x, y) == LIVE) {
        i++;
    }
    if(getUpLeft(x, y) == LIVE) {
        i++;
    }
    if(getUpRight(x, y) == LIVE) {
        i++;
    }
    if(getDownLeft(x, y) == LIVE) {
        i++;
    }
    if(getDownRight(x, y) == LIVE) {
        i++;
    }
    return i;
}
 
 
void processMutate(int x, int y) {
    if(rand() % 1000 > 997) {
        if(gridTmp[x][y] == DEAD) {
            gridTmp[x][y] = LIVE;
        }
    }
}
 
void flip() {
    char** tmp = grid;
    grid = gridTmp;
    gridTmp = tmp;
 
}
 
 
char getLeft(int x, int y) {
    if(x == 0) {
        return DEAD;
    }
    return grid[x - 1][y];
}
 
char getRight(int x, int y) {
    if(x == GRID_WIDTH - 1) {
        return DEAD;
    }
    return grid[x + 1][y];
}
 
char getUp(int x, int y) {
    if(y == 0) {
        return DEAD;
    }
    return grid[x][y - 1];
}
 
char getDown(int x, int y) {
    if(y == GRID_HEIGHT - 1) {
        return DEAD;
    }
    return grid[x][y + 1];
}
 
char getUpLeft(int x, int y) {
    if(x == 0 || y == 0) {
        return DEAD;
    }
    return grid[x - 1][y - 1];
}
 
char getUpRight(int x, int y) {
    if(x == GRID_WIDTH - 1 || y == 0) {
        return DEAD;
    }
    return grid[x + 1][y - 1];
}
 
char getDownLeft(int x, int y) {
    if(y == GRID_HEIGHT - 1 || x == 0) {
        return DEAD;
    }
    return grid[x - 1][y + 1];
}
 
char getDownRight(int x, int y) {
    if(y == GRID_HEIGHT - 1 || x == GRID_WIDTH - 1 ) {
        return DEAD;
    }
    return grid[x + 1][y + 1];
}
 
void clrscr() {
   COORD topLeft = {0, 0};
   DWORD cCharsWritten, dwConSize;
   CONSOLE_SCREEN_BUFFER_INFO cInfo;
              HANDLE hConsoleOutput = GetStdHandle((DWORD)-11);
 
   // Get the number of character cells in the current buffer
   GetConsoleScreenBufferInfo(hConsoleOutput, &cInfo);
   dwConSize = cInfo.dwSize.X * cInfo.dwSize.Y;
 
   // Fill the whole screen with blank chars
  // FillConsoleOutputCharacter(hConsoleOutput, (TCHAR)' ', dwConSize, topLeft, &cCharsWritten);
 
   // Get the current text attribute
   GetConsoleScreenBufferInfo(hConsoleOutput, &cInfo);
 
   // Set the buffer's attributes accordingly
   FillConsoleOutputAttribute(hConsoleOutput, cInfo.wAttributes, dwConSize, topLeft, &cCharsWritten);
 
   // Put the cursor at its home coordinates
   SetConsoleCursorPosition(hConsoleOutput, topLeft);
}
 
 
void sleep(unsigned int mseconds) {
    clock_t goal = mseconds + clock();
    while (goal > clock());
}
You can leave a response, or trackback from your own site.

Leave a Reply

Powered by WordPress | Designed by: WordPress Themes | Thanks to best wordpress themes, Find WordPress Themes and Themes Directory