/
Field.cpp
233 lines (210 loc) · 5.6 KB
/
Field.cpp
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
228
229
230
231
232
233
#include "stdafx.h"
#include "Field.h"
MyField::MyField(int rowN, int colN, int RectSizeN)
{
row = rowN;
col = colN;
RectSize = RectSizeN;
for (int i = 0; i < row*col; i++)
arrVector.push_back(0);
}
MyField::MyField()
{
row = 50;
col = 20;
RectSize = 20;
for (int i = 0; i < row*col; i++)
arrVector.push_back(0);
}
MyField::~MyField()
{
}
void MyField::Run()
{
std::vector<int> arrVectorTemp;
arrVectorTemp = CreateTemp(row, col, arrVectorTemp);
int a = 5;
for (int i = 0; i < row*col; i++)
{
int count = 0;
if (i - row - 1 >= 0 && i + row + 1 < row*col)
{
if (arrVector[i - row - 1] == 1) count++;
if (arrVector[i - row] == 1) count++;
if (arrVector[i - row + 1] == 1)count++;
if (arrVector[i - 1] == 1)count++;
if (arrVector[i + row - 1] == 1) count++;
if (arrVector[i + row] == 1) count++;
if (arrVector[i + row + 1] == 1) count++;
if (arrVector[i + 1] == 1) count++;
arrVectorTemp[i] = ChkRules(arrVector[i], count);
}
else if (i == 0)// ˳âà âåðõíÿ êë³òèíêà
{
if (arrVector[i + 1] == 1) count++;
if (arrVector[i + row] == 1)count++;
if (arrVector[i + row + 1] == 1)count++;
arrVectorTemp[i] = ChkRules(arrVector[i], count);
}
else if (i == row - 1)// Ïðàâà âåðõíÿ êë³òèíêà
{
if (arrVector[i - 1] == 1) count++;
if (arrVector[i + row] == 1)count++;
if (arrVector[i + row - 1] == 1)count++;
arrVectorTemp[i] = ChkRules(arrVector[i], count);
}
else if (i == row*col - row)// ˳âà íèæíÿ êë³òèíêà
{
if (arrVector[i - row] == 1) count++;
if (arrVector[i - row + 1] == 1)count++;
if (arrVector[i + 1] == 1)count++;
arrVectorTemp[i] = ChkRules(arrVector[i], count);
}
else if (i == row*col - 1)// Ïðàâà íèæíÿ êë³òèíêà
{
if (arrVector[i - 1] == 1) count++;
if (arrVector[i - row] == 1)count++;
if (arrVector[i - row - 1] == 1)count++;
arrVectorTemp[i] = ChkRules(arrVector[i], count);
}
else if (i > 0 && i < row - 1)//Âåðõí³é ðÿäîê
{
if (arrVector[i - 1] == 1) count++;
if (arrVector[i + 1] == 1) count++;
if (arrVector[i + row - 1] == 1)count++;
if (arrVector[i + row] == 1)count++;
if (arrVector[i + row + 1] == 1) count++;
arrVectorTemp[i] = ChkRules(arrVector[i], count);
}
else if (i >= row && i <= row*col - 2 * row && i % row == 0)//˳âèé ðÿäîê
{
if (arrVector[i - row] == 1) count++;
if (arrVector[i - row + 1] == 1) count++;
if (arrVector[i + 1] == 1)count++;
if (arrVector[i + row] == 1)count++;
if (arrVector[i + row + 1] == 1) count++;
arrVectorTemp[i] = ChkRules(arrVector[i], count);
}
else if (i >= (2 * row) - 1 && i <= (row*col - row - 1) && (((i + 1) % row) == 0))//Ïðàâèé ðÿäîê
{
if (arrVector[i - row] == 1) count++;
if (arrVector[i - row - 1] == 1) count++;
if (arrVector[i - 1] == 1)count++;
if (arrVector[i + row - 1] == 1)count++;
if (arrVector[i + row] == 1) count++;
arrVectorTemp[i] = ChkRules(arrVector[i], count);
}
else if (i > row*col - row)//Íèæí³é ðÿäîê
{
if (arrVector[i - 1] == 1) count++;
if (arrVector[i + 1] == 1) count++;
if (arrVector[i - row] == 1)count++;
if (arrVector[i - row - 1] == 1)count++;
if (arrVector[i - row + 1] == 1) count++;
arrVectorTemp[i] = ChkRules(arrVector[i], count);
}
}
for (int i = 0; i < row*col; i++)
arrVector[i] = arrVectorTemp[i];
}
void MyField::Clear()
{
for (std::vector<int>::iterator i = arrVector.begin(); i < arrVector.end(); i++)
*i = 0;
}
void MyField::DrawField(CPaintDC &dc)
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
SetDCPenColor(dc, RGB(0, 0, 0));
SetDCBrushColor(dc, RGB(255, 255, 255));
dc.Rectangle(i * RectSize, j * RectSize,
i * RectSize + RectSize, j * RectSize + RectSize);
if (arrVector[row*j + i] == 1)
{
SetColor(dc);
DrawRectByCoordanates(dc, i, j);
SetDefaultColor(dc);
}
}
}
SetDCPenColor(dc, RGB(0, 0, 0));
SetDCBrushColor(dc, RGB(0, 0, 0));
}
void MyField::DrawRect(CPoint pos, int &tempX, int &tempY, bool isDrawRect)
{
arrVector[row * tempY + tempX] = isDrawRect ? 1 : 0;
}
void MyField::SetSize(int rowN, int colN)
{
row = rowN;
col = colN;
}
void MyField::RedrawRect(CPaintDC &dc)
{
SetColor(dc);
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
if (arrVector[row*j + i] == 1)
{
DrawRectByCoordanates(dc, i, j);
}
}
}
SetDefaultColor(dc);
}
bool MyField::isRectDraw(int x, int y)
{
return arrVector[row * y + x] == 1 ? true : false;
}
int MyField::ChkRules(int num, int count)
{
int answer = 0;
if (num == 0 && count == 3)
return 1;
if (num == 1)
if (count == 2 || count == 3)
return 1;
else if (count <= 1 || count >= 4)
return 0;
return answer;
}
void MyField::ChangeParams(int rowN, int colN)
{
for (int i = 0; i < rowN; i++)
for (int j = 0; j < colN; j++)
arrVector.push_back(0);
}
std::vector<int> MyField::CreateTemp(int row, int col, std::vector<int> arrVectorTemp)
{
for (int i = 0; i < row*col; i++)
arrVectorTemp.push_back(0);
return arrVectorTemp;
}
void MyField::SetDefaultColor(CPaintDC &dc)
{
SetDCPenColor(dc, RGB(0, 0, 0));
SetDCBrushColor(dc, RGB(255, 255, 255));
}
void MyField::DrawRectByCoordanates(CPaintDC & dc, int i, int j)
{
dc.Rectangle(i * RectSize, j * RectSize,
i * RectSize + RectSize, j * RectSize + RectSize);
}
bool MyField::isClickOnField(CPoint pos)
{
return pos.x >= RectSize * row || pos.y >= RectSize * col ? false : true;
}
void MyField::SetColor(CPaintDC &dc)
{
SelectObject(dc, GetStockObject(DC_PEN));
SetDCPenColor(dc, RGB(70, 30, 90));
SelectObject(dc, GetStockObject(DC_BRUSH));
SetDCPenColor(dc, RGB(0, 0, 0));
SetDCBrushColor(dc, RGB(255, 255, 255));
SetDCBrushColor(dc, RGB(70, 30, 90));
}