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

0001 /*
0002  * Copyright (C) 2000-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 "gypsy.h"
0038 
0039 // own
0040 #include "dealerinfo.h"
0041 #include "patsolve/gypsysolver.h"
0042 #include "pileutils.h"
0043 // KF
0044 #include <KLocalizedString>
0045 
0046 Gypsy::Gypsy(const DealerInfo *di)
0047     : DealerScene(di)
0048 {
0049 }
0050 
0051 void Gypsy::initialize()
0052 {
0053     const qreal dist_x = 1.11;
0054     const qreal dist_y = 1.11;
0055 
0056     setDeckContents(2);
0057 
0058     talon = new PatPile(this, 0, QStringLiteral("talon"));
0059     talon->setPileRole(PatPile::Stock);
0060     talon->setLayoutPos(8.5 * dist_x + 0.4, 4 * dist_y);
0061     talon->setKeyboardSelectHint(KCardPile::NeverFocus);
0062     talon->setKeyboardDropHint(KCardPile::NeverFocus);
0063     connect(talon, &KCardPile::clicked, this, &DealerScene::drawDealRowOrRedeal);
0064 
0065     for (int i = 0; i < 8; ++i) {
0066         target[i] = new PatPile(this, i + 1, QStringLiteral("target%1").arg(i));
0067         target[i]->setPileRole(PatPile::Foundation);
0068         target[i]->setLayoutPos(dist_x * (8 + (i / 4)) + 0.4, (i % 4) * dist_y);
0069         target[i]->setKeyboardSelectHint(KCardPile::NeverFocus);
0070         target[i]->setKeyboardDropHint(KCardPile::ForceFocusTop);
0071     }
0072 
0073     for (int i = 0; i < 8; ++i) {
0074         store[i] = new PatPile(this, 9 + i, QStringLiteral("store%1").arg(i));
0075         store[i]->setPileRole(PatPile::Tableau);
0076         store[i]->setLayoutPos(dist_x * i, 0);
0077         store[i]->setAutoTurnTop(true);
0078         store[i]->setBottomPadding(4 * dist_y);
0079         store[i]->setHeightPolicy(KCardPile::GrowDown);
0080         store[i]->setKeyboardSelectHint(KCardPile::AutoFocusDeepestRemovable);
0081         store[i]->setKeyboardDropHint(KCardPile::AutoFocusTop);
0082     }
0083 
0084     setActions(DealerScene::Hint | DealerScene::Demo | DealerScene::Deal);
0085     setSolver(new GypsySolver(this));
0086 }
0087 
0088 void Gypsy::restart(const QList<KCard *> &cards)
0089 {
0090     QList<KCard *> cardList = cards;
0091 
0092     for (int round = 0; round < 8; ++round)
0093         addCardForDeal(store[round], cardList.takeLast(), false, store[round]->pos() + QPointF(-2 * deck()->cardWidth(), -1.1 * deck()->cardHeight()));
0094 
0095     for (int round = 0; round < 8; ++round)
0096         addCardForDeal(store[round], cardList.takeLast(), true, store[round]->pos() + QPointF(-3 * deck()->cardWidth(), -1.6 * deck()->cardHeight()));
0097 
0098     for (int round = 0; round < 8; ++round)
0099         addCardForDeal(store[round], cardList.takeLast(), true, store[round]->pos() + QPointF(-4 * deck()->cardWidth(), -2.1 * deck()->cardHeight()));
0100 
0101     while (!cardList.isEmpty()) {
0102         KCard *c = cardList.takeFirst();
0103         c->setPos(talon->pos());
0104         c->setFaceUp(false);
0105         talon->add(c);
0106     }
0107 
0108     startDealAnimation();
0109 
0110     Q_EMIT newCardsPossible(true);
0111 }
0112 
0113 bool Gypsy::checkAdd(const PatPile *pile, const QList<KCard *> &oldCards, const QList<KCard *> &newCards) const
0114 {
0115     switch (pile->pileRole()) {
0116     case PatPile::Tableau:
0117         return checkAddAlternateColorDescending(oldCards, newCards);
0118     case PatPile::Foundation:
0119         return checkAddSameSuitAscendingFromAce(oldCards, newCards);
0120     case PatPile::Stock:
0121     default:
0122         return false;
0123     }
0124 }
0125 
0126 bool Gypsy::checkRemove(const PatPile *pile, const QList<KCard *> &cards) const
0127 {
0128     switch (pile->pileRole()) {
0129     case PatPile::Tableau:
0130         return isAlternateColorDescending(cards);
0131     case PatPile::Foundation:
0132         return cards.first() == pile->topCard();
0133     case PatPile::Stock:
0134     default:
0135         return false;
0136     }
0137 }
0138 
0139 bool Gypsy::newCards()
0140 {
0141     if (talon->isEmpty())
0142         return false;
0143 
0144     for (int round = 0; round < 8; ++round) {
0145         KCard *c = talon->topCard();
0146         flipCardToPileAtSpeed(c, store[round], DEAL_SPEED);
0147         c->setZValue(c->zValue() + 8 - round);
0148     }
0149 
0150     if (talon->isEmpty())
0151         Q_EMIT newCardsPossible(false);
0152 
0153     return true;
0154 }
0155 
0156 void Gypsy::setGameState(const QString &state)
0157 {
0158     Q_UNUSED(state)
0159     Q_EMIT newCardsPossible(!talon->isEmpty());
0160 }
0161 
0162 static class GypsyDealerInfo : public DealerInfo
0163 {
0164 public:
0165     GypsyDealerInfo()
0166         : DealerInfo(kli18n("Gypsy"), GypsyId)
0167     {
0168     }
0169 
0170     DealerScene *createGame() const override
0171     {
0172         return new Gypsy(this);
0173     }
0174 } gypsyDealerInfo;
0175 
0176 #include "moc_gypsy.cpp"