| 1 | /*************************************************************************** |
| 2 | * Copyright (C) 2002-2003 Andi Peredri * |
| 3 | * [email protected] * |
| 4 | * Copyright (C) 2004-2007 Artur Wiebe * |
| 5 | * [email protected] * |
| 6 | * * |
| 7 | * This program is free software; you can redistribute it and/or modify * |
| 8 | * it under the terms of the GNU General Public License as published by * |
| 9 | * the Free Software Foundation; either version 2 of the License, or * |
| 10 | * (at your option) any later version. * |
| 11 | * * |
| 12 | * This program is distributed in the hope that it will be useful, * |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 15 | * GNU General Public License for more details. * |
| 16 | * * |
| 17 | * You should have received a copy of the GNU General Public License * |
| 18 | * along with this program; if not, write to the * |
| 19 | * Free Software Foundation, Inc., * |
| 20 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
| 21 | ***************************************************************************/ |
| 22 | #include <QFile> |
| 23 | #include <QTextStream> |
| 24 | #include <QProgressDialog> |
| 25 | #include <QDebug> |
| 26 | |
| 27 | #include "checkers.h" |
| 28 | #include "pdn.h" |
| 29 | |
| 30 | |
| 31 | #define PLAYER true |
| 32 | #define COMPUTER false |
| 33 | |
| 34 | #define END_OF_MOVELINE "@." |
| 35 | |
| 36 | |
| 37 | Pdn::Pdn() |
| 38 | { |
| 39 | } |
| 40 | |
| 41 | |
| 42 | Pdn::~Pdn() |
| 43 | { |
| 44 | qDeleteAll(m_database); |
| 45 | m_database.clear(); |
| 46 | } |
| 47 | |
| 48 | |
| 49 | bool Pdn::open(const QString& filename, QWidget* parent, |
| 50 | const QString& label, QString& text_to_log) |
| 51 | { |
| 52 | qDeleteAll(m_database); |
| 53 | m_database.clear(); |
| 54 | |
| 55 | QFile file(filename); |
| 56 | if(!file.open(QFile::ReadOnly)) { |
| 57 | return false; |
| 58 | } |
| 59 | |
| 60 | QTextStream ts(&file); |
| 61 | |
| 62 | QString str1, str2; |
| 63 | |
| 64 | QProgressDialog progress(parent); |
| 65 | progress.setModal(true); |
| 66 | progress.setLabelText(label); |
| 67 | progress.setRange(0, file.size()); |
| 68 | progress.setMinimumDuration(0); |
| 69 | |
| 70 | unsigned int line_nr = 1; |
| 71 | unsigned int game_started = 1; |
| 72 | bool in_tags = false; |
| 73 | while(!ts.atEnd()) { |
| 74 | str1 = ts.readLine().trimmed(); |
| 75 | if(ts.atEnd()) { |
| 76 | str2 += str1; |
| 77 | } |
| 78 | |
| 79 | if((str1.length() && str1[0]=='[') || ts.atEnd()) { |
| 80 | if(!in_tags) { |
| 81 | // tags begin again, so a game is ended. |
| 82 | if(str2.length()) { |
| 83 | if((m_database.count()%10)==0) { |
| 84 | progress.setValue(file.pos()); |
| 85 | } |
| 86 | |
| 87 | QString log_txt; |
| 88 | PdnGame* game = new PdnGame(str2, log_txt); |
| 89 | m_database.append(game); |
| 90 | |
| 91 | if(log_txt.length()) { |
| 92 | text_to_log |
| 93 | += QString("%1. game begins at line %2:\n") |
| 94 | .arg(m_database.count()) |
| 95 | .arg(game_started); |
| 96 | text_to_log += log_txt; |
| 97 | } |
| 98 | |
| 99 | game_started = line_nr; |
| 100 | str2=""; |
| 101 | } |
| 102 | |
| 103 | in_tags = true; |
| 104 | } |
| 105 | } else { |
| 106 | if(in_tags) { |
| 107 | in_tags = false; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | str2.append(str1+"\n"); |
| 112 | |
| 113 | if(progress.wasCanceled()) { |
| 114 | break; |
| 115 | } |
| 116 | |
| 117 | line_nr++; |
| 118 | } |
| 119 | |
| 120 | file.close(); |
| 121 | |
| 122 | return true; |
| 123 | } |
| 124 | |
| 125 | |
| 126 | bool Pdn::save(const QString& filename) |
| 127 | { |
| 128 | QFile file(filename); |
| 129 | if(!file.open(QFile::WriteOnly)) { |
| 130 | return false; |
| 131 | } |
| 132 | |
| 133 | QTextStream ts(&file); |
| 134 | |
| 135 | foreach(PdnGame* game, m_database) { |
| 136 | ts << game->toString() << endl << endl; |
| 137 | } |
| 138 | |
| 139 | file.close(); |
| 140 | return true; |
| 141 | } |
| 142 | |
| 143 | |
| 144 | |
| 145 | PdnGame* Pdn::newGame() |
| 146 | { |
| 147 | QString log_txt; // here: ignore TODO |
| 148 | PdnGame* game = new PdnGame("", log_txt); |
| 149 | m_database.append(game); |
| 150 | return game; |
| 151 | } |
| 152 | |
| 153 | |
| 154 | /*************************************************************************** |
| 155 | * * |
| 156 | * * |
| 157 | ***************************************************************************/ |
| 158 | PdnMove::PdnMove(QString line) |
| 159 | { |
| 160 | if(line[0]=='{') { |
| 161 | qDebug("a move must not begin with a comment."); |
| 162 | return; |
| 163 | } |
| 164 | |
| 165 | // first move. |
| 166 | m_first = line.section(' ', 0, 0); |
| 167 | line = line.mid(m_first.length()).trimmed(); |
| 168 | |
| 169 | // check for a first comment. |
| 170 | if(line[0]=='{') { |
| 171 | int end = line.indexOf('}', 1); |
| 172 | if(end>=0) { |
| 173 | m_comfirst = line.mid(1, end-1); |
| 174 | line.remove(0, end+1); |
| 175 | line = line.trimmed(); |
| 176 | } else { |
| 177 | qDebug("no comment ending of the first comment."); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | // second move. |
| 182 | m_second = line.section(' ', 0, 0); |
| 183 | line = line.mid(m_second.length()).trimmed(); |
| 184 | |
| 185 | // check for a second comment. |
| 186 | if(line[0]=='{') { |
| 187 | int end = line.indexOf('}', 1); |
| 188 | if(end>=0) { |
| 189 | m_comsecond = line.mid(1, end-1); |
| 190 | } else { |
| 191 | qDebug("no comment ending of the second comment."); |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | |
| 197 | /*************************************************************************** |
| 198 | * * |
| 199 | * * |
| 200 | ***************************************************************************/ |
| 201 | PdnGame::PdnGame(const QString& game_string, QString& log_txt) |
| 202 | { |
| 203 | white = PLAYER; |
| 204 | for(int i=0; i<12; i++) { |
| 205 | board[i]=MAN2; |
| 206 | } |
| 207 | for(int i=20; i<32; i++) { |
| 208 | board[i]=MAN1; |
| 209 | } |
| 210 | |
| 211 | if(!parse(game_string, log_txt)) { |
| 212 | qDebug(" errors occured while processing game."); // TODO |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | |
| 217 | PdnGame::~PdnGame() |
| 218 | { |
| 219 | qDeleteAll(m_moves); |
| 220 | m_moves.clear(); |
| 221 | } |
| 222 | |
| 223 | |
| 224 | QString PdnGame::get(Tag tag) const |
| 225 | { |
| 226 | switch(tag) { |
| 227 | case Date: return pdnDate; |
| 228 | case Site: return pdnSite; |
| 229 | case Type: return pdnType; |
| 230 | case Event: return pdnEvent; |
| 231 | case Round: return pdnRound; |
| 232 | case White: return pdnWhite; |
| 233 | case Black: return pdnBlack; |
| 234 | default: return pdnResult; |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | |
| 239 | void PdnGame::set(Tag tag, const QString& string) |
| 240 | { |
| 241 | switch(tag) { |
| 242 | case Date: pdnDate=string; break; |
| 243 | case Site: pdnSite=string; break; |
| 244 | case Type: pdnType=string; break; |
| 245 | case Event: pdnEvent=string; break; |
| 246 | case Round: pdnRound=string; break; |
| 247 | case White: pdnWhite=string; break; |
| 248 | case Black: pdnBlack=string; break; |
| 249 | default: pdnResult=string; |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | |
| 254 | bool PdnGame::parse_moves(const QString& line) |
| 255 | { |
| 256 | qDeleteAll(m_moves); |
| 257 | m_moves.clear(); |
| 258 | |
| 259 | QStringList list = line.split(' '); |
| 260 | |
| 261 | QString current_move; |
| 262 | int move_num = 0; |
| 263 | bool in_comment = false; |
| 264 | foreach(QString str, list) { |
| 265 | if(str.startsWith("{")) { |
| 266 | in_comment = true; |
| 267 | } |
| 268 | if(str.endsWith("}")) { |
| 269 | in_comment = false; |
| 270 | } |
| 271 | |
| 272 | if(str.endsWith(".") && !in_comment) { |
| 273 | if(str!=END_OF_MOVELINE) { |
| 274 | if((move_num+1) != str.mid(0, str.length()-1).toInt()) { |
| 275 | qDebug() << "Move num expected:" << move_num+1 |
| 276 | << "received:" << str; |
| 277 | return false; |
| 278 | } |
| 279 | move_num++; |
| 280 | } |
| 281 | |
| 282 | current_move = current_move.trimmed(); |
| 283 | if(current_move.length()) { |
| 284 | m_moves.append(new PdnMove(current_move)); |
| 285 | current_move = ""; |
| 286 | } |
| 287 | continue; |
| 288 | } |
| 289 | |
| 290 | if(str.isEmpty()) { |
| 291 | current_move += " "; |
| 292 | } else { |
| 293 | current_move += str + " "; |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | return true; |
| 298 | } |
| 299 | |
| 300 | |
| 301 | bool PdnGame::parse(const QString& pdngame, QString& log_txt) |
| 302 | { |
| 303 | QString fen; |
| 304 | QString moves; |
| 305 | int num = pdngame.count("\n"); // Number of lines |
| 306 | |
| 307 | for(int i=0; i<=num; i++) { |
| 308 | QString line = pdngame.section('\n',i ,i); |
| 309 | if(!line.length()) { |
| 310 | continue; |
| 311 | } |
| 312 | |
| 313 | if(line.startsWith("[")) { |
| 314 | line.remove(0, 1); |
| 315 | line = line.trimmed(); |
| 316 | |
| 317 | if(line.startsWith("GameType")) { |
| 318 | pdnType=line.section('"',1,1); |
| 319 | } else if(line.startsWith("FEN")) { |
| 320 | fen=line.section('"',1,1); |
| 321 | } else if(line.startsWith("Date")) { |
| 322 | pdnDate=line.section('"',1,1); |
| 323 | } else if(line.startsWith("Site")) { |
| 324 | pdnSite=line.section('"',1,1); |
| 325 | } else if(line.startsWith("Event")) { |
| 326 | pdnEvent=line.section('"',1,1); |
| 327 | } else if(line.startsWith("Round")) { |
| 328 | pdnRound=line.section('"',1,1); |
| 329 | } else if(line.startsWith("White")) { |
| 330 | pdnWhite=line.section('"',1,1); |
| 331 | } else if(line.startsWith("Black")) { |
| 332 | pdnBlack=line.section('"',1,1); |
| 333 | } else if(line.startsWith("Result")) { |
| 334 | pdnResult=line.section('"',1,1); |
| 335 | } else { |
| 336 | ; // Skip other unsupported tags |
| 337 | } |
| 338 | |
| 339 | } else { |
| 340 | moves += " " + line; |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | // parse move section. |
| 345 | if(moves.endsWith(pdnResult)) { |
| 346 | moves.truncate(moves.length()-pdnResult.length()); |
| 347 | } else { |
| 348 | log_txt += " +Different result at the end of the movelist:\n" |
| 349 | + QString(" \"%1\" expected, got \"%2\"\n") |
| 350 | .arg(pdnResult) |
| 351 | .arg(moves.right(pdnResult.length())); |
| 352 | |
| 353 | // need to remove the incorrect result. |
| 354 | if(moves.endsWith(" *")) { |
| 355 | log_txt += " => Ignoring \" *\" from the end.\n"; |
| 356 | moves.truncate(moves.length()-2); |
| 357 | } else { |
| 358 | int pos = moves.lastIndexOf('-') - 1; |
| 359 | bool skip_ws = true; |
| 360 | for(int i=pos; i>=0; i--) { |
| 361 | if(moves[i]==' ') { |
| 362 | if(!skip_ws) { |
| 363 | log_txt += " => Ignoring \"" |
| 364 | + moves.right(moves.length()-i-1) |
| 365 | + "\" from the end.\n", |
| 366 | moves.truncate(i+1); |
| 367 | break; |
| 368 | } |
| 369 | } else { |
| 370 | skip_ws = false; |
| 371 | } |
| 372 | } |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | if(!parse_moves(moves+" "END_OF_MOVELINE)) { // :) |
| 377 | log_txt += "\n +parsing moves failed."; |
| 378 | return false; |
| 379 | } |
| 380 | |
| 381 | // Translation of the GameType tag |
| 382 | switch(pdnType.toInt()) { |
| 383 | case ENGLISH: |
| 384 | case RUSSIAN: |
| 385 | break; |
| 386 | default: |
| 387 | // log_txt += "\n +setting game type to english."; |
| 388 | pdnType.setNum(ENGLISH); |
| 389 | break; |
| 390 | } |
| 391 | |
| 392 | // Parsing of the Forsyth-Edwards Notation (FEN) tag |
| 393 | if(fen.isNull()) { |
| 394 | return true; |
| 395 | } |
| 396 | |
| 397 | fen=fen.trimmed(); |
| 398 | |
| 399 | for(int i=fen.indexOf(" "); i!=-1; i=fen.indexOf(" ")) { |
| 400 | fen=fen.remove(i,1); |
| 401 | } |
| 402 | |
| 403 | if(fen.startsWith("W:W")) { |
| 404 | white=PLAYER; |
| 405 | } else if(fen.startsWith("B:W")) { |
| 406 | white=COMPUTER; |
| 407 | } else { |
| 408 | return false; |
| 409 | } |
| 410 | |
| 411 | QString string = fen.mid(3).section(":B",0,0); |
| 412 | if(!parse(string, white)) { |
| 413 | return false; |
| 414 | } |
| 415 | |
| 416 | string=fen.section(":B",1,1); |
| 417 | if(string.endsWith(".")) { |
| 418 | string.truncate(string.length()-1); |
| 419 | } |
| 420 | if(!parse(string, !white)) { |
| 421 | return false; |
| 422 | } |
| 423 | |
| 424 | return true; |
| 425 | } |
| 426 | |
| 427 | |
| 428 | bool PdnGame::parse(const QString& str, bool side) |
| 429 | { |
| 430 | QString notation; |
| 431 | |
| 432 | if(pdnType.toInt() == ENGLISH) { |
| 433 | notation=QString(ENOTATION); |
| 434 | } else { |
| 435 | notation=QString(RNOTATION); |
| 436 | } |
| 437 | |
| 438 | QStringList sections = str.split(","); |
| 439 | foreach(QString pos, sections) { |
| 440 | bool king=false; |
| 441 | |
| 442 | if(pos.startsWith("K")) { |
| 443 | pos=pos.remove(0,1); |
| 444 | king=true; |
| 445 | } |
| 446 | if(pos.length()==1) { |
| 447 | pos.append(' '); |
| 448 | } |
| 449 | if(pos.length()!=2) { |
| 450 | return false; |
| 451 | } |
| 452 | |
| 453 | int index = notation.indexOf(pos); |
| 454 | if(index%2) { |
| 455 | index=notation.indexOf(pos,index+1); |
| 456 | } |
| 457 | if(index == -1) { |
| 458 | return false; |
| 459 | } |
| 460 | |
| 461 | if(white==COMPUTER) { |
| 462 | index=62-index; |
| 463 | } |
| 464 | |
| 465 | if(side==PLAYER) { |
| 466 | board[index/2]=(king ? KING1 : MAN1); |
| 467 | } else { |
| 468 | board[index/2]=(king ? KING2 : MAN2); |
| 469 | } |
| 470 | } |
| 471 | return true; |
| 472 | } |
| 473 | |
| 474 | |
| 475 | PdnMove* PdnGame::getMove(int i) |
| 476 | { |
| 477 | if(i<m_moves.count()) { |
| 478 | return m_moves.at(i); |
| 479 | } |
| 480 | |
| 481 | // TODO - do we need this? |
| 482 | if(i>m_moves.count()) { |
| 483 | qDebug("PdnGame::getMove(%u) m_moves.count()=%u", |
| 484 | i, m_moves.count()); |
| 485 | } |
| 486 | |
| 487 | PdnMove* m = new PdnMove(""); |
| 488 | m_moves.append(m); |
| 489 | return m; |
| 490 | } |
| 491 | |
| 492 | |
| 493 | QString PdnGame::toString() |
| 494 | { |
| 495 | QString fen; |
| 496 | QString moves; |
| 497 | |
| 498 | /* |
| 499 | * fen |
| 500 | */ |
| 501 | if(!movesCount()) { |
| 502 | qDebug("FEN tag with lots of errors."); |
| 503 | |
| 504 | QString string1; |
| 505 | QString string2; |
| 506 | QString notation; |
| 507 | |
| 508 | if(pdnType.toInt() == ENGLISH) { |
| 509 | notation=QString(ENOTATION); |
| 510 | } else { |
| 511 | notation=QString(RNOTATION); |
| 512 | } |
| 513 | |
| 514 | for(int i=0; i<32; i++) { |
| 515 | int index=i*2; |
| 516 | if(white==COMPUTER) { |
| 517 | index=62-index; |
| 518 | } |
| 519 | |
| 520 | QString pos; |
| 521 | |
| 522 | switch(board[i]) { |
| 523 | case KING1: |
| 524 | pos.append('K'); |
| 525 | case MAN1: |
| 526 | pos.append(notation.mid(index,2).trimmed()); |
| 527 | if(string1.length()) { |
| 528 | string1.append(','); |
| 529 | } |
| 530 | string1.append(pos); |
| 531 | break; |
| 532 | |
| 533 | case KING2: |
| 534 | pos.append('K'); |
| 535 | case MAN2: |
| 536 | pos.append(notation.mid(index,2).trimmed()); |
| 537 | if(string2.length()) { |
| 538 | string2.append(','); |
| 539 | } |
| 540 | string2.append(pos); |
| 541 | default: |
| 542 | break; |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | if(white==PLAYER) { |
| 547 | fen.append("W:W"+string1+":B"+string2+"."); |
| 548 | } else { |
| 549 | fen.append("B:W"+string2+":B"+string1+"."); |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | /* |
| 554 | * moves |
| 555 | */ |
| 556 | unsigned int count = 1; |
| 557 | foreach(PdnMove* move, m_moves) { |
| 558 | moves += QString("%1. %2 %3%4%5\n") |
| 559 | .arg(count) |
| 560 | .arg(move->m_first) |
| 561 | .arg(move->m_comfirst.length() ? "{"+move->m_comfirst+"} " : "") |
| 562 | .arg(move->m_second) |
| 563 | .arg(move->m_comsecond.length() ? " {"+move->m_comsecond+"}" : ""); |
| 564 | count++; |
| 565 | } |
| 566 | |
| 567 | |
| 568 | /* |
| 569 | * create format and write tags+fen+moves. |
| 570 | */ |
| 571 | QString str; |
| 572 | |
| 573 | if(pdnEvent.length()) { |
| 574 | str.append("[Event \""+pdnEvent+"\"]\n"); |
| 575 | } |
| 576 | if(pdnSite.length()) { |
| 577 | str.append("[Site \"" +pdnSite +"\"]\n"); |
| 578 | } |
| 579 | if(pdnDate.length()) { |
| 580 | str.append("[Date \"" +pdnDate +"\"]\n"); |
| 581 | } |
| 582 | if(pdnRound.length()) { |
| 583 | str.append("[Round \""+pdnRound+"\"]\n"); |
| 584 | } |
| 585 | if(pdnWhite.length()) { |
| 586 | str.append("[White \""+pdnWhite+"\"]\n"); |
| 587 | } |
| 588 | if(pdnBlack.length()) { |
| 589 | str.append("[Black \""+pdnBlack+"\"]\n"); |
| 590 | } |
| 591 | |
| 592 | if(fen.length()) { |
| 593 | str.append("[SetUp \"1\"]\n"); |
| 594 | str.append("[FEN \""+fen+"\"]\n\n"); |
| 595 | } |
| 596 | |
| 597 | str.append("[Result \""+pdnResult+"\"]\n"); |
| 598 | str.append("[GameType \""+pdnType+"\"]\n"); |
| 599 | str.append(moves); |
| 600 | str.append(pdnResult+"\n"); |
| 601 | |
| 602 | return str; |
| 603 | } |