-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtable.cpp
More file actions
80 lines (68 loc) · 1.83 KB
/
table.cpp
File metadata and controls
80 lines (68 loc) · 1.83 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
#include "table.h"
int table::sum_sides(int const position)
{
int l_side = position % width;
int r_side = (position + 1) % width;
bool l_value = l_side == 0 ? data[position + width - 1] : data[position - 1];
bool r_value = r_side == 0 ? data[position - width + 1] : data[position + 1];
return l_value+r_value;
}
int table::sum_up(int const position)
{
int new_pos = position < width ? (width - 1) * height + position : position-width;
return data[new_pos] + sum_sides(new_pos);
}
int table::sum_down(int const position)
{
int new_pos = position+width <height*width ? position+width : position%height;
return data[new_pos] + sum_sides(new_pos);
}
int table::get_alive(int const& position)
{
return sum_up(position)+sum_down(position)+sum_sides(position);
}
void table::write_table_out(std::ofstream& handler)
{
for (int i = 0; i < height; ++i)
{
for (int j = 0; j < width; ++j)
{
handler << data[i * height + j]<<' ';
}
handler << std::endl;
}
handler << std::endl;
}
/*
void table::write_table_out_console()
{
for (int i = 0; i < height; ++i)
{
for (int j = 0; j < width; ++j)
{
std::cout << data[i * height + j]<<' ';
}
std::cout << std::endl;
}
std::cout << std::endl;
}
*/
void table::add_data(int const& pos,bool const& value)
{
data[pos] = value;
}
void table::do_game()
{
std::vector<bool> res(height*width);
for (int i = 0; i < height; ++i)
{
for (int j = 0; j < width; ++j)
{
int pos = i * height + j;
bool isalive = data[pos];
int sum = get_alive(pos);
res[pos] = (sum == 3) || (isalive && sum == 2);
}
}
data = res;
}