[go: up one dir, main page]

File: pdn.cc

package info (click to toggle)
kcheckers 0.8.1-6
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 796 kB
  • sloc: cpp: 3,852; makefile: 19
file content (541 lines) | stat: -rw-r--r-- 12,437 bytes parent folder | download | duplicates (4)
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
/***************************************************************************
 *   Copyright (C) 2002-2003 Andi Peredri                                  *
 *   andi@ukr.net                                                          *
 *   Copyright (C) 2004-2005 Artur Wiebe                                   *
 *   wibix@gmx.de                                                          *
 *                                                                         *
 *   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, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include <QFile>
#include <QTextStream>
#include <QtWidgets/QProgressDialog>
#include <QDebug>

#include "checkers.h"
#include "pdn.h"


#define PLAYER	true
#define COMPUTER  false

#define END_OF_MOVELINE		"@."


Pdn::Pdn()
{
}


Pdn::~Pdn()
{
	qDeleteAll(m_database);
	m_database.clear();
}


bool Pdn::open(const QString& filename, QWidget* parent,
		const QString& label, QString& text_to_log)
{
	qDeleteAll(m_database);
	m_database.clear();

	QFile file(filename);
	if(!file.open(QFile::ReadOnly)) return false;

	QTextStream ts(&file);

	QString str1, str2;

	QProgressDialog progress(parent);
	progress.setModal(true);
	progress.setLabelText(label);
	progress.setRange(0, file.size());
	progress.setMinimumDuration(0);

	unsigned int line_nr = 1;
	unsigned int game_started = 1;
	bool in_tags = false;
	while(!ts.atEnd()) {
		str1 = ts.readLine().trimmed();
		if(ts.atEnd())
			str2 += str1;

		if((str1.length() && str1[0]=='[') || ts.atEnd()) {
			if(!in_tags) {
				// tags begin again, so a game is ended.
				if(str2.length()) {
					if((m_database.count()%10)==0)
						progress.setValue(file.pos());

					QString log_txt;
					PdnGame* game = new PdnGame(str2, log_txt);
					m_database.append(game);

					if(log_txt.length()) {
						text_to_log
							+= QString("%1. game begins at line %2:\n")
							.arg(m_database.count())
							.arg(game_started);
						text_to_log += log_txt;
					}

					game_started = line_nr;
					str2="";
				}

				in_tags = true;
			}
		} else {
			if(in_tags)
				in_tags = false;
		}

		str2.append(str1+"\n");

		if(progress.wasCanceled())
			break;

		line_nr++;
	}

	file.close();

	return true;
}


bool Pdn::save(const QString& filename)
{
	QFile file(filename);
	if(!file.open(QFile::WriteOnly))
		return false;

	QTextStream ts(&file);

	foreach(PdnGame* game, m_database) {
		ts << game->toString() << endl << endl;
	}

	file.close();
	return true;
}



PdnGame* Pdn::newGame()
{
	QString log_txt;		// here: ignore TODO
	PdnGame* game = new PdnGame("", log_txt);
	m_database.append(game);
	return game;
}


/***************************************************************************
 *																		 *
 *																		 *
 ***************************************************************************/
PdnMove::PdnMove(QString line)
{
	if(line[0]=='{') {
		qDebug("a move must not begin with a comment.");
		return;
	}

	// first move.
	m_first = line.section(' ', 0, 0);
	line = line.mid(m_first.length()).trimmed();

	// check for a first comment.
	if(line[0]=='{') {
		int end = line.indexOf('}', 1);
		if(end>=0) {
			m_comfirst = line.mid(1, end-1);
			line.remove(0, end+1);
			line = line.trimmed();
		} else
			qDebug("no comment ending of the first comment.");
	}

	// second move.
	m_second = line.section(' ', 0, 0);
	line = line.mid(m_second.length()).trimmed();

	// check for a second comment.
	if(line[0]=='{') {
		int end = line.indexOf('}', 1);
		if(end>=0)
			m_comsecond = line.mid(1, end-1);
		else
			qDebug("no comment ending of the second comment.");
	}
}


/***************************************************************************
 *																		 *
 *																		 *
 ***************************************************************************/
PdnGame::PdnGame(const QString& game_string, QString& log_txt)
{
	white = PLAYER;
	for(int i=0;  i<12; i++)
		board[i]=MAN2;
	for(int i=20; i<32; i++)
		board[i]=MAN1;

	if(!parse(game_string, log_txt)) {
		qDebug("  errors occured while processing game.");	// TODO
	}
}


