blob: 88ff394a9676b370afe2abffb96725adada7b84a (
plain)
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
|
#include "game.hpp"
#define EMPTY_CHAR ' '
#define WALL_CHAR '#'
#define CRATE_CHAR 'U'
#define PAYLOAD_CHAR '*'
#define MARIO_CHAR 'm'
Game::Game(std::string fmt)
: m_playerDirection(DirectionDown)
{
size_t p;
m_height = std::count(fmt.begin(), fmt.end(), '\n');
m_grid = new Cell*[m_height];
m_width = fmt.find("\n");
for (int i = 0; (p = fmt.find("\n")) != std::string::npos; i++)
{
std::string token = fmt.substr(0, p);
m_grid[i] = new Cell[m_width];
for (size_t j = 0; j < token.size(); j++)
{
switch (token[j])
{
case EMPTY_CHAR:
m_grid[i][j] = CellEmpty;
break;
case WALL_CHAR:
m_grid[i][j] = CellWall;
break;
case CRATE_CHAR:
m_grid[i][j] = CellCrate;
break;
case PAYLOAD_CHAR:
m_grid[i][j] = CellPayload;
break;
case MARIO_CHAR:
m_grid[i][j] = CellEmpty;
m_playerPos.y = i;
m_playerPos.x = j;
break;
default:
m_grid[i][j] = CellEmpty;
exit(1);
}
}
fmt.erase(0, p + 1);
}
}
Game::~Game()
{
for (size_t i = 0; i < m_height; i++)
delete []m_grid[i];
delete []m_grid;
}
void Game::move(Direction direction)
{
m_playerDirection = direction;
switch (direction)
{
case DirectionUp:
m_playerPos.y--;
break;
case DirectionDown:
m_playerPos.y++;
break;
case DirectionLeft:
m_playerPos.x--;
break;
case DirectionRight:
m_playerPos.x++;
}
}
Game::Cell Game::get(int y, int x) const
{
return m_grid[y][x];
}
size_t Game::getHeight() const
{
return m_height;
}
size_t Game::getWidth() const
{
return m_width;
}
Game::Position const &Game::getPlayer() const
{
return m_playerPos;
}
|