~ubuntu-branches/ubuntu/natty/kdegames/natty-proposed

« back to all changes in this revision

Viewing changes to kpat/simon.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Rohan Garg
  • Date: 2010-05-28 20:16:42 UTC
  • mfrom: (1.2.45 upstream)
  • Revision ID: james.westby@ubuntu.com-20100528201642-vh75rp06hh5hjt6l
Tags: 4:4.4.80-0ubuntu1
* New upstream beta release
* New package for kajongg in debian/control
* Custom deb flag for kajongg in debian/rules
* New install file for kajongg
* Remove README.source from debian/
* Bump dep on kde-sc-dev-latest to 4.4.80

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
2
 * Copyright (C) 2000-2009 Stephan Kulow <coolo@kde.org>
 
3
 * Copyright (C) 2010 Parker Coates <parker.coates@kdemail.net>
3
4
 *
4
5
 * License of original code:
5
6
 * -------------------------------------------------------------------------
35
36
 
36
37
#include "simon.h"
37
38
 
38
 
#include "carddeck.h"
39
39
#include "dealerinfo.h"
 
40
#include "pileutils.h"
40
41
#include "patsolve/simonsolver.h"
41
42
 
42
 
#include <KDebug>
 
43
#include "Shuffle"
 
44
 
43
45
#include <KLocale>
44
46
 
45
47
 
46
 
Simon::Simon( )
47
 
    : DealerScene( )
 
