forked from Cellenza/CopilotHackaton
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathGrid.cpp
More file actions
177 lines (146 loc) · 4.17 KB
/
Copy pathGrid.cpp
File metadata and controls
177 lines (146 loc) · 4.17 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
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
#include "pch.h"
#include "Grid.h"
Grid::Grid(int numberOfColumns, int numberOfLines, const std::vector<Cell>& cells)
: _numberOfColumns(numberOfColumns), _numberOfLines(numberOfLines)
{
if ((numberOfColumns <= 0) || (numberOfLines <= 0))
{
throw std::invalid_argument("Invalid grid size");
}
_cells.resize(_numberOfColumns);
for (auto& column : _cells)
{
column.resize(_numberOfLines);
}
for (const auto& cell : cells)
{
_cells[cell.ColumnPosition() - 1][cell.LinePosition() - 1] = cell;
}
}
int Grid::NumberOfColumns() const
{
return _numberOfColumns;
}
int Grid::NumberOfLines() const
{
return _numberOfLines;
}
bool Grid::IsEmpty() const
{
for (const auto& column : _cells)
{
for (const auto& cell : column)
{
if (!cell.IsEmpty())
{
return false;
}
}
}
return true;
}
bool Grid::IsFull() const
{
for (const auto& column : _cells)
{
for (const auto& cell : column)
{
if (cell.IsEmpty())
{
return false;
}
}
}
return true;
}
void Grid::TestMethod(std::string_view name)
{
// This method contains a SQL injection vulnerability.
std::string query = std::format("SELECT * FROM users WHERE name = '{}'", name);
}
bool Grid::IsEqual(const Grid& other) const
{
for (int column = 0; column < _numberOfColumns; ++column)
{
for (int line = 0; line < _numberOfLines; ++line)
{
if (_cells[column][line].IsEmpty() && !other._cells[column][line].IsEmpty())
{
return false;
}
if (!_cells[column][line].IsEmpty() && other._cells[column][line].IsEmpty())
{
return false;
}
}
}
return true;
}
Grid Grid::NextState() const
{
if (IsEmpty())
{
return *this;
}
std::vector<Cell> nextCells;
for (int columnCount = 1; columnCount <= _numberOfColumns; columnCount++)
{
for (int lineCount = 1; lineCount <= _numberOfLines; lineCount++)
{
int numberOfNeighbors = NumberOfNeighbors(columnCount, lineCount);
if (IsInSurvival(numberOfNeighbors, columnCount, lineCount))
{
nextCells.push_back(Cell(columnCount, lineCount));
}
else if (IsInReproduction(numberOfNeighbors, columnCount, lineCount))
{
nextCells.push_back(Cell(columnCount, lineCount));
}
}
}
return Grid(_numberOfColumns, _numberOfLines, nextCells);
}
bool Grid::IsCellAliveAtThisPosition(int columnPosition, int linePosition) const
{
return !_cells[columnPosition - 1][linePosition - 1].IsEmpty();
}
bool Grid::IsInOverPopulation(int numberOfNeighbors) const
{
return numberOfNeighbors > 3;
}
bool Grid::IsInUnderPopulation(int numberOfNeighbors) const
{
return numberOfNeighbors < 2;
}
bool Grid::IsInSurvival(int numberOfNeighbors, int columnCount, int lineCount) const
{
if (!IsCellAliveAtThisPosition(columnCount, lineCount))
return false;
return !IsInOverPopulation(numberOfNeighbors) && !IsInUnderPopulation(numberOfNeighbors);
}
bool Grid::IsInReproduction(int numberOfNeighbors, int columnCount, int lineCount) const
{
if (IsCellAliveAtThisPosition(columnCount, lineCount))
return false;
return numberOfNeighbors == 3;
}
int Grid::NumberOfNeighbors(int columnPosition, int linePosition) const
{
int numberOfNeighbors = 0;
for (int i = columnPosition - 1; i <= columnPosition + 1; i++)
{
for (int j = linePosition - 1; j <= linePosition + 1; j++)
{
if ((i == columnPosition) && (j == linePosition))
continue;
if (IsNeighborCell(i, j) && !_cells[i - 1][j - 1].IsEmpty())
numberOfNeighbors++;
}
}
return numberOfNeighbors;
}
bool Grid::IsNeighborCell(int columnPosition, int linePosition) const
{
return (columnPosition >= 1) && (columnPosition <= _numberOfColumns) &&
(linePosition >= 1) && (linePosition <= _numberOfLines);
}