File indexing completed on 2024-05-12 04:04:49

0001 /*
0002  * Copyright (C) 2001-2009 Stephan Kulow <coolo@kde.org>
0003  * Copyright (C) 2010 Parker Coates <coates@kde.org>
0004  *
0005  * License of original code:
0006  * -------------------------------------------------------------------------
0007  *   Permission to use, copy, modify, and distribute this software and its
0008  *   documentation for any purpose and without fee is hereby granted,
0009  *   provided that the above copyright notice appear in all copies and that
0010  *   both that copyright notice and this permission notice appear in
0011  *   supporting documentation.
0012  *
0013  *   This file is provided AS IS with no warranties of any kind.  The author
0014  *   shall have no liability with respect to the infringement of copyrights,
0015  *   trade secrets or any patents by this file or any part thereof.  In no
0016  *   event will the author be liable for any lost revenue or profits or
0017  *   other special, indirect and consequential damages.
0018  * -------------------------------------------------------------------------
0019  *
0020  * License of modifications/additions made after 2009-01-01:
0021  * -------------------------------------------------------------------------
0022  *   This program is free software; you can redistribute it and/or
0023  *   modify it under the terms of the GNU General Public License as
0024  *   published by the Free Software Foundation; either version 2 of
0025  *   the License, or (at your option) any later version.
0026  *
0027  *   This program is distributed in the hope that it will be useful,
0028  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
0029  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0030  *   GNU General Public License for more details.
0031  *
0032  *   You should have received a copy of the GNU General Public License
0033  *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
0034  * -------------------------------------------------------------------------
0035  */
0036 
0037 #include "golf.h"
0038 
0039 // own
0040 #include "dealerinfo.h"
0041 #include "patsolve-config.h"
0042 #include "pileutils.h"
0043 #include "settings.h"
0044 #include "speeds.h"
0045 #if WITH_BH_SOLVER
0046 #include "patsolve/golfsolver.h"
0047 #endif
0048 // KF
0049 #include <KLocalizedString>
0050 
0051 Golf::Golf(const DealerInfo *di)
0052     : DealerScene(di)
0053 {
0054 }
0055 
0056 void Golf::initialize()
0057 {
0058     const qreal dist_x = 1.11;
0059     const qreal smallNeg = -1e-6;
0060 
0061     setDeckContents();
0062 
0063     talon = new PatPile(this, 0, QStringLiteral("talon"));
0064     talon->setPileRole(PatPile::Stock);
0065     talon->setLayoutPos(0, smallNeg);
0066     talon->setSpread(0, 0);
0067     talon->setKeyboardSelectHint(KCardPile::NeverFocus);
0068     talon->setKeyboardDropHint(KCardPile::NeverFocus);
0069     connect(talon, &KCardPile::clicked, this, &DealerScene::drawDealRowOrRedeal);
0070 
0071     waste = new PatPile(this, 8, QStringLiteral("waste"));
0072     waste->setPileRole(PatPile::Foundation);
0073     waste->setLayoutPos(1.1, smallNeg);
0074     waste->setSpread(0.12, 0);
0075     waste->setRightPadding(5 * dist_x);
0076     waste->setWidthPolicy(KCardPile::GrowRight);
0077     waste->setKeyboardSelectHint(KCardPile::NeverFocus);
0078     waste->setKeyboardDropHint(KCardPile::AutoFocusTop);
0079 
0080     for (int r = 0; r < 7; ++r) {
0081         stack[r] = new PatPile(this, 1 + r, QStringLiteral("stack%1").arg(r));
0082         stack[r]->setPileRole(PatPile::Tableau);
0083         stack[r]->setLayoutPos(r * dist_x, 0);
0084         // Manual tweak of the pile z values to make some animations better.
0085         stack[r]->setZValue((7 - r) / 100.0);
0086         stack[r]->setBottomPadding(1.3);
0087         stack[r]->setHeightPolicy(KCardPile::GrowDown);
0088         stack[r]->setKeyboardSelectHint(KCardPile::AutoFocusTop);
0089         stack[r]->setKeyboardDropHint(KCardPile::NeverFocus);
0090     }
0091 
0092     setActions(DealerScene::Hint | DealerScene::Demo | DealerScene::Draw);
0093 #if WITH_BH_SOLVER
0094     auto solver = new GolfSolver(this);
0095     solver->default_max_positions = Settings::golfSolverIterationsLimit();
0096     setSolver(solver);
0097 #endif
0098 
0099     connect(this, &KCardScene::cardClicked, this, &DealerScene::tryAutomaticMove);
0100 }
0101 
0102 bool Golf::checkAdd(const PatPile *pile, const QList<KCard *> &oldCards, const QList<KCard *> &newCards) const
0103 {
0104     return pile->pileRole() == PatPile::Foundation
0105         && (newCards.first()->rank() == oldCards.last()->rank() + 1 || newCards.first()->rank() == oldCards.last()->rank() - 1);
0106 }
0107 
0108 bool Golf::checkRemove(const PatPile *pile, const QList<KCard *> &cards) const
0109 {
0110     return pile->pileRole() == PatPile::Tableau && cards.first() == pile->topCard();
0111 }
0112 
0113 void Golf::restart(const QList<KCard *> &cards)
0114 {
0115     QList<KCard *> cardList = cards;
0116 
0117     for (int i = 0; i < 5; ++i)
0118         for (int r = 0; r < 7; ++r)
0119             addCardForDeal(stack[r], cardList.takeLast(), true, stack[6]->pos());
0120 
0121     while (!cardList.isEmpty()) {
0122         KCard *c = cardList.takeFirst();
0123         c->setPos(talon->pos());
0124         c->setFaceUp(false);
0125         talon->add(c);
0126     }
0127 
0128     startDealAnimation();
0129 
0130     flipCardToPile(talon->topCard(), waste, DURATION_MOVE);
0131 
0132     Q_EMIT newCardsPossible(true);
0133 }
0134 
0135 bool Golf::newCards()
0136 {
0137     if (talon->isEmpty())
0138         return false;
0139 
0140     flipCardToPile(talon->topCard(), waste, DURATION_MOVE);
0141 
0142     if (talon->isEmpty())
0143         Q_EMIT newCardsPossible(false);
0144 
0145     return true;
0146 }
0147 
0148 bool Golf::drop()
0149 {
0150     for (int i = 0; i < 7; ++i)
0151         if (!stack[i]->isEmpty())
0152             return false;
0153 
0154     if (!talon->isEmpty()) {
0155         flipCardToPile(talon->topCard(), waste, DURATION_MOVE);
0156         takeState();
0157         return true;
0158     }
0159 
0160     return false;
0161 }
0162 
0163 void Golf::setGameState(const QString &state)
0164 {
0165     Q_UNUSED(state);
0166     Q_EMIT newCardsPossible(!talon->isEmpty());
0167 }
0168 
0169 QString Golf::solverFormat() const
0170 {
0171     QString output;
0172     output += QLatin1String("Foundations: ") + (waste->isEmpty() ? QLatin1String("-") : cardToRankSuitString(waste->topCard())) + QLatin1Char('\n');
0173     output += QLatin1String("Talon: ");
0174     for (int i = talon->count() - 1; i >= 0; --i) {
0175         output += cardToRankSuitString(talon->at(i));
0176         if (i > 0)
0177             output += QLatin1Char(' ');
0178     }
0179     output += QLatin1Char('\n');
0180     for (int i = 0; i < 7; i++)
0181         cardsListToLine(output, stack[i]->cards());
0182     return output;
0183 }
0184 
0185 static class GolfDealerInfo : public DealerInfo
0186 {
0187 public:
0188     GolfDealerInfo()
0189         : DealerInfo(kli18n("Golf"), GolfId)
0190     {
0191     }
0192 
0193     DealerScene *createGame() const override
0194     {
0195         return new Golf(this);
0196     }
0197 } golfDealerInfo;
0198 
0199 #include "moc_golf.cpp"