-
Notifications
You must be signed in to change notification settings - Fork 0
/
BoardController.cpp
135 lines (103 loc) · 3.14 KB
/
BoardController.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include "BoardController.h"
#include "Constants.h"
#include <QDebug>
namespace {
Figure::Side _ChooseSide(int index)
{
auto row = index / Constants::board_size;
if(row <= Constants::black_second_row )
return Figure::Side::BLACK;
if (row >= Constants::white_second_row)
return Figure::Side::WHITE;
return Figure::Side::UNDEF;
}
Figure::Type _ChooseFigure(int index)
{
auto row = index / Constants::board_size;
if(row == Constants::white_second_row || row == Constants::black_second_row)
return Figure::Type::PAWN;
else if(row != Constants::white_first_row && row != Constants::black_first_row)
return Figure::Type::NONE;
auto column = index % Constants::board_size;
switch(column)
{
case 0: case 7:
return Figure::Type::ROOK;
case 1: case 6:
return Figure::Type::KNIGHT;
case 2: case 5:
return Figure::Type::BISHOP;
case 3:
return Figure::Type::QUEEN;
case 4:
return Figure::Type::KING;
}
return Figure::Type::NONE;
}
const int board_items_count = Constants::board_size * Constants::board_size;
}
BoardController::BoardController(QObject *parent) :
QObject(parent) ,
m_logic_controller(*this)
{
clearBoard();
}
bool BoardController::moveFigure(int from, int to)
{
if(!m_logic_controller.isMoveAllowed(from, to) || from == to)
return false;
auto victim = figureAt(to);
auto victim_type = victim->type();
auto victim_color = victim->side();
Transition t(from, to, victim_color, victim_type);
_moveFigure(from, to);
emit figureMoved(t);
return true;
}
const Figure *BoardController::figureAt(int index) const
{
return m_figures.at(index);
}
bool BoardController::isMoveAllowed(int from, int to)
{
return m_logic_controller.isMoveAllowed(from, to);
}
QQmlListProperty<Figure> BoardController::board()
{
return QQmlListProperty<Figure> (this, m_figures);
}
void BoardController::clearBoard()
{
m_figures.clear();
for(int i = 0; i < board_items_count; ++i)
m_figures.append(new Figure(this, Figure::Type::NONE, Figure::Side::UNDEF));
emit boardChanged(board());
}
void BoardController::resetBoard()
{
_initBoard();
emit boardChanged(board());
}
void BoardController::performTransition(const Transition &transition)
{
_moveFigure(transition.m_from, transition.m_to);
}
void BoardController::rollbackTransition(const Transition &transition)
{
_moveFigure(transition.m_to, transition.m_from, transition.m_victim_type, transition.m_victim_side);
}
void BoardController::_initBoard()
{
m_figures.clear();
for(int i = 0; i < board_items_count; ++i)
m_figures.append(new Figure(this, _ChooseFigure(i), _ChooseSide(i)));
}
void BoardController::_moveFigure(int from, int to, Figure::Type displacer_type, Figure::Side displacer_side)
{
std::swap(m_figures[from], m_figures[to]);
auto displacer = m_figures[from];
displacer->setType(displacer_type);
displacer->setSide(displacer_side);
qDebug() << "Move from " << from << " to " << to;
emit boardChanged(board());
}