-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameOfLife.h
96 lines (77 loc) · 2.1 KB
/
GameOfLife.h
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
#ifndef TOPCODER_GAMEOFLIFE_H
#define TOPCODER_GAMEOFLIFE_H
#include <string>
#include <vector>
class GameOfLife
{
public:
int alive(std::vector<std::string> start, std::string rules, int generations);
private:
int getNumAdj(std::vector<std::string> start, int x, int y);
};
int GameOfLife::alive(std::vector<std::string> start, std::string rules, int generations)
{
std::vector<std::string> grid(start);
for (int gen = 0; gen < generations; ++gen)
{
std::vector<std::string> workgrid(grid);
for (int i = 0; i < grid.size(); ++i)
{
for (int j = 0; j < grid[i].size(); ++j)
{
int cnt = getNumAdj(grid, i, j);
switch (rules[cnt])
{
case 'D':
workgrid[i][j] = '.';
break;
case 'S':
workgrid[i][j] = grid[i][j];
break;
case 'B':
workgrid[i][j] = 'X';
break;
}
}
}
grid = workgrid;
}
int cntSurvived = 0;
for (auto a : grid)
{
for (auto c : a)
{
if ('X' == c)
{
++cntSurvived;
}
}
}
return cntSurvived;
}
inline int GameOfLife::getNumAdj(std::vector<std::string> start, int x, int y)
{
int count = 0;
for (int i = x - 1; i < x + 2; ++i)
for (int j = y - 1; j < y + 2; ++j)
{
int procX = i;
int procY = j;
if (procX < 0)
procX = start.size() - 1;
if (procX > start.size() - 1)
procX = 0;
if (procY < 0)
procY = start[procX].size() - 1;
if (procY > start[procX].size() - 1)
procY = 0;
if ((procX != x || procY != y) &&
(start[procX][procY] == 'X'))
{
++count;
}
start[procX][procY] = '.';
}
return count;
}
#endif TOPCODER_GAMEOFLIFE_H