-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.cpp
260 lines (219 loc) · 7.52 KB
/
MainWindow.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/* This file is part of Qenolaba.
Copyright (C) 1997-2015 Josef Weidendorfer <Josef.Weidendorfer@gmx.de>
Qenolaba is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public
License as published by the Free Software Foundation, version 2.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; see the file COPYING. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "MainWindow.h"
#include "BoardWidget.h"
#include "Network.h"
#include <QStatusBar>
#include <QAction>
#include <QMenuBar>
#include <QMenu>
#include <QToolBar>
#include <QTimer>
#include <QMessageBox>
MainWindow::MainWindow(Network *n)
{
#ifdef QT_DBUS_SUPPORT
QDBusConnection con = QDBusConnection::sessionBus();
con.registerObject("/Qenolaba", this,
QDBusConnection::ExportScriptableSlots);
#endif
createActions();
createMenu();
createToolbar();
_statusbar = statusBar();
_statusLabel = new QLabel(_statusbar);
_statusbar->addWidget(_statusLabel, 1);
_computerDepth = 3;
_network = n;
_board = new Board();
_board->setDepth(_computerDepth);
_boardWidget = new BoardWidget(*_board);
_boardWidget->renderPieces(true);
setCentralWidget(_boardWidget);
connect(_boardWidget, SIGNAL(moveChoosen(Move&)),
SLOT(draw(Move&)));
if (n)
connect(n, SIGNAL(gotPosition(const char*)),
SLOT(newPosition(const char*)));
setWindowIcon(QIcon(":/app.png"));
updateStatus();
}
void MainWindow::createActions()
{
// file menu actions
_newAction = new QAction(tr("&New Game"), this);
_newAction->setShortcuts(QKeySequence::New);
_newAction->setStatusTip(tr("Start new game"));
connect(_newAction, SIGNAL(triggered()), this, SLOT(newGame()));
_quitAction = new QAction(tr("&Quit"), this);
_quitAction->setShortcut(tr("Ctrl+Q"));
_quitAction->setStatusTip(tr("Quit the application"));
connect(_quitAction, SIGNAL(triggered()), this, SLOT(close()));
_startAction = new QAction(tr("Red &starts game"), this);
_startAction->setCheckable(true);
_startAction->setChecked(true);
_startAction->setShortcut(tr("Ctrl+S"));
_startAction->setStatusTip(tr("Red side about to start the game"));
_redAction = new QAction(tr("Computer plays &Red"), this);
_redAction->setCheckable(true);
_redAction->setShortcut(tr("Ctrl+R"));
_redAction->setStatusTip(tr("Red side played by computer"));
_yellowAction = new QAction(tr("Computer plays &Yellow"), this);
_yellowAction->setCheckable(true);
_yellowAction->setChecked(true);
_yellowAction->setShortcut(tr("Ctrl+Y"));
_yellowAction->setStatusTip(tr("Yellow side played by computer"));
_d1Action = new QAction(tr("Level &1: Weak"), this);
_d1Action->setCheckable(true);
_d2Action = new QAction(tr("Level &2: Medium"), this);
_d2Action->setCheckable(true);
_d3Action = new QAction(tr("Level &3: Strong"), this);
_d3Action->setCheckable(true);
_d4Action = new QAction(tr("Level &4: Challange"), this);
_d4Action->setCheckable(true);
_depthGroup = new QActionGroup(this);
_depthGroup->addAction(_d1Action);
_depthGroup->addAction(_d2Action);
_depthGroup->addAction(_d3Action);
_depthGroup->addAction(_d4Action);
QAction* a = _d2Action;
switch(_computerDepth) {
case 2: a = _d1Action; break;
case 4: a = _d3Action; break;
case 5: a = _d4Action; break;
}
a->setChecked(true);
connect(_depthGroup, SIGNAL(triggered(QAction*)),
SLOT(depthSet(QAction*)));
// help menu actions
_aboutAction = new QAction(tr("&About Qenolaba..."), this);
_aboutAction->setStatusTip(tr("Show the application's About box"));
connect(_aboutAction, SIGNAL(triggered()), this, SLOT(about()));
}
void MainWindow::createMenu()
{
QMenuBar* mBar = menuBar();
QMenu* fileMenu = mBar->addMenu(tr("&File"));
fileMenu->addAction(_newAction);
fileMenu->addSeparator();
fileMenu->addAction(_quitAction);
QMenu* optionMenu = mBar->addMenu(tr("&Options"));
optionMenu->addAction(_startAction);
optionMenu->addAction(_redAction);
optionMenu->addAction(_yellowAction);
optionMenu->addSeparator();
optionMenu->addAction(_d1Action);
optionMenu->addAction(_d2Action);
optionMenu->addAction(_d3Action);
optionMenu->addAction(_d4Action);
QMenu* helpMenu = mBar->addMenu(tr("&Help"));
helpMenu->addAction(_aboutAction);
}
void MainWindow::createToolbar()
{
QToolBar* tb = new QToolBar(tr("Main Toolbar"), this);
tb->setObjectName("main-toolbar");
addToolBar(Qt::TopToolBarArea, tb);
tb->addAction(_newAction);
tb->addSeparator();
}
void MainWindow::initInput()
{
qDebug("%s", qPrintable(_board->getState()));
if (!updateStatus()) {
_boardWidget->updatePosition(true);
return;
}
if (((_board->actColor() == Board::color1) && _redAction->isChecked()) ||
((_board->actColor() == Board::color2) && _yellowAction->isChecked())) {
_moveToDraw = _board->bestMove();
QTimer::singleShot(50, this, SLOT(draw()));
}
else {
_board->generateMoves(_moveList);
_boardWidget->choseMove(&_moveList);
}
_boardWidget->updatePosition(true);
}
bool MainWindow::updateStatus()
{
if (_board->getColor1Count() == 0)
_statusLabel->setText(tr("Game not started yet"));
else if (_board->getColor1Count()<9)
_statusLabel->setText(tr("Yellow wins the game"));
else if (_board->getColor2Count()<9)
_statusLabel->setText(tr("Red wins the game"));
else if (_board->actColor() == Board::color1) {
_statusLabel->setText(tr("Red about to move"));
return true;
}
else if (_board->actColor() == Board::color2) {
_statusLabel->setText(tr("Yellow about to move"));
return true;
}
return false;
}
void MainWindow::newGame()
{
_board->begin(_startAction->isChecked() ? Board::color1 : Board::color2);
if (_network) _network->broadcast(_board);
initInput();
}
void MainWindow::about()
{
QString text, version;
version = QLatin1String("0.1");
text = QString("<h3>Qenolaba %1</h3>").arg(version);
text += tr("<p>Qenolaba is inspired by a famous board game with "
"hexagonal grid. It has a computer player and network "
"connectivity (just try to run it twice).</p>"
"<p>Qenolaba is open-source, and it is distributed under the "
"terms of the GPL v2.</p>"
"Author: "
"<a href=\"mailto:Josef.Weidendorfer@gmx.de\">"
"Josef Weidendorfer</a>");
QMessageBox::about(this, tr("About Qenolaba"), text);
}
void MainWindow::depthSet(QAction* a)
{
if (a == _d1Action) _computerDepth = 2;
if (a == _d2Action) _computerDepth = 3;
if (a == _d3Action) _computerDepth = 4;
if (a == _d4Action) _computerDepth = 5;
_board->setDepth(_computerDepth);
}
void MainWindow::draw(Move& m)
{
if (m.isValid()) {
_board->playMove(m);
if (_network) _network->broadcast(_board);
}
initInput();
}
void MainWindow::draw()
{
if (_moveToDraw.isValid()) {
_board->playMove(_moveToDraw);
if (_network) _network->broadcast(_board);
}
initInput();
}
void MainWindow::newPosition(const char* p)
{
QString s(p);
_board->setState(s);
qDebug("Got new position from network...");
initInput();
}