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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
| #include <windows.h>
#include <commctrl.h>
#include <stdio.h>
#include "resource.h"
#include "sol.h"
#define CHEIGHT 96
#define CWIDTH 71
class UserField
{
public:
void UserFieldInit(HDC f, HINSTANCE hinst, int x, int y)
{
HBITMAP bmp_buffer=CreateCompatibleBitmap(f,x,y);
field=CreateCompatibleDC(f);
SelectObject(field,(HGDIOBJ)bmp_buffer);
d=4;
dd=16;
rdeck_start=1;
rdeck_count=3;
data.init();
hb=CreateSolidBrush(RGB(100,180,190));
r.left=0;
r.top=0;
r.right=x-1;
r.bottom=y-1;
X=x;
Y=y;
hmlist=ImageList_Create(CWIDTH,CHEIGHT,ILC_COLOR,54,54);
HBITMAP hbm;
for (int i=0;i<54;i++)
{
hbm=LoadBitmap(hinst,MAKEINTRESOURCE(102+i));
ImageList_Add(hmlist,hbm,NULL);
}
buffer=f;
drag[0]=0;
deck_x=10;
rdeck_x=100;
for (int i=0;i<4;i++)
aim_x[i]=250+i*80;
upper_y=10;
for (int i=0;i<7;i++) bottom_x[i]=10+80*i;
bottom_y=120;
}
void UpdateAll(bool redraw = true)
{
//rdeck
// deck
FillRect(field,&r,hb);
if (data.koloda[0] && rdeck_count) // rdeck
{
for (int i=rdeck_start;i<rdeck_start+rdeck_count;i++)
{
ImageList_Draw(hmlist,data.koloda[i],field,rdeck_x,upper_y,ILD_NORMAL);
rdeck_x+=dd;
}
rdeck_x=100;
}
//ImageList_Draw(hmlist,0,field,10,10,ILD_NORMAL);
if (data.koloda[0])
{
ImageList_Draw(hmlist,52,field,deck_x,upper_y,ILD_NORMAL);
//to be added
}
else ImageList_Draw(hmlist,53,field,deck_x,upper_y,ILD_NORMAL);
////////////////////////
for (int i=0;i<7;i++)
{
if (data.polia1[0][i])
{
int tmp=data.polia1[0][i];
if (drag[0] && (drag[1]==1) && (drag[2]==i))
{
tmp=data.polia1[0][i];
data.polia1[0][i]=drag[3]-1;
}
int pos=bottom_y;
for (int j=1;j<=data.polia1[19][i];j++)
{
ImageList_Draw(hmlist,52,field,bottom_x[i],pos,ILD_NORMAL);
pos+=d;
}
for (int j=data.polia1[19][i]+1;j<=data.polia1[0][i];j++)
{
ImageList_Draw(hmlist,data.polia1[j][i],field,bottom_x[i],pos,ILD_NORMAL);
pos+=dd;
}
data.polia1[0][i]=tmp;
}
}
for (int i=0;i<4;i++)
{
if (data.polia2[0][i])
{
if ((drag[0]) &&(drag[2]==i)&&(drag[1]==2)&& (data.polia2[0][i]==1)) ImageList_Draw(hmlist,53,field,aim_x[i],upper_y,ILD_NORMAL);
else if((drag[0])&&(drag[1]==2) &&(drag[2]==i)) ImageList_Draw(hmlist,data.polia2[data.polia2[0][i]-1][i],field,aim_x[i],upper_y,ILD_NORMAL);
else ImageList_Draw(hmlist,data.polia2[data.polia2[0][i]][i],field,aim_x[i],upper_y,ILD_NORMAL);
}
else ImageList_Draw(hmlist,53,field,aim_x[i],upper_y,ILD_NORMAL);
}
if (redraw) BitBlt(buffer,0,0,X,Y,field,0,0,SRCCOPY);
}
bool IsCaptured(int x, int y)
{
//upper left
//////////
//polia1
int a,b;
for (int i=0;i<7;i++)
{
if (data.polia1[0][i])
{
if ((x>=bottom_x[i]) && (x<=bottom_x[i]+CWIDTH))
{
a=bottom_y+d*data.polia1[19][i];
if ((y>=a)&&
(y<=a+CHEIGHT+(data.polia1[0][i]-data.polia1[19][i])*dd))
{
if ((data.polia1[0][i]==data.polia1[19][i])&&data.polia1[0][i]) // open hidden card
{
data.polia1[19][i]--;
return false;
}
dx=x-bottom_x[i];
drag[0]=1; // match
drag[1]=1; // polia1
drag[2]=i;
for (int j=data.polia1[19][i]+1;j<data.polia1[0][i];j++)
{
b=a+dd;
if ((y>=a)&&(y<b))
{
dy=y-a;
drag[3]=j;
return true;
}
a=b;
}
dy=y-a;
drag[3]=data.polia1[0][i];
return true;
}
}
}
}
// rdeck
if ((x>=rdeck_x+dd*(rdeck_count-1)) && (x<=rdeck_x+dd*(rdeck_count-1)+CWIDTH) &&
(y>=upper_y)&&(y<=upper_y+CHEIGHT))
{
if (!rdeck_count) return false;
//MessageBox(0,"","",MB_ICONSTOP);
drag[0]=1;
drag[1]=4;
drag[2]=rdeck_start+rdeck_count-1;
dx=x-(rdeck_x+dd*(rdeck_count-1));
dy=y-upper_y;
rdeck_count-=1;
printf("dx=%i dy=%i \n",dx,dy);
return true;
}
// polia2
for (int i=0;i<4;i++)
{
if (data.polia2[0][i])
{
if ((y>=upper_y) && (y<=upper_y+CHEIGHT))
{
if ((x>=aim_x[i])&&(x<=aim_x[i]+CWIDTH))
{
drag[0]=1;
drag[1]=2;
drag[2]=i;
//data.polia2[0][i]-=1;
dx=x-aim_x[i];
dy=y-upper_y;
return true;
}
}
}
}
return false;
}
void Drag(int x, int y)
{
UpdateAll(false); // blinking!!!!
if (drag[0])
{
int posx=x-dx;
int posy=y-dy;
if (drag[1]==1) // polia1
{
//MessageBox(0,"","",MB_ICONSTOP);
//ImageList_Draw(hmlist,/*data.polia1[i][drag[2]]*/ 8,field,posx,posy,ILD_NORMAL);
//printf("%i %i \n",drag[3],data.polia1[0][drag[2]]);
for (int i=drag[3];i<=data.polia1[0][drag[2]];i++)
{
ImageList_Draw(hmlist,data.polia1[i][drag[2]] ,field,posx,posy,ILD_NORMAL);
posy+=dd;
}
}
else if (drag[1]==4) // rdeck
ImageList_Draw(hmlist,data.koloda[rdeck_start+rdeck_count] ,field,posx,posy,ILD_NORMAL);
else // polia2
ImageList_Draw(hmlist,data.polia2[data.polia2[0][drag[2]]][drag[2]] ,field,posx,posy,ILD_NORMAL);
}
BitBlt(buffer,0,0,X,Y,field,0,0,SRCCOPY);
}
///////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////
bool DeckAction(int x,int y)
{
if ((x>=deck_x)&&(x<=deck_x+CWIDTH) && (y>=upper_y) && (y<=upper_y+CHEIGHT))
{
if ((rdeck_start+3)>data.koloda[0]) rdeck_start=1;
else rdeck_start+=3;
printf("s=%i c=%i\n",rdeck_start,rdeck_count);
rdeck_count=((data.koloda[0])>=(rdeck_start+2))?(3):(data.koloda[0]-rdeck_start+1);
rdeck_x=100;
UpdateAll();
return true;
}
return false;
}
///////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////
void Drop(int x, int y)
{
bool ok=false;
if ((y>=bottom_y))
{
for (int i=0;i<7;i++)
{
if ((x>=bottom_x[i])&&(x<=bottom_x[i]+CWIDTH)&&
(y<=bottom_y+CHEIGHT+d*data.polia1[19][i]+dd*(data.polia1[0][i]-data.polia1[19][i]-1)))
{
ok=data.move(drag[1],1,drag[2],drag[3],i);
}
}
}
if ((y>=upper_y)&&(y<=upper_y+CHEIGHT))
{
for (int i=0;i<4;i++)
{
if ((x>=aim_x[i])&&(x<=aim_x[i]+CWIDTH))
{
ok=data.move(drag[1],2,drag[2],drag[3],i);
}
}
}
if ((drag[1]==4) && !ok) rdeck_count++; // if not dropper
drag[0]=0;
if (data.isvictory())
{
MessageBox(0,"new session","You won",MB_OK);
data.init();
}
UpdateAll();
}
///////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////
void UserField::NewSession()
{
data.init();
rdeck_start=1;
rdeck_count=3;
}
protected:
private:
int X;
int Y;
int d; // distance by covered (back)
int dd; // covered front
int dx; // drag difference
int dy;
HDC field;
HDC buffer;
sol data;
HIMAGELIST hmlist;
int deck_x;
int rdeck_x;
int diff_rdeck_x;
int aim_x[4];
int upper_y;
int rdeck_start;
int rdeck_count;
//////////////////
int bottom_x[7];
int bottom_y;
int drag[4]; // [0] - true/false, [1]-[3] location
HBRUSH hb; // brush
RECT r;
}; |