-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.cc
77 lines (63 loc) · 1.41 KB
/
map.cc
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
#include "map.h"
#ifdef DEBUG
#include <iostream>
#endif
namespace map {
Map::Map() : width(WIDTH), height(HEIGHT) {
field = new MapType*[height*width];
for (unsigned i=0; i < height; ++i) {
field[i] = new MapType[width];
}
}
Map::~Map() {
for (unsigned i=0; i < height; ++i) {
delete [] field[i];
}
delete [] field;
}
void Map::init() {
if (field == NULL) {
throw "Map not initialized";
}
// initialize all cells with an empty place
for (unsigned i=0; i < height; ++i)
for (unsigned j=0; j < height; ++j) {
field[i][j] = FLOOR;
}
}
void Map::fill() {
// default fill, just create a box of WALL
for (unsigned i=0; i < height; ++i) {
field[i][0] = field[i][width-1] = WALL;
}
for (unsigned j=0; j < width; ++j) {
field[0][j] = field[height-1][j] = WALL;
}
}
MapType Map::getType(unsigned const i, unsigned const j) const {
if (i >= height || j >= width) {
throw "Index out of map range. ";
}
return field[i][j];
}
#ifdef DEBUG
std::ostream& operator<< (std::ostream& os, const Map& map) {
for (unsigned i=0; i < map.getHeight(); ++i) {
for (unsigned j=0; j < map.getWidth(); ++j) {
char c=FLOOR;
switch (map.getType(i,j)) {
case WALL : c = '#';
break;
case OBJECT : c = '.';
break;
case FLOOR:
default : c = ' ';
}
os << c;
}
os << std::endl;
}
return os;
}
#endif
} // namespace map