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 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663
|
/*
* 'same game' -- try to remove all the coloured squares by
* selecting regions of contiguous colours.
*/
/*
* TODO on grid generation:
*
* - Generation speed could still be improved.
* * 15x10c3 is the only really difficult one of the existing
* presets. The others are all either small enough, or have
* the great flexibility given by four colours, that they
* don't take long at all.
* * I still suspect many problems arise from separate
* subareas. I wonder if we can also somehow prioritise left-
* or rightmost insertions so as to avoid area splitting at
* all where feasible? It's not easy, though, because the
* current shuffle-then-try-all-options approach to move
* choice doesn't leave room for `soft' probabilistic
* prioritisation: we either try all class A moves before any
* class B ones, or we don't.
*
* - The current generation algorithm inserts exactly two squares
* at a time, with a single exception at the beginning of
* generation for grids of odd overall size. An obvious
* extension would be to permit larger inverse moves during
* generation.
* * this might reduce the number of failed generations by
* making the insertion algorithm more flexible
* * on the other hand, it would be significantly more complex
* * if I do this I'll need to take out the odd-subarea
* avoidance
* * a nice feature of the current algorithm is that the
* computer's `intended' solution always receives the minimum
* possible score, so that pretty much the player's entire
* score represents how much better they did than the
* computer.
*
* - Is it possible we can _temporarily_ tolerate neighbouring
* squares of the same colour, until we've finished setting up
* our inverse move?
* * or perhaps even not choose the colour of our inserted
* region until we have finished placing it, and _then_ look
* at what colours border on it?
* * I don't think this is currently meaningful unless we're
* placing more than a domino at a time.
*
* - possibly write out a full solution so that Solve can somehow
* show it step by step?
* * aux_info would have to encode the click points
* * solve_game() would have to encode not only those click
* points but also give a move string which reconstructed the
* initial state
* * the game_state would include a pointer to a solution move
* list, plus an index into that list
* * game_changed_state would auto-select the next move if
* handed a new state which had a solution move list active
* * execute_move, if passed such a state as input, would check
* to see whether the move being made was the same as the one
* stated by the solution, and if so would advance the move
* index. Failing that it would return a game_state without a
* solution move list active at all.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <ctype.h>
#include <math.h>
#include "puzzles.h"
#define TILE_INNER (ds->tileinner)
#define TILE_GAP (ds->tilegap)
#define TILE_SIZE (TILE_INNER + TILE_GAP)
#define PREFERRED_TILE_SIZE 32
#define BORDER (TILE_SIZE / 2)
#define HIGHLIGHT_WIDTH 2
#define FLASH_FRAME 0.13F
#define COORD(x) ( (x) * TILE_SIZE + BORDER )
#define FROMCOORD(x) ( ((x) - BORDER + TILE_SIZE) / TILE_SIZE - 1 )
#define X(state, i) ( (i) % (state)->params.w )
#define Y(state, i) ( (i) / (state)->params.w )
#define C(state, x, y) ( (y) * (state)->w + (x) )
enum {
COL_BACKGROUND,
COL_1, COL_2, COL_3, COL_4, COL_5, COL_6, COL_7, COL_8, COL_9,
COL_IMPOSSIBLE, COL_SEL, COL_HIGHLIGHT, COL_LOWLIGHT,
NCOLOURS
};
/* scoresub is 1 or 2 (for (n-1)^2 or (n-2)^2) */
struct game_params {
int w, h, ncols, scoresub;
int soluble; /* choose generation algorithm */
};
/* These flags must be unique across all uses; in the game_state,
* the game_ui, and the drawstate (as they all get combined in the
* drawstate). */
#define TILE_COLMASK 0x00ff
#define TILE_SELECTED 0x0100 /* used in ui and drawstate */
#define TILE_JOINRIGHT 0x0200 /* used in drawstate */
#define TILE_JOINDOWN 0x0400 /* used in drawstate */
#define TILE_JOINDIAG 0x0800 /* used in drawstate */
#define TILE_HASSEL 0x1000 /* used in drawstate */
#define TILE_IMPOSSIBLE 0x2000 /* used in drawstate */
#define TILE(gs,x,y) ((gs)->tiles[(gs)->params.w*(y)+(x)])
#define COL(gs,x,y) (TILE(gs,x,y) & TILE_COLMASK)
#define ISSEL(gs,x,y) (TILE(gs,x,y) & TILE_SELECTED)
#define SWAPTILE(gs,x1,y1,x2,y2) do { \
int t = TILE(gs,x1,y1); \
TILE(gs,x1,y1) = TILE(gs,x2,y2); \
TILE(gs,x2,y2) = t; \
} while (0)
static int npoints(game_params *params, int nsel)
{
int sdiff = nsel - params->scoresub;
return (sdiff > 0) ? sdiff * sdiff : 0;
}
struct game_state {
struct game_params params;
int n;
int *tiles; /* colour only */
int score;
int complete, impossible;
};
static game_params *default_params(void)
{
game_params *ret = snew(game_params);
ret->w = 5;
ret->h = 5;
ret->ncols = 3;
ret->scoresub = 2;
ret->soluble = TRUE;
return ret;
}
static const struct game_params samegame_presets[] = {
{ 5, 5, 3, 2, TRUE },
{ 10, 5, 3, 2, TRUE },
#ifdef SLOW_SYSTEM
{ 10, 10, 3, 2, TRUE },
#else
{ 15, 10, 3, 2, TRUE },
#endif
{ 15, 10, 4, 2, TRUE },
{ 20, 15, 4, 2, TRUE }
};
static int game_fetch_preset(int i, char **name, game_params **params)
{
game_params *ret;
char str[80];
if (i < 0 || i >= lenof(samegame_presets))
return FALSE;
ret = snew(game_params);
*ret = samegame_presets[i];
sprintf(str, "%dx%d, %d colours", ret->w, ret->h, ret->ncols);
*name = dupstr(str);
*params = ret;
return TRUE;
}
static void free_params(game_params *params)
{
sfree(params);
}
static game_params *dup_params(game_params *params)
{
game_params *ret = snew(game_params);
*ret = *params; /* structure copy */
return ret;
}
static void decode_params(game_params *params, char const *string)
{
char const *p = string;
params->w = atoi(p);
while (*p && isdigit((unsigned char)*p)) p++;
if (*p == 'x') {
p++;
params->h = atoi(p);
while (*p && isdigit((unsigned char)*p)) p++;
} else {
params->h = params->w;
}
if (*p == 'c') {
p++;
params->ncols = atoi(p);
while (*p && isdigit((unsigned char)*p)) p++;
} else {
params->ncols = 3;
}
if (*p == 's') {
p++;
params->scoresub = atoi(p);
while (*p && isdigit((unsigned char)*p)) p++;
} else {
params->scoresub = 2;
}
if (*p == 'r') {
p++;
params->soluble = FALSE;
}
}
static char *encode_params(game_params *params, int full)
{
char ret[80];
sprintf(ret, "%dx%dc%ds%d%s",
params->w, params->h, params->ncols, params->scoresub,
full && !params->soluble ? "r" : "");
return dupstr(ret);
}
static config_item *game_configure(game_params *params)
{
config_item *ret;
char buf[80];
ret = snewn(6, config_item);
ret[0].name = "Width";
ret[0].type = C_STRING;
sprintf(buf, "%d", params->w);
ret[0].sval = dupstr(buf);
ret[0].ival = 0;
ret[1].name = "Height";
ret[1].type = C_STRING;
sprintf(buf, "%d", params->h);
ret[1].sval = dupstr(buf);
ret[1].ival = 0;
ret[2].name = "No. of colours";
ret[2].type = C_STRING;
sprintf(buf, "%d", params->ncols);
ret[2].sval = dupstr(buf);
ret[2].ival = 0;
ret[3].name = "Scoring system";
ret[3].type = C_CHOICES;
ret[3].sval = ":(n-1)^2:(n-2)^2";
ret[3].ival = params->scoresub-1;
ret[4].name = "Ensure solubility";
ret[4].type = C_BOOLEAN;
ret[4].sval = NULL;
ret[4].ival = params->soluble;
ret[5].name = NULL;
ret[5].type = C_END;
ret[5].sval = NULL;
ret[5].ival = 0;
return ret;
}
static game_params *custom_params(config_item *cfg)
{
game_params *ret = snew(game_params);
ret->w = atoi(cfg[0].sval);
ret->h = atoi(cfg[1].sval);
ret->ncols = atoi(cfg[2].sval);
ret->scoresub = cfg[3].ival + 1;
ret->soluble = cfg[4].ival;
return ret;
}
static char *validate_params(game_params *params, int full)
{
if (params->w < 1 || params->h < 1)
return "Width and height must both be positive";
if (params->ncols > 9)
return "Maximum of 9 colours";
if (params->soluble) {
if (params->ncols < 3)
return "Number of colours must be at least three";
if (params->w * params->h <= 1)
return "Grid area must be greater than 1";
} else {
if (params->ncols < 2)
return "Number of colours must be at least three";
/* ...and we must make sure we can generate at least 2 squares
* of each colour so it's theoretically soluble. */
if ((params->w * params->h) < (params->ncols * 2))
return "Too many colours makes given grid size impossible";
}
if ((params->scoresub < 1) || (params->scoresub > 2))
return "Scoring system not recognised";
return NULL;
}
/*
* Guaranteed-soluble grid generator.
*/
static void gen_grid(int w, int h, int nc, int *grid, random_state *rs)
{
int wh = w*h, tc = nc+1;
int i, j, k, c, x, y, pos, n;
int *list, *grid2;
int ok, failures = 0;
/*
* We'll use `list' to track the possible places to put our
* next insertion. There are up to h places to insert in each
* column: in a column of height n there are n+1 places because
* we can insert at the very bottom or the very top, but a
* column of height h can't have anything at all inserted in it
* so we have up to h in each column. Likewise, with n columns
* present there are n+1 places to fit a new one in between but
* we can't insert a column if there are already w; so there
* are a maximum of w new columns too. Total is wh + w.
*/
list = snewn(wh + w, int);
grid2 = snewn(wh, int);
do {
/*
* Start with two or three squares - depending on parity of w*h
* - of a random colour.
*/
for (i = 0; i < wh; i++)
grid[i] = 0;
j = 2 + (wh % 2);
c = 1 + random_upto(rs, nc);
if (j <= w) {
for (i = 0; i < j; i++)
grid[(h-1)*w+i] = c;
} else {
assert(j <= h);
for (i = 0; i < j; i++)
grid[(h-1-i)*w] = c;
}
/*
* Now repeatedly insert a two-square blob in the grid, of
* whatever colour will go at the position we chose.
*/
while (1) {
n = 0;
/*
* Build up a list of insertion points. Each point is
* encoded as y*w+x; insertion points between columns are
* encoded as h*w+x.
*/
if (grid[wh - 1] == 0) {
/*
* The final column is empty, so we can insert new
* columns.
*/
for (i = 0; i < w; i++) {
list[n++] = wh + i;
if (grid[(h-1)*w + i] == 0)
break;
}
}
/*
* Now look for places to insert within columns.
*/
for (i = 0; i < w; i++) {
if (grid[(h-1)*w+i] == 0)
break; /* no more columns */
if (grid[i] != 0)
continue; /* this column is full */
for (j = h; j-- > 0 ;) {
list[n++] = j*w+i;
if (grid[j*w+i] == 0)
break; /* this column is exhausted */
}
}
if (n == 0)
break; /* we're done */
#ifdef GENERATION_DIAGNOSTICS
printf("initial grid:\n");
{
int x,y;
for (y = 0; y < h; y++) {
for (x = 0; x < w; x++) {
if (grid[y*w+x] == 0)
printf("-");
else
printf("%d", grid[y*w+x]);
}
printf("\n");
}
}
#endif
/*
* Now go through the list one element at a time in
* random order, and actually attempt to insert
* something there.
*/
while (n-- > 0) {
int dirs[4], ndirs, dir;
i = random_upto(rs, n+1);
pos = list[i];
list[i] = list[n];
x = pos % w;
y = pos / w;
memcpy(grid2, grid, wh * sizeof(int));
if (y == h) {
/*
* Insert a column at position x.
*/
for (i = w-1; i > x; i--)
for (j = 0; j < h; j++)
grid2[j*w+i] = grid2[j*w+(i-1)];
/*
* Clear the new column.
*/
for (j = 0; j < h; j++)
grid2[j*w+x] = 0;
/*
* Decrement y so that our first square is actually
* inserted _in_ the grid rather than just below it.
*/
y--;
}
/*
* Insert a square within column x at position y.
*/
for (i = 0; i+1 <= y; i++)
grid2[i*w+x] = grid2[(i+1)*w+x];
#ifdef GENERATION_DIAGNOSTICS
printf("trying at n=%d (%d,%d)\n", n, x, y);
grid2[y*w+x] = tc;
{
int x,y;
for (y = 0; y < h; y++) {
for (x = 0; x < w; x++) {
if (grid2[y*w+x] == 0)
printf("-");
else if (grid2[y*w+x] <= nc)
printf("%d", grid2[y*w+x]);
else
printf("*");
}
printf("\n");
}
}
#endif
/*
* Pick our square colour so that it doesn't match any
* of its neighbours.
*/
{
int wrongcol[4], nwrong = 0;
/*
* List the neighbouring colours.
*/
if (x > 0)
wrongcol[nwrong++] = grid2[y*w+(x-1)];
if (x+1 < w)
wrongcol[nwrong++] = grid2[y*w+(x+1)];
if (y > 0)
wrongcol[nwrong++] = grid2[(y-1)*w+x];
if (y+1 < h)
wrongcol[nwrong++] = grid2[(y+1)*w+x];
/*
* Eliminate duplicates. We can afford a shoddy
* algorithm here because the problem size is
* bounded.
*/
for (i = j = 0 ;; i++) {
int pos = -1, min = 0;
if (j > 0)
min = wrongcol[j-1];
for (k = i; k < nwrong; k++)
if (wrongcol[k] > min &&
(pos == -1 || wrongcol[k] < wrongcol[pos]))
pos = k;
if (pos >= 0) {
int v = wrongcol[pos];
wrongcol[pos] = wrongcol[j];
wrongcol[j++] = v;
} else
break;
}
nwrong = j;
/*
* If no colour will go here, stop trying.
*/
if (nwrong == nc)
continue;
/*
* Otherwise, pick a colour from the remaining
* ones.
*/
c = 1 + random_upto(rs, nc - nwrong);
for (i = 0; i < nwrong; i++) {
if (c >= wrongcol[i])
c++;
else
break;
}
}
/*
* Place the new square.
*
* Although I've _chosen_ the new region's colour
* (so that we can check adjacency), I'm going to
* actually place it as an invalid colour (tc)
* until I'm sure it's viable. This is so that I
* can conveniently check that I really have made a
* _valid_ inverse move later on.
*/
#ifdef GENERATION_DIAGNOSTICS
printf("picked colour %d\n", c);
#endif
grid2[y*w+x] = tc;
/*
* Now attempt to extend it in one of three ways: left,
* right or up.
*/
ndirs = 0;
if (x > 0 &&
grid2[y*w+(x-1)] != c &&
grid2[x-1] == 0 &&
(y+1 >= h || grid2[(y+1)*w+(x-1)] != c) &&
(y+1 >= h || grid2[(y+1)*w+(x-1)] != 0) &&
(x <= 1 || grid2[y*w+(x-2)] != c))
dirs[ndirs++] = -1; /* left */
if (x+1 < w &&
grid2[y*w+(x+1)] != c &&
grid2[x+1] == 0 &&
(y+1 >= h || grid2[(y+1)*w+(x+1)] != c) &&
(y+1 >= h || grid2[(y+1)*w+(x+1)] != 0) &&
(x+2 >= w || grid2[y*w+(x+2)] != c))
dirs[ndirs++] = +1; /* right */
if (y > 0 &&
grid2[x] == 0 &&
(x <= 0 || grid2[(y-1)*w+(x-1)] != c) &&
(x+1 >= w || grid2[(y-1)*w+(x+1)] != c)) {
/*
* We add this possibility _twice_, so that the
* probability of placing a vertical domino is
* about the same as that of a horizontal. This
* should yield less bias in the generated
* grids.
*/
dirs[ndirs++] = 0; /* up */
dirs[ndirs++] = 0; /* up */
}
if (ndirs == 0)
continue;
dir = dirs[random_upto(rs, ndirs)];
#ifdef GENERATION_DIAGNOSTICS
printf("picked dir %d\n", dir);
#endif
/*
* Insert a square within column (x+dir) at position y.
*/
for (i = 0; i+1 <= y; i++)
grid2[i*w+x+dir] = grid2[(i+1)*w+x+dir];
grid2[y*w+x+dir] = tc;
/*
* See if we've divided the remaining grid squares
* into sub-areas. If so, we need every sub-area to
* have an even area or we won't be able to
* complete generation.
*
* If the height is odd and not all columns are
* present, we can increase the area of a subarea
* by adding a new column in it, so in that
* situation we don't mind having as many odd
* subareas as there are spare columns.
*
* If the height is even, we can't fix it at all.
*/
{
int nerrs = 0, nfix = 0;
k = 0; /* current subarea size */
for (i = 0; i < w; i++) {
if (grid2[(h-1)*w+i] == 0) {
if (h % 2)
nfix++;
continue;
}
for (j = 0; j < h && grid2[j*w+i] == 0; j++);
assert(j < h);
if (j == 0) {
/*
* End of previous subarea.
*/
if (k % 2)
nerrs++;
k = 0;
} else {
k += j;
}
}
if (k % 2)
nerrs++;
if (nerrs > nfix)
continue; /* try a different placement */
}
/*
* We've made a move. Verify that it is a valid
* move and that if made it would indeed yield the
* previous grid state. The criteria are:
*
* (a) removing all the squares of colour tc (and
* shuffling the columns up etc) from grid2
* would yield grid
* (b) no square of colour tc is adjacent to one
* of colour c
* (c) all the squares of colour tc form a single
* connected component
*
* We verify the latter property at the same time
* as checking that removing all the tc squares
* would yield the previous grid. Then we colour
* the tc squares in colour c by breadth-first
* search, which conveniently permits us to test
* that they're all connected.
*/
{
int x1, x2, y1, y2;
int ok = TRUE;
int fillstart = -1, ntc = 0;
#ifdef GENERATION_DIAGNOSTICS
{
int x,y;
printf("testing move (new, old):\n");
for (y = 0; y < h; y++) {
for (x = 0; x < w; x++) {
if (grid2[y*w+x] == 0)
printf("-");
else if (grid2[y*w+x] <= nc)
printf("%d", grid2[y*w+x]);
else
printf("*");
}
printf(" ");
for (x = 0; x < w; x++) {
if (grid[y*w+x] == 0)
printf("-");
else
printf("%d", grid[y*w+x]);
}
printf("\n");
}
}
#endif
for (x1 = x2 = 0; x2 < w; x2++) {
int usedcol = FALSE;
for (y1 = y2 = h-1; y2 >= 0; y2--) {
if (grid2[y2*w+x2] == tc) {
ntc++;
if (fillstart == -1)
fillstart = y2*w+x2;
if ((y2+1 < h && grid2[(y2+1)*w+x2] == c) ||
(y2-1 >= 0 && grid2[(y2-1)*w+x2] == c) ||
(x2+1 < w && grid2[y2*w+x2+1] == c) ||
(x2-1 >= 0 && grid2[y2*w+x2-1] == c)) {
#ifdef GENERATION_DIAGNOSTICS
printf("adjacency failure at %d,%d\n",
x2, y2);
#endif
ok = FALSE;
}
continue;
}
if (grid2[y2*w+x2] == 0)
break;
usedcol = TRUE;
if (grid2[y2*w+x2] != grid[y1*w+x1]) {
#ifdef GENERATION_DIAGNOSTICS
printf("matching failure at %d,%d vs %d,%d\n",
x2, y2, x1, y1);
#endif
ok = FALSE;
}
y1--;
}
/*
* If we've reached the top of the column
* in grid2, verify that we've also reached
* the top of the column in `grid'.
*/
if (usedcol) {
while (y1 >= 0) {
if (grid[y1*w+x1] != 0) {
#ifdef GENERATION_DIAGNOSTICS
printf("junk at column top (%d,%d)\n",
x1, y1);
#endif
ok = FALSE;
}
y1--;
}
}
if (!ok)
break;
if (usedcol)
x1++;
}
if (!ok) {
assert(!"This should never happen");
/*
* If this game is compiled NDEBUG so that
* the assertion doesn't bring it to a
* crashing halt, the only thing we can do
* is to give up, loop round again, and
* hope to randomly avoid making whatever
* type of move just caused this failure.
*/
continue;
}
/*
* Now use bfs to fill in the tc section as
* colour c. We use `list' to store the set of
* squares we have to process.
*/
i = j = 0;
assert(fillstart >= 0);
list[i++] = fillstart;
#ifdef OUTPUT_SOLUTION
printf("M");
#endif
while (j < i) {
k = list[j];
x = k % w;
y = k / w;
#ifdef OUTPUT_SOLUTION
printf("%s%d", j ? "," : "", k);
#endif
j++;
assert(grid2[k] == tc);
grid2[k] = c;
if (x > 0 && grid2[k-1] == tc)
list[i++] = k-1;
if (x+1 < w && grid2[k+1] == tc)
list[i++] = k+1;
if (y > 0 && grid2[k-w] == tc)
list[i++] = k-w;
if (y+1 < h && grid2[k+w] == tc)
list[i++] = k+w;
}
#ifdef OUTPUT_SOLUTION
printf("\n");
#endif
/*
* Check that we've filled the same number of
* tc squares as we originally found.
*/
assert(j == ntc);
}
memcpy(grid, grid2, wh * sizeof(int));
break; /* done it! */
}
#ifdef GENERATION_DIAGNOSTICS
{
int x,y;
printf("n=%d\n", n);
for (y = 0; y < h; y++) {
for (x = 0; x < w; x++) {
if (grid[y*w+x] == 0)
printf("-");
else
printf("%d", grid[y*w+x]);
}
printf("\n");
}
}
#endif
if (n < 0)
break;
}
ok = TRUE;
for (i = 0; i < wh; i++)
if (grid[i] == 0) {
ok = FALSE;
failures++;
#if defined GENERATION_DIAGNOSTICS || defined SHOW_INCOMPLETE
{
int x,y;
printf("incomplete grid:\n");
for (y = 0; y < h; y++) {
for (x = 0; x < w; x++) {
if (grid[y*w+x] == 0)
printf("-");
else
printf("%d", grid[y*w+x]);
}
printf("\n");
}
}
#endif
break;
}
} while (!ok);
#if defined GENERATION_DIAGNOSTICS || defined COUNT_FAILURES
printf("%d failures\n", failures);
#endif
#ifdef GENERATION_DIAGNOSTICS
{
int x,y;
printf("final grid:\n");
for (y = 0; y < h; y++) {
for (x = 0; x < w; x++) {
printf("%d", grid[y*w+x]);
}
printf("\n");
}
}
#endif
sfree(grid2);
sfree(list);
}
/*
* Not-guaranteed-soluble grid generator; kept as a legacy, and in
* case someone finds the slightly odd quality of the guaranteed-
* soluble grids to be aesthetically displeasing or finds its CPU
* utilisation to be excessive.
*/
static void gen_grid_random(int w, int h, int nc, int *grid, random_state *rs)
{
int i, j, c;
int n = w * h;
for (i = 0; i < n; i++)
grid[i] = 0;
/*
* Our sole concession to not gratuitously generating insoluble
* grids is to ensure we have at least two of every colour.
*/
for (c = 1; c <= nc; c++) {
for (j = 0; j < 2; j++) {
do {
i = (int)random_upto(rs, n);
} while (grid[i] != 0);
grid[i] = c;
}
}
/*
* Fill in the rest of the grid at random.
*/
for (i = 0; i < n; i++) {
if (grid[i] == 0)
grid[i] = (int)random_upto(rs, nc)+1;
}
}
static char *new_game_desc(game_params *params, random_state *rs,
char **aux, int interactive)
{
char *ret;
int n, i, retlen, *tiles;
n = params->w * params->h;
tiles = snewn(n, int);
if (params->soluble)
gen_grid(params->w, params->h, params->ncols, tiles, rs);
else
gen_grid_random(params->w, params->h, params->ncols, tiles, rs);
ret = NULL;
retlen = 0;
for (i = 0; i < n; i++) {
char buf[80];
int k;
k = sprintf(buf, "%d,", tiles[i]);
ret = sresize(ret, retlen + k + 1, char);
strcpy(ret + retlen, buf);
retlen += k;
}
ret[retlen-1] = '\0'; /* delete last comma */
sfree(tiles);
return ret;
}
static char *validate_desc(game_params *params, char *desc)
{
int area = params->w * params->h, i;
char *p = desc;
for (i = 0; i < area; i++) {
char *q = p;
int n;
if (!isdigit((unsigned char)*p))
return "Not enough numbers in string";
while (isdigit((unsigned char)*p)) p++;
if (i < area-1 && *p != ',')
return "Expected comma after number";
else if (i == area-1 && *p)
return "Excess junk at end of string";
n = atoi(q);
if (n < 0 || n > params->ncols)
return "Colour out of range";
if (*p) p++; /* eat comma */
}
return NULL;
}
static game_state *new_game(midend *me, game_params *params, char *desc)
{
game_state *state = snew(game_state);
char *p = desc;
int i;
state->params = *params; /* struct copy */
state->n = state->params.w * state->params.h;
state->tiles = snewn(state->n, int);
for (i = 0; i < state->n; i++) {
assert(*p);
state->tiles[i] = atoi(p);
while (*p && *p != ',')
p++;
if (*p) p++; /* eat comma */
}
state->complete = state->impossible = 0;
state->score = 0;
return state;
}
static game_state *dup_game(game_state *state)
{
game_state *ret = snew(game_state);
*ret = *state; /* structure copy, except... */
ret->tiles = snewn(state->n, int);
memcpy(ret->tiles, state->tiles, state->n * sizeof(int));
return ret;
}
static void free_game(game_state *state)
{
sfree(state->tiles);
sfree(state);
}
static char *solve_game(game_state *state, game_state *currstate,
char *aux, char **error)
{
return NULL;
}
static char *game_text_format(game_state *state)
{
char *ret, *p;
int x, y, maxlen;
maxlen = state->params.h * (state->params.w + 1);
ret = snewn(maxlen+1, char);
p = ret;
for (y = 0; y < state->params.h; y++) {
for (x = 0; x < state->params.w; x++) {
int t = TILE(state,x,y);
if (t <= 0) *p++ = ' ';
else if (t < 10) *p++ = '0'+t;
else *p++ = 'a'+(t-10);
}
*p++ = '\n';
}
assert(p - ret == maxlen);
*p = '\0';
return ret;
}
struct game_ui {
struct game_params params;
int *tiles; /* selected-ness only */
int nselected;
int xsel, ysel, displaysel;
};
static game_ui *new_ui(game_state *state)
{
game_ui *ui = snew(game_ui);
ui->params = state->params; /* structure copy */
ui->tiles = snewn(state->n, int);
memset(ui->tiles, 0, state->n*sizeof(int));
ui->nselected = 0;
ui->xsel = ui->ysel = ui->displaysel = 0;
return ui;
}
static void free_ui(game_ui *ui)
{
sfree(ui->tiles);
sfree(ui);
}
static char *encode_ui(game_ui *ui)
{
return NULL;
}
static void decode_ui(game_ui *ui, char *encoding)
{
}
static void sel_clear(game_ui *ui, game_state *state)
{
int i;
for (i = 0; i < state->n; i++)
ui->tiles[i] &= ~TILE_SELECTED;
ui->nselected = 0;
}
static void game_changed_state(game_ui *ui, game_state *oldstate,
game_state *newstate)
{
sel_clear(ui, newstate);
/*
* If the game state has just changed into an unplayable one
* (either completed or impossible), we vanish the keyboard-
* control cursor.
*/
if (newstate->complete || newstate->impossible)
ui->displaysel = 0;
}
static char *sel_movedesc(game_ui *ui, game_state *state)
{
int i;
char *ret, *sep, buf[80];
int retlen, retsize;
retsize = 256;
ret = snewn(retsize, char);
retlen = 0;
ret[retlen++] = 'M';
sep = "";
for (i = 0; i < state->n; i++) {
if (ui->tiles[i] & TILE_SELECTED) {
sprintf(buf, "%s%d", sep, i);
sep = ",";
if (retlen + strlen(buf) >= retsize) {
retsize = retlen + strlen(buf) + 256;
ret = sresize(ret, retsize, char);
}
strcpy(ret + retlen, buf);
retlen += strlen(buf);
ui->tiles[i] &= ~TILE_SELECTED;
}
}
ui->nselected = 0;
assert(retlen < retsize);
ret[retlen++] = '\0';
return sresize(ret, retlen, char);
}
static void sel_expand(game_ui *ui, game_state *state, int tx, int ty)
{
int ns = 1, nadded, x, y, c;
TILE(ui,tx,ty) |= TILE_SELECTED;
do {
nadded = 0;
for (x = 0; x < state->params.w; x++) {
for (y = 0; y < state->params.h; y++) {
if (x == tx && y == ty) continue;
if (ISSEL(ui,x,y)) continue;
c = COL(state,x,y);
if ((x > 0) &&
ISSEL(ui,x-1,y) && COL(state,x-1,y) == c) {
TILE(ui,x,y) |= TILE_SELECTED;
nadded++;
continue;
}
if ((x+1 < state->params.w) &&
ISSEL(ui,x+1,y) && COL(state,x+1,y) == c) {
TILE(ui,x,y) |= TILE_SELECTED;
nadded++;
continue;
}
if ((y > 0) &&
ISSEL(ui,x,y-1) && COL(state,x,y-1) == c) {
TILE(ui,x,y) |= TILE_SELECTED;
nadded++;
continue;
}
if ((y+1 < state->params.h) &&
ISSEL(ui,x,y+1) && COL(state,x,y+1) == c) {
TILE(ui,x,y) |= TILE_SELECTED;
nadded++;
continue;
}
}
}
ns += nadded;
} while (nadded > 0);
if (ns > 1) {
ui->nselected = ns;
} else {
sel_clear(ui, state);
}
}
static int sg_emptycol(game_state *ret, int x)
{
int y;
for (y = 0; y < ret->params.h; y++) {
if (COL(ret,x,y)) return 0;
}
return 1;
}
static void sg_snuggle(game_state *ret)
{
int x,y, ndone;
/* make all unsupported tiles fall down. */
do {
ndone = 0;
for (x = 0; x < ret->params.w; x++) {
for (y = ret->params.h-1; y > 0; y--) {
if (COL(ret,x,y) != 0) continue;
if (COL(ret,x,y-1) != 0) {
SWAPTILE(ret,x,y,x,y-1);
ndone++;
}
}
}
} while (ndone);
/* shuffle all columns as far left as they can go. */
do {
ndone = 0;
for (x = 0; x < ret->params.w-1; x++) {
if (sg_emptycol(ret,x) && !sg_emptycol(ret,x+1)) {
ndone++;
for (y = 0; y < ret->params.h; y++) {
SWAPTILE(ret,x,y,x+1,y);
}
}
}
} while (ndone);
}
static void sg_check(game_state *ret)
{
int x,y, complete = 1, impossible = 1;
for (x = 0; x < ret->params.w; x++) {
for (y = 0; y < ret->params.h; y++) {
if (COL(ret,x,y) == 0)
continue;
complete = 0;
if (x+1 < ret->params.w) {
if (COL(ret,x,y) == COL(ret,x+1,y))
impossible = 0;
}
if (y+1 < ret->params.h) {
if (COL(ret,x,y) == COL(ret,x,y+1))
impossible = 0;
}
}
}
ret->complete = complete;
ret->impossible = impossible;
}
struct game_drawstate {
int started, bgcolour;
int tileinner, tilegap;
int *tiles; /* contains colour and SELECTED. */
};
static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds,
int x, int y, int button)
{
int tx, ty;
char *ret = "";
ui->displaysel = 0;
if (button == RIGHT_BUTTON || button == LEFT_BUTTON) {
tx = FROMCOORD(x); ty= FROMCOORD(y);
} else if (button == CURSOR_UP || button == CURSOR_DOWN ||
button == CURSOR_LEFT || button == CURSOR_RIGHT) {
int dx = 0, dy = 0;
ui->displaysel = 1;
dx = (button == CURSOR_LEFT) ? -1 : ((button == CURSOR_RIGHT) ? +1 : 0);
dy = (button == CURSOR_DOWN) ? +1 : ((button == CURSOR_UP) ? -1 : 0);
ui->xsel = (ui->xsel + state->params.w + dx) % state->params.w;
ui->ysel = (ui->ysel + state->params.h + dy) % state->params.h;
return ret;
} else if (button == CURSOR_SELECT || button == ' ' || button == '\r' ||
button == '\n') {
ui->displaysel = 1;
tx = ui->xsel;
ty = ui->ysel;
} else
return NULL;
if (tx < 0 || tx >= state->params.w || ty < 0 || ty >= state->params.h)
return NULL;
if (COL(state, tx, ty) == 0) return NULL;
if (ISSEL(ui,tx,ty)) {
if (button == RIGHT_BUTTON)
sel_clear(ui, state);
else
ret = sel_movedesc(ui, state);
} else {
sel_clear(ui, state); /* might be no-op */
sel_expand(ui, state, tx, ty);
}
return ret;
}
static game_state *execute_move(game_state *from, char *move)
{
int i, n;
game_state *ret;
if (move[0] == 'M') {
ret = dup_game(from);
n = 0;
move++;
while (*move) {
i = atoi(move);
if (i < 0 || i >= ret->n) {
free_game(ret);
return NULL;
}
n++;
ret->tiles[i] = 0;
while (*move && isdigit((unsigned char)*move)) move++;
if (*move == ',') move++;
}
ret->score += npoints(&ret->params, n);
sg_snuggle(ret); /* shifts blanks down and to the left */
sg_check(ret); /* checks for completeness or impossibility */
return ret;
} else
return NULL; /* couldn't parse move string */
}
/* ----------------------------------------------------------------------
* Drawing routines.
*/
static void game_set_size(drawing *dr, game_drawstate *ds,
game_params *params, int tilesize)
{
ds->tilegap = 2;
ds->tileinner = tilesize - ds->tilegap;
}
static void game_compute_size(game_params *params, int tilesize,
int *x, int *y)
{
/* Ick: fake up tile size variables for macro expansion purposes */
game_drawstate ads, *ds = &ads;
game_set_size(NULL, ds, params, tilesize);
*x = TILE_SIZE * params->w + 2 * BORDER - TILE_GAP;
*y = TILE_SIZE * params->h + 2 * BORDER - TILE_GAP;
}
static float *game_colours(frontend *fe, int *ncolours)
{
float *ret = snewn(3 * NCOLOURS, float);
frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]);
ret[COL_1 * 3 + 0] = 0.0F;
ret[COL_1 * 3 + 1] = 0.0F;
ret[COL_1 * 3 + 2] = 1.0F;
ret[COL_2 * 3 + 0] = 0.0F;
ret[COL_2 * 3 + 1] = 0.5F;
ret[COL_2 * 3 + 2] = 0.0F;
ret[COL_3 * 3 + 0] = 1.0F;
ret[COL_3 * 3 + 1] = 0.0F;
ret[COL_3 * 3 + 2] = 0.0F;
ret[COL_4 * 3 + 0] = 1.0F;
ret[COL_4 * 3 + 1] = 1.0F;
ret[COL_4 * 3 + 2] = 0.0F;
ret[COL_5 * 3 + 0] = 1.0F;
ret[COL_5 * 3 + 1] = 0.0F;
ret[COL_5 * 3 + 2] = 1.0F;
ret[COL_6 * 3 + 0] = 0.0F;
ret[COL_6 * 3 + 1] = 1.0F;
ret[COL_6 * 3 + 2] = 1.0F;
ret[COL_7 * 3 + 0] = 0.5F;
ret[COL_7 * 3 + 1] = 0.5F;
ret[COL_7 * 3 + 2] = 1.0F;
ret[COL_8 * 3 + 0] = 0.5F;
ret[COL_8 * 3 + 1] = 1.0F;
ret[COL_8 * 3 + 2] = 0.5F;
ret[COL_9 * 3 + 0] = 1.0F;
ret[COL_9 * 3 + 1] = 0.5F;
ret[COL_9 * 3 + 2] = 0.5F;
ret[COL_IMPOSSIBLE * 3 + 0] = 0.0F;
ret[COL_IMPOSSIBLE * 3 + 1] = 0.0F;
ret[COL_IMPOSSIBLE * 3 + 2] = 0.0F;
ret[COL_SEL * 3 + 0] = 1.0F;
ret[COL_SEL * 3 + 1] = 1.0F;
ret[COL_SEL * 3 + 2] = 1.0F;
ret[COL_HIGHLIGHT * 3 + 0] = 1.0F;
ret[COL_HIGHLIGHT * 3 + 1] = 1.0F;
ret[COL_HIGHLIGHT * 3 + 2] = 1.0F;
ret[COL_LOWLIGHT * 3 + 0] = ret[COL_BACKGROUND * 3 + 0] * 2.0 / 3.0;
ret[COL_LOWLIGHT * 3 + 1] = ret[COL_BACKGROUND * 3 + 1] * 2.0 / 3.0;
ret[COL_LOWLIGHT * 3 + 2] = ret[COL_BACKGROUND * 3 + 2] * 2.0 / 3.0;
*ncolours = NCOLOURS;
return ret;
}
static game_drawstate *game_new_drawstate(drawing *dr, game_state *state)
{
struct game_drawstate *ds = snew(struct game_drawstate);
int i;
ds->started = 0;
ds->tileinner = ds->tilegap = 0; /* not decided yet */
ds->tiles = snewn(state->n, int);
ds->bgcolour = -1;
for (i = 0; i < state->n; i++)
ds->tiles[i] = -1;
return ds;
}
static void game_free_drawstate(drawing *dr, game_drawstate *ds)
{
sfree(ds->tiles);
sfree(ds);
}
/* Drawing routing for the tile at (x,y) is responsible for drawing
* itself and the gaps to its right and below. If we're the same colour
* as the tile to our right, then we fill in the gap; ditto below, and if
* both then we fill the teeny tiny square in the corner as well.
*/
static void tile_redraw(drawing *dr, game_drawstate *ds,
int x, int y, int dright, int dbelow,
int tile, int bgcolour)
{
int outer = bgcolour, inner = outer, col = tile & TILE_COLMASK;
if (col) {
if (tile & TILE_IMPOSSIBLE) {
outer = col;
inner = COL_IMPOSSIBLE;
} else if (tile & TILE_SELECTED) {
outer = COL_SEL;
inner = col;
} else {
outer = inner = col;
}
}
draw_rect(dr, COORD(x), COORD(y), TILE_INNER, TILE_INNER, outer);
draw_rect(dr, COORD(x)+TILE_INNER/4, COORD(y)+TILE_INNER/4,
TILE_INNER/2, TILE_INNER/2, inner);
if (dright)
draw_rect(dr, COORD(x)+TILE_INNER, COORD(y), TILE_GAP, TILE_INNER,
(tile & TILE_JOINRIGHT) ? outer : bgcolour);
if (dbelow)
draw_rect(dr, COORD(x), COORD(y)+TILE_INNER, TILE_INNER, TILE_GAP,
(tile & TILE_JOINDOWN) ? outer : bgcolour);
if (dright && dbelow)
draw_rect(dr, COORD(x)+TILE_INNER, COORD(y)+TILE_INNER, TILE_GAP, TILE_GAP,
(tile & TILE_JOINDIAG) ? outer : bgcolour);
if (tile & TILE_HASSEL) {
int sx = COORD(x)+2, sy = COORD(y)+2, ssz = TILE_INNER-5;
int scol = (outer == COL_SEL) ? COL_LOWLIGHT : COL_HIGHLIGHT;
draw_line(dr, sx, sy, sx+ssz, sy, scol);
draw_line(dr, sx+ssz, sy, sx+ssz, sy+ssz, scol);
draw_line(dr, sx+ssz, sy+ssz, sx, sy+ssz, scol);
draw_line(dr, sx, sy+ssz, sx, sy, scol);
}
draw_update(dr, COORD(x), COORD(y), TILE_SIZE, TILE_SIZE);
}
static void game_redraw(drawing *dr, game_drawstate *ds, game_state *oldstate,
game_state *state, int dir, game_ui *ui,
float animtime, float flashtime)
{
int bgcolour, x, y;
/* This was entirely cloned from fifteen.c; it should probably be
* moved into some generic 'draw-recessed-rectangle' utility fn. */
if (!ds->started) {
int coords[10];
draw_rect(dr, 0, 0,
TILE_SIZE * state->params.w + 2 * BORDER,
TILE_SIZE * state->params.h + 2 * BORDER, COL_BACKGROUND);
draw_update(dr, 0, 0,
TILE_SIZE * state->params.w + 2 * BORDER,
TILE_SIZE * state->params.h + 2 * BORDER);
/*
* Recessed area containing the whole puzzle.
*/
coords[0] = COORD(state->params.w) + HIGHLIGHT_WIDTH - 1 - TILE_GAP;
coords[1] = COORD(state->params.h) + HIGHLIGHT_WIDTH - 1 - TILE_GAP;
coords[2] = COORD(state->params.w) + HIGHLIGHT_WIDTH - 1 - TILE_GAP;
coords[3] = COORD(0) - HIGHLIGHT_WIDTH;
coords[4] = coords[2] - TILE_SIZE;
coords[5] = coords[3] + TILE_SIZE;
coords[8] = COORD(0) - HIGHLIGHT_WIDTH;
coords[9] = COORD(state->params.h) + HIGHLIGHT_WIDTH - 1 - TILE_GAP;
coords[6] = coords[8] + TILE_SIZE;
coords[7] = coords[9] - TILE_SIZE;
draw_polygon(dr, coords, 5, COL_HIGHLIGHT, COL_HIGHLIGHT);
coords[1] = COORD(0) - HIGHLIGHT_WIDTH;
coords[0] = COORD(0) - HIGHLIGHT_WIDTH;
draw_polygon(dr, coords, 5, COL_LOWLIGHT, COL_LOWLIGHT);
ds->started = 1;
}
if (flashtime > 0.0) {
int frame = (int)(flashtime / FLASH_FRAME);
bgcolour = (frame % 2 ? COL_LOWLIGHT : COL_HIGHLIGHT);
} else
bgcolour = COL_BACKGROUND;
for (x = 0; x < state->params.w; x++) {
for (y = 0; y < state->params.h; y++) {
int i = (state->params.w * y) + x;
int col = COL(state,x,y), tile = col;
int dright = (x+1 < state->params.w);
int dbelow = (y+1 < state->params.h);
tile |= ISSEL(ui,x,y);
if (state->impossible)
tile |= TILE_IMPOSSIBLE;
if (dright && COL(state,x+1,y) == col)
tile |= TILE_JOINRIGHT;
if (dbelow && COL(state,x,y+1) == col)
tile |= TILE_JOINDOWN;
if ((tile & TILE_JOINRIGHT) && (tile & TILE_JOINDOWN) &&
COL(state,x+1,y+1) == col)
tile |= TILE_JOINDIAG;
if (ui->displaysel && ui->xsel == x && ui->ysel == y)
tile |= TILE_HASSEL;
/* For now we're never expecting oldstate at all (because we have
* no animation); when we do we might well want to be looking
* at the tile colours from oldstate, not state. */
if ((oldstate && COL(oldstate,x,y) != col) ||
(ds->bgcolour != bgcolour) ||
(tile != ds->tiles[i])) {
tile_redraw(dr, ds, x, y, dright, dbelow, tile, bgcolour);
ds->tiles[i] = tile;
}
}
}
ds->bgcolour = bgcolour;
{
char status[255], score[80];
sprintf(score, "Score: %d", state->score);
if (state->complete)
sprintf(status, "COMPLETE! %s", score);
else if (state->impossible)
sprintf(status, "Cannot move! %s", score);
else if (ui->nselected)
sprintf(status, "%s Selected: %d (%d)",
score, ui->nselected, npoints(&state->params, ui->nselected));
else
sprintf(status, "%s", score);
status_bar(dr, status);
}
}
static float game_anim_length(game_state *oldstate, game_state *newstate,
int dir, game_ui *ui)
{
return 0.0F;
}
static float game_flash_length(game_state *oldstate, game_state *newstate,
int dir, game_ui *ui)
{
if ((!oldstate->complete && newstate->complete) ||
(!oldstate->impossible && newstate->impossible))
return 2 * FLASH_FRAME;
else
return 0.0F;
}
static int game_timing_state(game_state *state, game_ui *ui)
{
return TRUE;
}
static void game_print_size(game_params *params, float *x, float *y)
{
}
static void game_print(drawing *dr, game_state *state, int tilesize)
{
}
#ifdef COMBINED
#define thegame samegame
#endif
const struct game thegame = {
"Same Game", "samegame",
default_params,
game_fetch_preset,
decode_params,
encode_params,
free_params,
dup_params,
TRUE, game_configure, custom_params,
validate_params,
new_game_desc,
validate_desc,
new_game,
dup_game,
free_game,
FALSE, solve_game,
TRUE, game_text_format,
new_ui,
free_ui,
encode_ui,
decode_ui,
game_changed_state,
interpret_move,
execute_move,
PREFERRED_TILE_SIZE, game_compute_size, game_set_size,
game_colours,
game_new_drawstate,
game_free_drawstate,
game_redraw,
game_anim_length,
game_flash_length,
FALSE, FALSE, game_print_size, game_print,
TRUE, /* wants_statusbar */
FALSE, game_timing_state,
0, /* flags */
};
|