PdnGame::~PdnGame()
{
	qDeleteAll(m_moves);
	m_moves.clear();
}


QString PdnGame::get(Tag tag) const
{
	switch(tag) {
	case Date:  return pdnDate;
	case Site:  return pdnSite;
	case Type:  return pdnType;
	case Event: return pdnEvent;
	case Round: return pdnRound;
	case White: return pdnWhite;
	case Black: return pdnBlack;
	default:	return pdnResult;
	}
}


void PdnGame::set(Tag tag, const QString& string)
{
	switch(tag) {
	case Date:  pdnDate=string;		break;
	case Site:  pdnSite=string;		break;
	case Type:  pdnType=string;		break;
	case Event: pdnEvent=string;break;
	case Round: pdnRound=string;break;
	case White: pdnWhite=string;break;
	case Black: pdnBlack=string;break;
	default:	pdnResult=string;
	}
}


bool PdnGame::parse_moves(const QString& line)
{
	qDeleteAll(m_moves);
	m_moves.clear();

	QStringList list = line.split(' ');

	QString current_move;
	int move_num = 0;
	bool in_comment = false;
	foreach(QString str, list) {
		if(str.startsWith("{"))
			in_comment = true;
		if(str.endsWith("}"))
			in_comment = false;
		
		if(str.endsWith(".") && !in_comment) {
			if(str!=END_OF_MOVELINE) {
				if((move_num+1) != str.mid(0, str.length()-1).toInt()) {
					qDebug() << "Move num expected:" << move_num+1
						<< "received:" << str;
					return false;
				}
				move_num++;
			}

			current_move = current_move.trimmed();
			if(current_move.length()) {
				m_moves.append(new PdnMove(current_move));
				current_move = "";
			}
			continue;
		}

			if(str.isEmpty())
			current_move += " ";
		else
			current_move += str + " ";
	}

	return true;
}


bool PdnGame::parse(const QString& pdngame, QString& log_txt)
{
	QString fen;
	QString moves;
	int num = pdngame.count("\n");	// Number of lines

	for(int i=0; i<=num; i++) {
		QString line = pdngame.section('\n',i ,i);
		if(!line.length())
			continue;

		if(line.startsWith("[")) {
			line.remove(0, 1);
			line = line.trimmed();

			if(line.startsWith("GameType"))	  pdnType=line.section('"',1,1);
			else if(line.startsWith("FEN"))		  fen=line.section('"',1,1);
			else if(line.startsWith("Date"))	 pdnDate=line.section('"',1,1);
			else if(line.startsWith("Site"))	 pdnSite=line.section('"',1,1);
			else if(line.startsWith("Event"))   pdnEvent=line.section('"',1,1);
			else if(line.startsWith("Round"))   pdnRound=line.section('"',1,1);
			else if(line.startsWith("White"))   pdnWhite=line.section('"',1,1);
			else if(line.startsWith("Black"))   pdnBlack=line.section('"',1,1);
			else if(line.startsWith("Result")) pdnResult=line.section('"',1,1);
			else ;  // Skip other unsupported tags

		} else {
			moves += " " + line;
		}
	}

	// parse move section.
	if(moves.endsWith(pdnResult))
		moves.truncate(moves.length()-pdnResult.length());
	else {
		log_txt += "  +Different result at the end of the movelist:\n"
			+ QString("	  \"%1\" expected, got \"%2\"\n")
			.arg(pdnResult)
			.arg(moves.right(pdnResult.length()));

		// need to remove the incorrect result.
		if(moves.endsWith(" *")) {
			log_txt += "	  => Ignoring \" *\" from the end.\n";
			moves.truncate(moves.length()-2);
		} else {
			int pos = moves.lastIndexOf('-') - 1;
			bool skip_ws = true;
			for(int i=pos; i>=0; i--) {
				if(moves[i]==' ') {
					if(!skip_ws) {
						log_txt += "	  => Ignoring \""
							+ moves.right(moves.length()-i-1)
							+ "\" from the end.\n",
						moves.truncate(i+1);
						break;
					}
				} else {
					skip_ws = false;
				}
			}
		}
	}

	if(!parse_moves(moves+" "END_OF_MOVELINE)) {		// :)
		log_txt += "\n +parsing moves failed.";
		return false;
	}

	// Translation of the GameType tag
	switch(pdnType.toInt()) {
	case ENGLISH:
	case RUSSIAN:
		break;
	default:
//		log_txt += "\n +setting game type to english.";
		pdnType.setNum(ENGLISH);
		break;
	}

	// Parsing of the Forsyth-Edwards Notation (FEN) tag
	if(fen.isNull())
		return true;

	fen=fen.trimmed();

	for(int i=fen.indexOf(" "); i!=-1; i=fen.indexOf(" "))
		fen=fen.remove(i,1);

	if(fen.startsWith("W:W"))
		white=PLAYER;
	else if(fen.startsWith("B:W"))
		white=COMPUTER;
	else
		return false;

	QString string = fen.mid(3).section(":B",0,0);
	if(!parse(string, white))
		return false;

	string=fen.section(":B",1,1);
	if(string.endsWith("."))
		string.truncate(string.length()-1);
	if(!parse(string, !white))
		return false;

	return true;
}


