-
Notifications
You must be signed in to change notification settings - Fork 0
/
World.cpp
237 lines (192 loc) · 4.31 KB
/
World.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
234
235
236
237
/*World Class*/
//An all purpose class to hold two dimensional information.
#include "stdafx.h"
#include "World.h"
//Prototypes:
int random(int);
World::World()
{
m_world = NULL;
m_worldsize = CSize(0,0);
}
World::~World()
{
DeleteWorld();
}
void World::CreateWorld(int width, int height)
{
DeleteWorld();
m_worldsize.cx = width;
ASSERT(m_worldsize.cx > 0);
m_worldsize.cy = height;
ASSERT(m_worldsize.cy > 0);
ASSERT(0 < m_worldsize.cx * m_worldsize.cy);
m_world = new char[m_worldsize.cx * m_worldsize.cy];
}
void World::RandomWorld()
{
int loop;
ASSERT(isWorld());
for (loop = 0; loop < m_worldsize.cx * m_worldsize.cy; loop++)
{
WriteWorld(AddressToCoordinates(loop), random(255));
}
}
void World::RandomWorld(int rand_max)
{
int loop;
ASSERT(isWorld());
for (loop = 0; loop < m_worldsize.cx * m_worldsize.cy; loop++)
{
WriteWorld(AddressToCoordinates(loop), random(rand_max));
}
}
int random(int range)
{
if (range == 0)
{
return(0);
}
ASSERT(range);
range = abs(range); //Avoid dividing by 0!
ASSERT(range);
return(rand() % range);
}
void World::WriteWorld(int x, int y, char value) const
{
CPoint writespot(x,y);
if (writespot.x < 0)
{
writespot.x = m_worldsize.cx - abs(writespot.x);
}
if (writespot.y < 0)
{
writespot.y = m_worldsize.cy - abs(writespot.y);
}
if (writespot.x >= m_worldsize.cx)
{
writespot.x = writespot.x % m_worldsize.cx;
}
if (writespot.y >= m_worldsize.cy)
{
writespot.y = writespot.y % m_worldsize.cy;
}
WriteWorld(writespot, value);
}
void World::WriteWorld(CPoint point, char value) const
{
ASSERT(point.x < m_worldsize.cx);
ASSERT(point.y < m_worldsize.cy);
int spot;
spot = (point.y * m_worldsize.cx) + point.x;
ASSERT(spot < (m_worldsize.cx * m_worldsize.cy));
ASSERT(spot >= 0);
m_world[spot] = value; //Do the real write.
}
char World::ReadWorld(int x, int y) const
{
CPoint readspot(x,y);
if (readspot.x < 0)
{
readspot.x = m_worldsize.cx - abs(readspot.x);
}
if (readspot.y < 0)
{
readspot.y = m_worldsize.cy - abs(readspot.y);
}
if (readspot.x >= m_worldsize.cx)
{
readspot.x-=m_worldsize.cx;
}
if (readspot.y >= m_worldsize.cy)
{
readspot.y-=m_worldsize.cy;
}
return(ReadWorld(readspot));
}
char World::ReadWorld(CPoint point) const
{
ASSERT(point.x < m_worldsize.cx);
ASSERT(point.x >= 0);
ASSERT(point.y < m_worldsize.cy);
ASSERT(point.y >= 0);
int spot;
spot = (point.y * m_worldsize.cx) + point.x;
ASSERT(spot < m_worldsize.cx * m_worldsize.cy);
ASSERT(spot >= 0);
return(m_world[spot]); //Do the real read.
}
void World::DeleteWorld()
{
if (isWorld())
{
delete[] m_world;
m_world = NULL;
m_worldsize.cx = 0;
m_worldsize.cy = 0;
}
}
CPoint World::AddressToCoordinates(int number)
{
ASSERT(number % m_worldsize.cx <= m_worldsize.cx);
ASSERT(number/m_worldsize.cx <= m_worldsize.cy);
return(CPoint(number % m_worldsize.cx, number/m_worldsize.cx));
}
void World::CycleWorld()
{
//This does the maintenance. It must be customized to the app.
return;
}
BOOL World::isWorld() //Is this environment setup?
{
if (m_world == NULL || m_worldsize.cx == 0 || m_worldsize.cy == 0)
{
return((BOOL)false);
}
return(true);
}
SIZE World::GetWorldSize()
{
#ifdef _DEBUG
if (isWorld() == false)
{
TRACE("This World object is incomplete.\n");
ASSERT(isWorld());
}
#endif
return(m_worldsize);
}
World& World::operator=(World& in_world)
{
CSize world_size = in_world.GetWorldSize();
CreateWorld(world_size.cx, world_size.cy);
for (int width = 0; width < world_size.cx; width++)
{
for (int height = 0; height < world_size.cy; height++)
{
WriteWorld(width, height, in_world.ReadWorld(width, height));
}
}
return(*this);
}
World& World::Copy(World& in_world)
{
CSize world_size = in_world.GetWorldSize();
CreateWorld(world_size.cx, world_size.cy);
for (int width = 0; width < world_size.cx; width++)
{
for (int height = 0; height < world_size.cy; height++)
{
WriteWorld(width, height, in_world.ReadWorld(width, height));
}
}
return(*this);
}
void World::ZeroWorld()
{
if (!isWorld())
{
return;
}
memset(m_world, 0, m_worldsize.cx * m_worldsize.cy);
}