Repository navigation
Expand file tree
/
Copy pathdraughtboard.cpp
More file actions
161 lines (131 loc) · 4.39 KB
/
Copy pathdraughtboard.cpp
File metadata and controls
161 lines (131 loc) · 4.39 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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
* draughtBoard.cpp
* Represent a checkers gameboard.
*
* @author Yoan DUMAS
* @versionn 1.0
* @date 18/12/2013
*/
#include <iostream>
#include <stdlib.h>
#include "draughtBoard.h"
#include "draughtpiece.h"
void DraughtBoard::init() {
for(int i = 0 ; i < DB_LENGTH ; ++i) {
for(int j = 0 ; j < DB_LENGTH ; ++j) {
gameboard_[i][j] = NULL;
}
}
}
DraughtBoard::DraughtBoard() {
gameboard_ = (Piece ***) malloc(DB_LENGTH * sizeof(Piece **));
if(gameboard_) {
for(int i = 0 ; i < DB_LENGTH ; ++i) {
gameboard_[i] = (Piece **) malloc(DB_LENGTH * sizeof(Piece *));
if(!gameboard_[i]) {
std::cerr << "Memory allocation fail." << std::endl;
break;
}
}
} else {
std::cerr << "Memory allocation fail." << std::endl;
}
init();
// Initialize the QLabel table
labelTable_ = (ClickableLabel **) malloc(DB_LENGTH * DB_LENGTH * sizeof(ClickableLabel *));
if(labelTable_) {
bool shift = true;
for(int i = 0 ; i < DB_LENGTH * DB_LENGTH ; ++i) {
labelTable_[i] = new ClickableLabel((position_t){i / 8, i % 8});
// Set the background color of the square.
if(i % 8 == 0) {
shift = !shift;
}
if((i % 2 == 0 && !shift) || ((i % 2 != 0 && shift))) {
// The square is black
labelTable_[i]->setStyleSheet("QLabel { background-color : black;}");
} else {
// The square is white
labelTable_[i]->setStyleSheet("QLabel { background-color : white;}");
}
}
} else {
std::cerr << "Memory allocation fail." << std::endl;
}
}
color_t DraughtBoard::getColor(const position_t & inPos) const {
return (inPos.i + inPos.j) % 2 != BLACK;
}
QWidget * DraughtBoard::draw() const {
QWidget * db = new QWidget;
// Create the layout for the draughtboard
QGridLayout * layout = new QGridLayout;
// Add QLabels to the layout
position_t p;
for(int i = 0 ; i < DB_LENGTH * DB_LENGTH ; ++i) {
p = (position_t){i / DB_LENGTH, i % DB_LENGTH};
if(!isFree(p)) {
labelTable_[i]->setPixmap(*getPiece(p)->draw());
} else {
labelTable_[i]->setPixmap(0);
}
layout->addWidget(labelTable_[i], i % DB_LENGTH, i / DB_LENGTH);
}
// Suppress margin and padding between sqares
layout->setMargin(0);
layout->setSpacing(0);
db->setLayout(layout);
return db;
}
DraughtBoard::~DraughtBoard() {
for(int i = 0 ; i < DB_LENGTH ; ++i) {
delete gameboard_[i];
gameboard_[i] = NULL;
}
delete gameboard_;
gameboard_ = NULL;
}
bool DraughtBoard::isFree(const position_t & inPos) const throw(std::out_of_range) {
if( inPos.i < 0 || inPos.i > DB_LENGTH - 1 || inPos.j < 0 || inPos.j > DB_LENGTH - 1) {
throw std::out_of_range("The given position is out of range.");
}
return gameboard_[inPos.i][inPos.j] == NULL;
}
void DraughtBoard::addPiece(Piece & inPiece) throw(SquareNotEmpty) {
position_t tmpPos = inPiece.getPosition();
if(!isFree(tmpPos)) {
throw SquareNotEmpty("The square is busy by another piece.");
} else {
// We add the piece to the draughtboard
gameboard_[tmpPos.i][tmpPos.j] = &inPiece;
}
}
void DraughtBoard::deletePiece(const position_t & inPos) throw(SquareEmpty) {
if(isFree(inPos)) {
throw SquareEmpty("The square is empty.");
} else {
gameboard_[inPos.i][inPos.j] = NULL;
}
}
void DraughtBoard::move(const position_t & inSrc, const position_t & inDst) throw(SquareNotEmpty, SquareEmpty) {
if(isFree(inSrc)) {
throw SquareEmpty("The square is empty.");
}
if(!isFree(inDst)) {
throw SquareNotEmpty("The square is busy by another piece.");
}
// Move the Piece to its destination square
gameboard_[inDst.i][inDst.j] = gameboard_[inSrc.i][inSrc.j];
gameboard_[inSrc.i][inSrc.j] = NULL;
// Update the position of the piece
gameboard_[inDst.i][inDst.j]->setPosition(inDst);
}
const Piece * DraughtBoard::getPiece(const position_t & inPosition) const {
if(isFree(inPosition)) {
throw SquareEmpty("The square is empty.");
}
return gameboard_[inPosition.i][inPosition.j];
}
ClickableLabel ** DraughtBoard::getLabelTable() {
return labelTable_;
}