summaryrefslogtreecommitdiff
path: root/noncore/games/solitaire/cardpile.cpp
Unidiff
Diffstat (limited to 'noncore/games/solitaire/cardpile.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/games/solitaire/cardpile.cpp114
1 files changed, 114 insertions, 0 deletions
diff --git a/noncore/games/solitaire/cardpile.cpp b/noncore/games/solitaire/cardpile.cpp
new file mode 100644
index 0000000..0b738d2
--- a/dev/null
+++ b/noncore/games/solitaire/cardpile.cpp
@@ -0,0 +1,114 @@
1/**********************************************************************
2** Copyright (C) 2000 Trolltech AS. All rights reserved.
3**
4** This file is part of Qtopia Environment.
5**
6** This file may be distributed and/or modified under the terms of the
7** GNU General Public License version 2 as published by the Free Software
8** Foundation and appearing in the file LICENSE.GPL included in the
9** packaging of this file.
10**
11** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
12** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13**
14** See http://www.trolltech.com/gpl/ for GPL licensing information.
15**
16** Contact info@trolltech.com if any conditions of this licensing are
17** not clear to you.
18**
19**********************************************************************/
20
21#include "cardpile.h"
22#include "card.h"
23
24#include <qpe/config.h>
25#include <qpoint.h>
26
27#include <qlist.h>
28
29
30CardPile::CardPile(int x, int y) : pileX(x), pileY(y), dealing(FALSE) {
31 pileWidth = 0;
32 pileHeight = 0;
33 pileNextX = pileX;
34 pileNextY = pileY;
35 pileCenterX = x + pileWidth / 2;
36 pileCenterY = y + pileHeight / 2;
37 pileRadius = (pileWidth > pileHeight) ? pileWidth : pileHeight;
38}
39
40
41int CardPile::distanceFromPile(int x, int y) {
42 return (pileCenterX-x)*(pileCenterX-x)+(pileCenterY-y)*(pileCenterY-y);
43}
44
45
46int CardPile::distanceFromNextPos(int x, int y) {
47 return (pileNextX-x)*(pileNextX-x)+(pileNextY-y)*(pileNextY-y);
48}
49
50
51Card *CardPile::cardInfront(Card *c) {
52 CardPile *p = c->getCardPile();
53 if (p) {
54 p->at(p->find(c));
55 return p->next();
56 } else {
57 return NULL;
58 }
59}
60
61
62bool CardPile::kingOnTop() {
63 Card *top = cardOnTop();
64 return top && top->getValue() == king;
65}
66
67
68bool CardPile::addCardToTop(Card *c) {
69 if (dealing || isAllowedOnTop(c)) {
70 append((const Card *)c);
71 cardAddedToTop(c);
72 return TRUE;
73 }
74 return FALSE;
75}
76
77
78bool CardPile::addCardToBottom(Card *c) {
79 if (dealing || isAllowedOnBottom(c)) {
80 prepend((const Card *)c);
81 cardAddedToBottom(c);
82 return TRUE;
83 }
84 return FALSE;
85}
86
87
88bool CardPile::removeCard(Card *c) {
89 if (dealing || isAllowedToBeMoved(c)) {
90 take(find(c));
91 cardRemoved(c);
92 return TRUE;
93 }
94 return FALSE;
95}
96
97
98void CardPile::writeConfig( Config& cfg, QString name ) {
99 int numberOfCards = 0;
100 cfg.setGroup( name );
101 Card *card = cardOnBottom();
102 while ( card ) {
103 QString cardStr;
104 cardStr.sprintf( "%i", numberOfCards );
105 int val = (int)card->getValue() - 1 + ( (int)card->getSuit() - 1 ) * 13;
106 cfg.writeEntry( "Card" + cardStr, val );
107 cfg.writeEntry( "CardFacing" + cardStr, card->isFacing() );
108 card = cardInfront( card );
109 numberOfCards++;
110 }
111 cfg.writeEntry("NumberOfCards", numberOfCards);
112}
113
114