48
void Simon::initialize()
48
49
{
49
 
    setDeck(new CardDeck());
 
50
    static_cast<KStandardCardDeck*>( deck() )->setDeckContents();
50
51
 
51
52
    const qreal dist_x = 1.11;
52
53
 
53
 
    for (int i=0; i<4; i++) {
54
 
        target[i] = new Pile(i+1, QString( "target%1" ).arg( i ));
55
 
        target[i]->setPilePos((i+3)*dist_x, 0);
56
 
        target[i]->setRemoveFlags(Pile::disallow);
57
 
        target[i]->setAddFlags(Pile::several);
58
 
        target[i]->setCheckIndex(0);
59
 
        target[i]->setTarget(true);
60
 
        addPile(target[i]);
 
54
    for ( int i = 0; i < 4; ++i )
 
55
    {
 
56
        target[i] = new PatPile( this, i + 1, QString( "target%1" ).arg( i ) );
 
57
        target[i]->setPileRole(PatPile::Foundation);
 
58
        target[i]->setLayoutPos((i+3)*dist_x, 0);
 
59
        target[i]->setSpread(0, 0);
 
60
        target[i]->setKeyboardSelectHint( KCardPile::NeverFocus );
 
61
        target[i]->setKeyboardDropHint( KCardPile::AutoFocusTop );
61
62
    }
62
63
 
63
 
    for (int i=0; i<10; i++) {
64
 
        store[i] = new Pile(5+i, QString( "store%1" ).arg( i ));
65
 
        store[i]->setPilePos(dist_x*i, 1.2);
66
 
        store[i]->setAddFlags(Pile::addSpread | Pile::several);
67
 
        store[i]->setRemoveFlags(Pile::several);
68
 
        store[i]->setReservedSpace( QSizeF( 1.0, 3.5 ) );
69
 
        store[i]->setCheckIndex(1);
70
 
        addPile(store[i]);
 
64
    for ( int i = 0; i < 10; ++i )
 
65
    {
 
66
        store[i] = new PatPile( this, 5 + i, QString( "store%1" ).arg( i ) );
 
67
        store[i]->setPileRole(PatPile::Tableau);
 
68
        store[i]->setLayoutPos(dist_x*i, 1.2);
 
69
        store[i]->setReservedSpace( 0, 0, 1, 3.5 );
 
70
        store[i]->setZValue( 0.01 * i );
 
71
        store[i]->setKeyboardSelectHint( KCardPile::AutoFocusDeepestRemovable );
 
72
        store[i]->setKeyboardDropHint( KCardPile::AutoFocusTop );
71
73
    }
72
74
 
73
75
    setActions(DealerScene::Hint | DealerScene::Demo);
75
77
    //setNeededFutureMoves( 1 ); // could be some nonsense moves
76
78
}
77
79
 
78
 
void Simon::restart() {
79
 
    deck()->returnAllCards();
80
 
    deck()->shuffle( gameNumber() );
81
 
    deal();
82
 
}
83
 
 
84
 
void Simon::deal() {
85
 
    for ( int piles = 9; piles >= 3; piles-- )
86
 
    {
87
 
        for (int j = 0; j < piles; j++)
88
 
            addCardForDeal(store[j], deck()->takeCard(), true, QPointF(0,-deck()->cardHeight()));
89
 
    }
90
 
    for ( int j = 0; j < 10; j++ )
91
 
        addCardForDeal(store[j], deck()->takeCard(), true, QPointF(0,-deck()->cardHeight()));
92
 
 
93
 
    Q_ASSERT(!deck()->hasUndealtCards());
 
80
void Simon::restart()
 
81
{
 
82
    QList<KCard*> cards = shuffled( deck()->cards(), gameNumber() );
 
83
 
 
84
    QPointF initPos( 0, -deck()->cardHeight() );
 
85
 
 
86
    for ( int piles = 9; piles >= 3; --piles )
 
87
        for ( int j = 0; j < piles; ++j )
 
88
            addCardForDeal( store[j], cards.takeLast(), true, initPos );
 
89
 
 
90
    for ( int j = 0; j < 10; ++j )
 
91
        addCardForDeal( store[j], cards.takeLast(), true, initPos );
 
92
 
 
93
    Q_ASSERT( cards.isEmpty() );
94
94
 
95
95
    startDealAnimation();
96
96
}
97
97
 
98
 
bool Simon::checkPrefering( int checkIndex, const Pile *c1, const CardList& c2) const
99
 
{
100
 
    if (checkIndex == 1) {
101
 
        if (c1->isEmpty())
102
 
            return false;
103
 
 
104
 
        return (c1->top()->suit() == c2.first()->suit());
105
 
    } else return false; // it's just important to keep this unique
106
 
}
107
 
 
108
 
bool Simon::checkAdd( int checkIndex, const Pile *c1, const CardList& c2) const
109
 
{
110
 
    if (checkIndex == 1) {
111
 
        if (c1->isEmpty())
112
 
            return true;
113
 
 
114
 
        return (c1->top()->rank() == c2.first()->rank() + 1);
115
 
    } else {
116
 
        if (!c1->isEmpty())
117
 
            return false;
118
 
        return (c2.first()->rank() == Card::King && c2.last()->rank() == Card::Ace);
119
 
    }
120
 
}
121
 
 
122
 
bool Simon::checkRemove(int checkIndex, const Pile *p, const Card *c) const
123
 
{
124
 
    if (checkIndex != 1)
125
 
        return false;
126
 
 
127
 
    // ok if just one card
128
 
    if (c == p->top())
129
 
        return true;
130
 
 
131
 
    // Now we're trying to move two or more cards.
132
 
 
133
 
    // First, let's check if the column is in valid
134
 
    // (that is, in sequence, alternated colors).
135
 
    int index = p->indexOf(c) + 1;
136
 
    const Card *before = c;
137
 
    while (true)
138
 
    {
139
 
        c = p->at(index++);
140
 
 
141
 
        if (!((c->rank() == (before->rank()-1))
142
 
              && (c->suit() == before->suit())))
143
 
        {
144
 
            return false;
145
 
        }
146
 
        if (c == p->top())
147
 
            return true;
148
 
        before = c;
149
 
    }
150
 
 
151
 
    return true;
 
98
bool Simon::checkPrefering(const PatPile * pile, const QList<KCard*> & oldCards, const QList<KCard*> & newCards) const
 
99
{
 
100
    return pile->pileRole() == PatPile::Tableau
 
101
           && !oldCards.isEmpty()
 
102
           && getSuit( oldCards.last() ) == getSuit( newCards.first() );
 
103
}
 
104
 
 
105
bool Simon::checkAdd(const PatPile * pile, const QList<KCard*> & oldCards, const QList<KCard*> & newCards) const
 
106
{
 
107
    if (pile->pileRole() == PatPile::Tableau)
 
108
    {
 
109
        return oldCards.isEmpty()
 
110
               || getRank( oldCards.last() ) == getRank( newCards.first() ) + 1;
 
111
    }
 
112
    else
 
113
    {
 
114
        return oldCards.isEmpty()
 
115
               && getRank( newCards.first() ) == KStandardCardDeck::King
 
116
               && getRank( newCards.last() ) == KStandardCardDeck::Ace;
 
117
    }
 
118
}
 
119
 
 
120
bool Simon::checkRemove(const PatPile * pile, const QList<KCard*> & cards) const
 
121
{
 
122
    return pile->pileRole() == PatPile::Tableau
 
123
           && isSameSuitDescending(cards);
152
124
}
153
125
 
154
126