This repository was archived by the owner on Oct 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameOfLifeMap.cpp
More file actions
110 lines (98 loc) · 2.52 KB
/
GameOfLifeMap.cpp
File metadata and controls
110 lines (98 loc) · 2.52 KB
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
// Lab 11b, Apply The STL map Template To The Classic Game Of Life Simulation
// Programmer: Minos Park
// Editor(s) used: Sublime Text 2
// Compiler(s) used: G++
#include <iostream>
#include <map>
using namespace std;
#include <cstdlib>
struct cell
{
int row;
int col;
bool operator==(const cell& c) const {return row == c.row && col == c.col;}
bool operator<(const cell& c)const{return(1000*row +col)<(1000*c.row+c.col);}
};
map<cell, int> grid, newGrid;
const int MINROW = -25;
const int MAXROW = 25;
const int MINCOL = -35;
const int MAXCOL = 35;
cell temp;
int counter(int row, int col)
{
cell tempo;
int count = 0;
for (tempo.row = row - 1; tempo.row <= row + 1; tempo.row++)
for (tempo.col = col - 1; tempo.col <= col + 1; tempo.col++)
if (tempo.row != row || tempo.col != col)
if (grid.find(tempo) != grid.end())
++count;
return count;
}
void initialize()
{
cout << "Sequence of pairs of coordinates, terminated with a pair of -1's" << endl;
char buf[100];
while (true)
{
cin >> buf; temp.row = atoi(buf);
cin >> buf; temp.col = atoi(buf);
if (temp.row == -1 && temp.col == -1) break;
grid[temp] = 'X';
}
cin.ignore();
}
void print()
{
cout << endl << "The Current Life Configuration" << endl;
for (temp.row = MINROW; temp.row <= MAXROW; temp.row++)
{
for (temp.col = MINCOL; temp.col <= MAXCOL; temp.col++)
if (grid.find(temp) != grid.end())
cout << "X";
else
cout << ' ';
cout << endl;
}
cout << endl;
}
void update()
{
newGrid.clear();
for (temp.row = MINROW; temp.row <= MAXROW; temp.row++)
{
for (temp.col = MINCOL; temp.col <= MAXCOL; temp.col++)
{
switch (counter(temp.row, temp.col))
{
case 2:
if (grid.find(temp) != grid.end()) newGrid[temp] = 'X';
break;
case 3:
newGrid[temp] = 'X';
break;
}
}
}
grid = newGrid;
}
int main()
{
// print my name and this assignment's title
cout << "Lab 11b, Apply The STL map Template To The Classic Game Of Life Simulation\n";
cout << "Programmer: Minos Park\n";
cout << "Editor(s) used: Sublime Text 2\n";
cout << "Compiler(s) used: G++\n";
cout << "File: " << __FILE__ << endl;
cout << "Complied: " << __DATE__ << " at " << __TIME__ << endl << endl;
initialize();
print();
for (int i = 1; grid.size(); i++)
{
cout << "Generation " << i << ". Press ENTER to continue, or \'q\' to quit...\n";
if (cin.get() == 'q') break;
update();
print();
}
}