1/*
2 * Copyright (C) 2000-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 "clock.h"
38
39#include "dealerinfo.h"
40#include "patsolve/clocksolver.h"
41
42#include <KLocale>
43
44
45Clock::Clock( const DealerInfo * di )
46 : DealerScene( di )
47{
48}
49
50void Clock::initialize()
51{
52 setSceneAlignment( AlignHCenter | AlignVCenter );
53
54 setDeckContents();
55
56 const qreal dist_x = 1.11;
57 const qreal ys[12] = { 0./96, 15./96, 52./96, 158./96, 264./96, 301./96, 316./96, 301./96, 264./96, 158./96, 52./96, 15./96};
58 const qreal xs[12] = { 200./72, 280./72, 360./72, 400./72, 360./72, 280./72, 200./72, 120./72, 40./72, 0./72, 40./72, 120./72};
59
60 for ( int i = 0; i < 12; ++i )
61 {
62 target[i] = new PatPile( this, i + 1, QString("target%1").arg(i) );
63 target[i]->setPileRole(PatPile::Foundation);
64 target[i]->setLayoutPos(4 * dist_x + 0.4 + xs[i], 0.2 + ys[i]);
65 target[i]->setSpread(0, 0);
66 target[i]->setKeyboardSelectHint( KCardPile::NeverFocus );
67 target[i]->setKeyboardDropHint( KCardPile::ForceFocusTop );
68 }
69
70 for ( int i = 0; i < 8; ++i )
71 {
72 store[i] = new PatPile( this, 14 + i, QString("store%1").arg(i) );
73 store[i]->setPileRole(PatPile::Tableau);
74 store[i]->setLayoutPos(dist_x*(i%4), 2.5 * (i/4));
75 store[i]->setBottomPadding( 1.3 );
76 store[i]->setKeyboardSelectHint( KCardPile::AutoFocusTop );
77 store[i]->setKeyboardDropHint( KCardPile::AutoFocusTop );
78 }
79
80 setActions(DealerScene::Hint | DealerScene::Demo);
81 setSolver( new ClockSolver( this ) );
82}
83
84void Clock::restart( const QList<KCard*> & cards )
85{
86 static const KCardDeck::Suit suits[12] = { KCardDeck::Diamonds, KCardDeck::Spades, KCardDeck::Hearts, KCardDeck::Clubs,
87 KCardDeck::Diamonds, KCardDeck::Spades, KCardDeck::Hearts, KCardDeck::Clubs,
88 KCardDeck::Diamonds, KCardDeck::Spades, KCardDeck::Hearts, KCardDeck::Clubs };
89 static const KCardDeck::Rank ranks[12] = { KCardDeck::Nine, KCardDeck::Ten, KCardDeck::Jack, KCardDeck::Queen,
90 KCardDeck::King, KCardDeck::Two, KCardDeck::Three, KCardDeck::Four,
91 KCardDeck::Five, KCardDeck::Six, KCardDeck::Seven, KCardDeck::Eight };
92
93 const QPointF center = ( target[0]->pos() + target[6]->pos() ) / 2;
94
95 int j = 0;
96 QList<KCard*> cardList = cards;
97 while ( !cardList.isEmpty() )
98 {
99 KCard * c = cardList.takeLast();
100 for ( int i = 0; i < 12; ++i )
101 {
102 if ( c->rank() == ranks[i] && c->suit() == suits[i] )
103 {
104 QPointF initPos = (2 * center + target[(i + 2) % 12]->pos()) / 3;
105 addCardForDeal( target[i], c, true, initPos );
106 c = 0;
107 break;
108 }
109 }
110 if ( c )
111 {
112 addCardForDeal( store[j], c, true, store[ j < 4 ? 0 : 4 ]->pos() );
113 j = (j + 1) % 8;
114 }
115 }
116
117 startDealAnimation();
118}
119
120
121bool Clock::drop()
122{
123 return false;
124}
125
126
127bool Clock::checkAdd(const PatPile * pile, const QList<KCard*> & oldCards, const QList<KCard*> & newCards) const
128{
129 if ( pile->pileRole() == PatPile::Tableau )
130 {
131 return oldCards.isEmpty()
132 || newCards.first()->rank() == oldCards.last()->rank() - 1;
133 }
134 else
135 {
136 return oldCards.last()->suit() == newCards.first()->suit()
137 && ( newCards.first()->rank() == oldCards.last()->rank() + 1
138 || ( oldCards.last()->rank() == KCardDeck::King
139 && newCards.first()->rank() == KCardDeck::Ace ) );
140 }
141}
142
143
144bool Clock::checkRemove(const PatPile* pile, const QList<KCard*> & cards) const
145{
146 return pile->pileRole() == PatPile::Tableau
147 && cards.first() == pile->topCard();
148}
149
150
151static class ClockDealerInfo : public DealerInfo
152{
153public:
154 ClockDealerInfo()
155 : DealerInfo(I18N_NOOP("Grandfather's Clock"), GrandfathersClockId)
156 {}
157
158 virtual DealerScene *createGame() const
159 {
160 return new Clock( this );
161 }
162} clockDealerInfo;
163
164
165#include "clock.moc"
166