This repository has been archived by the owner on Apr 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCellType.cpp
74 lines (63 loc) · 1.64 KB
/
CellType.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
#include <exception>
#include <stdexcept>
#include <iostream>
#include "CellType.hh"
void fillMap(CellType **map);
CellType** initMap(void) noexcept;
CellType** initMap(void) noexcept
{
unsigned int width;
unsigned int height;
CellType **map;
std::cin >> width;
std::cin >> height;
map = new CellType*[width];
for (unsigned int i = 0; i < width; ++i) {
map[i] = new CellType[height];
}
return map;
}
void fillMap(CellType **map)
{
char character;
for (unsigned int i = 0; i < (sizeof(map) / sizeof(*map)); ++i) {
std::cin >> character;
for (unsigned int j = 0; j < (sizeof(map[i]) / sizeof(*map)); ++j) {
switch(character) {
case 'G':
map[i][j] = CellType::GrassCell;
break;
case 'W':
map[i][j] = CellType::WaterCell;
break;
case 'R':
map[i][j] = CellType::RockCell;
break;
case 'M':
map[i][j] = CellType::MountainCell;
break;
default:
throw std::runtime_error("Unvalid map type input");
}
}
}
}
void freeMap(CellType **map) noexcept
{
for (unsigned int i = 0; i < (sizeof(map) / sizeof(*map)); ++i) {
delete[] map[i];
}
delete[] map;
}
CellType** parseMap(void) noexcept
{
CellType **map;
try {
map = initMap();
fillMap(map);
} catch (std::invalid_argument) {
std::cout << "invalid map data" << std::endl;
freeMap(map);
}
return map;
}