-
Notifications
You must be signed in to change notification settings - Fork 4
/
Panel.cpp
170 lines (137 loc) · 3.13 KB
/
Panel.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
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
162
163
164
165
166
167
168
169
170
#include "Panel.h"
#include <QPainter>
Panel::Panel(QWidget *parent) : QWidget(parent)
{
m_gameCtrl = NULL;
}
Panel::~Panel()
{
}
void Panel::create(QRect validRect, int row, int column)
{
m_validRect = validRect;
m_row = row;
m_column = column;
if (m_gameCtrl)
delete m_gameCtrl;
m_gameCtrl = new Controller(this, m_row, m_column);
}
void Panel::reset()
{
m_gameCtrl->reset();
while (!m_stepRecord.empty())
{
delete m_stepRecord.top().qiziWidget;
m_stepRecord.pop();
}
}
void Panel::start(Qizi qizi, bool firstDrop)
{
m_gameCtrl->start(qizi, firstDrop);
}
Qizi Panel::myQizi()
{
return m_gameCtrl->myQizi();
}
Qizi Panel::otherQizi()
{
return m_gameCtrl->otherQizi();
}
void Panel::mousePressEvent(QMouseEvent* event)
{
if (!m_gameCtrl->isMyTurn())
return;
Pos pos = pointToPos(event->pos());
if (pos.column == -1 || pos.row == -1)
return;
Step step;
step.pos = pos;
step.qizi = m_gameCtrl->myQizi();
emit dropMine(step);
QWidget::mousePressEvent(event);
}
Pos Panel::pointToPos(const QPoint& pt)
{
float gridWidth = 1.0f * m_validRect.width() / (m_column-1);
float gridHeight = 1.0f * m_validRect.height() / (m_row-1);
float offsetX = gridWidth / 3.0f;
float offsetY = gridHeight / 3.0f;
Pos pos;
pos.column = pos.row = -1;
if (pt.x() < (m_validRect.left() - offsetX) ||
pt.x() > (m_validRect.left() + m_validRect.width() + offsetX) ||
pt.y() < (m_validRect.top() - offsetY) ||
pt.y() > (m_validRect.top() + m_validRect.height() + offsetY))
{
return pos;
}
for (int c = 0; c < m_column; c++)
{
float x = m_validRect.left() + c * gridWidth;
if (pt.x() >= (x - offsetX) &&
pt.x() <= (x + offsetX))
{
pos.column = c;
break;
}
}
for (int r = 0; r < m_row; r++)
{
float y = m_validRect.top() + r * gridHeight;
if (pt.y() >= (y - offsetY) &&
pt.y() <= (y + offsetY))
{
pos.row = r;
break;
}
}
return pos;
}
QPoint Panel::posToPoint(const Pos& pos)
{
float gridWidth = 1.0f * m_validRect.width() / (m_column - 1);
float gridHeight = 1.0f * m_validRect.height() / (m_row - 1);
QPoint pt;
pt.setX(m_validRect.left() + pos.column * gridWidth);
pt.setY(m_validRect.top() + pos.row * gridHeight);
return pt;
}
void Panel::paintEvent(QPaintEvent* event)
{
QPainter painter(this);
static QPixmap board(":/res/board.png");
painter.drawPixmap(rect(), board);
QWidget::paintEvent(event);
}
void Panel::dropQizi(Step step)
{
m_gameCtrl->drop(step);
QiziWidget* widget = new QiziWidget(this, step.qizi);
widget->resize(28, 28);
QPoint pt = posToPoint(step.pos);
widget->move(pt.x() - widget->width() / 2,
pt.y() - widget->height() / 2);
widget->show();
StepRecord record;
record.step = step;
record.qiziWidget = widget;
m_stepRecord.push(record);
if (m_gameCtrl->checkWin(step))
emit win(step.qizi);
}
void Panel::regret(Qizi qizi)
{
while (!m_stepRecord.empty())
{
StepRecord last = m_stepRecord.top();
delete last.qiziWidget;
m_gameCtrl->remove(last.step.pos);
m_stepRecord.pop();
if (last.step.qizi == qizi)
break;
}
if (qizi == m_gameCtrl->myQizi())
m_gameCtrl->setMyTurn(true);
else
m_gameCtrl->setMyTurn(false);
}