1/*
2 * Copyright (C) 1995 Paul Olav Tvete <paul@troll.no>
3 * Copyright (C) 2000-2009 Stephan Kulow <coolo@kde.org>
4 * Copyright (C) 2009-2010 Parker Coates <coates@kde.org>
5 *
6 * License of original code:
7 * -------------------------------------------------------------------------
8 * Permission to use, copy, modify, and distribute this software and its
9 * documentation for any purpose and without fee is hereby granted,
10 * provided that the above copyright notice appear in all copies and that
11 * both that copyright notice and this permission notice appear in
12 * supporting documentation.
13 *
14 * This file is provided AS IS with no warranties of any kind. The author
15 * shall have no liability with respect to the infringement of copyrights,
16 * trade secrets or any patents by this file or any part thereof. In no
17 * event will the author be liable for any lost revenue or profits or
18 * other special, indirect and consequential damages.
19 * -------------------------------------------------------------------------
20 *
21 * License of modifications/additions made after 2009-01-01:
22 * -------------------------------------------------------------------------
23 * This program is free software; you can redistribute it and/or
24 * modify it under the terms of the GNU General Public License as
25 * published by the Free Software Foundation; either version 2 of
26 * the License, or (at your option) any later version.
27 *
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU General Public License for more details.
32 *
33 * You should have received a copy of the GNU General Public License
34 * along with this program. If not, see <http://www.gnu.org/licenses/>.
35 * -------------------------------------------------------------------------
36 */
37
38#ifndef DEALER_H
39#define DEALER_H
40
41class CardDiff;
42class DealerInfo;
43#include "gamestate.h"
44class MessageBox;
45class MoveHint;
46#include "patpile.h"
47class Solver;
48class SolverThread;
49#include "speeds.h"
50#include "view.h"
51#include "patsolve/patsolve.h"
52
53#include "KCardDeck"
54#include "KCardScene"
55
56class QAction;
57#include <QtCore/QMap>
58#include <QtCore/QStack>
59#include <QtCore/QTimer>
60class QDomDocument;
61
62
63class DealerScene : public KCardScene
64{
65 Q_OBJECT
66
67public:
68 enum { None = 0, Hint = 1, Demo = 2, Draw = 4, Deal = 8, Redeal = 16 } Actions;
69
70 DealerScene( const DealerInfo * di );
71 ~DealerScene();
72
73 virtual void initialize() = 0;
74
75 virtual void relayoutScene();
76 void updateWonItem();
77
78 void addPatPile( PatPile * pile );
79 void removePatPile( PatPile * pile );
80 QList<PatPile*> patPiles() const;
81
82 void setAutoDropEnabled( bool enabled );
83 bool autoDropEnabled() const;
84
85 int gameNumber() const;
86
87 int gameId() const;
88
89 void setActions( int actions );
90 int actions() const;
91
92 virtual QList<QAction*> configActions() const;
93
94 void startHint();
95 void stopHint();
96 bool isHintActive() const;
97
98 void startDrop();
99 void stopDrop();
100 bool isDropActive() const;
101
102 void startDemo();
103 void stopDemo();
104 bool isDemoActive() const;
105
106 void setSolverEnabled( bool enabled );
107 Solver * solver() const;
108 void startSolver();
109
110 virtual bool isGameLost() const;
111 virtual bool isGameWon() const;
112
113 bool allowedToStartNewGame();
114 int moveCount() const;
115
116 void saveFile( QIODevice * io );
117 bool loadFile( QIODevice * io );
118 void saveLegacyFile( QIODevice * io );
119 bool loadLegacyFile( QIODevice * io );
120
121 virtual void mapOldId(int id);
122 virtual int oldId() const;
123 void recordGameStatistics();
124
125 QImage createDump() const;
126
127signals:
128 void undoPossible(bool poss);
129 void redoPossible(bool poss);
130 void newCardsPossible(bool poss);
131 void gameInProgress(bool inProgress);
132
133 void demoActive(bool active);
134 void hintActive(bool active);
135 void dropActive(bool active);
136 void updateMoves(int moves);
137
138 void solverStateChanged(QString text);
139
140 void cardsPickedUp();
141 void cardsPutDown();
142
143public slots:
144 void startNew( int dealNumber = -1 );
145
146 void undo();
147 void redo();
148
149 void stop();
150
151 void drawDealRowOrRedeal();
152
153protected:
154 virtual bool allowedToAdd(const KCardPile * pile, const QList<KCard*> & cards) const;
155 virtual bool allowedToRemove(const KCardPile * pile, const KCard * card) const;
156
157 virtual bool checkAdd( const PatPile * pile, const QList<KCard*> & oldCards, const QList<KCard*> & newCards ) const;
158 virtual bool checkRemove( const PatPile * pile, const QList<KCard*> & cards ) const;
159 virtual bool checkPrefering( const PatPile * pile, const QList<KCard*> & oldCards, const QList<KCard*> & newCards ) const;
160
161 virtual void cardsMoved( const QList<KCard*> & cards, KCardPile * oldPile, KCardPile * newPile );
162
163 virtual void mouseDoubleClickEvent( QGraphicsSceneMouseEvent * mouseEvent );
164 virtual void mousePressEvent( QGraphicsSceneMouseEvent * mouseEvent );
165 virtual void mouseReleaseEvent( QGraphicsSceneMouseEvent * mouseEvent );
166
167 virtual void restart( const QList<KCard*> & cards ) = 0;
168
169 void setSolver( Solver * solver );
170
171 virtual QList<MoveHint> getHints();
172
173 // reimplement these to store and load game-specific information in the state structure
174 virtual QString getGameState() const;
175 virtual void setGameState( const QString & state );
176
177 // reimplement these to store and load game-specific options in the saved game file
178 virtual QString getGameOptions() const;
179 virtual void setGameOptions( const QString & options );
180
181 void addCardForDeal( KCardPile * pile, KCard * card, bool faceUp, QPointF startPos );
182 void startDealAnimation();
183
184 void setNeededFutureMoves( int moves );
185 int neededFutureMoves() const;
186
187 void setDeckContents( int copies = 1,
188 const QList<KCardDeck::Suit> & suits = KCardDeck::standardSuits() );
189
190 void multiStepMove( const QList<KCard*> & cards,
191 KCardPile * pile,
192 const QList<KCardPile*> & freePiles,
193 const QList<KCardPile*> & freeCells,
194 int duration );
195
196 QList<MoveHint> getSolverHints();
197
198protected slots:
199 void takeState();
200 virtual void animationDone();
201 virtual bool newCards();
202 virtual bool drop();
203 virtual bool tryAutomaticMove( KCard * card );
204
205private slots:
206 void stopAndRestartSolver();
207 void slotSolverEnded();
208 void slotSolverFinished( int result );
209
210 void demo();
211
212 void showWonMessage();
213
214private:
215 void undoOrRedo( bool undo );
216
217 void resetInternals();
218
219 MoveHint chooseHint();
220
221 void won();
222
223 int speedUpTime( int delay ) const;
224
225 void multiStepSubMove( QList<KCard*> cards,
226 KCardPile * pile,
227 QList<KCardPile*> freePiles,
228 const QList<KCardPile*> & freeCells );
229 void continueMultiStepMove();
230
231 const DealerInfo * const m_di;
232
233 Solver * m_solver;
234 SolverThread * m_solverThread;
235 QList<MOVE> m_winningMoves;
236
237 KCard * m_peekedCard;
238 MessageBox * m_wonItem;
239
240 QTimer m_solverUpdateTimer;
241 QTimer m_demoTimer;
242 QTimer m_dropTimer;
243
244 int m_dealNumber;
245 int m_loadedMoveCount;
246 int m_neededFutureMoves;
247 int m_supportedActions;
248
249 bool m_autoDropEnabled;
250 bool m_solverEnabled;
251
252 bool m_dealStarted;
253 bool m_dealWasEverWinnable;
254 bool m_dealHasBeenWon;
255 bool m_dealWasJustSaved;
256 bool m_statisticsRecorded;
257 bool m_playerReceivedHelp;
258
259 // We need a flag to avoid telling someone the game is lost
260 // just because the winning animation moved the cards away
261 bool m_toldAboutWonGame;
262 bool m_toldAboutLostGame;
263
264 QSet<KCard*> m_cardsRemovedFromFoundations;
265 qreal m_dropSpeedFactor;
266 bool m_interruptAutoDrop;
267
268 bool m_dealInProgress;
269 bool m_hintInProgress;
270 bool m_demoInProgress;
271 bool m_dropInProgress;
272
273 bool m_hintQueued;
274 bool m_demoQueued;
275 bool m_dropQueued;
276 bool m_newCardsQueued;
277 bool m_takeStateQueued;
278
279 QStack<GameState*> m_undoStack;
280 GameState * m_currentState;
281 QStack<GameState*> m_redoStack;
282 QHash<KCard*,CardState> m_lastKnownCardStates;
283
284 QList<QPair<KCard*,KCardPile*> > m_multiStepMoves;
285 int m_multiStepDuration;
286
287 QList<PatPile*> m_patPiles;
288
289 QMap<KCard*,QPointF> m_initDealPositions;
290};
291
292#endif
293