-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathController.cpp
110 lines (91 loc) · 2.48 KB
/
Controller.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
/**
* Fichier impl�mentant le contr�leur. La vue doit contacter le contr�leur pour interagir avec le mod�le.
* \file Controller.cpp
* \author Erreur-404 et Mo-LK
* \date 19 avril 2022
* Cr�� le 12 avril 2022
*/
#include "Controller.hpp"
namespace control
{
Controller::Controller() : board_(new model::Chessboard())
{
QObject::connect(board_, & model::Chessboard::checkSignal,
this, &control::Controller::forwardCheck);
QObject::connect(board_, &model::Chessboard::gameover,
this, &control::Controller::forwardGameover);
QObject::connect(board_, &model::Chessboard::pieceAdded,
this, &control::Controller::forwardPieceAdded);
}
Controller::~Controller()
{
delete board_;
}
void Controller::forwardCheck(int x, int y)
{
emit notifyCheck(x, y);
}
void Controller::forwardGameover()
{
emit notifyGameover();
}
void Controller::forwardPieceAdded(int x, int y, std::string type, std::string pieceColor)
{
emit notifyPieceAdded(x, y, type, pieceColor);
}
void Controller::startGame(int strategyNumber)
{
model::StartupPositions* strategy = getStrategy(strategyNumber);
try {
board_->startGame(strategy);
}
catch (model::TooManyKingsException& e) {
std::cerr << "ERROR: " << e.what() << std::endl;
}
delete strategy;
}
bool Controller::move(int sourceX, int sourceY, int destinationX, int destinationY)
{
model::AbsPiece* piece = board_->getPiece(sourceX, sourceY);
if (board_->movePiece(piece, destinationX, destinationY)) {
return true;
}
else {
emit invalidMove(destinationX, destinationY);
return false;
}
}
bool Controller::isValidTile(int x, int y) const
{
const model::AbsPiece* selectedPiece = getPiece(x, y);
const bool isEmptyTile = selectedPiece == nullptr;
if (!isEmptyTile) {
const model::Color currentPlayer = getColor();
const model::Color selectedPieceColor = selectedPiece->getColor();
const bool isCorrectColor = selectedPieceColor == currentPlayer;
if (isCorrectColor) {
return true;
}
}
return false;
}
model::StartupPositions* Controller::getStrategy(int strategyNumber)
{
switch (strategyNumber) {
case 1:
return new model::StartupPositions1();
break;
case 2:
return new model::StartupPositions2();
break;
case 3:
return new model::StartupPositions3();
break;
case 4:
return new model::StartupPositions4();
break;
default:
return new model::StartupPositions1();
}
}
}