forked from KDE/kpat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
340 lines (309 loc) · 13.4 KB
/
main.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
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
/*
* Copyright (C) 1995 Paul Olav Tvete <[email protected]>
* Copyright (C) 2000-2009 Stephan Kulow <[email protected]>
*
* License of original code:
* -------------------------------------------------------------------------
* Permission to use, copy, modify, and distribute this software and its
* documentation for any purpose and without fee is hereby granted,
* provided that the above copyright notice appear in all copies and that
* both that copyright notice and this permission notice appear in
* supporting documentation.
*
* This file is provided AS IS with no warranties of any kind. The author
* shall have no liability with respect to the infringement of copyrights,
* trade secrets or any patents by this file or any part thereof. In no
* event will the author be liable for any lost revenue or profits or
* other special, indirect and consequential damages.
* -------------------------------------------------------------------------
*
* License of modifications/additions made after 2009-01-01:
* -------------------------------------------------------------------------
* 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 2 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/>.
* -------------------------------------------------------------------------
*/
#include "dealer.h"
#include "dealerinfo.h"
#include "mainwindow.h"
#include "version.h"
#include "patsolve/patsolve.h"
#include "KCardTheme"
#include "KCardDeck"
#include <KAboutData>
#include <KCrash>
#include <QDebug>
#include <KLocalizedString>
#include <KDBusService>
#include <QFile>
#include <QTime>
#include <QResizeEvent>
#include <QDomDocument>
#include <Kdelibs4ConfigMigrator>
#include <climits>
#include <ctime>
#include <QStandardPaths>
#include <QApplication>
#include <QCommandLineParser>
#include <QCommandLineOption>
static DealerScene *getDealer( int wanted_game )
{
foreach ( DealerInfo * di, DealerInfoList::self()->games() )
{
if ( di->providesId( wanted_game ) )
{
DealerScene * d = di->createGame();
Q_ASSERT( d );
d->setDeck( new KCardDeck( KCardTheme(), d ) );
d->initialize();
if ( !d->solver() )
{
qCritical() << "There is no solver for" << di->nameForId( wanted_game );;
return 0;
}
return d;
}
}
return 0;
}
// A function to remove all nonalphanumeric characters from a string
// and convert all letters to lowercase.
QString lowerAlphaNum( const QString & string )
{
QString result;
for ( int i = 0; i < string.size(); ++i )
{
QChar c = string.at( i );
if ( c.isLetterOrNumber() )
result += c.toLower();
}
return result;
}
int main( int argc, char **argv )
{
QApplication app(argc, argv);
KLocalizedString::setApplicationDomain("kpat");
Kdelibs4ConfigMigrator migrate(QStringLiteral("kpat"));
migrate.setConfigFiles(QStringList() << QStringLiteral("kpatrc"));
migrate.setUiFiles(QStringList() << QStringLiteral("kpatui.rc"));
migrate.migrate();
KAboutData aboutData( QStringLiteral("kpat"),
i18n("KPatience"),
KPAT_VERSION,
i18n("KDE Patience Game"),
KAboutLicense::GPL_V2,
i18n("© 1995 Paul Olav Tvete\n© 2000 Stephan Kulow"),
QString(),
QStringLiteral("http://games.kde.org/kpat") );
aboutData.setOrganizationDomain(QByteArray("kde.org"));
aboutData.addAuthor( i18n("Paul Olav Tvete"),
i18n("Author of original Qt version"),
QStringLiteral("[email protected]") );
aboutData.addAuthor( i18n("Mario Weilguni"),
i18n("Initial KDE port"),
QStringLiteral("[email protected]") );
aboutData.addAuthor( i18n("Matthias Ettrich"),
QString(),
QStringLiteral("[email protected]") );
aboutData.addAuthor( i18n("Rodolfo Borges"),
i18n("New game types"),
QStringLiteral("[email protected]") );
aboutData.addAuthor( i18n("Peter H. Ruegg"),
QString(),
QStringLiteral("[email protected]") );
aboutData.addAuthor( i18n("Michael Koch"),
i18n("Bug fixes"),
QStringLiteral("[email protected]") );
aboutData.addAuthor( i18n("Marcus Meissner"),
i18n("Shuffle algorithm for game numbers"),
QStringLiteral("[email protected]") );
aboutData.addAuthor( i18n("Tom Holroyd"),
i18n("Initial patience solver"),
QStringLiteral("[email protected]") );
aboutData.addAuthor( i18n("Stephan Kulow"),
i18n("Rewrite and current maintainer"),
QStringLiteral("[email protected]") );
aboutData.addAuthor( i18n("Erik Sigra"),
i18n("Klondike improvements"),
QStringLiteral("[email protected]") );
aboutData.addAuthor( i18n("Josh Metzler"),
i18n("Spider implementation"),
QStringLiteral("[email protected]") );
aboutData.addAuthor( i18n("Maren Pakura"),
i18n("Documentation"),
QStringLiteral("[email protected]") );
aboutData.addAuthor( i18n("Inge Wallin"),
i18n("Bug fixes"),
QStringLiteral("[email protected]") );
aboutData.addAuthor( i18n("Simon Hürlimann"),
i18n("Menu and toolbar work"),
QStringLiteral("[email protected]") );
aboutData.addAuthor( i18n("Parker Coates"),
i18n("Cleanup and polish"),
QStringLiteral("[email protected]") );
// Create a KLocale earlier than normal so that we can use i18n to translate
// the names of the game types in the help text.
QMap<QString, int> indexMap;
QStringList gameList;
foreach ( const DealerInfo *di, DealerInfoList::self()->games() )
{
KLocalizedString localizedKey = ki18n( di->untranslatedBaseName().constData() );
//QT5 const QString translatedKey = lowerAlphaNum( localizedKey.toString( tmpLocale ) );
//QT5 gameList << translatedKey;
//QT5 indexMap.insert( translatedKey, di->baseId() );
indexMap.insert( di->baseIdString(), di->baseId() );
}
gameList.sort();
const QString listSeparator = i18nc( "List separator", ", " );
QCommandLineParser parser;
KAboutData::setApplicationData(aboutData);
KCrash::initialize();
parser.addVersionOption();
parser.addHelpOption();
parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("solvegame"), i18n( "Try to find a solution to the given savegame" ), QStringLiteral("file")));
parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("solve"), i18n("Dealer to solve (debug)" ), QStringLiteral("num")));
parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("start"), i18n("Game range start (default 0:INT_MAX)" ), QStringLiteral("num")));
parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("end"), i18n("Game range end (default start:start if start given)" ), QStringLiteral("num")));
parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("gametype"), i18n("Skip the selection screen and load a particular game type. Valid values are: %1",gameList.join(listSeparator)), QStringLiteral("game")));
parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("testdir"), i18n( "Directory with test cases" ), QStringLiteral("directory")));
parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("generate"), i18n( "Generate random test cases" )));
parser.addPositionalArgument(QStringLiteral("file"), i18n("File to load"));
aboutData.setupCommandLine(&parser);
parser.process(app);
aboutData.processCommandLine(&parser);
app.setWindowIcon(QIcon::fromTheme(QStringLiteral("kpat")));
QString savegame = parser.value( QStringLiteral("solvegame") );
if ( !savegame.isEmpty() )
{
QFile of(savegame);
of.open(QIODevice::ReadOnly);
QDomDocument doc;
doc.setContent(&of);
DealerScene *f = getDealer( doc.documentElement().attribute(QStringLiteral("id")).toInt() );
f->loadLegacyFile( &of );
f->solver()->translate_layout();
int ret = f->solver()->patsolve();
if ( ret == Solver::SolutionExists )
fprintf( stdout, "won\n");
else if ( ret == Solver::NoSolutionExists )
fprintf( stdout, "lost\n" );
else
fprintf( stdout, "unknown\n");
return 0;
}
QString testdir = parser.value(QStringLiteral("testdir"));
if ( !testdir.isEmpty() ) {
qsrand(std::time(nullptr));
if ( parser.isSet(QStringLiteral("generate")) ) {
for (int dealer = 0; dealer < 20; dealer++) {
DealerScene *f = getDealer( dealer );
if (!f) continue;
int count = 100;
QTime mytime;
while (count) {
if (f->deck()) f->deck()->stopAnimations();
int i = qrand() % INT_MAX;
f->startNew( i );
mytime.start();
f->solver()->translate_layout();
int ret = f->solver()->patsolve();
if ( ret == Solver::SolutionExists ) {
fprintf( stdout, "%d: %d won (%d ms)\n", dealer, i, mytime.elapsed() );
count--;
QFile file(QStringLiteral("%1/%2-%3-1").arg(testdir).arg(dealer).arg(i));
file.open( QFile::WriteOnly );
f->saveLegacyFile( &file );
}
else if ( ret == Solver::NoSolutionExists ) {
fprintf( stdout, "%d: %d lost (%d ms)\n", dealer, i, mytime.elapsed() );
count--;
QFile file(QStringLiteral("%1/%2-%3-0").arg(testdir).arg(dealer).arg(i));
file.open( QFile::WriteOnly );
f->saveLegacyFile( &file );
} else {
fprintf( stdout, "%d: %d unknown (%d ms)\n", dealer, i, mytime.elapsed() );
}
}
}
}
return 0;
}
bool ok = false;
int wanted_game = -1;
if ( parser.isSet( QStringLiteral("solve") ) )
wanted_game = parser.value(QStringLiteral("solve")).toInt( &ok );
if ( ok )
{
ok = false;
int end_index = -1;
if ( parser.isSet( QStringLiteral("end") ) )
end_index = parser.value(QStringLiteral("end")).toInt( &ok );
if ( !ok )
end_index = -1;
ok = false;
int start_index = -1;
if ( parser.isSet( QStringLiteral("start") ) )
start_index = parser.value(QStringLiteral("start")).toInt( &ok );
if ( !ok ) {
start_index = 0;
end_index = INT_MAX;
} else {
if ( end_index == -1 )
end_index = start_index;
}
DealerScene *f = getDealer( wanted_game );
if ( !f )
return 1;
QTime mytime;
for ( int i = start_index; i <= end_index; i++ )
{
mytime.start();
f->deck()->stopAnimations();
f->startNew( i );
f->solver()->translate_layout();
int ret = f->solver()->patsolve();
if ( ret == Solver::SolutionExists )
fprintf( stdout, "%d won (%d ms)\n", i, mytime.elapsed() );
else if ( ret == Solver::NoSolutionExists )
fprintf( stdout, "%d lost (%d ms)\n", i, mytime.elapsed() );
else
fprintf( stdout, "%d unknown (%d ms)\n", i, mytime.elapsed() );
}
fprintf( stdout, "all_moves %ld\n", all_moves );
return 0;
}
QString gametype = parser.value(QStringLiteral("gametype")).toLower();
QFile savedState( QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + QLatin1Char('/') + saved_state_file);
MainWindow *w = new MainWindow;
if (parser.positionalArguments().count())
{
if ( !w->loadGame( QUrl::fromLocalFile(parser.positionalArguments().at( 0 )), true ) )
w->slotShowGameSelectionScreen();
}
else if (indexMap.contains(gametype))
{
w->slotGameSelected(indexMap.value(gametype));
}
else if (savedState.exists())
{
if ( !w->loadGame( QUrl::fromLocalFile(savedState.fileName()), false ) )
w->slotShowGameSelectionScreen();
}
else
{
w->slotShowGameSelectionScreen();
}
w->show();
const KDBusService dbusService(KDBusService::Multiple);
return app.exec();
}