-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCDiscController.cpp
More file actions
274 lines (230 loc) · 7.46 KB
/
Copy pathCDiscController.cpp
File metadata and controls
274 lines (230 loc) · 7.46 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
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#include "CDiscController.h"
CDiscController::CDiscController(HWND hwndMain):
CController(hwndMain)
{
assert(CParams::WindowHeight % CParams::iGridCellDim == 0);
assert(CParams::WindowWidth % CParams::iGridCellDim == 0);
}
CDiscController::~CDiscController(void)
{
for (auto i = m_vecSweepers.begin(); i != m_vecSweepers.end(); ++i)
delete *i;
for (auto i = m_vecObjects.begin(); i != m_vecObjects.end(); ++i)
delete *i;
}
void CDiscController::InitializeLearningAlgorithm(void)
{
/**
This is the basic discrete environment so there are no learning algorithms
to initialize yet...
*/
}
void CDiscController::InitializeSweepers(void)
{
for (int i=0; i<m_NumSweepers; ++i)
{
m_vecSweepers.push_back(new CDiscMinesweeper());
}
}
void CDiscController::InitializeMines(void)
{
for (int i=0; i<m_NumMines; ++i)
{
int x = RandInt(0,CParams::WindowWidth/CParams::iGridCellDim)*CParams::iGridCellDim;
int y = RandInt(0,CParams::WindowHeight/CParams::iGridCellDim)*CParams::iGridCellDim;
m_vecObjects.push_back(new CDiscCollisionObject(CCollisionObject::Mine,
SVector2D<int>(x,y)));
}
}
void CDiscController::InitializeSuperMines(void)
{
for (int i=0; i<m_NumSuperMines; ++i)
{
m_vecObjects.push_back(new CDiscCollisionObject(CCollisionObject::SuperMine,
SVector2D<int>(RandInt(0,CParams::WindowWidth/CParams::iGridCellDim)*CParams::iGridCellDim,
RandInt(0,CParams::WindowHeight/CParams::iGridCellDim)*CParams::iGridCellDim)));
}
}
void CDiscController::InitializeRocks(void)
{
for (int i=0; i<m_NumRocks; ++i)
{
m_vecObjects.push_back(new CDiscCollisionObject(CCollisionObject::Rock,
SVector2D<int>(RandInt(0,CParams::WindowWidth/CParams::iGridCellDim)*CParams::iGridCellDim,
RandInt(0,CParams::WindowHeight/CParams::iGridCellDim)*CParams::iGridCellDim)));
}
}
//-------------------------------------Update-----------------------------
//
// This is the main workhorse. The entire simulation is controlled from here.
//
// The comments should explain what is going on adequately.
//-------------------------------------------------------------------------
bool CDiscController::Update()
{
//run the sweepers through CParams::iNumTicks amount of cycles. During
//this loop each sweeper is constantly updated with the appropriate
//information from its surroundings. The output from the learning algorithm is obtained
//and the sweeper is moved. If it encounters a mine its MinesGathered is
//updated appropriately,
if (m_iTicks++ < CParams::iNumTicks)
{
for (int i=0; i<m_NumSweepers; ++i)
{
if ((m_vecSweepers[i])->isDead()) continue;
//update the position
if (!(m_vecSweepers[i])->Update(m_vecObjects))
{
//error in processing the learning algorithm
MessageBox(m_hwndMain, "An error occured while processing!", "Error", MB_OK);
return false;
}
//see if it's found a mine
int GrabHit = ((m_vecSweepers[i])->CheckForObject(m_vecObjects,
CParams::dMineScale));
if (GrabHit >= 0)
{
switch(m_vecObjects[GrabHit]->getType()){
case CDiscCollisionObject::Mine:
{
//we have discovered a mine so increase MinesGathered
(m_vecSweepers[i])->IncrementMinesGathered();
CDiscCollisionObject* oldObject = m_vecObjects[GrabHit];
oldObject->die();
break;
}
case CDiscCollisionObject::Rock:
{
//destroy the sweeper until it reincarnates in the next round
//CDiscCollisionObject* oldObject = m_vecObjects[GrabHit];
//oldObject->die();
(m_vecSweepers[i])->die();
break;
}
case CDiscCollisionObject::SuperMine:
{
//destroy both the sweeper and the supermine until both reincarnate in the next round
CDiscCollisionObject* oldObject = m_vecObjects[GrabHit];
oldObject->die();
(m_vecSweepers[i])->die();
break;
}
}
}
}
}
//Time to update the sweepers for the next iteration
else
{
//update the stats to be used in our stat window
double sum = 0;
int maxMines = -1;
int deaths = 0;
for (auto i = m_vecSweepers.begin(); i != m_vecSweepers.end(); ++i){
sum += (*i)->MinesGathered();
if ((*i)->isDead())
deaths++;
maxMines = max((*i)->MinesGathered(),maxMines);
}
m_vecAvMinesGathered.push_back(sum/float(m_vecSweepers.size()));
m_vecMostMinesGathered.push_back(maxMines);
m_vecDeaths.push_back(deaths);
//increment the iteration counter
++m_iIterations;
//reset cycles
m_iTicks = 0;
//reset the sweepers positions etc
for (int i=0; i<m_NumSweepers; ++i)
{
(m_vecSweepers[i])->Reset();
}
//reset the objects:
for (auto i = m_vecObjects.begin(); i != m_vecObjects.end(); ++i)
(*i)->Reset();
}
return true;
}
//------------------------------------Render()--------------------------------------
//
//----------------------------------------------------------------------------------
void CDiscController::Render(HDC surface)
{
//render the stats
string s = "Iteration: " + itos(m_iIterations);
TextOut(surface, 5, 0, s.c_str(), s.size());
//do not render if running at accelerated speed
if (!m_bFastRender)
{
//keep a record of the old pen
m_OldPen = (HPEN)SelectObject(surface, m_GreenPen);
//render the mines
for (int i=0; i<m_NumMines+m_NumSuperMines+m_NumRocks; ++i)
{
if ((m_vecObjects[i])->isDead()) continue; //skip if dead
if ( m_vecObjects[i]->getType() == CCollisionObject::Mine)
{
SelectObject(surface, m_GreenPen);
}
else if ( m_vecObjects[i]->getType() == CCollisionObject::Rock)
{
SelectObject(surface, m_BluePen );
}
else if ( m_vecObjects[i]->getType() == CCollisionObject::SuperMine)
{
SelectObject(surface, m_RedPen);
}
//grab the vertices for the mine shape
vector<SPoint> mineVB = m_MineVB;
WorldTransform<int>(mineVB, m_vecObjects[i]->getPosition());
//draw the mines
MoveToEx(surface, (int)mineVB[0].x, (int)mineVB[0].y, NULL);
for (int vert=1; vert<mineVB.size(); ++vert)
{
LineTo(surface, (int)mineVB[vert].x, (int)mineVB[vert].y);
}
LineTo(surface, (int)mineVB[0].x, (int)mineVB[0].y);
}
//we want some sweepers displayed in red
SelectObject(surface, m_RedPen);
//render the sweepers
for (int i=0; i<m_NumSweepers; i++)
{
if (i == CParams::iNumElite)
{
SelectObject(surface, m_OldPen);
}
if (m_vecSweepers[i]->isDead()) continue; //skip if dead
//grab the sweeper vertices
vector<SPoint> sweeperVB = m_SweeperVB;
//transform the vertex buffer
(m_vecSweepers[i])->WorldTransform(sweeperVB);
//draw the sweeper left track
MoveToEx(surface, (int)sweeperVB[0].x, (int)sweeperVB[0].y, NULL);
for (int vert=1; vert<4; ++vert)
{
LineTo(surface, (int)sweeperVB[vert].x, (int)sweeperVB[vert].y);
}
LineTo(surface, (int)sweeperVB[0].x, (int)sweeperVB[0].y);
//draw the sweeper right track
MoveToEx(surface, (int)sweeperVB[4].x, (int)sweeperVB[4].y, NULL);
for (int vert=5; vert<8; ++vert)
{
LineTo(surface, (int)sweeperVB[vert].x, (int)sweeperVB[vert].y);
}
LineTo(surface, (int)sweeperVB[4].x, (int)sweeperVB[4].y);
MoveToEx(surface, (int)sweeperVB[8].x, (int)sweeperVB[8].y, NULL);
LineTo(surface, (int)sweeperVB[9].x, (int)sweeperVB[9].y);
MoveToEx(surface, (int)sweeperVB[10].x, (int)sweeperVB[10].y, NULL);
for (int vert=11; vert<16; ++vert)
{
LineTo(surface, (int)sweeperVB[vert].x, (int)sweeperVB[vert].y);
}
}
//put the old pen back
SelectObject(surface, m_OldPen);
}//end if
else
{
PlotStats(surface);
}
}