1/*
2 * Copyright (C) 2010 Parker Coates <coates@kde.org>
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of
7 * the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 *
17 */
18
19#include "pileutils.h"
20
21#include "KCard"
22#include "KCardDeck"
23
24
25namespace
26{
27 inline int alternateColor( int color )
28 {
29 return color == KCardDeck::Red ? KCardDeck::Black : KCardDeck::Red;
30 }
31}
32
33
34bool isSameSuitAscending( const QList<KCard*> & cards )
35{
36 if ( cards.size() <= 1 )
37 return true;
38
39 int suit = cards.first()->suit();
40 int lastRank = cards.first()->rank();
41
42 for( int i = 1; i < cards.size(); ++i )
43 {
44 ++lastRank;
45 if ( cards[i]->suit() != suit || cards[i]->rank() != lastRank )
46 return false;
47 }
48 return true;
49}
50
51
52bool isSameSuitDescending( const QList<KCard*> & cards )
53{
54 if ( cards.size() <= 1 )
55 return true;
56
57 int suit = cards.first()->suit();
58 int lastRank = cards.first()->rank();
59
60 for( int i = 1; i < cards.size(); ++i )
61 {
62 --lastRank;
63 if ( cards[i]->suit() != suit || cards[i]->rank() != lastRank )
64 return false;
65 }
66 return true;
67}
68
69
70bool isAlternateColorDescending( const QList<KCard*> & cards )
71{
72 if ( cards.size() <= 1 )
73 return true;
74
75 int lastColor = cards.first()->color();
76 int lastRank = cards.first()->rank();
77
78 for( int i = 1; i < cards.size(); ++i )
79 {
80 lastColor = alternateColor( lastColor );
81 --lastRank;
82
83 if ( cards[i]->color() != lastColor || cards[i]->rank() != lastRank )
84 return false;
85 }
86 return true;
87}
88
89
90bool checkAddSameSuitAscendingFromAce( const QList<KCard*> & oldCards, const QList<KCard*> & newCards )
91{
92 if ( !isSameSuitAscending( newCards ) )
93 return false;
94
95 if ( oldCards.isEmpty() )
96 return newCards.first()->rank() == KCardDeck::Ace;
97 else
98 return newCards.first()->suit() == oldCards.last()->suit()
99 && newCards.first()->rank() == oldCards.last()->rank() + 1;
100}
101
102
103bool checkAddAlternateColorDescending( const QList<KCard*> & oldCards, const QList<KCard*> & newCards )
104{
105 return isAlternateColorDescending( newCards )
106 && ( oldCards.isEmpty()
107 || ( newCards.first()->color() == alternateColor( oldCards.last()->color() )
108 && newCards.first()->rank() == oldCards.last()->rank() - 1 ) );
109}
110
111
112bool checkAddAlternateColorDescendingFromKing( const QList<KCard*> & oldCards, const QList<KCard*> & newCards )
113{
114 if ( !isAlternateColorDescending( newCards ) )
115 return false;
116
117 if ( oldCards.isEmpty() )
118 return newCards.first()->rank() == KCardDeck::King;
119 else
120 return newCards.first()->color() == alternateColor( oldCards.last()->color() )
121 && newCards.first()->rank() == oldCards.last()->rank() - 1;
122}
123
124