| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
#include <stdlib.h> |
| 19 |
#include <stdio.h> |
| 20 |
#include <string.h> |
| 21 |
#include <time.h> |
| 22 |
|
| 23 |
#include "imagelib.h" |
| 24 |
#include "table.h" |
| 25 |
#include "cards.h" |
| 26 |
|
| 27 |
#define W CARD_WIDTH |
| 28 |
#define H CARD_HEIGHT |
| 29 |
#define M CARD_MARGIN |
| 30 |
#define F CARD_FAN_DOWN |
| 31 |
|
| 32 |
static Picture *splash, *youwin, *youlose, *arrow, *no_arrow; |
| 33 |
|
| 34 |
static Stack *deck; |
| 35 |
static Stack *outcells[8]; |
| 36 |
static Stack *maincells[10]; |
| 37 |
|
| 38 |
static int suit_mask = 0; |
| 39 |
|
| 40 |
static void auto_move(); |
| 41 |
static void check_for_end_of_game(); |
| 42 |
static void set_arrows(); |
| 43 |
static void clear_arrows(); |
| 44 |
static int ax, adx, ay, ady; |
| 45 |
|
| 46 |
static void |
| 47 |
start_again() |
| 48 |
{ |
| 49 |
int i, d, s, v; |
| 50 |
|
| 51 |
clear_arrows(); |
| 52 |
|
| 53 |
for (i=0; i<8; i++) |
| 54 |
stack_flip_stack(outcells[i], deck, 0); |
| 55 |
for (i=0; i<10; i++) |
| 56 |
stack_flip_stack(maincells[i], deck, 0); |
| 57 |
|
| 58 |
while(stack_take_card(deck) != -1); |
| 59 |
for (d=0; d<2; d++) |
| 60 |
for (s=0; s<4; s++) |
| 61 |
for (v=ACE; v<=KING; v++) |
| 62 |
stack_add_card(deck, MAKE_CARD(s & suit_mask, v, FACEDOWN)); |
| 63 |
|
| 64 |
stack_shuffle(deck); |
| 65 |
stack_shuffle(deck); |
| 66 |
stack_shuffle(deck); |
| 67 |
for (i=0; i<40; i++) |
| 68 |
stack_move_card(deck, maincells[i%10], 0); |
| 69 |
for (i=0; i<4; i++) |
| 70 |
stack_move_card(deck, maincells[i*3], 0); |
| 71 |
for (i=0; i<10; i++) |
| 72 |
stack_flip_card(deck, maincells[i], 0); |
| 73 |
|
| 74 |
stack_undo_reset(); |
| 75 |
|
| 76 |
set_arrows(); |
| 77 |
} |
| 78 |
|
| 79 |
static void |
| 80 |
init() |
| 81 |
{ |
| 82 |
int s; |
| 83 |
Picture *empty; |
| 84 |
|
| 85 |
stack_load_standard_deck(); |
| 86 |
empty = get_picture("empty"); |
| 87 |
|
| 88 |
set_centered_pic(splash); |
| 89 |
|
| 90 |
for (s=0; s<8; s++) { |
| 91 |
outcells[s] = stack_create((2+s)*(W+M), M); |
| 92 |
stack_set_empty_picture(outcells[s], empty); |
| 93 |
} |
| 94 |
|
| 95 |
for (s=0; s<10; s++) { |
| 96 |
maincells[s] = stack_create(M+s*(M+W), H+M); |
| 97 |
stack_set_offset(maincells[s], STACK_OFFSET_DOWN); |
| 98 |
} |
| 99 |
|
| 100 |
deck = stack_create(M, M); |
| 101 |
|
| 102 |
start_again(); |
| 103 |
} |
| 104 |
|
| 105 |
static void |
| 106 |
resize(int w, int h) |
| 107 |
{ |
| 108 |
int margin, offset, cw, ch, s, fd; |
| 109 |
|
| 110 |
Picture *empty; |
| 111 |
|
| 112 |
stack_set_card_size (w/11, w/11*4/3); |
| 113 |
stack_get_card_size (&cw, &ch); |
| 114 |
|
| 115 |
empty = (Picture *)get_image("empty", cw, ch, 0); |
| 116 |
|
| 117 |
margin = (w - 10*cw) / 11; |
| 118 |
offset = (w - margin*11 - cw*10) / 2 + margin; |
| 119 |
|
| 120 |
for (s=0; s<8; s++) { |
| 121 |
stack_move(outcells[s], (s+2)*(cw+margin)+offset, margin); |
| 122 |
stack_set_empty_picture(outcells[s], empty); |
| 123 |
} |
| 124 |
for (s=0; s<10; s++) |
| 125 |
stack_move(maincells[s], offset + s*(cw+margin), ch + (offset<0?0:offset)); |
| 126 |
stack_move(deck, margin+offset, margin); |
| 127 |
|
| 128 |
stack_get_fans(&fd, NULL, NULL, NULL); |
| 129 |
|
| 130 |
ax = offset + cw/2; |
| 131 |
adx = margin + cw; |
| 132 |
ay = offset + ch*2 + 2; |
| 133 |
ady = fd; |
| 134 |
|
| 135 |
set_arrows(); |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
static int no_arrows = 0; |
| 140 |
static int supress_arrows = 0; |
| 141 |
static int arrowsx[10] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; |
| 142 |
static int arrowsy[10] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; |
| 143 |
|
| 144 |
static void |
| 145 |
set_arrow(int column, int on) |
| 146 |
{ |
| 147 |
int x = ax + adx*column; |
| 148 |
if (supress_arrows || no_arrows) return; |
| 149 |
if (arrowsy[column]) |
| 150 |
put_picture(no_arrow, arrowsx[column]-no_arrow->w/2, arrowsy[column], |
| 151 |
0, 0, no_arrow->w, no_arrow->h); |
| 152 |
|
| 153 |
if (on) |
| 154 |
{ |
| 155 |
arrowsx[column] = x; |
| 156 |
arrowsy[column] = ay+ady*(stack_count_cards(maincells[column])-1); |
| 157 |
put_picture(arrow, arrowsx[column]-arrow->w/2, arrowsy[column], |
| 158 |
0, 0, arrow->w, arrow->h); |
| 159 |
} |
| 160 |
else |
| 161 |
arrowsy[column] = 0; |
| 162 |
} |
| 163 |
|
| 164 |
static void |
| 165 |
redraw_arrows() |
| 166 |
{ |
| 167 |
int i; |
| 168 |
if(no_arrows) return; |
| 169 |
for (i=0; i<10; i++) |
| 170 |
{ |
| 171 |
if (arrowsy[i]) |
| 172 |
put_picture(arrow, arrowsx[i]-arrow->w/2, arrowsy[i], |
| 173 |
0, 0, arrow->w, arrow->h); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
static void |
| 178 |
clear_arrows() |
| 179 |
{ |
| 180 |
int i; |
| 181 |
for (i=0; i<10; i++) |
| 182 |
set_arrow(i, 0); |
| 183 |
} |
| 184 |
|
| 185 |
static void |
| 186 |
set_arrows() |
| 187 |
{ |
| 188 |
int i, j, k, src_next; |
| 189 |
for (i=0; i<10; i++) |
| 190 |
{ |
| 191 |
Stack *dest = maincells[i]; |
| 192 |
int dest_count = stack_count_cards(dest); |
| 193 |
int dest_top = stack_get_card(dest, dest_count-1); |
| 194 |
int n = 0; |
| 195 |
int add = 0; |
| 196 |
|
| 197 |
set_arrow(i, 0); |
| 198 |
if (dest_count == 0) { set_arrow(i,1); add=1; }; |
| 199 |
|
| 200 |
for (j=0; j<10 && add==0; j++) { |
| 201 |
Stack *src = maincells[j]; |
| 202 |
int src_count = stack_count_cards(src); |
| 203 |
int src_top; |
| 204 |
int src_next; |
| 205 |
n = src_count; |
| 206 |
while (n--) { |
| 207 |
if ((i == j) || (src_count == 0)) n = 0; |
| 208 |
else { |
| 209 |
src_top = stack_get_card(src, n); |
| 210 |
src_next = stack_get_card(src, n-1); |
| 211 |
if (VALUE(src_top) == VALUE(dest_top)-1) { |
| 212 |
int better = 0; |
| 213 |
|
| 214 |
if(n == 0) better = 1; |
| 215 |
if (!better) { |
| 216 |
if (VALUE(src_top) != VALUE(src_next)-1) better = 1; |
| 217 |
} |
| 218 |
if (!better) { |
| 219 |
if (SUIT(src_top) != SUIT(src_next)) better = 1; |
| 220 |
} |
| 221 |
if (!better) { |
| 222 |
if (FACEDOWNP(src_next)) better = 1; |
| 223 |
} |
| 224 |
if (better) add = 1; |
| 225 |
} |
| 226 |
if (n == 0 || VALUE(src_top) != VALUE(src_next)-1 || |
| 227 |
SUIT(src_top) != SUIT(src_next) || |
| 228 |
FACEDOWNP(src_next) == FACEDOWN ) n = 0; |
| 229 |
} |
| 230 |
} |
| 231 |
} |
| 232 |
if (add) set_arrow(i,1); |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
static void |
| 238 |
redraw() |
| 239 |
{ |
| 240 |
stack_redraw(); |
| 241 |
redraw_arrows(); |
| 242 |
} |
| 243 |
|
| 244 |
extern char spider_help[]; |
| 245 |
|
| 246 |
static void |
| 247 |
key(int k, int x, int y) |
| 248 |
{ |
| 249 |
Picture *p = get_centered_pic(); |
| 250 |
|
| 251 |
set_centered_pic(0); |
| 252 |
if (p == splash) return; |
| 253 |
|
| 254 |
switch(k) { |
| 255 |
case 3: case 27: case 'q': case 'Q': |
| 256 |
exit(0); |
| 257 |
break; |
| 258 |
case KEY_F(1): case 'h': case 'H': |
| 259 |
help("bj.html", spider_help); |
| 260 |
break; |
| 261 |
case KEY_F(2): case 'r': |
| 262 |
start_again(); |
| 263 |
break; |
| 264 |
case 8: case 127: case KEY_DELETE: |
| 265 |
set_centered_pic(0); |
| 266 |
clear_arrows(); |
| 267 |
stack_undo(); |
| 268 |
set_arrows(); |
| 269 |
break; |
| 270 |
case '1': |
| 271 |
suit_mask = 0; |
| 272 |
start_again(); |
| 273 |
break; |
| 274 |
case '2': |
| 275 |
suit_mask = 2; |
| 276 |
start_again(); |
| 277 |
break; |
| 278 |
case '4': |
| 279 |
suit_mask = 3; |
| 280 |
start_again(); |
| 281 |
break; |
| 282 |
case 'a': |
| 283 |
if (no_arrows) { |
| 284 |
no_arrows = 0; |
| 285 |
set_arrows(); |
| 286 |
} else { |
| 287 |
clear_arrows(); |
| 288 |
no_arrows = 1; |
| 289 |
} |
| 290 |
break; |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
static Stack *src_stack = 0; |
| 295 |
static int src_n = 0; |
| 296 |
|
| 297 |
static Stack *dest_stack; |
| 298 |
static int last_n=-1; |
| 299 |
|
| 300 |
static int |
| 301 |
n_droppable_s(Stack *dest_stack) |
| 302 |
{ |
| 303 |
int src_top, dest_top, src_count, dest_count, i, src_next; |
| 304 |
|
| 305 |
src_count = stack_count_cards(src_stack); |
| 306 |
dest_count = stack_count_cards(dest_stack); |
| 307 |
dest_top = stack_get_card(dest_stack, dest_count-1); |
| 308 |
|
| 309 |
for (i=0; i<10; i++) { |
| 310 |
if (dest_stack == maincells[i]) { |
| 311 |
int n = src_count; |
| 312 |
|
| 313 |
while (n--) |
| 314 |
{ |
| 315 |
src_top = stack_get_card(src_stack, n); |
| 316 |
if ((VALUE(src_top) == VALUE(dest_top)-1) |
| 317 |
|| (n == src_n && dest_count == 0)) return n; |
| 318 |
|
| 319 |
src_next = stack_get_card(src_stack, n-1); |
| 320 |
if (n == 0 || VALUE(src_top) != VALUE(src_next)-1 || |
| 321 |
SUIT(src_top) != SUIT(src_next) || |
| 322 |
FACEDOWNP(src_next) == FACEDOWN ) { |
| 323 |
if ((dest_count == 0) || (dest_stack == src_stack))return n; |
| 324 |
else return src_count; |
| 325 |
} |
| 326 |
} |
| 327 |
return src_count; |
| 328 |
} |
| 329 |
} |
| 330 |
return src_count; |
| 331 |
} |
| 332 |
|
| 333 |
static int |
| 334 |
n_droppable(int x, int y) |
| 335 |
{ |
| 336 |
if (!src_stack) |
| 337 |
return -1; |
| 338 |
if (!stack_drag_find(x, y, &dest_stack)) |
| 339 |
{ |
| 340 |
dest_stack = src_stack; |
| 341 |
return last_n != -1 ? last_n : src_n; |
| 342 |
} |
| 343 |
return n_droppable_s(dest_stack); |
| 344 |
} |
| 345 |
|
| 346 |
|
| 347 |
static void |
| 348 |
auto_move() |
| 349 |
{ |
| 350 |
int i, c, s, v, f, n; |
| 351 |
|
| 352 |
for (i=0; i<10; i++) { |
| 353 |
n = stack_count_cards(maincells[i]); |
| 354 |
c = stack_get_card(maincells[i],n-1); |
| 355 |
if (FACEDOWNP(c)) |
| 356 |
stack_flip_card(maincells[i], maincells[i], 1); |
| 357 |
if ((VALUE(c) == ACE) && n >= 13) { |
| 358 |
s = SUIT(c); |
| 359 |
v = 2; |
| 360 |
f = FACEUP; |
| 361 |
n -= 2; |
| 362 |
while((n>=0)&&(stack_get_card(maincells[i],n) == MAKE_CARD(s,v,f))) { |
| 363 |
n--; v++; |
| 364 |
} |
| 365 |
if (v == 14) { |
| 366 |
|
| 367 |
c =0; |
| 368 |
while(stack_count_cards(outcells[c++]) != 0); |
| 369 |
c--; |
| 370 |
for(v=ACE; v<=KING; v++) |
| 371 |
stack_animate(maincells[i], outcells[c], 1); |
| 372 |
i--; |
| 373 |
} |
| 374 |
} |
| 375 |
} |
| 376 |
} |
| 377 |
|
| 378 |
static void |
| 379 |
check_for_end_of_game() |
| 380 |
{ |
| 381 |
auto_move(); |
| 382 |
if (stack_count_cards(outcells[7]) == 13) set_centered_pic(youwin); |
| 383 |
if (stack_count_cards(deck) == 0) { |
| 384 |
int i; |
| 385 |
int end = 1; |
| 386 |
set_arrows(); |
| 387 |
for (i=0; i<10; i++) { |
| 388 |
if (arrowsy[i] != 0) end = 0; |
| 389 |
} |
| 390 |
|
| 391 |
} |
| 392 |
} |
| 393 |
|
| 394 |
static void |
| 395 |
click(int x, int y, int b) |
| 396 |
{ |
| 397 |
int c, f; |
| 398 |
Picture *cp = get_centered_pic(); |
| 399 |
|
| 400 |
if (cp == splash){ |
| 401 |
set_centered_pic(0); |
| 402 |
return; |
| 403 |
} |
| 404 |
|
| 405 |
if (cp == youwin) { |
| 406 |
set_centered_pic(0); |
| 407 |
start_again(); |
| 408 |
return; |
| 409 |
} |
| 410 |
|
| 411 |
if (cp == youlose) { |
| 412 |
set_centered_pic(0); |
| 413 |
return; |
| 414 |
} |
| 415 |
|
| 416 |
src_stack = 0; |
| 417 |
f = stack_find(x, y, &src_stack, &src_n); |
| 418 |
if (!f) return; |
| 419 |
|
| 420 |
if (b > 1) |
| 421 |
{ |
| 422 |
stack_peek_card(src_stack, src_n, 1); |
| 423 |
return; |
| 424 |
} |
| 425 |
last_n = -1; |
| 426 |
|
| 427 |
for (c=0; c<8; c++) |
| 428 |
if (src_stack == outcells[c]) |
| 429 |
return; |
| 430 |
|
| 431 |
if (src_stack == deck) { |
| 432 |
int empty_stack=0; |
| 433 |
for (c=0; c<10; c++) { |
| 434 |
if (stack_count_cards(maincells[c]) == 0) empty_stack = 1; |
| 435 |
} |
| 436 |
clear_arrows(); |
| 437 |
if (empty_stack == 0) { |
| 438 |
for (c=0; c<10; c++) { |
| 439 |
stack_flip_card(deck, maincells[c], c); |
| 440 |
} |
| 441 |
} |
| 442 |
} |
| 443 |
|
| 444 |
if (stack_count_cards(src_stack) > 0) |
| 445 |
stack_begin_drag(src_stack, src_n, x, y); |
| 446 |
else |
| 447 |
src_stack = 0; |
| 448 |
} |
| 449 |
|
| 450 |
static void |
| 451 |
drag(int x, int y, int b) |
| 452 |
{ |
| 453 |
if (b > 1) return; |
| 454 |
last_n = n_droppable(x, y); |
| 455 |
stack_continue_drag(last_n, x, y); |
| 456 |
} |
| 457 |
|
| 458 |
static void |
| 459 |
drop(int x, int y, int b) |
| 460 |
{ |
| 461 |
last_n = n_droppable(x, y); |
| 462 |
|
| 463 |
if (b > 1) |
| 464 |
{ |
| 465 |
if (src_stack) |
| 466 |
stack_peek_card(src_stack, src_n, 0); |
| 467 |
return; |
| 468 |
} |
| 469 |
clear_arrows(); |
| 470 |
stack_drop(dest_stack, last_n, 0); |
| 471 |
check_for_end_of_game(); |
| 472 |
set_arrows(); |
| 473 |
} |
| 474 |
|
| 475 |
static FunctionMapping fmap[] = { |
| 476 |
{ "click", (void *)click }, |
| 477 |
{ "drag", (void *)drag }, |
| 478 |
{ "drop", (void *)drop }, |
| 479 |
{ "init", (void *)init }, |
| 480 |
{ "key", (void *)key }, |
| 481 |
{ "redraw", (void *)redraw }, |
| 482 |
{ "resize", (void *)resize }, |
| 483 |
{ 0, 0 } |
| 484 |
}; |
| 485 |
|
| 486 |
int |
| 487 |
main(int argc, char **argv) |
| 488 |
{ |
| 489 |
register_imagelib(appimglib_imagelib); |
| 490 |
init_ace(argc, argv, fmap); |
| 491 |
splash = get_picture("spider"); |
| 492 |
youwin = get_picture("youwin"); |
| 493 |
youlose = get_picture("youlose"); |
| 494 |
arrow = get_picture("golf-arrow"); |
| 495 |
no_arrow = get_picture("golf-noarrow"); |
| 496 |
if (table_width == 0 || table_height == 0) |
| 497 |
{ |
| 498 |
table_width = 10*(W+M) + M; |
| 499 |
table_height = 600; |
| 500 |
} |
| 501 |
init_table(table_width, table_height); |
| 502 |
table_loop(); |
| 503 |
return 0; |
| 504 |
} |