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