summaryrefslogtreecommitdiff
path: root/quacker/quacker.h
blob: cb4702331c9456ee724e8c35f98310acff67a1e6 (plain)
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/*
 *  Quackle -- Crossword game artificial intelligence and analysis tool
 *  Copyright (C) 2005-2019 Jason Katz-Brown, John O'Laughlin, and John Fultz.
 *
 *  This program 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; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  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. If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef QUACKER_QUACKER_H
#define QUACKER_QUACKER_H

// #define ENABLE_GRAPHICAL_REPORT

#include <QMainWindow>
#include <QMap>
#include <QMessageBox>

#include <datamanager.h>
#include "oppothread.h"
#include <sim.h>

#include <quackleio/dictimplementation.h>

class QAction;
class QActionGroup;
class QComboBox;
class QCheckBox;
class QFrame;
class QGroupBox;
class QLineEdit;
class QPushButton;
class QSplitter;
class QTabWidget;
class QTimer;
class QVBoxLayout;

class OppoThreadProgressBar;

namespace Quackle
{
	class ComputerPlayer;
	class Game;
	class GamePosition;
	class History;
	class HistoryLocation;
	class Move;
	class Rack;
}

namespace QuackleIO
{
	class Logania;
}

class BaseView;
class HistoryView;
class Letterbox;
class ListerDialog;
class QuackerSettings;
class Settings;
class SimViewer;
class View;
class Macondo;

class TopLevel : public QMainWindow
{
Q_OBJECT

public:
	TopLevel(QWidget *parent = 0);
	~TopLevel();

	void closeEvent(QCloseEvent *closeEvent);

	// returns text wrapped in an HTML tag to ensure dialog wrapping
	static QString dialogText(const QString &text);

public slots:
	void open();
	void newGame();

	// like loadFile, but set the filename, initial directory,
	// and other things
	void openFile(const QString &filename);

	// actually loads the file
	void loadFile(const QString &filename);

	void save();
	void saveAs();
	void writeFile(const QString &filename);
	void generateList();
	void letterbox();

	void kibitz();
	void kibitz(int numberOfPlays, Quackle::ComputerPlayer *computerPlayer = 0);
	void kibitzFifty();
	void kibitzAll();
	void kibitzAs(Quackle::ComputerPlayer *computerPlayer);

	void firstPosition();
	void nextPosition();
	void previousPosition();

	void reportAs(Quackle::ComputerPlayer *computerPlayer);
	void htmlReport();
	void graphicalReport();
	void commitTopChoice();
	void simulate(bool startSimulation);
	void simulateToggled(bool startSimulation);
	void clearSimulationResults();

	void showAscii();
	void writeAsciiToFile(const QString &text, const QString &filename);
	void copyToClipboard(const QString &text);
	void print();
	void firstTimeRun();
	void about();
	void hints();

	void showConfigDialog();

	// set up our game object based on a shuffled playerList
	void initializeGame(const Quackle::PlayerList *players);

	// call timerControl, and tell user about it
	void pause(bool paused);

protected slots:
	// final set up of initial data structures
	// and an example game
	void finishInitialization();

	// say hi
	void introduceToUser();

	// prepares current position and spawns computer player thread
	// if necessary then sends orders to update the UI
	void advanceGame();

	// asks oppo thread to find best move
	void startOppoThread();

	// called by oppo thread when it's done
	void computerPlayerDone();

	// called by player thread when it's done
	void kibitzThreadFinished();

	// called by a oppo/player thread when it has a status update
	void playerFractionDone(double fraction, OppoThread *thread);

	// update both positional and history views,
	// then wait for human to make a play
	void showToHuman();

	// use this to control timer!
	void timerControl(bool paused);

	// commit candidate play then advance the game
	void commit();

	// add a pass candidate
	void pass();

	// handle an overdraw
	void overdraw();

	void statusMessage(const QString &message);

	// set game's candidate to move and update views
	void setCandidateMove(const Quackle::Move *move, bool *carryOnPtr = nullptr);
	void removeCandidateMoves(const Quackle::MoveList *moves);

	// set current player's rack and update views
	void setRack(const Quackle::Rack &rack);

	// set current position's explanatory note
	void setNote(const UVString &note);

	// set history location to view
	void goToHistoryLocation(const Quackle::HistoryLocation *location);

	// stop simulation, opponenent thread, etc
	void stopEverything();

	// update all views (usually because of a settings change)
	void updateAllViews();

	// update *positional* views - emit positionChanged
	void updatePositionViews();

	// updates move views from either simulation results if available
	// or kibitzed moves
	void updateMoveViews();

	// update history views when a new position is added
	void updateHistoryViews();

	void setCaption(const QString &text = QString());
	void setModified(bool modified);

	// main timer
	void timeout();

	// simulation timer
	void incrementSimulation();
	void updateSimViews();

	// simulator settings:
	void pliesSet(int plyIndex);
	void ignoreOpposChanged();
	void updatePliesCombo();
	void logfileEnabled(bool on);
	void logfileChanged();
	void chooseLogfile();
	void partialOppoRackEnabled(bool on);
	void partialOppoRackChanged();
	void showSimulationDetails();

	// Birthday
	void startBirthday();
	void birthdayBash();
	void birthdayGram(int index, bool on);

signals:
	// emitted when views (eg board) should update based on the
	// current position (includes board information, current candidate play
	// that should be shown)
	void positionChanged(const Quackle::GamePosition *position);

	void movesChanged(const Quackle::MoveList *moves);

	// emitted when views of history must update
	void historyChanged(const Quackle::History &history);

protected:
	Quackle::DataManager m_dataManager;
	Quackle::Game *m_game;
	Quackle::Simulator *m_simulator;

private:
	void saveSettings();
	void loadSettings();

	// returns 0 for save, 1 for discard, 2 for cancel
	QMessageBox::StandardButton askToSave();

	// returns true if user wants to make play anyway
	bool askToCarryOn(const QString &text);

	// used to know when to update UI when performing heavy calculation
	bool m_initializationChuu;

	// are dictionaries etc properly set up?
	bool setupCheck();

	// hook up signals and slots associated with a view
	void plugIntoBaseMatrix(BaseView *view);
	void plugIntoMatrix(View *view);
	void plugIntoPositionMatrix(View *view);
	void plugIntoMoveMatrix(View *view);
	void plugIntoHistoryMatrix(HistoryView *view);

	void parseCommandLineOptions();

	bool isPlayerOnTurnComputer() const;
	bool isCommitAllowed() const;
	bool shouldOutcraftyCurrentPlayer() const;
	void startOutcraftyingCurrentPlayer();
	void stopOutcraftyingCurrentPlayer();

	// Used to sanitize a move for a player with unknown racks.
	// Changes exchanges to exchange tiles on the current rack.
	// If move is a place, changes current player's rack to something
	// that includes the used tiles.
	// Returns true if the validization was successful.
	bool validifyMove(Quackle::Move &move);

	void kibitzFinished();

	OppoThreadProgressBar *createProgressBarForThread(OppoThread *thread);
	void removeProgressIndicators();
	void removeProgressIndicator(OppoThread *thread);
	OppoThreadProgressBar *progressBarOfThread(OppoThread *thread);

	// generic game title
	QString gameTitle();

	UVString m_firstPlayerName;
	QString playerString() const;

	QList<OppoThread *> m_oppoThreads;
	QList<OppoThread *> m_otherOppoThreads;

	QTimer *m_timer;
	QTimer *m_simulationTimer;

	QMap<OppoThread *, OppoThreadProgressBar *> m_progressIndicators;

	View *m_brb;

	QSplitter *m_splitter;

	QVBoxLayout *m_leftSideLayout;

	enum TabIndex { HistoryTabIndex = 0, ChoicesTabIndex = 1, SettingsTabIndex = 2, MacondoTabIndex = 3 };
	QTabWidget *m_tabWidget;

	HistoryView *m_history;
	HistoryView *m_dashboard;

	QWidget *m_choicesWidget;
	View *m_moveBox;
	View *m_noteEditor;
	QFrame *m_frameWidget;

	Settings *m_settings;
	Macondo *m_macondo;

	ListerDialog *m_listerDialog;
	void updateListerDialogWithRack();

	Letterbox *m_letterbox;
	QuackerSettings *m_quackerSettings;

// encapsulated simulator settings widget
	QGroupBox *m_simulatorWidget;
	QPushButton *m_showDetailsButton;
	QLineEdit *m_logfileEdit;
	QGroupBox *m_logfileEnable;
	QPushButton *m_logfileChooser;
	QLineEdit *m_partialOppoRackEdit;
	QGroupBox *m_partialOppoRackEnable;
	QCheckBox *m_ignoreOpposCheck;

	static const int m_pliesToOffer = 6;
	QComboBox *m_pliesCombo;

	SimViewer *m_simViewer;

	void setLogfileEnabled(bool enabled);
	bool isLogfileEnabled() const;

	// the value of the file-choosing lineedit
	QString userSpecifiedLogfile() const;

	// what user specified, or empty string if logging disabled
	QString logfile() const;

	void setPartialOppoRackEnabled(bool enabled);
	bool isPartialOppoRackEnabled() const;

	// the value of the partial oppo rack-choosing lineedit
	QString userSpecifiedPartialOppoRack() const;

	// what user specified, or empty string if logging disabled
	QString partialOppoRack() const;

	int m_plies;
// end encapsulation, hah

	QString m_filename;
	QuackleIO::Logania *m_logania;
	bool m_modified;
	QString m_ourCaption;

	QString getInitialDirectory() const;
	void setInitialDirectory(const QString &filename);
	QString m_initialDirectory;
	QString gameFileFilters() const;
	QString defaultGameFileFilter() const;

	QActionGroup *m_kibitzAsActions;
	QActionGroup *m_reportAsActions;
	QAction *m_htmlReportAction;
	QAction *m_showAsciiAction;
	QAction *m_generateAction;
	QAction *m_newAction;
	QAction *m_openAction;
	QAction *m_saveAction;
	QAction *m_saveAsAction;
	QAction *m_pauseAction;
	QAction *m_firstPositionAction;
	QAction *m_nextPositionAction;
	QAction *m_previousPositionAction;
	QAction *m_commitAction;
	QAction *m_passAction;
	QAction *m_overdrawAction;
	QAction *m_kibitzAction;
	QAction *m_kibitzFiftyAction;
	QAction *m_kibitzAllAction;
	QAction *m_commitTopChoiceAction;
	QAction *m_simulateAction;
	QAction *m_simulateClearAction;
	QAction *m_simulateDetailsAction;
	QAction *m_preferencesAction;

#ifdef ENABLE_GRAPHICAL_REPORT
	QAction *m_graphicalReportAction;
#endif

	// make sure moves in our simulator match those in current position
	void ensureUpToDateSimulatorMoveList();

	void createMenu();
	void createWidgets();
	void switchToTab(TabIndex index);

	// Birthday
	QTimer *m_birthdayTimer;
	int m_birthdayIndex;
};

class KibitzerListener : public QObject
{
Q_OBJECT

public:
	KibitzerListener(Quackle::ComputerPlayer *computerPlayer, QObject *parent);

public slots:
	void kibitzTriggered();
	void reportTriggered();

signals:
	void kibitzAs(Quackle::ComputerPlayer *computerPlayer);
	void reportAs(Quackle::ComputerPlayer *computerPlayer);

private:
	// returns true if the action should still be carried out despite slowness
	bool slownessCheck();

	Quackle::ComputerPlayer *m_computerPlayer;
	bool m_slowPlayerWarningTriggered;
};

#endif