-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGameController.cpp
More file actions
60 lines (51 loc) · 2.04 KB
/
GameController.cpp
File metadata and controls
60 lines (51 loc) · 2.04 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
#include "GameController.h"
GameController::GameController(QObject* parent): QObject(parent) { this->reset(); }
quint32 GameController::getScore() const { return this->game.get_score(); }
BoardModel* GameController::getBoard() { return &this->board; }
bool GameController::isInGame() const { return this->inGame; }
quint8 GameController::getGameSize() const { return this->game.get_size(); }
void GameController::setInGame(bool status) {
if (status == this->inGame) { return; }
this->inGame = status;
emit inGameChanged();
}
void GameController::setGameSize(quint8 gameSize) {
if (gameSize > MAX_GAME_SIZE || gameSize < MIN_GAME_SIZE) { return; }
this->game = game2048::Game(gameSize);
emit gameSizeChanged();
this->reset();
}
void GameController::move(const Direction towards) {
std::vector<game2048::GameAction> moveResult = this->game.move(directionToGameDirection(towards));
for (const auto& action: moveResult) {
if (action.merged) {
this->board.startRemove(action.start_index);
this->board.edit(action.end_index, action.end_index, true);
} else if (action.spawned_number) {
this->board.append({action.start_index, action.spawned_number, false});
} else {
this->board.edit(action.start_index, action.end_index, false);
}
}
emit scoreChanged();
}
void GameController::reset() {
this->game.reset();
this->board.reset();
for (const auto& x: this->game.get_board()) {
if (x == 0) { continue; }
this->board.append({this->game.to_index(&x), x, false});
}
emit scoreChanged();
}
bool GameController::canMove() { return this->game.can_move(); }
void GameController::deleteTileAt(quint8 index) { this->board.remove(index); }
game2048::Direction GameController::directionToGameDirection(Direction direction) {
switch (direction) {
case Direction::up: return game2048::Direction::up;
case Direction::down: return game2048::Direction::down;
case Direction::right: return game2048::Direction::right;
case Direction::left: return game2048::Direction::left;
default: throw std::invalid_argument("Invalid argument for direction.");
}
}