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 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969
|
/* ****************************************************************************
This file is part of the game 'KJumpingCube'
Copyright (C) 1998-2000 by Matthias Kiefer <matthias.kiefer@gmx.de>
Copyright (C) 2012-2013 by Ian Wadham <iandw.au@gmail.com>
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.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**************************************************************************** */
#include "game.h"
#include "version.h"
#include "ai_main.h"
#include "ai_box.h"
#include "kcubeboxwidget.h"
#include "settingswidget.h"
#include <KConfigDialog> // IDW test.
#include <KLocalizedString>
#include <KMessageBox>
#include <KFileDialog>
#include <KTemporaryFile>
#include <kio/netaccess.h>
#include <QTimer>
#include "prefs.h"
#define LARGE_NUMBER 999999
Game::Game (const int d, KCubeBoxWidget * view, QWidget * parent)
:
QObject ((QObject *) parent), // Delete Game when window is deleted.
m_activity (Idle),
m_waitingState (Nil),
m_waitingToMove (false),
m_moveNo (0), // Game not started.
m_endMoveNo (LARGE_NUMBER), // Game not finished.
m_interrupting (false),
m_newSettings (false),
m_parent (parent),
m_view (view),
m_settingsPage (0),
m_side (d),
m_currentPlayer (One),
m_index (0),
m_fullSpeed (false),
computerPlOne (false),
computerPlTwo (false),
m_pauseForComputer (false),
m_pauseForStep (false)
{
qDebug() << "CONSTRUCT Game: side" << m_side;
m_box = new AI_Box (this, m_side);
m_ai = new AI_Main (this, m_side);
m_steps = new QList<int>;
connect (m_view, SIGNAL(mouseClick(int,int)), SLOT(startHumanMove(int,int)));
connect (m_ai, SIGNAL(done(int)), SLOT(moveCalculationDone(int)));
connect (m_view, SIGNAL(animationDone(int)), SLOT(animationDone(int)));
}
Game::~Game()
{
if ((m_activity != Idle) && (m_activity != Aborting)) {
shutdown();
}
delete m_steps;
}
void Game::gameActions (const int action)
{
qDebug() << "GAME ACTION IS" << action;
if ((m_activity != Idle) && (action != BUTTON) && (action != NEW)) {
m_view->showPopup (i18n("Sorry, doing a move..."));
return;
}
switch (action) {
case NEW:
newGame();
break;
case HINT:
if (! isComputer (m_currentPlayer)) {
KCubeWidget::enableClicks (false);
computeMove();
}
break;
case BUTTON:
buttonClick();
break;
case UNDO:
undo();
break;
case REDO:
redo();
break;
case SAVE:
case SAVE_AS:
saveGame (action == SAVE_AS);
break;
case LOAD:
loadGame();
break;
default:
break;
}
}
void Game::showWinner()
{
emit buttonChange (false, false, i18n("Game over"));
QString s = i18n("The winner is Player %1!", m_currentPlayer);
KMessageBox::information (m_view, s, i18n("Winner"));
}
void Game::showSettingsDialog (bool show)
{
// Show the Preferences/Settings/Configuration dialog.
KConfigDialog * settings = KConfigDialog::exists ("settings");
if (! settings) {
settings = new KConfigDialog (m_parent, "settings", Prefs::self());
settings->setFaceType (KPageDialog::Plain);
SettingsWidget * widget = new SettingsWidget (m_parent);
settings->addPage (widget, i18n("General"), "games-config-options");
connect (settings, SIGNAL(settingsChanged(QString)), SLOT(newSettings()));
m_settingsPage = widget; // Used when reverting/editing settings.
}
if (! show) return;
settings->show();
settings->raise(); // Force the dialog to be in front.
}
void Game::newSettings()
{
qDebug() << "NEW SETTINGS" << m_newSettings << "m_activity" << m_activity
<< "size:" << Prefs::cubeDim() << "m_side" << m_side;
loadImmediateSettings();
m_newSettings = true;
if (m_side != Prefs::cubeDim()) {
QMetaObject::invokeMethod (this, "newGame", Qt::QueuedConnection);
return;
}
// Waiting to move and not Hinting?
else if (m_waitingToMove && (m_activity == Idle)) {
loadPlayerSettings();
m_newSettings = false;
setUpNextTurn();
}
// Else, the remaining settings will be loaded at the next start of a turn.
// They set computer pause on/off and computer player 1 or 2 on/off.
}
void Game::loadImmediateSettings()
{
qDebug() << "GAME LOAD IMMEDIATE SETTINGS entered";
// Color changes can take place as soon as control returns to the event loop.
// Changes of animation type or speed will take effect next time there is an
// animation step to do, regardless of current activity.
bool reColorCubes = m_view->loadSettings();
m_fullSpeed = Prefs::animationNone();
m_pauseForStep = Prefs::pauseForStep();
if (reColorCubes) {
emit playerChanged (m_currentPlayer); // Re-display status bar icon.
}
// Choices of computer AIs and skills will take effect next time there is a
// computer move or hint. They will not affect any calculation in progress.
m_ai->setSkill (Prefs::skill1(), Prefs::kepler1(), Prefs::newton1(),
Prefs::skill2(), Prefs::kepler2(), Prefs::newton2());
qDebug() << "m_pauseForStep" << m_pauseForStep;
qDebug() << "PLAYER 1 settings: skill" << Prefs::skill1()
<< "Kepler" << Prefs::kepler1() << "Newton" << Prefs::newton1();
qDebug() << "PLAYER 2 settings: skill" << Prefs::skill2()
<< "Kepler" << Prefs::kepler2() << "Newton" << Prefs::newton2();
}
void Game::loadPlayerSettings()
{
qDebug() << "GAME LOAD PLAYER SETTINGS entered";
bool oldComputerPlayer = isComputer (m_currentPlayer);
m_pauseForComputer = Prefs::pauseForComputer();
computerPlOne = Prefs::computerPlayer1();
computerPlTwo = Prefs::computerPlayer2();
qDebug() << "AI 1" << computerPlOne << "AI 2" << computerPlTwo
<< "m_pauseForComputer" << m_pauseForComputer;
if (isComputer (m_currentPlayer) && (! oldComputerPlayer)) {
qDebug() << "New computer player set: must wait.";
m_waitingState = ComputerToMove; // New player: don't start playing yet.
}
}
void Game::startHumanMove (int x, int y)
{
int index = x * m_side + y;
bool humanPlayer = (! isComputer (m_currentPlayer));
qDebug() << "CLICK" << x << y << "index" << index;
if (! humanPlayer) {
buttonClick();
}
else if (humanPlayer && ((m_currentPlayer == m_box->owner(index)) ||
(m_box->owner(index) == Nobody))) {
m_waitingToMove = false;
m_moveNo++;
m_endMoveNo = LARGE_NUMBER;
qDebug() << "doMove (" << index;
KCubeWidget::enableClicks (false);
doMove (index);
}
}
void Game::setUpNextTurn()
{
// Called from newSettings(), new game, load, change player and undo/redo.
if (m_newSettings) {
m_newSettings = false;
loadPlayerSettings();
}
qDebug() << "setUpNextTurn" << m_currentPlayer
<< computerPlOne << computerPlTwo << "pause" << m_pauseForComputer
<< "wait" << m_waitingState << "waiting" << m_waitingToMove;
if (isComputer (m_currentPlayer)) {
// A computer player is to move.
qDebug() << "(m_pauseForComputer || (m_waitingState == ComputerToMove))"
<< (m_pauseForComputer || (m_waitingState == ComputerToMove));
if (m_pauseForComputer || (m_waitingState == ComputerToMove) ||
(m_moveNo == 0)) {
m_waitingState = ComputerToMove;
m_waitingToMove = true;
if (computerPlOne && computerPlTwo) {
if (m_moveNo == 0) {
emit buttonChange (true, false, i18n("Start game"));
}
else {
emit buttonChange (true, false, i18n("Continue game"));
}
}
else {
emit buttonChange (true, false, i18n("Start computer move"));
}
// Wait for a button-click to show that the user is ready.
qDebug() << "COMPUTER MUST WAIT";
KCubeWidget::enableClicks (true);
return;
}
// Start the computer's move.
qDebug() << "COMPUTER MUST MOVE";
m_waitingState = Nil;
m_waitingToMove = false;
KCubeWidget::enableClicks (false);
computeMove();
}
else {
// A human player is to move.
qDebug() << "HUMAN TO MOVE";
KCubeWidget::enableClicks (true);
m_waitingState = Nil;
m_waitingToMove = true;
if (computerPlOne || computerPlTwo) {
emit buttonChange (false, false, i18n("Your turn"));
}
else {
emit buttonChange (false, false, i18n("Player %1", m_currentPlayer));
}
// Wait for a click on the cube to be moved.
}
}
void Game::computeMove()
{
#if AILog > 1
t.start(); // Start timing the AI calculation.
#endif
m_view->setWaitCursor();
m_activity = Computing;
setStopAction();
emit setAction (HINT, false);
if (isComputer (m_currentPlayer)) {
emit statusMessage (i18n("Computer player %1 is moving")
.arg(m_currentPlayer), false);
}
m_ai->getMove (m_currentPlayer, m_box);
}
void Game::moveCalculationDone (int index)
{
// We do not care if we interrupted the computer. It was probably taking
// too long, so we will use the best move it had so far.
// We are starting a new game or closing KJumpingCube. See shutdown().
if (m_activity == Aborting) {
m_activity = Idle;
return;
}
m_activity = Idle;
if ((index < 0) || (index >= (m_side * m_side))) {
m_view->setNormalCursor();
KMessageBox::sorry (m_view,
i18n ("The computer could not find a valid move."));
// IDW TODO - What to do about state values and BUTTON ???
return;
}
#if AILog > 1
qDebug() << "TIME of MOVE" << t.elapsed();
qDebug() << "==============================================================";
#endif
// Blink the cube to be moved (twice).
m_view->startAnimation (false, index);
m_activity = ShowingMove;
setStopAction();
}
void Game::showingDone (int index)
{
if (isComputer (m_currentPlayer)) {
m_moveNo++;
m_endMoveNo = LARGE_NUMBER;
qDebug() << "m_moveNo" << m_moveNo << "isComputer()" << (isComputer (m_currentPlayer));
doMove (index); // Animate computer player's move.
}
else {
moveDone(); // Finish Hint animation.
setUpNextTurn(); // Wait: unless player setting changed.
}
}
void Game::doMove (int index)
{
bool computerMove = ((computerPlOne && m_currentPlayer == One) ||
(computerPlTwo && m_currentPlayer == Two));
// Make a copy of the position and player to move for the Undo function.
m_box->copyPosition (m_currentPlayer, computerMove, index);
#if AILog > 0
if (! computerMove) { // Record a human move in the statistics.
m_ai->postMove (m_currentPlayer, index, m_side);
}
#endif
emit setAction (UNDO, true); // Update Undo and Redo actions.
emit setAction (REDO, false);
m_steps->clear();
bool won = m_box->doMove (m_currentPlayer, index, 0, m_steps);
#if AILog > 1
qDebug() << "GAME WON?" << won << "STEPS" << (* m_steps);
// m_box->printBox();
#endif
if (m_steps->count() > 1) {
m_view->setWaitCursor(); //This will be a stoppable animation.
}
m_activity = AnimatingMove;
doStep();
}
void Game::doStep()
{
// Re-draw all cubes affected by a move, proceeding one step at a time.
int index;
bool startStep = true;
do {
if (! m_steps->isEmpty()) {
index = m_steps->takeFirst(); // Get a cube to be re-drawn.
// Check if the player wins at this step (no more cubes are re-drawn).
if (index == 0) {
moveDone();
m_endMoveNo = m_moveNo;
#if AILog > 0
qDebug() << "\nCALLING dumpStats()";
m_ai->dumpStats();
#endif
showWinner();
return;
}
// Update the view of a cube, either immediately or via animation.
startStep = (index > 0); // + -> increment, - -> expand.
index = startStep ? (index - 1) : (-index - 1);
int value = m_view->cubeValue (index); // Pre-update in view.
int max = m_box->maxValue (index);
if (startStep) { // Add 1 and take.
m_view->displayCube (index, m_currentPlayer, value + 1);
if ((value >= max) && (! m_fullSpeed)) {
m_view->highlightCube (index, true);
}
}
else if (m_fullSpeed) { // Decrease immediately.
m_view->displayCube (index, m_currentPlayer, value - max);
m_view->highlightCube (index, false); // Maybe user hit Stop.
}
else { // Animate cascade step.
if (m_pauseForStep && (m_waitingState != WaitingToStep)) {
// Pause: return the step to the list and wait for a buttonClick.
m_steps->prepend (-index - 1);
m_waitingState = WaitingToStep;
emit buttonChange (true, false, i18n("Show next step"));
return;
}
// Now set the button up and start the animation.
setStopAction();
m_view->startAnimation (true, index);
}
}
else {
// Views of the cubes at all steps of the move have been updated.
moveDone();
changePlayer();
return;
}
} while (startStep || m_fullSpeed);
}
void Game::stepAnimationDone (int index)
{
// Finished a move step. Decrease the cube that expanded and display it.
int value = m_view->cubeValue (index);
int max = m_box->maxValue (index);
m_view->displayCube (index, m_currentPlayer, value - max);
doStep(); // Do next animation step (if any).
}
void Game::moveDone()
{
// Called after non-animated move, animated move, end of game or hint action.
m_view->setNormalCursor();
emit statusMessage (QString(""), false); // Clear the status bar.
m_activity = Idle;
setAction (HINT, true);
m_fullSpeed = Prefs::animationNone();
m_view->hidePopup();
if (m_interrupting) {
m_interrupting = false;
m_waitingState = ComputerToMove;
}
}
Player Game::changePlayer()
{
m_currentPlayer = (m_currentPlayer == One) ? Two : One;
emit playerChanged (m_currentPlayer);
setUpNextTurn();
return m_currentPlayer;
}
void Game::buttonClick()
{
qDebug() << "BUTTON CLICK seen: waiting" << m_waitingToMove
<< "m_activity" << m_activity << "m_waitingState" << m_waitingState;
if (m_waitingState == Nil) { // Button is red: stop an activity.
if ((! m_pauseForComputer) && (! m_interrupting) &&
(computerPlOne && computerPlTwo)) {
m_interrupting = true; // Interrupt a non-stop AI v AI game.
m_view->showPopup (i18n("Finishing move..."));
setStopAction(); // Change to text for current activity.
}
else if (m_activity == Computing) {
m_ai->stop(); // Stop calculating a move or hint.
m_activity = Stopping;
}
else if (m_activity == ShowingMove) {
int index = m_view->killAnimation();
showingDone (index); // Stop showing where a move or hint is.
m_view->highlightCube (index, false);
}
else if (m_activity == AnimatingMove) {
int index = m_view->killAnimation();
m_fullSpeed = true; // Go to end of move right now, skipping
stepAnimationDone (index); // all later steps of animation.
}
}
else { // Button is green: start an activity.
switch (m_waitingState) {
case WaitingToStep:
doStep(); // Start next animation step.
break;
case ComputerToMove:
computeMove(); // Start next computer move.
break;
default:
break;
}
m_waitingState = Nil;
}
}
void Game::setStopAction()
{
// Red button setting for non-stop AI v. AI game.
if ((! m_pauseForComputer) && (! m_interrupting) &&
(computerPlOne && computerPlTwo)) {
if (m_activity == Computing) { // Starting AI v. AI move.
emit buttonChange (true, true, i18n("Interrupt game"));
}
return; // Continuing AI v. AI move.
}
// Red button settings for AI v. human, two human players or pausing game.
if (m_activity == Computing) { // Calculating hint or AI move.
emit buttonChange (true, true, i18n("Stop computing"));
}
else if (m_activity == ShowingMove) { // Showing hint or AI move.
emit buttonChange (true, true, i18n("Stop showing move"));
}
else if (m_activity == AnimatingMove) { // Animating AI or human move.
emit buttonChange (true, true, i18n("Stop animation"));
}
}
/* ************************************************************************** */
/* STANDARD GAME ACTIONS */
/* ************************************************************************** */
void Game::newGame()
{
qDebug() << "NEW GAME entered: waiting" << m_waitingToMove
<< "won?" << (m_moveNo >= m_endMoveNo);
if (newGameOK()) {
qDebug() << "QDEBUG: newGameOK() =" << true;
shutdown(); // Stop the current move (if any).
m_view->setNormalCursor();
m_view->hidePopup();
loadImmediateSettings();
loadPlayerSettings();
m_newSettings = false;
qDebug() << "newGame() loadSettings DONE: waiting" << m_waitingToMove
<< "won?" << (m_moveNo >= m_endMoveNo)
<< "move" << m_moveNo << m_endMoveNo;
qDebug() << "setDim (" << Prefs::cubeDim() << ") m_side" << m_side;
setDim (Prefs::cubeDim());
qDebug() << "Entering reset();";
reset(); // Clear cubebox, initialise states.
emit setAction (UNDO, false);
emit setAction (REDO, false);
emit statusMessage (i18n("New Game"), false);
m_moveNo = 0;
m_endMoveNo = LARGE_NUMBER;
setUpNextTurn();
}
else qDebug() << "QDEBUG: newGameOK() =" << false;
}
void Game::saveGame (bool saveAs)
{
if (saveAs || m_gameURL.isEmpty()) {
int result=0;
KUrl url;
do {
url = KFileDialog::getSaveUrl (m_gameURL.url(), "*.kjc", m_view, 0);
if (url.isEmpty())
return;
// check filename
QRegExp pattern ("*.kjc", Qt::CaseSensitive, QRegExp::Wildcard);
if (! pattern.exactMatch (url.fileName())) {
url.setFileName (url.fileName()+".kjc");
}
if (KIO::NetAccess::exists (url, KIO::NetAccess::DestinationSide,
m_view)) {
QString mes=i18n("The file %1 exists.\n"
"Do you want to overwrite it?", url.url());
result = KMessageBox::warningContinueCancel
(m_view, mes, QString(), KGuiItem(i18n("Overwrite")));
if (result == KMessageBox::Cancel)
return;
}
} while (result == KMessageBox::No);
m_gameURL = url;
}
KTemporaryFile tempFile;
tempFile.open();
KConfig config (tempFile.fileName(), KConfig::SimpleConfig);
KConfigGroup main (&config, "KJumpingCube");
main.writeEntry ("Version", KJC_VERSION);
KConfigGroup game (&config, "Game");
saveProperties (game);
config.sync();
if (KIO::NetAccess::upload (tempFile.fileName(), m_gameURL, m_view)) {
emit statusMessage (i18n("Game saved as %1", m_gameURL.url()), false);
}
else {
KMessageBox::sorry (m_view, i18n("There was an error in saving file\n%1",
m_gameURL.url()));
}
}
void Game::loadGame()
{
bool fileOk=true;
KUrl url;
do {
url = KFileDialog::getOpenUrl (m_gameURL.url(), "*.kjc", m_view, 0);
if (url.isEmpty())
return;
if (! KIO::NetAccess::exists(url, KIO::NetAccess::SourceSide, m_view)) {
QString mes = i18n("The file %1 does not exist!", url.url());
KMessageBox::sorry (m_view, mes);
fileOk = false;
}
} while (! fileOk);
QString tempFile;
if (KIO::NetAccess::download (url, tempFile, m_view)) {
KConfig config( tempFile, KConfig::SimpleConfig);
KConfigGroup main (&config, "KJumpingCube");
if (! main.hasKey ("Version")) {
QString mes = i18n("The file %1 is not a KJumpingCube gamefile!",
url.url());
KMessageBox::sorry (m_view, mes);
return;
}
m_gameURL = url;
KConfigGroup game (&config, "Game");
readProperties (game);
emit setAction (UNDO, false);
KIO::NetAccess::removeTempFile (tempFile);
}
else
KMessageBox::sorry (m_view, i18n("There was an error loading file\n%1",
url.url()));
}
void Game::undo()
{
bool moreToUndo = undoRedo (-1);
emit setAction (UNDO, moreToUndo);
emit setAction (REDO, true);
}
void Game::redo()
{
bool moreToRedo = undoRedo (+1);
emit setAction (REDO, moreToRedo);
emit setAction (UNDO, true);
}
bool Game::newGameOK()
{
if ((m_moveNo == 0) || (m_moveNo >= m_endMoveNo)) {
// OK: game finished or not yet started. Settings might have changed.
return true;
}
// Check if it is OK to abandon the current game.
QString query;
if (m_side != Prefs::cubeDim()) {
query = i18n("You have changed the size setting of the game and "
"that requires a new game to start.\n\n"
"Do you wish to abandon the current game or continue "
"playing and restore the previous size setting?");
}
else {
query = i18n("You have requested a new game, but "
"there is already a game in progress.\n\n"
"Do you wish to abandon the current game?");
}
qDebug() << "QUERY:" << query;
int reply = KMessageBox::questionYesNo (m_view, query, i18n("New Game?"),
KGuiItem (i18n("Abandon Game")),
KGuiItem (i18n("Continue Game")));
if (reply == KMessageBox::Yes) {
qDebug() << "ABANDON GAME";
return true; // Start a new game.
}
if (m_side != Prefs::cubeDim()) {
// Restore the setting: also the dialog-box copy if it has been created.
qDebug() << "Reset size" << Prefs::cubeDim() << "back to" << m_side;
Prefs::setCubeDim (m_side);
if (m_settingsPage) {
m_settingsPage->kcfg_CubeDim->setValue (m_side);
}
Prefs::self()->writeConfig();
}
qDebug() << "CONTINUE GAME";
return false; // Continue the current game.
}
void Game::reset()
{
m_view->reset();
m_box->clear();
m_fullSpeed = Prefs::animationNone(); // Animate cascade moves?
m_currentPlayer = One;
m_waitingState = computerPlOne ? ComputerToMove : Nil;
qDebug() << "RESET: activity" << m_activity << "wait" << m_waitingState;
#if AILog > 0
m_ai->startStats();
#endif
}
bool Game::undoRedo (int change)
{
Player oldPlayer = m_currentPlayer;
bool isAI = false;
int index = 0;
bool moreToDo = (change < 0) ?
m_box->undoPosition (m_currentPlayer, isAI, index) :
m_box->redoPosition (m_currentPlayer, isAI, index);
// Update the cube display after an undo or redo.
for (int n = 0; n < (m_side * m_side); n++) {
m_view->displayCube (n, m_box->owner (n), m_box->value (n));
m_view->highlightCube (n, false);
}
m_view->timedCubeHighlight (index); // Show which cube was moved.
m_moveNo = m_moveNo + change;
if (m_moveNo < m_endMoveNo) {
if (oldPlayer != m_currentPlayer) {
emit playerChanged (m_currentPlayer);
}
m_waitingState = isComputer (m_currentPlayer) ? ComputerToMove
: m_waitingState;
setUpNextTurn();
}
else { // End of game: show winner.
moreToDo = false;
m_currentPlayer = oldPlayer;
showWinner();
}
return moreToDo;
}
void Game::setDim (int d)
{
if (d != m_side) {
shutdown();
delete m_box;
m_box = new AI_Box (this, d);
qDebug() << "AI_Box CONSTRUCTED by Game::setDim()";
m_side = d;
m_view->setDim (d);
}
}
void Game::shutdown()
{
// Shut down gracefully, avoiding a possible crash when the user hits Quit.
m_view->killAnimation(); // Stop animation immediately (if active).
if (m_activity == Computing) {
m_ai->stop(); // Stop AI ASAP (moveCalculationDone() => Idle).
m_activity = Aborting;
}
else if (m_activity == Stopping) {
m_activity = Aborting;
}
else if (m_activity != Aborting) {
m_activity = Idle; // In case it was ShowingMove or AnimatingMove.
}
}
void Game::saveProperties (KConfigGroup & config)
{
// Save the current player.
config.writeEntry ("onTurn", (int) m_currentPlayer);
QStringList list;
QString owner, value, key;
// Save the position currently on the board.
for (int x = 0; x < m_side; x++) {
for (int y = 0; y < m_side; y++) {
key.sprintf ("%u,%u", x, y);
int index = x * m_side + y;
owner.sprintf ("%u", m_box->owner (index));
value.sprintf ("%u", m_box->value (index));
list.append (owner.toAscii());
list.append (value.toAscii());
config.writeEntry (key, list);
list.clear();
}
}
// Save the game and player settings.
config.writeEntry ("CubeDim", m_side);
config.writeEntry ("PauseForComputer", m_pauseForComputer ? 1 : 0);
config.writeEntry ("ComputerPlayer1", computerPlOne);
config.writeEntry ("ComputerPlayer2", computerPlTwo);
config.writeEntry ("Kepler1", Prefs::kepler1());
config.writeEntry ("Kepler2", Prefs::kepler2());
config.writeEntry ("Newton1", Prefs::newton1());
config.writeEntry ("Newton2", Prefs::newton2());
config.writeEntry ("Skill1", Prefs::skill1());
config.writeEntry ("Skill2", Prefs::skill2());
}
void Game::readProperties (const KConfigGroup& config)
{
QStringList list;
QString key;
int owner, value, maxValue;
// Dimension must be 3 to 15 (see definition in ai_box.h).
int cubeDim = config.readEntry ("CubeDim", minSide);
if ((cubeDim < minSide) || (cubeDim > maxSide)) {
KMessageBox::sorry (m_view, i18n("The file's cube box size is outside "
"the range %1 to %2. It will be set to %1.")
.arg(minSide).arg(maxSide));
cubeDim = 3;
}
m_side = 1; // Create a new cube box.
setDim (cubeDim);
reset(); // IDW TODO - NEEDED? Is newGame() init VALID here?
for (int x = 0; x < m_side; x++) {
for (int y = 0; y < m_side; y++) {
key.sprintf ("%u,%u", x, y);
list = config.readEntry (key, QStringList());
// List length must be 2, owner must be 0-2, value >= 1 and <= max().
if (list.count() < 2) {
KMessageBox::sorry (m_view, i18n("Missing input line for cube %1.")
.arg(key));
owner = 0;
value = 1;
}
else {
owner = list.at(0).toInt();
value = list.at(1).toInt();
}
if ((owner < 0) || (owner > 2)) {
KMessageBox::sorry (m_view, i18n("Owner of cube %1 is outside the "
"range 0 to 2.").arg(key));
owner = 0;
}
int index = x * m_side + y;
maxValue = (owner == 0) ? 1 : m_box->maxValue (index);
if ((value < 1) || (value > maxValue)) {
KMessageBox::sorry (m_view, i18n("Value of cube %1 is outside the "
"range 1 to %2.")
.arg(key).arg(maxValue));
value = maxValue;
}
m_view->displayCube (index, (Player) owner, value);
m_box->setOwner (index, (Player) owner);
m_box->setValue (index, value);
list.clear();
}
}
// Set current player - must be 1 or 2.
int onTurn = config.readEntry ("onTurn", 1);
if ((onTurn < 1) || (onTurn > 2)) {
KMessageBox::sorry (m_view, i18n("Current player is neither 1 nor 2."));
onTurn = 1;
}
m_currentPlayer = (Player) onTurn;
emit playerChanged (m_currentPlayer);
// Restore the game and player settings.
loadSavedSettings (config);
Prefs::self()->writeConfig();
setUpNextTurn();
}
void Game::loadSavedSettings (const KConfigGroup& config)
{
showSettingsDialog (false); // Load the settings dialog but do not show it.
if (m_side != Prefs::cubeDim()) {
// Update the size setting for the loaded game.
Prefs::setCubeDim (m_side);
m_settingsPage->kcfg_CubeDim->setValue (m_side);
}
int pause = config.readEntry ("PauseForComputer", -1);
if (pause < 0) { // Older files will not contain more settings,
return; // so keep the existing settings.
}
// Load the PauseForComputer and player settings.
bool boolValue = pause > 0 ? true : false;
Prefs::setPauseForComputer (boolValue);
m_settingsPage->kcfg_PauseForComputer->setChecked (boolValue);
m_pauseForComputer = boolValue;
boolValue = config.readEntry ("ComputerPlayer1", false);
Prefs::setComputerPlayer1 (boolValue);
m_settingsPage->kcfg_ComputerPlayer1->setChecked (boolValue);
computerPlOne = boolValue;
boolValue = config.readEntry ("ComputerPlayer2", true);
Prefs::setComputerPlayer2 (boolValue);
m_settingsPage->kcfg_ComputerPlayer2->setChecked (boolValue);
computerPlTwo = boolValue;
boolValue = config.readEntry ("Kepler1", true);
Prefs::setKepler1 (boolValue);
m_settingsPage->kcfg_Kepler1->setChecked (boolValue);
boolValue = config.readEntry ("Kepler2", true);
Prefs::setKepler2 (boolValue);
m_settingsPage->kcfg_Kepler2->setChecked (boolValue);
boolValue = config.readEntry ("Newton1", false);
Prefs::setNewton1 (boolValue);
m_settingsPage->kcfg_Newton1->setChecked (boolValue);
boolValue = config.readEntry ("Newton2", false);
Prefs::setNewton2 (boolValue);
m_settingsPage->kcfg_Newton2->setChecked (boolValue);
int intValue = config.readEntry ("Skill1", 2);
Prefs::setSkill1 (intValue);
m_settingsPage->kcfg_Skill1->setValue (intValue);
intValue = config.readEntry ("Skill2", 2);
Prefs::setSkill2 (intValue);
m_settingsPage->kcfg_Skill2->setValue (intValue);
m_ai->setSkill (Prefs::skill1(), Prefs::kepler1(), Prefs::newton1(),
Prefs::skill2(), Prefs::kepler2(), Prefs::newton2());
}
bool Game::isComputer (Player player) const
{
if (player == One)
return computerPlOne;
else
return computerPlTwo;
}
void Game::animationDone (int index)
{
if (m_activity == ShowingMove) {
showingDone (index);
}
else {
stepAnimationDone (index);
}
}
#include "game.moc"
|