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 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
|
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include "fc_pro_iface_pos.h"
#include "inline.h"
#include "min_and_max.h"
#include "alloc_wrap.h"
#if 0
static const char * ranks_map = "0A23456789TJQK";
static char * rank_to_string(int rank, char * buf)
{
buf[0] = ranks_map[rank];
buf[1] = '\0';
return buf;
}
static const char * suits_map = "HCDS";
static char * suit_to_string(int suit, char * buf)
{
buf[0] = suits_map[suit];
buf[1] = '\0';
return buf;
}
static char * card_to_string(fcs_card_t card, char * buf)
{
rank_to_string(card&0x0F,buf);
suit_to_string(card>>4,buf+1);
return buf;
}
#endif
static GCC_INLINE int Cvtf89(int fcn)
{
return (fcn >= 7) ? (fcn+3) : fcn;
}
char * fc_solve_moves_processed_render_move(fcs_extended_move_t move, char * string)
{
switch(fcs_move_get_type(move.move))
{
case FCS_MOVE_TYPE_STACK_TO_STACK:
if (move.to_empty_stack && (fcs_move_get_num_cards_in_seq(move.move) > 1))
{
sprintf(string, "%i%iv%x",
1+fcs_move_get_src_stack(move.move),
1+fcs_move_get_dest_stack(move.move),
fcs_move_get_num_cards_in_seq(move.move)
);
}
else
{
sprintf(string, "%i%i",
1+fcs_move_get_src_stack(move.move),
1+fcs_move_get_dest_stack(move.move)
);
}
break;
case FCS_MOVE_TYPE_FREECELL_TO_STACK:
sprintf(string, "%c%i",
('a'+Cvtf89(fcs_move_get_src_freecell(move.move))),
1+fcs_move_get_dest_stack(move.move)
);
break;
case FCS_MOVE_TYPE_FREECELL_TO_FREECELL:
{
char c1, c2;
c1 = (char)('a'+Cvtf89(fcs_move_get_src_freecell(move.move)));
c2 = (char)('a'+Cvtf89(fcs_move_get_dest_freecell(move.move)));
sprintf(string, "%c%c", c1, c2);
}
break;
case FCS_MOVE_TYPE_STACK_TO_FREECELL:
sprintf(string, "%i%c",
1+fcs_move_get_src_stack(move.move),
('a'+Cvtf89(fcs_move_get_dest_freecell(move.move)))
);
break;
case FCS_MOVE_TYPE_STACK_TO_FOUNDATION:
sprintf(string, "%ih", 1+fcs_move_get_src_stack(move.move));
break;
case FCS_MOVE_TYPE_FREECELL_TO_FOUNDATION:
sprintf(string, "%ch", ('a'+Cvtf89(fcs_move_get_src_freecell(move.move))));
break;
case FCS_MOVE_TYPE_SEQ_TO_FOUNDATION:
sprintf(string, "%ih", fcs_move_get_src_stack(move.move));
break;
default:
string[0] = '\0';
break;
}
return string+strlen(string);
}
#define MOVES_PROCESSED_GROW_BY 32
static GCC_INLINE void moves_processed_add_new_move(fcs_moves_processed_t * const moves, const fcs_extended_move_t new_move)
{
if (! ((++moves->num_moves) & (MOVES_PROCESSED_GROW_BY - 1)))
{
moves->moves =
SREALLOC (moves->moves, moves->num_moves + MOVES_PROCESSED_GROW_BY);
}
moves->moves[moves->num_moves-1] = new_move;
}
void fc_solve_moves_processed_gen(
fcs_moves_processed_t * const ret,
fcs_state_keyval_pair_t * const orig,
const int num_freecells,
const fcs_moves_sequence_t * const moves_seq
)
{
fcs_state_keyval_pair_t pos_proto;
DECLARE_IND_BUF_T(indirect_stacks_buffer)
fcs_kv_state_t orig_pass;
orig_pass.key = &(orig->s);
orig_pass.val = &(orig->info);
fcs_kv_state_t pos_pass;
pos_pass.key = &(pos_proto.s);
pos_pass.val = &(pos_proto.info);
fcs_duplicate_kv_state(&pos_pass, &orig_pass);
for (int i = 0 ; i < 8 ; i++)
{
fcs_copy_stack(pos_proto.s, pos_proto.info, i, indirect_stacks_buffer);
}
#define pos (pos_proto.s)
int virtual_stack_len[8];
#ifndef NDEBUG
int virtual_freecell_len[12];
#endif
int i, j, move_idx, num_back_end_moves;
fcs_move_t move, out_move, * next_move_ptr;
num_back_end_moves = moves_seq->num_moves;
next_move_ptr = moves_seq->moves;
ret->num_moves = 0;
ret->moves = SMALLOC(ret->moves, MOVES_PROCESSED_GROW_BY);
ret->next_move_idx = 0;
for(i=0;i<8;i++)
{
fcs_cards_column_t col = fcs_state_get_col(pos, i);
virtual_stack_len[i] = fcs_col_len(col);
}
#ifndef NDEBUG
for(i=0;i<num_freecells;i++)
{
virtual_freecell_len[i] = (! fcs_freecell_is_empty(pos, i)) ? 1 : 0;
}
#endif
for(move_idx=0; move_idx < num_back_end_moves ; move_idx ++)
{
/*
* Move safe cards to the foundations
* */
while (1)
{
#if 0
{
/* Check the intermediate position validity */
char exists[4*13];
int rank, suit;
int fc, col, count;
fcs_card_t card;
memset(exists, '\0', sizeof(exists));
for (suit=0;suit<4;suit++)
{
for(rank=1;rank<=fcs_foundation_value(pos, suit);rank++)
{
exists[rank-1+suit*13] = 1;
}
}
for (col=0;col<8;col++)
{
fcs_cards_column_t col_col = fcs_state_get_col(pos, col);
count = fcs_col_len(col_col);
for (i=0;i<count;i++)
{
card = fcs_col_get_card(col_col, i);
exists[fcs_card_rank(card)-1+fcs_card_suit(card)*13] = 1;
}
}
for (fc=0;fc<num_freecells;fc++)
{
card = fcs_freecell_card(pos, fc);
if (! fcs_card_is_empty(card))
{
exists[fcs_card_rank(card)-1+fcs_card_suit(card)*13] = 1;
}
}
for (i=0;i<52;i++)
{
if (exists[i] != 1)
{
printf("Invalid position!!!!!!!!!!!\n");
exit(-1);
}
}
}
#endif
#if 0
fcs_state_locs_struct_t locs;
fc_solve_init_locs(&locs);
printf("STATE=<<<\n%s\n>>>\n",
fc_solve_state_as_string(
&pos,
&pos_proto.info,
&locs,
4,
8,
1,
TRUE,
FALSE,
TRUE
)
);
#endif
for (i = 0 ; i < 8 ; i++)
{
int rank, suit;
fcs_card_t card;
fcs_cards_column_t col = fcs_state_get_col(pos, i);
if (fcs_col_len(col) > 0)
{
card = fcs_col_get_card(col, fcs_col_len(col) - 1);
rank = fcs_card_rank(card);
suit = fcs_card_suit(card);
/* Check if we can safely move it */
if ((fcs_foundation_value(pos, suit^0x1) >= rank-2) &&
(fcs_foundation_value(pos, suit^0x1^0x2) >= rank-2) &&
(fcs_foundation_value(pos, suit^0x2) >= rank-3) &&
(fcs_foundation_value(pos, suit) == rank-1))
{
fcs_increment_foundation(pos, suit);
fcs_col_pop_top(col);
/* An Automove. */
break;
}
}
}
if (i < 8)
{
continue;
}
for(j=0;j<num_freecells;j++)
{
int rank, suit;
fcs_card_t card;
if (! fcs_freecell_is_empty(pos, j))
{
card = fcs_freecell_card(pos, j);
rank = fcs_card_rank(card);
suit = fcs_card_suit(card);
/* Check if we can safely move it */
if ((fcs_foundation_value(pos, suit^0x1) >= rank-2) &&
(fcs_foundation_value(pos, suit^0x1^0x2) >= rank-2) &&
(fcs_foundation_value(pos, suit^0x2) >= rank-3) &&
(fcs_foundation_value(pos, suit) == rank-1))
{
fcs_increment_foundation(pos, suit);
fcs_empty_freecell(pos, j);
/* We've just done an auto-move */
break;
}
}
}
if ((i == 8) && (j == num_freecells))
{
break;
}
}
move = *(next_move_ptr++);
{
int src, dest;
fcs_card_t card;
switch(fcs_move_get_type(move))
{
case FCS_MOVE_TYPE_STACK_TO_FOUNDATION:
{
src = fcs_move_get_src_stack(move);
fcs_cards_column_t col = fcs_state_get_col(pos, src);
assert(virtual_stack_len[src] >= fcs_col_len(col));
if (virtual_stack_len[src] == fcs_col_len(col))
{
fcs_col_pop_card(col, card);
fcs_increment_foundation(pos, fcs_card_suit(card));
virtual_stack_len[src]--;
{
fcs_extended_move_t ext_move;
ext_move.move = move;
/* Stub value to settle gcc. Isn't used. */
ext_move.to_empty_stack = 0;
moves_processed_add_new_move(ret, ext_move);
}
}
else
{
virtual_stack_len[src]--;
}
}
break;
case FCS_MOVE_TYPE_FREECELL_TO_FOUNDATION:
{
src = fcs_move_get_src_freecell(move);
assert((virtual_freecell_len[src] == 1));
if (fcs_freecell_is_empty(pos, src))
{
/* Do nothing */
}
else
{
{
fcs_extended_move_t ext_move;
ext_move.move = move;
/* Stub value to settle gcc. Isn't used. */
ext_move.to_empty_stack = 0;
moves_processed_add_new_move(ret, ext_move);
}
fcs_increment_foundation(pos, fcs_freecell_card_suit(pos, src));
fcs_empty_freecell(pos, src);
}
#ifndef NDEBUG
virtual_freecell_len[src] = 0;
#endif
}
break;
case FCS_MOVE_TYPE_FREECELL_TO_STACK:
{
src = fcs_move_get_src_freecell(move);
dest = fcs_move_get_dest_stack(move);
assert(virtual_freecell_len[src] == 1);
if (fcs_freecell_is_empty(pos, src))
{
/* Do nothing */
}
else
{
{
fcs_extended_move_t ext_move;
ext_move.move = move;
/* Stub value to settle gcc. Isn't used. */
ext_move.to_empty_stack = 0;
moves_processed_add_new_move(ret, ext_move);
}
fcs_cards_column_t dest_col = fcs_state_get_col(pos, dest);
fcs_col_push_card(dest_col, fcs_freecell_card(pos, src));
fcs_empty_freecell(pos, src);
}
#ifndef NDEBUG
virtual_freecell_len[src] = 0;
#endif
virtual_stack_len[dest]++;
}
break;
case FCS_MOVE_TYPE_STACK_TO_FREECELL:
{
src = fcs_move_get_src_stack(move);
dest = fcs_move_get_dest_freecell(move);
assert(virtual_stack_len[src] > 0);
fcs_cards_column_t col = fcs_state_get_col(pos, src);
assert(fcs_col_len(col) <= virtual_stack_len[src]);
if (fcs_col_len(col) < virtual_stack_len[src])
{
/* Do nothing */
}
else
{
{
fcs_extended_move_t ext_move;
ext_move.move = move;
/* Stub value to settle gcc. Isn't used. */
ext_move.to_empty_stack = 0;
moves_processed_add_new_move(ret, ext_move);
}
fcs_card_t temp_card;
fcs_col_pop_card(col, temp_card);
fcs_put_card_in_freecell(pos, dest, temp_card);
}
virtual_stack_len[src]--;
#ifndef NDEBUG
virtual_freecell_len[dest] = 1;
#endif
}
break;
case FCS_MOVE_TYPE_STACK_TO_STACK:
{
src = fcs_move_get_src_stack(move);
dest = fcs_move_get_dest_stack(move);
int num_cards = fcs_move_get_num_cards_in_seq(move);
fcs_cards_column_t src_col = fcs_state_get_col(pos, src);
fcs_cards_column_t dest_col = fcs_state_get_col(pos, dest);
int src_len = fcs_col_len(src_col);
assert(virtual_stack_len[src] >= src_len);
if (virtual_stack_len[src] > src_len)
{
int virt_num_cards = min((virtual_stack_len[src]-src_len), num_cards);
virtual_stack_len[src] -= virt_num_cards;
virtual_stack_len[dest] += virt_num_cards;
num_cards -= virt_num_cards;
}
if (num_cards > 0)
{
fcs_move_set_type(out_move, FCS_MOVE_TYPE_STACK_TO_STACK);
fcs_move_set_src_stack(out_move, src);
fcs_move_set_dest_stack(out_move, dest);
fcs_move_set_num_cards_in_seq(out_move, num_cards);
{
fcs_extended_move_t ext_move;
ext_move.move = out_move;
ext_move.to_empty_stack = (fcs_col_len(dest_col) == 0);
moves_processed_add_new_move(ret, ext_move);
}
for(i=0;i<num_cards;i++)
{
fcs_col_push_col_card(dest_col, src_col, fcs_col_len(src_col)-num_cards+i);
}
for(i=0;i<num_cards;i++)
{
fcs_col_pop_top(src_col);
}
virtual_stack_len[dest] += num_cards;
virtual_stack_len[src] -= num_cards;
}
}
break;
}
}
}
}
|