bool PdnGame::parse(const QString& str, bool side)
{
	QString notation;

	if(pdnType.toInt() == ENGLISH)
		notation=QString(ENOTATION);
	else
		notation=QString(RNOTATION);

	QStringList sections = str.split(",");
	foreach(QString pos, sections) {
		bool king=false;

		if(pos.startsWith("K")) {
			pos=pos.remove(0,1);
			king=true;
		}
		if(pos.length()==1)
			pos.append(' ');
		if(pos.length()!=2)
			return false;

		int index = notation.indexOf(pos);
		if(index%2)
			index=notation.indexOf(pos,index+1);
		if(index == -1)
			return false;

		if(white==COMPUTER)
			index=62-index;

		if(side==PLAYER)
			board[index/2]=(king ? KING1 : MAN1);
		else
			board[index/2]=(king ? KING2 : MAN2);
	}
	return true;
}


PdnMove* PdnGame::getMove(int i)
{
	if(i<m_moves.count()) {
		return m_moves.at(i);
	}

	// TODO - do we need this?
	if(i>m_moves.count())
		qDebug("PdnGame::getMove(%u) m_moves.count()=%u",
				i, m_moves.count());

	PdnMove* m = new PdnMove("");
	m_moves.append(m);
	return m;
}


QString PdnGame::toString()
{
	QString fen;
	QString moves;

	/*
	 * fen
	 */
	if(!movesCount()) {
			qDebug("FEN tag with lots of errors.");
		QString string1;
		QString string2;
		QString notation;

		if(pdnType.toInt() == ENGLISH)
			notation=QString(ENOTATION);
		else
				notation=QString(RNOTATION);

		for(int i=0; i<32; i++) {
			int index=i*2;
			if(white==COMPUTER) index=62-index;

			QString pos;

			switch(board[i]) {
			case KING1:
				pos.append('K');
			case MAN1:
				pos.append(notation.mid(index,2).trimmed());
				if(string1.length()) string1.append(',');
				string1.append(pos);
				break;
			case KING2:
				pos.append('K');
			case MAN2:
				pos.append(notation.mid(index,2).trimmed());
				if(string2.length()) string2.append(',');
				string2.append(pos);
			default:
				break;
			}
		}
			if(white==PLAYER)
				fen.append("W:W"+string1+":B"+string2+".");
		else
			fen.append("B:W"+string2+":B"+string1+".");
	}

	/*
	 * moves
	 */
	unsigned int count = 1;
	foreach(PdnMove* move, m_moves) {
		moves += QString("%1. %2 %3%4%5\n")
			.arg(count)
			.arg(move->m_first)
			.arg(move->m_comfirst.length() ? "{"+move->m_comfirst+"} " : "")
			.arg(move->m_second)
			.arg(move->m_comsecond.length() ? " {"+move->m_comsecond+"}" : "");
		count++;
	}


	/*
	 * create format and write tags+fen+moves.
	 */
	QString str;

	if(pdnEvent.length())		str.append("[Event \""+pdnEvent+"\"]\n");
	if(pdnSite.length())		str.append("[Site \"" +pdnSite +"\"]\n");
	if(pdnDate.length())		str.append("[Date \"" +pdnDate +"\"]\n");
	if(pdnRound.length())		str.append("[Round \""+pdnRound+"\"]\n");
	if(pdnWhite.length())		str.append("[White \""+pdnWhite+"\"]\n");
	if(pdnBlack.length())		str.append("[Black \""+pdnBlack+"\"]\n");

	if(fen.length()) {
		str.append("[SetUp \"1\"]\n");
		str.append("[FEN \""+fen+"\"]\n\n");
	}

	str.append("[Result \""+pdnResult+"\"]\n");
	str.append("[GameType \""+pdnType+"\"]\n");
	str.append(moves);
	str.append(pdnResult+"\n");

	return str;
}