-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcards.c
executable file
·213 lines (185 loc) · 4.51 KB
/
cards.c
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// originally from freecell by Sascha Volkenandt (sascha@akv-soft.de)
// Thanks to Thomas Guenther <tom@toms-cafe.de> for his patch to fix time_ms
#include <vdr/tools.h>
#include <vdr/plugin.h>
#include <stdlib.h>
#include "cards.h"
#include "cursor.h"
#include "osd.h"
const char *ShapeNames[SHAPE_COUNT] = {
"herz",
"karo",
"pik",
"kreuz"
};
const char *ValueNames[VALUE_COUNT] = {
"ace",
"two",
"three",
"four",
"five",
"six",
"seven",
"eight",
"nine",
"ten",
"jack",
"queen",
"king"
};
cCard::cCard(int Shape, int Value): cBitmap(Width(), Height(), 4) {
char *filename;
mShape = Shape;
mValue = Value;
mClosed = false;
if (Shape == -1 && Value == -1) {
asprintf(&filename, "%s/cover1.xpm", cPlugin::ConfigDirectory("solitaire"));
} else {
asprintf(&filename, "%s/%s_%s.xpm", cPlugin::ConfigDirectory("solitaire"),
ShapeNames[mShape], ValueNames[mValue]);
}
::LoadXpm(this, filename);
free(filename);
}
cCard::~cCard() {
}
bool cCard::Accepts(const cCard *Card) {
return (mValue > VALUE_ACE && mValue == Card->mValue + 1
&& SHAPE_RED(mShape) != SHAPE_RED(Card->mShape));
}
bool cCard::Closed() {
return mClosed;
}
void cCard::OpenCard() {
mClosed = false;
}
void cCard::CloseCard() {
mClosed = true;
}
cCardStack::cCardStack(int X, int Y, bool Stacked) {
mX = X;
mY = Y;
mStacked = Stacked;
mSource = false;
#if VDRVERSNUM >= 10318
mSeed = cTimeMs::Now();
#else
mSeed = time_ms();
#endif
mClosedCard = new cCard(-1, -1);
}
cCardStack::~cCardStack() {
Clear();
}
void cCardStack::Clear(void) {
while (Count() > 0)
delete TakeFirst();
}
void cCardStack::Refill(void) {
printf("\nRefill\n\n");
Clear();
for (int it = 0; it < VALUE_COUNT; ++it) {
Append(new cCard(SHAPE_HERZ, it));
Append(new cCard(SHAPE_KARO, it));
Append(new cCard(SHAPE_PIK, it));
Append(new cCard(SHAPE_KREUZ, it));
}
}
void cCardStack::Shuffle(void) {
cTBList<cCard*> cards = *this;
cTBList<cCard*>::Clear();
while (cards.Count()) {
int idx = (int)((double)cards.Count() * rand_r(&mSeed) / (RAND_MAX + 1.0));
Append(cards.Take(idx));
}
}
bool cCardStack::Accepts(const cCard *Card) {
if (Count() == 0)
return Card->mValue == VALUE_KING;
return Last()->Accepts(Card);
}
bool cCardStack::Solves(const cCard *Card) {
if (Count() == 0)
return Card->mValue == VALUE_ACE;
return Last()->mShape == Card->mShape && Last()->mValue + 1 == Card->mValue;
}
bool cCardStack::Solved(void) {
bool solved = Count() > 0 && Last()->mValue == VALUE_KING;
int shape = -1;
if (solved) {
for (uint it = 0; it < Count(); ++it) {
if (shape == -1)
shape = At(it)->mShape;
else if (shape != At(it)->mShape) {
solved = false;
break;
}
if (At(it)->mValue != (int)it) {
solved = false;
break;
}
}
}
return solved;
}
void cCardStack::GetCursorPos(int &x, int &y) {
x = mX + cCard::Width() / 2 - cCursor::Width() / 2;
if (mStacked) {
int cards = Count() - 1;
if (cards < 0)
cards = 0;
y = mY + cards * cCard::Height() / cCard::Distance() + cCard::Height() / 2
- cCursor::Height() / 2;
} else {
y = mY + cCard::Height() / 2 - cCursor::Height() / 2;
}
}
void cCardStack::GetFloatingPos(int &x, int &y) {
x = mX;
y = mY;
if (mStacked)
y += (Count() + 1) * cCard::Height() / cCard::Distance();
}
#if VDRVERSNUM < 10307
void cCardStack::Paint(cOsdBase *Osd) {
#else /* VDRVERSNUM < 10307 */
void cCardStack::Paint(cOsd *Osd) {
#endif /* VDRVERSNUM < 10307 */
ASSERT(mX == -1 || mY == -1);
Frame(Osd, mX, mY, mX + cCard::Width() - 1, mY + cCard::Height() - 1,
clrWhite);
if (mStacked)
for (uint it = 0; it < Count(); ++it)
#if VDRVERSNUM < 10307
if (At(it)->Closed())
Osd->SetBitmap(mX, mY + it * cCard::Height() / cCard::Distance(), *mClosedCard);
else
Osd->SetBitmap(mX, mY + it * cCard::Height() / cCard::Distance(), *At(it));
#else /* VDRVERSNUM < 10307 */
if (At(it)->Closed())
Osd->DrawBitmap(mX, mY + it * cCard::Height() / cCard::Distance(), *mClosedCard);
else
Osd->DrawBitmap(mX, mY + it * cCard::Height() / cCard::Distance(), *At(it));
#endif /* VDRVERSNUM < 10307 */
else if (Count() > 0)
#if VDRVERSNUM < 10307
if (Last()->Closed())
Osd->SetBitmap(mX, mY, *mClosedCard);
else
Osd->SetBitmap(mX, mY, *Last());
#else /* VDRVERSNUM < 10307 */
if (Last()->Closed())
Osd->DrawBitmap(mX, mY, *mClosedCard);
else
Osd->DrawBitmap(mX, mY, *Last());
#endif /* VDRVERSNUM < 10307 */
}
void cCardStack::SetAsSource() {
mSource = true;
}
void cCardStack::ResetSource() {
mSource = false;
}
bool cCardStack::IsSource() {
return mSource;
}