-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpoint.cpp
More file actions
147 lines (127 loc) · 3.4 KB
/
point.cpp
File metadata and controls
147 lines (127 loc) · 3.4 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
#include "point.h"
#include "board.h"
#include <QDragEnterEvent>
#include <QDebug>
#include <QMimeData>
const int Point::scalex = 53;
const int Point::scaley = 260;
Point::Point(Color color, Place place, QWidget* parent):
QLabel(parent),
color(color),
place(place)
{
changeStyle('a');
setAcceptDrops(true);
}
// FIXME: use Q_ASSERT
void Point::removeChecker() {
if (!checkers.isEmpty()) {
checkers.pop();
}
else
qDebug() << "hiba";
}
int Point::getCheckersNo() {
return checkers.size();
}
int Point::checkersColor() {
if (getCheckersNo()!=0)
return topChecker()->getMyColor();
else return 2;
}
Checker* Point::topChecker() {
if (!checkers.isEmpty()) {
return checkers.top();
}
else {
qDebug() << "hiba";
return 0;
}
}
void Point::clearCheckers() {
checkers.clear();
}
void Point::addChecker(Checker * checker) {
checkers.push(checker);
int nth = getCheckersNo();
int checkersHeight = 50;
int pointsHeight = scaley;
// if 1st-5th
if (nth<6)
if (place == UP)
checker->move(QPoint(0,(nth*checkersHeight)-checkersHeight));
else
checker->move(-QPoint(0,nth*checkersHeight-pointsHeight));
else
// if 6th-9th
if ((nth>=6) && (nth<10))
if (place == UP)
checker->move(QPoint(0,nth*checkersHeight-pointsHeight-15));
else
checker->move(-QPoint(0,nth*checkersHeight-490));
else
// if 10th-12th
if ((nth>=10) && (nth<13))
if (place == UP)
checker->move(QPoint(0,nth*checkersHeight-455));
else
checker->move(-QPoint(0,nth*checkersHeight-670));
else
// if 12th-14th
if ((nth>=13) && (nth<15))
if (place == UP)
checker->move(QPoint(0,nth*checkersHeight-580));
else
checker->move(-QPoint(0,nth*checkersHeight-795));
else
checker->move(QPoint(0,nth*checkersHeight-655));
checker->setPoint(this);
checker->show();
}
void Point::dragEnterEvent(QDragEnterEvent * event) {
if (event->mimeData()->hasFormat("application/x-backgammon-checkerdrop")) {
int thisCheckersColor = ((Checker*) event->source())->getMyColor();
if ((getCheckersNo() < 2) || (thisCheckersColor == checkersColor())) {
event->acceptProposedAction();
} else
event->ignore();
} else
event->ignore();
}
void Point::dropEvent(QDropEvent * event) {
if (event->mimeData()->hasFormat("application/x-backgammon-checkerdrop")) {
int thisCheckersColor = ((Checker*) event->source())->getMyColor();
if ((getCheckersNo() < 2) || (thisCheckersColor == checkersColor())) {
event->acceptProposedAction();
Checker *thisChecker = (Checker*) event->source();
thisChecker->getMyPlace()->removeChecker();
if ((getCheckersNo() == 1) && (thisCheckersColor != checkersColor())) { //hit
emit killedChecker(topChecker());
removeChecker();
}
addChecker(thisChecker);
} else
event->ignore();
} else
event->ignore();
}
void Point::changeStyle(const char codeletter) {
if (place == END) {
setPixmap(QPixmap(QString(":/images/semmi.png")).scaled(scalex, scaley));
return;
}
QString direction;
if (place == DOWN)
direction = "d";
else
direction = "";
QString colorstr;
if (color == WHITE)
colorstr = "w";
else
colorstr = "b";
setPixmap(QPixmap(QString(":/images/") + colorstr + direction + "point" + codeletter + ".png").scaled(scalex, scaley));
}
int Point::myPlace() {
return place;
}