summaryrefslogtreecommitdiffstats
path: root/kpat/kings.cpp
blob: 22faf307d9d9b6137ccab821194669e724ca598c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include "kings.h"
#include <klocale.h>
#include <kdebug.h>
#include "deck.h"
#include <assert.h>
#include "freecell-solver/fcs_enums.h"
#include "cardmaps.h"

Kings::Kings( KMainWindow* tqparent, const char *name )
    : FreecellBase( 2, 8, 8, FCS_ES_FILLED_BY_KINGS_ONLY, true, tqparent, name )
{
    const int dist_x = cardMap::CARDX() * 11 / 10 + 1;

    for (int i=0; i<8; i++) {
        target[i]->move((8 + i/4) * dist_x + 10 + cardMap::CARDX() * 4 / 10, 10 + (i % 4) * cardMap::CARDY() * 14 / 10 );
        store[i]->move(10+dist_x*i, 10 + cardMap::CARDY() * 5 / 4);
        store[i]->setSpread(13);
        freecell[i]->move(10 + dist_x * i, 10);
    }
}

void Kings::demo()
{
    Dealer::demo();
}

void Kings::deal() {
    CardList cards = deck->cards();
    CardList::Iterator it = cards.begin();
    int cn = 0;
    for (int stack = -1; stack < 8; )
    {
        while (it != cards.end() && (*it)->rank() != Card::King) {
            if (stack >= 0) {
                store[stack]->add(*it, false, true);
                cn++;
            }
            ++it;
        }
        if (it == cards.end())
            break;
        cn++;
        store[++stack]->add(*it, false, true);
        if (stack == 0) {
            cards = deck->cards(); // reset to start
            it = cards.begin();
        } else
            ++it;
    }
    assert(cn == 104);
}

bool Kings::isGameLost() const {
	int i,indexi;
	Card *c,*cnext,*ctarget;
	CardList targets,ctops;

	for(i=0; i < 8; i++){
		if(freecell[i]->isEmpty())
			return false;
		if(store[i]->isEmpty())
			return false;
		if(store[i]->top()->rank() == Card::Ace)
			return false;
		}

	for(i=0; i < 8; i++){
		if(!target[i]->isEmpty())
			targets.append(target[i]->top());

		if(!store[i]->isEmpty())
			ctops.append(store[i]->top());
		} 

	for(i=0; i < 8; i++){
		if(store[i]->isEmpty())
			continue;

		c=store[i]->top();
		for (CardList::Iterator it = targets.begin(); it != targets.end(); ++it) {
			ctarget=*it;
			if(c->rank()-1 == ctarget->rank() &&
				c->suit() == ctarget->suit()){
				kdDebug(11111)<< "test 1" << endl;
				return false;
			}
		}
		
		for(indexi=store[i]->indexOf(store[i]->top()); indexi>=0;indexi--){
			c=store[i]->at(indexi);
			if(indexi > 0)
				cnext=store[i]->at(indexi-1);

			for (CardList::Iterator it = ctops.begin(); it != ctops.end(); ++it) {
				ctarget=*it;
				if(c->rank()+1 == ctarget->rank() &&
					c->isRed() != ctarget->isRed()){

					if(indexi == 0)
						return false;

					if(cnext->rank() != ctarget->rank()
					   || cnext->suit() != ctarget->suit())
						return false;
				}
			}
			if(cnext->rank() != c->rank()+1 && 
				cnext->isRed() != c->isRed())
				break;
		}
	}

	return true;
}

#if 0
NOTE: When this is reenabled, renumber the following patiences back again:
Golf
Klondike, draw 3
Spider Easy
Spider Medium
Spider Hard

static class LocalDealerInfo12 : public DealerInfo
{
public:
    LocalDealerInfo12() : DealerInfo(I18N_NOOP("&The Kings"), 12) {}
    virtual Dealer *createGame(KMainWindow *tqparent) { return new Kings(tqparent); }
} gfdi12;
#endif

#include "kings.moc"