1/*
2 * Copyright (C) 2001-2009 Stephan Kulow <coolo@kde.org>
3 * Copyright (C) 2010 Parker Coates <coates@kde.org>
4 *
5 * License of original code:
6 * -------------------------------------------------------------------------
7 * Permission to use, copy, modify, and distribute this software and its
8 * documentation for any purpose and without fee is hereby granted,
9 * provided that the above copyright notice appear in all copies and that
10 * both that copyright notice and this permission notice appear in
11 * supporting documentation.
12 *
13 * This file is provided AS IS with no warranties of any kind. The author
14 * shall have no liability with respect to the infringement of copyrights,
15 * trade secrets or any patents by this file or any part thereof. In no
16 * event will the author be liable for any lost revenue or profits or
17 * other special, indirect and consequential damages.
18 * -------------------------------------------------------------------------
19 *
20 * License of modifications/additions made after 2009-01-01:
21 * -------------------------------------------------------------------------
22 * This program is free software; you can redistribute it and/or
23 * modify it under the terms of the GNU General Public License as
24 * published by the Free Software Foundation; either version 2 of
25 * the License, or (at your option) any later version.
26 *
27 * This program is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU General Public License for more details.
31 *
32 * You should have received a copy of the GNU General Public License
33 * along with this program. If not, see <http://www.gnu.org/licenses/>.
34 * -------------------------------------------------------------------------
35 */
36
37#include "golf.h"
38
39#include "dealerinfo.h"
40#include "speeds.h"
41#include "patsolve/golfsolver.h"
42
43#include <KLocale>
44
45
46Golf::Golf( const DealerInfo * di )
47 : DealerScene( di )
48{
49}
50
51
52void Golf::initialize()
53{
54 const qreal dist_x = 1.11;
55 const qreal smallNeg = -1e-6;
56
57 setDeckContents();
58
59 talon = new PatPile( this, 0, "talon" );
60 talon->setPileRole(PatPile::Stock);
61 talon->setLayoutPos(0, smallNeg);
62 talon->setSpread(0, 0);
63 talon->setKeyboardSelectHint( KCardPile::NeverFocus );
64 talon->setKeyboardDropHint( KCardPile::NeverFocus );
65 connect( talon, SIGNAL(clicked(KCard*)), SLOT(drawDealRowOrRedeal()) );
66
67 waste = new PatPile( this, 8, "waste" );
68 waste->setPileRole(PatPile::Foundation);
69 waste->setLayoutPos(1.1, smallNeg);
70 waste->setSpread(0.12, 0);
71 waste->setRightPadding( 5 * dist_x );
72 waste->setWidthPolicy( KCardPile::GrowRight );
73 waste->setKeyboardSelectHint( KCardPile::NeverFocus );
74 waste->setKeyboardDropHint( KCardPile::AutoFocusTop );
75
76 for( int r = 0; r < 7; ++r )
77 {
78 stack[r] = new PatPile( this, 1 + r, QString("stack%1").arg(r) );
79 stack[r]->setPileRole(PatPile::Tableau);
80 stack[r]->setLayoutPos(r*dist_x,0);
81 // Manual tweak of the pile z values to make some animations better.
82 stack[r]->setZValue((7-r)/100.0);
83 stack[r]->setBottomPadding( 1.3 );
84 stack[r]->setHeightPolicy( KCardPile::GrowDown );
85 stack[r]->setKeyboardSelectHint( KCardPile::AutoFocusTop );
86 stack[r]->setKeyboardDropHint( KCardPile::NeverFocus );
87 }
88
89 setActions(DealerScene::Hint | DealerScene::Demo | DealerScene::Draw);
90 setSolver( new GolfSolver( this ) );
91
92 connect( this, SIGNAL(cardClicked(KCard*)), this, SLOT(tryAutomaticMove(KCard*)) );
93}
94
95
96bool Golf::checkAdd(const PatPile * pile, const QList<KCard*> & oldCards, const QList<KCard*> & newCards) const
97{
98 return pile->pileRole() == PatPile::Foundation
99 && ( newCards.first()->rank() == oldCards.last()->rank() + 1
100 || newCards.first()->rank() == oldCards.last()->rank() - 1 );
101}
102
103
104bool Golf::checkRemove(const PatPile * pile, const QList<KCard*> & cards) const
105{
106 return pile->pileRole() == PatPile::Tableau
107 && cards.first() == pile->topCard();
108}
109
110
111void Golf::restart( const QList<KCard*> & cards )
112{
113 QList<KCard*> cardList = cards;
114
115 for ( int i = 0; i < 5; ++i )
116 for ( int r = 0; r < 7; ++r )
117 addCardForDeal( stack[r], cardList.takeLast(), true, stack[6]->pos() );
118
119 while ( !cardList.isEmpty() )
120 {
121 KCard * c = cardList.takeFirst();
122 c->setPos( talon->pos() );
123 c->setFaceUp( false );
124 talon->add( c );
125 }
126
127 startDealAnimation();
128
129 flipCardToPile(talon->topCard(), waste, DURATION_MOVE);
130
131 emit newCardsPossible( true );
132}
133
134
135bool Golf::newCards()
136{
137 if ( talon->isEmpty() )
138 return false;
139
140 flipCardToPile(talon->topCard(), waste, DURATION_MOVE);
141
142 if ( talon->isEmpty() )
143 emit newCardsPossible( false );
144
145 return true;
146}
147
148
149bool Golf::drop()
150{
151 for ( int i = 0; i < 7; ++i )
152 if ( !stack[i]->isEmpty() )
153 return false;
154
155 if ( !talon->isEmpty() )
156 {
157 flipCardToPile( talon->topCard(), waste, DURATION_MOVE );
158 takeState();
159 return true;
160 }
161
162 return false;
163}
164
165
166void Golf::setGameState( const QString & state )
167{
168 Q_UNUSED( state );
169 emit newCardsPossible( !talon->isEmpty() );
170}
171
172
173static class GolfDealerInfo : public DealerInfo
174{
175public:
176 GolfDealerInfo()
177 : DealerInfo(I18N_NOOP("Golf"), GolfId)
178 {}
179
180 virtual DealerScene *createGame() const
181 {
182 return new Golf( this );
183 }
184} golfDealerInfo;
185
186
187#include "golf.moc"
188