| 1 | /* |
| 2 | * guess.c: Mastermind clone. |
| 3 | */ |
| 4 | |
| 5 | #include <stdio.h> |
| 6 | #include <stdlib.h> |
| 7 | #include <string.h> |
| 8 | #include <assert.h> |
| 9 | #include <ctype.h> |
| 10 | #include <math.h> |
| 11 | |
| 12 | #include "puzzles.h" |
| 13 | |
| 14 | #define FLASH_FRAME 0.5F |
| 15 | |
| 16 | enum { |
| 17 | COL_BACKGROUND, |
| 18 | COL_FRAME, COL_CURSOR, COL_FLASH, COL_HOLD, |
| 19 | COL_EMPTY, /* must be COL_1 - 1 */ |
| 20 | COL_1, COL_2, COL_3, COL_4, COL_5, COL_6, COL_7, COL_8, COL_9, COL_10, |
| 21 | COL_CORRECTPLACE, COL_CORRECTCOLOUR, |
| 22 | NCOLOURS |
| 23 | }; |
| 24 | |
| 25 | struct game_params { |
| 26 | int ncolours, npegs, nguesses; |
| 27 | int allow_blank, allow_multiple; |
| 28 | }; |
| 29 | |
| 30 | #define FEEDBACK_CORRECTPLACE 1 |
| 31 | #define FEEDBACK_CORRECTCOLOUR 2 |
| 32 | |
| 33 | typedef struct pegrow { |
| 34 | int npegs; |
| 35 | int *pegs; /* 0 is 'empty' */ |
| 36 | int *feedback; /* may well be unused */ |
| 37 | } *pegrow; |
| 38 | |
| 39 | struct game_state { |
| 40 | game_params params; |
| 41 | pegrow *guesses; /* length params->nguesses */ |
| 42 | pegrow solution; |
| 43 | int next_go; /* from 0 to nguesses-1; |
| 44 | if next_go == nguesses then they've lost. */ |
| 45 | int solved; |
| 46 | }; |
| 47 | |
| 48 | static game_params *default_params(void) |
| 49 | { |
| 50 | game_params *ret = snew(game_params); |
| 51 | |
| 52 | /* AFAIK this is the canonical Mastermind ruleset. */ |
| 53 | ret->ncolours = 6; |
| 54 | ret->npegs = 4; |
| 55 | ret->nguesses = 10; |
| 56 | |
| 57 | ret->allow_blank = 0; |
| 58 | ret->allow_multiple = 1; |
| 59 | |
| 60 | return ret; |
| 61 | } |
| 62 | |
| 63 | static void free_params(game_params *params) |
| 64 | { |
| 65 | sfree(params); |
| 66 | } |
| 67 | |
| 68 | static game_params *dup_params(game_params *params) |
| 69 | { |
| 70 | game_params *ret = snew(game_params); |
| 71 | *ret = *params; /* structure copy */ |
| 72 | return ret; |
| 73 | } |
| 74 | |
| 75 | static const struct { |
| 76 | char *name; |
| 77 | game_params params; |
| 78 | } guess_presets[] = { |
| 79 | {"Standard", {6, 4, 10, FALSE, TRUE}}, |
| 80 | {"Super", {8, 5, 12, FALSE, TRUE}}, |
| 81 | }; |
| 82 | |
| 83 | |
| 84 | static int game_fetch_preset(int i, char **name, game_params **params) |
| 85 | { |
| 86 | if (i < 0 || i >= lenof(guess_presets)) |
| 87 | return FALSE; |
| 88 | |
| 89 | *name = dupstr(guess_presets[i].name); |
| 90 | /* |
| 91 | * get round annoying const issues |
| 92 | */ |
| 93 | { |
| 94 | game_params tmp = guess_presets[i].params; |
| 95 | *params = dup_params(&tmp); |
| 96 | } |
| 97 | |
| 98 | return TRUE; |
| 99 | } |
| 100 | |
| 101 | static void decode_params(game_params *params, char const *string) |
| 102 | { |
| 103 | char const *p = string; |
| 104 | game_params *defs = default_params(); |
| 105 | |
| 106 | *params = *defs; free_params(defs); |
| 107 | |
| 108 | while (*p) { |
| 109 | switch (*p++) { |
| 110 | case 'c': |
| 111 | params->ncolours = atoi(p); |
| 112 | while (*p && isdigit((unsigned char)*p)) p++; |
| 113 | break; |
| 114 | |
| 115 | case 'p': |
| 116 | params->npegs = atoi(p); |
| 117 | while (*p && isdigit((unsigned char)*p)) p++; |
| 118 | break; |
| 119 | |
| 120 | case 'g': |
| 121 | params->nguesses = atoi(p); |
| 122 | while (*p && isdigit((unsigned char)*p)) p++; |
| 123 | break; |
| 124 | |
| 125 | case 'b': |
| 126 | params->allow_blank = 1; |
| 127 | break; |
| 128 | |
| 129 | case 'B': |
| 130 | params->allow_blank = 0; |
| 131 | break; |
| 132 | |
| 133 | case 'm': |
| 134 | params->allow_multiple = 1; |
| 135 | break; |
| 136 | |
| 137 | case 'M': |
| 138 | params->allow_multiple = 0; |
| 139 | break; |
| 140 | |
| 141 | default: |
| 142 | ; |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | static char *encode_params(game_params *params, int full) |
| 148 | { |
| 149 | char data[256]; |
| 150 | |
| 151 | sprintf(data, "c%dp%dg%d%s%s", |
| 152 | params->ncolours, params->npegs, params->nguesses, |
| 153 | params->allow_blank ? "b" : "B", params->allow_multiple ? "m" : "M"); |
| 154 | |
| 155 | return dupstr(data); |
| 156 | } |
| 157 | |
| 158 | static config_item *game_configure(game_params *params) |
| 159 | { |
| 160 | config_item *ret; |
| 161 | char buf[80]; |
| 162 | |
| 163 | ret = snewn(6, config_item); |
| 164 | |
| 165 | ret[0].name = "Colours"; |
| 166 | ret[0].type = C_STRING; |
| 167 | sprintf(buf, "%d", params->ncolours); |
| 168 | ret[0].sval = dupstr(buf); |
| 169 | ret[0].ival = 0; |
| 170 | |
| 171 | ret[1].name = "Pegs per guess"; |
| 172 | ret[1].type = C_STRING; |
| 173 | sprintf(buf, "%d", params->npegs); |
| 174 | ret[1].sval = dupstr(buf); |
| 175 | ret[1].ival = 0; |
| 176 | |
| 177 | ret[2].name = "Guesses"; |
| 178 | ret[2].type = C_STRING; |
| 179 | sprintf(buf, "%d", params->nguesses); |
| 180 | ret[2].sval = dupstr(buf); |
| 181 | ret[2].ival = 0; |
| 182 | |
| 183 | ret[3].name = "Allow blanks"; |
| 184 | ret[3].type = C_BOOLEAN; |
| 185 | ret[3].sval = NULL; |
| 186 | ret[3].ival = params->allow_blank; |
| 187 | |
| 188 | ret[4].name = "Allow duplicates"; |
| 189 | ret[4].type = C_BOOLEAN; |
| 190 | ret[4].sval = NULL; |
| 191 | ret[4].ival = params->allow_multiple; |
| 192 | |
| 193 | ret[5].name = NULL; |
| 194 | ret[5].type = C_END; |
| 195 | ret[5].sval = NULL; |
| 196 | ret[5].ival = 0; |
| 197 | |
| 198 | return ret; |
| 199 | } |
| 200 | |
| 201 | static game_params *custom_params(config_item *cfg) |
| 202 | { |
| 203 | game_params *ret = snew(game_params); |
| 204 | |
| 205 | ret->ncolours = atoi(cfg[0].sval); |
| 206 | ret->npegs = atoi(cfg[1].sval); |
| 207 | ret->nguesses = atoi(cfg[2].sval); |
| 208 | |
| 209 | ret->allow_blank = cfg[3].ival; |
| 210 | ret->allow_multiple = cfg[4].ival; |
| 211 | |
| 212 | return ret; |
| 213 | } |
| 214 | |
| 215 | static char *validate_params(game_params *params, int full) |
| 216 | { |
| 217 | if (params->ncolours < 2 || params->npegs < 2) |
| 218 | return "Trivial solutions are uninteresting"; |
| 219 | /* NB as well as the no. of colours we define, max(ncolours) must |
| 220 | * also fit in an unsigned char; see new_game_desc. */ |
| 221 | if (params->ncolours > 10) |
| 222 | return "Too many colours"; |
| 223 | if (params->nguesses < 1) |
| 224 | return "Must have at least one guess"; |
| 225 | if (!params->allow_multiple && params->ncolours < params->npegs) |
| 226 | return "Disallowing multiple colours requires at least as many colours as pegs"; |
| 227 | return NULL; |
| 228 | } |
| 229 | |
| 230 | static pegrow new_pegrow(int npegs) |
| 231 | { |
| 232 | pegrow pegs = snew(struct pegrow); |
| 233 | |
| 234 | pegs->npegs = npegs; |
| 235 | pegs->pegs = snewn(pegs->npegs, int); |
| 236 | memset(pegs->pegs, 0, pegs->npegs * sizeof(int)); |
| 237 | pegs->feedback = snewn(pegs->npegs, int); |
| 238 | memset(pegs->feedback, 0, pegs->npegs * sizeof(int)); |
| 239 | |
| 240 | return pegs; |
| 241 | } |
| 242 | |
| 243 | static pegrow dup_pegrow(pegrow pegs) |
| 244 | { |
| 245 | pegrow newpegs = new_pegrow(pegs->npegs); |
| 246 | |
| 247 | memcpy(newpegs->pegs, pegs->pegs, newpegs->npegs * sizeof(int)); |
| 248 | memcpy(newpegs->feedback, pegs->feedback, newpegs->npegs * sizeof(int)); |
| 249 | |
| 250 | return newpegs; |
| 251 | } |
| 252 | |
| 253 | static void invalidate_pegrow(pegrow pegs) |
| 254 | { |
| 255 | memset(pegs->pegs, -1, pegs->npegs * sizeof(int)); |
| 256 | memset(pegs->feedback, -1, pegs->npegs * sizeof(int)); |
| 257 | } |
| 258 | |
| 259 | static void free_pegrow(pegrow pegs) |
| 260 | { |
| 261 | sfree(pegs->pegs); |
| 262 | sfree(pegs->feedback); |
| 263 | sfree(pegs); |
| 264 | } |
| 265 | |
| 266 | static char *new_game_desc(game_params *params, random_state *rs, |
| 267 | char **aux, int interactive) |
| 268 | { |
| 269 | unsigned char *bmp = snewn(params->npegs, unsigned char); |
| 270 | char *ret; |
| 271 | int i, c; |
| 272 | pegrow colcount = new_pegrow(params->ncolours); |
| 273 | |
| 274 | for (i = 0; i < params->npegs; i++) { |
| 275 | newcol: |
| 276 | c = random_upto(rs, params->ncolours); |
| 277 | if (!params->allow_multiple && colcount->pegs[c]) goto newcol; |
| 278 | colcount->pegs[c]++; |
| 279 | bmp[i] = (unsigned char)(c+1); |
| 280 | } |
| 281 | obfuscate_bitmap(bmp, params->npegs*8, FALSE); |
| 282 | |
| 283 | ret = bin2hex(bmp, params->npegs); |
| 284 | sfree(bmp); |
| 285 | free_pegrow(colcount); |
| 286 | return ret; |
| 287 | } |
| 288 | |
| 289 | static char *validate_desc(game_params *params, char *desc) |
| 290 | { |
| 291 | unsigned char *bmp; |
| 292 | int i; |
| 293 | |
| 294 | /* desc is just an (obfuscated) bitmap of the solution; check that |
| 295 | * it's the correct length and (when unobfuscated) contains only |
| 296 | * sensible colours. */ |
| 297 | if (strlen(desc) != params->npegs * 2) |
| 298 | return "Game description is wrong length"; |
| 299 | bmp = hex2bin(desc, params->npegs); |
| 300 | obfuscate_bitmap(bmp, params->npegs*8, TRUE); |
| 301 | for (i = 0; i < params->npegs; i++) { |
| 302 | if (bmp[i] < 1 || bmp[i] > params->ncolours) { |
| 303 | sfree(bmp); |
| 304 | return "Game description is corrupted"; |
| 305 | } |
| 306 | } |
| 307 | sfree(bmp); |
| 308 | |
| 309 | return NULL; |
| 310 | } |
| 311 | |
| 312 | static game_state *new_game(midend *me, game_params *params, char *desc) |
| 313 | { |
| 314 | game_state *state = snew(game_state); |
| 315 | unsigned char *bmp; |
| 316 | int i; |
| 317 | |
| 318 | state->params = *params; |
| 319 | state->guesses = snewn(params->nguesses, pegrow); |
| 320 | for (i = 0; i < params->nguesses; i++) |
| 321 | state->guesses[i] = new_pegrow(params->npegs); |
| 322 | state->solution = new_pegrow(params->npegs); |
| 323 | |
| 324 | bmp = hex2bin(desc, params->npegs); |
| 325 | obfuscate_bitmap(bmp, params->npegs*8, TRUE); |
| 326 | for (i = 0; i < params->npegs; i++) |
| 327 | state->solution->pegs[i] = (int)bmp[i]; |
| 328 | sfree(bmp); |
| 329 | |
| 330 | state->next_go = state->solved = 0; |
| 331 | |
| 332 | return state; |
| 333 | } |
| 334 | |
| 335 | static game_state *dup_game(game_state *state) |
| 336 | { |
| 337 | game_state *ret = snew(game_state); |
| 338 | int i; |
| 339 | |
| 340 | *ret = *state; |
| 341 | |
| 342 | ret->guesses = snewn(state->params.nguesses, pegrow); |
| 343 | for (i = 0; i < state->params.nguesses; i++) |
| 344 | ret->guesses[i] = dup_pegrow(state->guesses[i]); |
| 345 | ret->solution = dup_pegrow(state->solution); |
| 346 | |
| 347 | return ret; |
| 348 | } |
| 349 | |
| 350 | static void free_game(game_state *state) |
| 351 | { |
| 352 | int i; |
| 353 | |
| 354 | free_pegrow(state->solution); |
| 355 | for (i = 0; i < state->params.nguesses; i++) |
| 356 | free_pegrow(state->guesses[i]); |
| 357 | sfree(state->guesses); |
| 358 | |
| 359 | sfree(state); |
| 360 | } |
| 361 | |
| 362 | static char *solve_game(game_state *state, game_state *currstate, |
| 363 | char *aux, char **error) |
| 364 | { |
| 365 | return dupstr("S"); |
| 366 | } |
| 367 | |
| 368 | static char *game_text_format(game_state *state) |
| 369 | { |
| 370 | return NULL; |
| 371 | } |
| 372 | |
| 373 | static int is_markable(game_params *params, pegrow pegs) |
| 374 | { |
| 375 | int i, nset = 0, nrequired, ret = 0; |
| 376 | pegrow colcount = new_pegrow(params->ncolours); |
| 377 | |
| 378 | nrequired = params->allow_blank ? 1 : params->npegs; |
| 379 | |
| 380 | for (i = 0; i < params->npegs; i++) { |
| 381 | int c = pegs->pegs[i]; |
| 382 | if (c > 0) { |
| 383 | colcount->pegs[c-1]++; |
| 384 | nset++; |
| 385 | } |
| 386 | } |
| 387 | if (nset < nrequired) goto done; |
| 388 | |
| 389 | if (!params->allow_multiple) { |
| 390 | for (i = 0; i < params->ncolours; i++) { |
| 391 | if (colcount->pegs[i] > 1) goto done; |
| 392 | } |
| 393 | } |
| 394 | ret = 1; |
| 395 | done: |
| 396 | free_pegrow(colcount); |
| 397 | return ret; |
| 398 | } |
| 399 | |
| 400 | struct game_ui { |
| 401 | game_params params; |
| 402 | pegrow curr_pegs; /* half-finished current move */ |
| 403 | int *holds; |
| 404 | int colour_cur; /* position of up-down colour picker cursor */ |
| 405 | int peg_cur; /* position of left-right peg picker cursor */ |
| 406 | int display_cur, markable; |
| 407 | |
| 408 | int drag_col, drag_x, drag_y; /* x and y are *center* of peg! */ |
| 409 | int drag_opeg; /* peg index, if dragged from a peg (from current guess), otherwise -1 */ |
| 410 | }; |
| 411 | |
| 412 | static game_ui *new_ui(game_state *state) |
| 413 | { |
| 414 | game_ui *ui = snew(game_ui); |
| 415 | memset(ui, 0, sizeof(game_ui)); |
| 416 | ui->params = state->params; /* structure copy */ |
| 417 | ui->curr_pegs = new_pegrow(state->params.npegs); |
| 418 | ui->holds = snewn(state->params.npegs, int); |
| 419 | memset(ui->holds, 0, sizeof(int)*state->params.npegs); |
| 420 | ui->drag_opeg = -1; |
| 421 | return ui; |
| 422 | } |
| 423 | |
| 424 | static void free_ui(game_ui *ui) |
| 425 | { |
| 426 | free_pegrow(ui->curr_pegs); |
| 427 | sfree(ui->holds); |
| 428 | sfree(ui); |
| 429 | } |
| 430 | |
| 431 | static char *encode_ui(game_ui *ui) |
| 432 | { |
| 433 | char *ret, *p, *sep; |
| 434 | int i; |
| 435 | |
| 436 | /* |
| 437 | * For this game it's worth storing the contents of the current |
| 438 | * guess. |
| 439 | */ |
| 440 | ret = snewn(40 * ui->curr_pegs->npegs, char); |
| 441 | p = ret; |
| 442 | sep = ""; |
| 443 | for (i = 0; i < ui->curr_pegs->npegs; i++) { |
| 444 | p += sprintf(p, "%s%d", sep, ui->curr_pegs->pegs[i]); |
| 445 | sep = ","; |
| 446 | } |
| 447 | assert(p - ret < 40 * ui->curr_pegs->npegs); |
| 448 | return sresize(ret, p - ret + 1, char); |
| 449 | } |
| 450 | |
| 451 | static void decode_ui(game_ui *ui, char *encoding) |
| 452 | { |
| 453 | int i; |
| 454 | char *p = encoding; |
| 455 | for (i = 0; i < ui->curr_pegs->npegs; i++) { |
| 456 | ui->curr_pegs->pegs[i] = atoi(p); |
| 457 | while (*p && isdigit((unsigned char)*p)) p++; |
| 458 | if (*p == ',') p++; |
| 459 | } |
| 460 | ui->markable = is_markable(&ui->params, ui->curr_pegs); |
| 461 | } |
| 462 | |
| 463 | static void game_changed_state(game_ui *ui, game_state *oldstate, |
| 464 | game_state *newstate) |
| 465 | { |
| 466 | int i; |
| 467 | |
| 468 | /* just clear the row-in-progress when we have an undo/redo. */ |
| 469 | for (i = 0; i < ui->curr_pegs->npegs; i++) |
| 470 | ui->curr_pegs->pegs[i] = 0; |
| 471 | ui->markable = FALSE; |
| 472 | } |
| 473 | |
| 474 | #define PEGSZ (ds->pegsz) |
| 475 | #define PEGOFF (ds->pegsz + ds->gapsz) |
| 476 | #define HINTSZ (ds->hintsz) |
| 477 | #define HINTOFF (ds->hintsz + ds->gapsz) |
| 478 | |
| 479 | #define GAP (ds->gapsz) |
| 480 | #define CGAP (ds->gapsz / 2) |
| 481 | |
| 482 | #define PEGRAD (ds->pegrad) |
| 483 | #define HINTRAD (ds->hintrad) |
| 484 | |
| 485 | #define COL_OX (ds->colx) |
| 486 | #define COL_OY (ds->coly) |
| 487 | #define COL_X(c) (COL_OX) |
| 488 | #define COL_Y(c) (COL_OY + (c)*PEGOFF) |
| 489 | #define COL_W PEGOFF |
| 490 | #define COL_H (ds->colours->npegs*PEGOFF) |
| 491 | |
| 492 | #define GUESS_OX (ds->guessx) |
| 493 | #define GUESS_OY (ds->guessy) |
| 494 | #define GUESS_X(g,p) (GUESS_OX + (p)*PEGOFF) |
| 495 | #define GUESS_Y(g,p) (GUESS_OY + (g)*PEGOFF) |
| 496 | #define GUESS_W (ds->solution->npegs*PEGOFF) |
| 497 | #define GUESS_H (ds->nguesses*PEGOFF) |
| 498 | |
| 499 | #define HINT_OX (GUESS_OX + GUESS_W + ds->gapsz) |
| 500 | #define HINT_OY (GUESS_OY + (PEGSZ - HINTOFF - HINTSZ) / 2) |
| 501 | #define HINT_X(g) HINT_OX |
| 502 | #define HINT_Y(g) (HINT_OY + (g)*PEGOFF) |
| 503 | #define HINT_W ((ds->hintw*HINTOFF) - GAP) |
| 504 | #define HINT_H GUESS_H |
| 505 | |
| 506 | #define SOLN_OX GUESS_OX |
| 507 | #define SOLN_OY (GUESS_OY + GUESS_H + ds->gapsz + 2) |
| 508 | #define SOLN_W GUESS_W |
| 509 | #define SOLN_H PEGOFF |
| 510 | |
| 511 | struct game_drawstate { |
| 512 | int nguesses; |
| 513 | pegrow *guesses; /* same size as state->guesses */ |
| 514 | pegrow solution; /* only displayed if state->solved */ |
| 515 | pegrow colours; /* length ncolours, not npegs */ |
| 516 | |
| 517 | int pegsz, hintsz, gapsz; /* peg size (diameter), etc. */ |
| 518 | int pegrad, hintrad; /* radius of peg, hint */ |
| 519 | int border; |
| 520 | int colx, coly; /* origin of colours vertical bar */ |
| 521 | int guessx, guessy; /* origin of guesses */ |
| 522 | int solnx, solny; /* origin of solution */ |
| 523 | int hintw; /* no. of hint tiles we're wide per row */ |
| 524 | int w, h, started, solved; |
| 525 | |
| 526 | int next_go; |
| 527 | |
| 528 | blitter *blit_peg; |
| 529 | int drag_col, blit_ox, blit_oy; |
| 530 | }; |
| 531 | |
| 532 | static void set_peg(game_params *params, game_ui *ui, int peg, int col) |
| 533 | { |
| 534 | ui->curr_pegs->pegs[peg] = col; |
| 535 | ui->markable = is_markable(params, ui->curr_pegs); |
| 536 | } |
| 537 | |
| 538 | static int mark_pegs(pegrow guess, pegrow solution, int ncols) |
| 539 | { |
| 540 | int nc_place = 0, nc_colour = 0, i, j; |
| 541 | |
| 542 | assert(guess && solution && (guess->npegs == solution->npegs)); |
| 543 | |
| 544 | for (i = 0; i < guess->npegs; i++) { |
| 545 | if (guess->pegs[i] == solution->pegs[i]) nc_place++; |
| 546 | } |
| 547 | |
| 548 | /* slight bit of cleverness: we have the following formula, from |
| 549 | * http://mathworld.wolfram.com/Mastermind.html that gives: |
| 550 | * |
| 551 | * nc_colour = sum(colours, min(#solution, #guess)) - nc_place |
| 552 | * |
| 553 | * I think this is due to Knuth. |
| 554 | */ |
| 555 | for (i = 1; i <= ncols; i++) { |
| 556 | int n_guess = 0, n_solution = 0; |
| 557 | for (j = 0; j < guess->npegs; j++) { |
| 558 | if (guess->pegs[j] == i) n_guess++; |
| 559 | if (solution->pegs[j] == i) n_solution++; |
| 560 | } |
| 561 | nc_colour += min(n_guess, n_solution); |
| 562 | } |
| 563 | nc_colour -= nc_place; |
| 564 | |
| 565 | debug(("mark_pegs, %d pegs, %d right place, %d right colour", |
| 566 | guess->npegs, nc_place, nc_colour)); |
| 567 | assert((nc_colour + nc_place) <= guess->npegs); |
| 568 | |
| 569 | memset(guess->feedback, 0, guess->npegs*sizeof(int)); |
| 570 | for (i = 0, j = 0; i < nc_place; i++) |
| 571 | guess->feedback[j++] = FEEDBACK_CORRECTPLACE; |
| 572 | for (i = 0; i < nc_colour; i++) |
| 573 | guess->feedback[j++] = FEEDBACK_CORRECTCOLOUR; |
| 574 | |
| 575 | return nc_place; |
| 576 | } |
| 577 | |
| 578 | static char *encode_move(game_state *from, game_ui *ui) |
| 579 | { |
| 580 | char *buf, *p, *sep; |
| 581 | int len, i, solved; |
| 582 | pegrow tmppegs; |
| 583 | |
| 584 | len = ui->curr_pegs->npegs * 20 + 2; |
| 585 | buf = snewn(len, char); |
| 586 | p = buf; |
| 587 | *p++ = 'G'; |
| 588 | sep = ""; |
| 589 | for (i = 0; i < ui->curr_pegs->npegs; i++) { |
| 590 | p += sprintf(p, "%s%d", sep, ui->curr_pegs->pegs[i]); |
| 591 | sep = ","; |
| 592 | } |
| 593 | *p++ = '\0'; |
| 594 | assert(p - buf <= len); |
| 595 | buf = sresize(buf, len, char); |
| 596 | |
| 597 | tmppegs = dup_pegrow(ui->curr_pegs); |
| 598 | solved = mark_pegs(tmppegs, from->solution, from->params.ncolours); |
| 599 | solved = (solved == from->params.ncolours); |
| 600 | free_pegrow(tmppegs); |
| 601 | |
| 602 | for (i = 0; i < from->solution->npegs; i++) { |
| 603 | if (!ui->holds[i] || solved) { |
| 604 | ui->curr_pegs->pegs[i] = 0; |
| 605 | } |
| 606 | if (solved) ui->holds[i] = 0; |
| 607 | } |
| 608 | ui->markable = is_markable(&from->params, ui->curr_pegs); |
| 609 | if (!ui->markable && ui->peg_cur == from->solution->npegs) |
| 610 | ui->peg_cur--; |
| 611 | |
| 612 | return buf; |
| 613 | } |
| 614 | |
| 615 | static char *interpret_move(game_state *from, game_ui *ui, game_drawstate *ds, |
| 616 | int x, int y, int button) |
| 617 | { |
| 618 | int over_col = 0; /* one-indexed */ |
| 619 | int over_guess = -1; /* zero-indexed */ |
| 620 | int over_past_guess_y = -1; /* zero-indexed */ |
| 621 | int over_past_guess_x = -1; /* zero-indexed */ |
| 622 | int over_hint = 0; /* zero or one */ |
| 623 | char *ret = NULL; |
| 624 | |
| 625 | int guess_ox = GUESS_X(from->next_go, 0); |
| 626 | int guess_oy = GUESS_Y(from->next_go, 0); |
| 627 | |
| 628 | if (from->solved) return NULL; |
| 629 | |
| 630 | if (x >= COL_OX && x <= (COL_OX + COL_W) && |
| 631 | y >= COL_OY && y <= (COL_OY + COL_H)) { |
| 632 | over_col = ((y - COL_OY) / PEGOFF) + 1; |
| 633 | } else if (x >= guess_ox && |
| 634 | y >= guess_oy && y <= (guess_oy + GUESS_H)) { |
| 635 | if (x <= (guess_ox + GUESS_W)) { |
| 636 | over_guess = (x - guess_ox) / PEGOFF; |
| 637 | } else { |
| 638 | over_hint = 1; |
| 639 | } |
| 640 | } else if (x >= guess_ox && x <= (guess_ox + GUESS_W) && |
| 641 | y >= GUESS_OY && y < guess_oy) { |
| 642 | over_past_guess_y = (y - GUESS_OY) / PEGOFF; |
| 643 | over_past_guess_x = (x - guess_ox) / PEGOFF; |
| 644 | } |
| 645 | debug(("make_move: over_col %d, over_guess %d, over_hint %d," |
| 646 | " over_past_guess (%d,%d)", over_col, over_guess, over_hint, |
| 647 | over_past_guess_x, over_past_guess_y)); |
| 648 | |
| 649 | assert(ds->blit_peg); |
| 650 | |
| 651 | /* mouse input */ |
| 652 | if (button == LEFT_BUTTON) { |
| 653 | if (over_col > 0) { |
| 654 | ui->drag_col = over_col; |
| 655 | ui->drag_opeg = -1; |
| 656 | debug(("Start dragging from colours")); |
| 657 | } else if (over_guess > -1) { |
| 658 | int col = ui->curr_pegs->pegs[over_guess]; |
| 659 | if (col) { |
| 660 | ui->drag_col = col; |
| 661 | ui->drag_opeg = over_guess; |
| 662 | debug(("Start dragging from a guess")); |
| 663 | } |
| 664 | } else if (over_past_guess_y > -1) { |
| 665 | int col = |
| 666 | from->guesses[over_past_guess_y]->pegs[over_past_guess_x]; |
| 667 | if (col) { |
| 668 | ui->drag_col = col; |
| 669 | ui->drag_opeg = -1; |
| 670 | debug(("Start dragging from a past guess")); |
| 671 | } |
| 672 | } |
| 673 | if (ui->drag_col) { |
| 674 | ui->drag_x = x; |
| 675 | ui->drag_y = y; |
| 676 | debug(("Start dragging, col = %d, (%d,%d)", |
| 677 | ui->drag_col, ui->drag_x, ui->drag_y)); |
| 678 | ret = ""; |
| 679 | } |
| 680 | } else if (button == LEFT_DRAG && ui->drag_col) { |
| 681 | ui->drag_x = x; |
| 682 | ui->drag_y = y; |
| 683 | debug(("Keep dragging, (%d,%d)", ui->drag_x, ui->drag_y)); |
| 684 | ret = ""; |
| 685 | } else if (button == LEFT_RELEASE && ui->drag_col) { |
| 686 | if (over_guess > -1) { |
| 687 | debug(("Dropping colour %d onto guess peg %d", |
| 688 | ui->drag_col, over_guess)); |
| 689 | set_peg(&from->params, ui, over_guess, ui->drag_col); |
| 690 | } else { |
| 691 | if (ui->drag_opeg > -1) { |
| 692 | debug(("Removing colour %d from peg %d", |
| 693 | ui->drag_col, ui->drag_opeg)); |
| 694 | set_peg(&from->params, ui, ui->drag_opeg, 0); |
| 695 | } |
| 696 | } |
| 697 | ui->drag_col = 0; |
| 698 | ui->drag_opeg = -1; |
| 699 | ui->display_cur = 0; |
| 700 | debug(("Stop dragging.")); |
| 701 | ret = ""; |
| 702 | } else if (button == RIGHT_BUTTON) { |
| 703 | if (over_guess > -1) { |
| 704 | /* we use ths feedback in the game_ui to signify |
| 705 | * 'carry this peg to the next guess as well'. */ |
| 706 | ui->holds[over_guess] = 1 - ui->holds[over_guess]; |
| 707 | ret = ""; |
| 708 | } |
| 709 | } else if (button == LEFT_RELEASE && over_hint && ui->markable) { |
| 710 | /* NB this won't trigger if on the end of a drag; that's on |
| 711 | * purpose, in case you drop by mistake... */ |
| 712 | ret = encode_move(from, ui); |
| 713 | } |
| 714 | |
| 715 | /* keyboard input */ |
| 716 | if (button == CURSOR_UP || button == CURSOR_DOWN) { |
| 717 | ui->display_cur = 1; |
| 718 | if (button == CURSOR_DOWN && (ui->colour_cur+1) < from->params.ncolours) |
| 719 | ui->colour_cur++; |
| 720 | if (button == CURSOR_UP && ui->colour_cur > 0) |
| 721 | ui->colour_cur--; |
| 722 | ret = ""; |
| 723 | } else if (button == CURSOR_LEFT || button == CURSOR_RIGHT) { |
| 724 | int maxcur = from->params.npegs; |
| 725 | if (ui->markable) maxcur++; |
| 726 | |
| 727 | ui->display_cur = 1; |
| 728 | if (button == CURSOR_RIGHT && (ui->peg_cur+1) < maxcur) |
| 729 | ui->peg_cur++; |
| 730 | if (button == CURSOR_LEFT && ui->peg_cur > 0) |
| 731 | ui->peg_cur--; |
| 732 | ret = ""; |
| 733 | } else if (button == CURSOR_SELECT || button == ' ' || button == '\r' || |
| 734 | button == '\n') { |
| 735 | ui->display_cur = 1; |
| 736 | if (ui->peg_cur == from->params.npegs) { |
| 737 | ret = encode_move(from, ui); |
| 738 | } else { |
| 739 | set_peg(&from->params, ui, ui->peg_cur, ui->colour_cur+1); |
| 740 | ret = ""; |
| 741 | } |
| 742 | } else if (button == 'D' || button == 'd' || button == '\b') { |
| 743 | ui->display_cur = 1; |
| 744 | set_peg(&from->params, ui, ui->peg_cur, 0); |
| 745 | ret = ""; |
| 746 | } else if (button == 'H' || button == 'h') { |
| 747 | ui->display_cur = 1; |
| 748 | ui->holds[ui->peg_cur] = 1 - ui->holds[ui->peg_cur]; |
| 749 | ret = ""; |
| 750 | } |
| 751 | return ret; |
| 752 | } |
| 753 | |
| 754 | static game_state *execute_move(game_state *from, char *move) |
| 755 | { |
| 756 | int i, nc_place; |
| 757 | game_state *ret; |
| 758 | char *p; |
| 759 | |
| 760 | if (!strcmp(move, "S")) { |
| 761 | ret = dup_game(from); |
| 762 | ret->solved = 1; |
| 763 | return ret; |
| 764 | } else if (move[0] == 'G') { |
| 765 | p = move+1; |
| 766 | |
| 767 | ret = dup_game(from); |
| 768 | |
| 769 | for (i = 0; i < from->solution->npegs; i++) { |
| 770 | int val = atoi(p); |
| 771 | int min_colour = from->params.allow_blank? 0 : 1; |
| 772 | if (val < min_colour || val > from->params.ncolours) { |
| 773 | free_game(ret); |
| 774 | return NULL; |
| 775 | } |
| 776 | ret->guesses[from->next_go]->pegs[i] = atoi(p); |
| 777 | while (*p && isdigit((unsigned char)*p)) p++; |
| 778 | if (*p == ',') p++; |
| 779 | } |
| 780 | |
| 781 | nc_place = mark_pegs(ret->guesses[from->next_go], ret->solution, ret->params.ncolours); |
| 782 | |
| 783 | if (nc_place == ret->solution->npegs) { |
| 784 | ret->solved = 1; /* win! */ |
| 785 | } else { |
| 786 | ret->next_go = from->next_go + 1; |
| 787 | if (ret->next_go >= ret->params.nguesses) |
| 788 | ret->solved = 1; /* 'lose' so we show the pegs. */ |
| 789 | } |
| 790 | |
| 791 | return ret; |
| 792 | } else |
| 793 | return NULL; |
| 794 | } |
| 795 | |
| 796 | /* ---------------------------------------------------------------------- |
| 797 | * Drawing routines. |
| 798 | */ |
| 799 | |
| 800 | #define PEG_PREFER_SZ 32 |
| 801 | |
| 802 | /* next three are multipliers for pegsz. It will look much nicer if |
| 803 | * (2*PEG_HINT) + PEG_GAP = 1.0 as the hints are formatted like that. */ |
| 804 | #define PEG_GAP 0.10 |
| 805 | #define PEG_HINT 0.35 |
| 806 | |
| 807 | #define BORDER 0.5 |
| 808 | |
| 809 | static void game_compute_size(game_params *params, int tilesize, |
| 810 | int *x, int *y) |
| 811 | { |
| 812 | double hmul, vmul_c, vmul_g, vmul; |
| 813 | int hintw = (params->npegs+1)/2; |
| 814 | |
| 815 | hmul = BORDER * 2.0 + /* border */ |
| 816 | 1.0 * 2.0 + /* vertical colour bar */ |
| 817 | 1.0 * params->npegs + /* guess pegs */ |
| 818 | PEG_GAP * params->npegs + /* guess gaps */ |
| 819 | PEG_HINT * hintw + /* hint pegs */ |
| 820 | PEG_GAP * (hintw - 1); /* hint gaps */ |
| 821 | |
| 822 | vmul_c = BORDER * 2.0 + /* border */ |
| 823 | 1.0 * params->ncolours + /* colour pegs */ |
| 824 | PEG_GAP * (params->ncolours - 1); /* colour gaps */ |
| 825 | |
| 826 | vmul_g = BORDER * 2.0 + /* border */ |
| 827 | 1.0 * (params->nguesses + 1) + /* guesses plus solution */ |
| 828 | PEG_GAP * (params->nguesses + 1); /* gaps plus gap above soln */ |
| 829 | |
| 830 | vmul = max(vmul_c, vmul_g); |
| 831 | |
| 832 | *x = (int)ceil((double)tilesize * hmul); |
| 833 | *y = (int)ceil((double)tilesize * vmul); |
| 834 | } |
| 835 | |
| 836 | static void game_set_size(drawing *dr, game_drawstate *ds, |
| 837 | game_params *params, int tilesize) |
| 838 | { |
| 839 | int colh, guessh; |
| 840 | |
| 841 | ds->pegsz = tilesize; |
| 842 | |
| 843 | ds->hintsz = (int)((double)ds->pegsz * PEG_HINT); |
| 844 | ds->gapsz = (int)((double)ds->pegsz * PEG_GAP); |
| 845 | ds->border = (int)((double)ds->pegsz * BORDER); |
| 846 | |
| 847 | ds->pegrad = (ds->pegsz -1)/2; /* radius of peg to fit in pegsz (which is 2r+1) */ |
| 848 | ds->hintrad = (ds->hintsz-1)/2; |
| 849 | |
| 850 | colh = ((ds->pegsz + ds->gapsz) * params->ncolours) - ds->gapsz; |
| 851 | guessh = ((ds->pegsz + ds->gapsz) * params->nguesses); /* guesses */ |
| 852 | guessh += ds->gapsz + ds->pegsz; /* solution */ |
| 853 | |
| 854 | game_compute_size(params, tilesize, &ds->w, &ds->h); |
| 855 | ds->colx = ds->border; |
| 856 | ds->coly = (ds->h - colh) / 2; |
| 857 | |
| 858 | ds->guessx = ds->solnx = ds->border + ds->pegsz * 2; /* border + colours */ |
| 859 | ds->guessy = (ds->h - guessh) / 2; |
| 860 | ds->solny = ds->guessy + ((ds->pegsz + ds->gapsz) * params->nguesses) + ds->gapsz; |
| 861 | |
| 862 | assert(ds->pegsz > 0); |
| 863 | assert(!ds->blit_peg); /* set_size is never called twice */ |
| 864 | ds->blit_peg = blitter_new(dr, ds->pegsz, ds->pegsz); |
| 865 | } |
| 866 | |
| 867 | static float *game_colours(frontend *fe, int *ncolours) |
| 868 | { |
| 869 | float *ret = snewn(3 * NCOLOURS, float), max; |
| 870 | int i; |
| 871 | |
| 872 | frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]); |
| 873 | |
| 874 | /* red */ |
| 875 | ret[COL_1 * 3 + 0] = 1.0F; |
| 876 | ret[COL_1 * 3 + 1] = 0.0F; |
| 877 | ret[COL_1 * 3 + 2] = 0.0F; |
| 878 | |
| 879 | /* yellow (toned down a bit due to pale grey background) */ |
| 880 | ret[COL_2 * 3 + 0] = 0.7F; |
| 881 | ret[COL_2 * 3 + 1] = 0.7F; |
| 882 | ret[COL_2 * 3 + 2] = 0.0F; |
| 883 | |
| 884 | /* green (also toned down) */ |
| 885 | ret[COL_3 * 3 + 0] = 0.0F; |
| 886 | ret[COL_3 * 3 + 1] = 0.5F; |
| 887 | ret[COL_3 * 3 + 2] = 0.0F; |
| 888 | |
| 889 | /* blue */ |
| 890 | ret[COL_4 * 3 + 0] = 0.0F; |
| 891 | ret[COL_4 * 3 + 1] = 0.0F; |
| 892 | ret[COL_4 * 3 + 2] = 1.0F; |
| 893 | |
| 894 | /* orange */ |
| 895 | ret[COL_5 * 3 + 0] = 1.0F; |
| 896 | ret[COL_5 * 3 + 1] = 0.5F; |
| 897 | ret[COL_5 * 3 + 2] = 0.0F; |
| 898 | |
| 899 | /* purple */ |
| 900 | ret[COL_6 * 3 + 0] = 0.5F; |
| 901 | ret[COL_6 * 3 + 1] = 0.0F; |
| 902 | ret[COL_6 * 3 + 2] = 0.7F; |
| 903 | |
| 904 | /* brown */ |
| 905 | ret[COL_7 * 3 + 0] = 0.4F; |
| 906 | ret[COL_7 * 3 + 1] = 0.2F; |
| 907 | ret[COL_7 * 3 + 2] = 0.2F; |
| 908 | |
| 909 | /* light blue */ |
| 910 | ret[COL_8 * 3 + 0] = 0.4F; |
| 911 | ret[COL_8 * 3 + 1] = 0.7F; |
| 912 | ret[COL_8 * 3 + 2] = 1.0F; |
| 913 | |
| 914 | /* light green */ |
| 915 | ret[COL_9 * 3 + 0] = 0.5F; |
| 916 | ret[COL_9 * 3 + 1] = 0.8F; |
| 917 | ret[COL_9 * 3 + 2] = 0.5F; |
| 918 | |
| 919 | /* pink */ |
| 920 | ret[COL_10 * 3 + 0] = 1.0F; |
| 921 | ret[COL_10 * 3 + 1] = 0.6F; |
| 922 | ret[COL_10 * 3 + 2] = 1.0F; |
| 923 | |
| 924 | ret[COL_FRAME * 3 + 0] = 0.0F; |
| 925 | ret[COL_FRAME * 3 + 1] = 0.0F; |
| 926 | ret[COL_FRAME * 3 + 2] = 0.0F; |
| 927 | |
| 928 | ret[COL_CURSOR * 3 + 0] = 0.0F; |
| 929 | ret[COL_CURSOR * 3 + 1] = 0.0F; |
| 930 | ret[COL_CURSOR * 3 + 2] = 0.0F; |
| 931 | |
| 932 | ret[COL_FLASH * 3 + 0] = 0.5F; |
| 933 | ret[COL_FLASH * 3 + 1] = 1.0F; |
| 934 | ret[COL_FLASH * 3 + 2] = 1.0F; |
| 935 | |
| 936 | ret[COL_HOLD * 3 + 0] = 1.0F; |
| 937 | ret[COL_HOLD * 3 + 1] = 0.5F; |
| 938 | ret[COL_HOLD * 3 + 2] = 0.5F; |
| 939 | |
| 940 | ret[COL_CORRECTPLACE*3 + 0] = 0.0F; |
| 941 | ret[COL_CORRECTPLACE*3 + 1] = 0.0F; |
| 942 | ret[COL_CORRECTPLACE*3 + 2] = 0.0F; |
| 943 | |
| 944 | ret[COL_CORRECTCOLOUR*3 + 0] = 1.0F; |
| 945 | ret[COL_CORRECTCOLOUR*3 + 1] = 1.0F; |
| 946 | ret[COL_CORRECTCOLOUR*3 + 2] = 1.0F; |
| 947 | |
| 948 | /* We want to make sure we can distinguish COL_CORRECTCOLOUR |
| 949 | * (which we hard-code as white) from COL_BACKGROUND (which |
| 950 | * could default to white on some platforms). |
| 951 | * Code borrowed from fifteen.c. */ |
| 952 | max = ret[COL_BACKGROUND*3]; |
| 953 | for (i = 1; i < 3; i++) |
| 954 | if (ret[COL_BACKGROUND*3+i] > max) |
| 955 | max = ret[COL_BACKGROUND*3+i]; |
| 956 | if (max * 1.2F > 1.0F) { |
| 957 | for (i = 0; i < 3; i++) |
| 958 | ret[COL_BACKGROUND*3+i] /= (max * 1.2F); |
| 959 | } |
| 960 | |
| 961 | /* We also want to be able to tell the difference between BACKGROUND |
| 962 | * and EMPTY, for similar distinguishing-hint reasons. */ |
| 963 | ret[COL_EMPTY * 3 + 0] = ret[COL_BACKGROUND * 3 + 0] * 2.0 / 3.0; |
| 964 | ret[COL_EMPTY * 3 + 1] = ret[COL_BACKGROUND * 3 + 1] * 2.0 / 3.0; |
| 965 | ret[COL_EMPTY * 3 + 2] = ret[COL_BACKGROUND * 3 + 2] * 2.0 / 3.0; |
| 966 | |
| 967 | *ncolours = NCOLOURS; |
| 968 | return ret; |
| 969 | } |
| 970 | |
| 971 | static game_drawstate *game_new_drawstate(drawing *dr, game_state *state) |
| 972 | { |
| 973 | struct game_drawstate *ds = snew(struct game_drawstate); |
| 974 | int i; |
| 975 | |
| 976 | memset(ds, 0, sizeof(struct game_drawstate)); |
| 977 | |
| 978 | ds->guesses = snewn(state->params.nguesses, pegrow); |
| 979 | ds->nguesses = state->params.nguesses; |
| 980 | for (i = 0; i < state->params.nguesses; i++) { |
| 981 | ds->guesses[i] = new_pegrow(state->params.npegs); |
| 982 | invalidate_pegrow(ds->guesses[i]); |
| 983 | } |
| 984 | ds->solution = new_pegrow(state->params.npegs); |
| 985 | invalidate_pegrow(ds->solution); |
| 986 | ds->colours = new_pegrow(state->params.ncolours); |
| 987 | invalidate_pegrow(ds->colours); |
| 988 | |
| 989 | ds->hintw = (state->params.npegs+1)/2; /* must round up */ |
| 990 | |
| 991 | ds->blit_peg = NULL; |
| 992 | |
| 993 | return ds; |
| 994 | } |
| 995 | |
| 996 | static void game_free_drawstate(drawing *dr, game_drawstate *ds) |
| 997 | { |
| 998 | int i; |
| 999 | |
| 1000 | if (ds->blit_peg) blitter_free(dr, ds->blit_peg); |
| 1001 | free_pegrow(ds->colours); |
| 1002 | free_pegrow(ds->solution); |
| 1003 | for (i = 0; i < ds->nguesses; i++) |
| 1004 | free_pegrow(ds->guesses[i]); |
| 1005 | sfree(ds->guesses); |
| 1006 | sfree(ds); |
| 1007 | } |
| 1008 | |
| 1009 | static void draw_peg(drawing *dr, game_drawstate *ds, int cx, int cy, |
| 1010 | int moving, int col) |
| 1011 | { |
| 1012 | /* |
| 1013 | * Some platforms antialias circles, which means we shouldn't |
| 1014 | * overwrite a circle of one colour with a circle of another |
| 1015 | * colour without erasing the background first. However, if the |
| 1016 | * peg is the one being dragged, we don't erase the background |
| 1017 | * because we _want_ it to alpha-blend nicely into whatever's |
| 1018 | * behind it. |
| 1019 | */ |
| 1020 | if (!moving) |
| 1021 | draw_rect(dr, cx-CGAP, cy-CGAP, PEGSZ+CGAP*2, PEGSZ+CGAP*2, |
| 1022 | COL_BACKGROUND); |
| 1023 | if (PEGRAD > 0) { |
| 1024 | draw_circle(dr, cx+PEGRAD, cy+PEGRAD, PEGRAD, |
| 1025 | COL_EMPTY + col, COL_EMPTY + col); |
| 1026 | } else |
| 1027 | draw_rect(dr, cx, cy, PEGSZ, PEGSZ, COL_EMPTY + col); |
| 1028 | draw_update(dr, cx-CGAP, cy-CGAP, PEGSZ+CGAP*2, PEGSZ+CGAP*2); |
| 1029 | } |
| 1030 | |
| 1031 | static void draw_cursor(drawing *dr, game_drawstate *ds, int x, int y) |
| 1032 | { |
| 1033 | draw_circle(dr, x+PEGRAD, y+PEGRAD, PEGRAD+CGAP, -1, COL_CURSOR); |
| 1034 | |
| 1035 | draw_update(dr, x-CGAP, y-CGAP, PEGSZ+CGAP*2, PEGSZ+CGAP*2); |
| 1036 | } |
| 1037 | |
| 1038 | static void guess_redraw(drawing *dr, game_drawstate *ds, int guess, |
| 1039 | pegrow src, int *holds, int cur_col, int force) |
| 1040 | { |
| 1041 | pegrow dest; |
| 1042 | int rowx, rowy, i, scol; |
| 1043 | |
| 1044 | if (guess == -1) { |
| 1045 | dest = ds->solution; |
| 1046 | rowx = SOLN_OX; |
| 1047 | rowy = SOLN_OY; |
| 1048 | } else { |
| 1049 | dest = ds->guesses[guess]; |
| 1050 | rowx = GUESS_X(guess,0); |
| 1051 | rowy = GUESS_Y(guess,0); |
| 1052 | } |
| 1053 | if (src) assert(src->npegs == dest->npegs); |
| 1054 | |
| 1055 | for (i = 0; i < dest->npegs; i++) { |
| 1056 | scol = src ? src->pegs[i] : 0; |
| 1057 | if (i == cur_col) |
| 1058 | scol |= 0x1000; |
| 1059 | if (holds && holds[i]) |
| 1060 | scol |= 0x2000; |
| 1061 | if ((dest->pegs[i] != scol) || force) { |
| 1062 | draw_peg(dr, ds, rowx + PEGOFF * i, rowy, FALSE, scol &~ 0x3000); |
| 1063 | /* |
| 1064 | * Hold marker. |
| 1065 | */ |
| 1066 | draw_rect(dr, rowx + PEGOFF * i, rowy + PEGSZ + ds->gapsz/2, |
| 1067 | PEGSZ, 2, (scol & 0x2000 ? COL_HOLD : COL_BACKGROUND)); |
| 1068 | draw_update(dr, rowx + PEGOFF * i, rowy + PEGSZ + ds->gapsz/2, |
| 1069 | PEGSZ, 2); |
| 1070 | if (scol & 0x1000) |
| 1071 | draw_cursor(dr, ds, rowx + PEGOFF * i, rowy); |
| 1072 | } |
| 1073 | dest->pegs[i] = scol; |
| 1074 | } |
| 1075 | } |
| 1076 | |
| 1077 | static void hint_redraw(drawing *dr, game_drawstate *ds, int guess, |
| 1078 | pegrow src, int force, int cursor, int markable) |
| 1079 | { |
| 1080 | pegrow dest = ds->guesses[guess]; |
| 1081 | int rowx, rowy, i, scol, col, hintlen; |
| 1082 | int need_redraw; |
| 1083 | int emptycol = (markable ? COL_FLASH : COL_EMPTY); |
| 1084 | |
| 1085 | if (src) assert(src->npegs == dest->npegs); |
| 1086 | |
| 1087 | hintlen = (dest->npegs + 1)/2; |
| 1088 | |
| 1089 | /* |
| 1090 | * Because of the possible presence of the cursor around this |
| 1091 | * entire section, we redraw all or none of it but never part. |
| 1092 | */ |
| 1093 | need_redraw = FALSE; |
| 1094 | |
| 1095 | for (i = 0; i < dest->npegs; i++) { |
| 1096 | scol = src ? src->feedback[i] : 0; |
| 1097 | if (i == 0 && cursor) |
| 1098 | scol |= 0x1000; |
| 1099 | if (i == 0 && markable) |
| 1100 | scol |= 0x2000; |
| 1101 | if ((scol != dest->feedback[i]) || force) { |
| 1102 | need_redraw = TRUE; |
| 1103 | } |
| 1104 | dest->feedback[i] = scol; |
| 1105 | } |
| 1106 | |
| 1107 | if (need_redraw) { |
| 1108 | int hinth = HINTSZ + GAP + HINTSZ; |
| 1109 | int hx,hy,hw,hh; |
| 1110 | |
| 1111 | hx = HINT_X(guess)-GAP; hy = HINT_Y(guess)-GAP; |
| 1112 | hw = HINT_W+GAP*2; hh = hinth+GAP*2; |
| 1113 | |
| 1114 | /* erase a large background rectangle */ |
| 1115 | draw_rect(dr, hx, hy, hw, hh, COL_BACKGROUND); |
| 1116 | |
| 1117 | for (i = 0; i < dest->npegs; i++) { |
| 1118 | scol = src ? src->feedback[i] : 0; |
| 1119 | col = ((scol == FEEDBACK_CORRECTPLACE) ? COL_CORRECTPLACE : |
| 1120 | (scol == FEEDBACK_CORRECTCOLOUR) ? COL_CORRECTCOLOUR : |
| 1121 | emptycol); |
| 1122 | |
| 1123 | rowx = HINT_X(guess); |
| 1124 | rowy = HINT_Y(guess); |
| 1125 | if (i < hintlen) { |
| 1126 | rowx += HINTOFF * i; |
| 1127 | } else { |
| 1128 | rowx += HINTOFF * (i - hintlen); |
| 1129 | rowy += HINTOFF; |
| 1130 | } |
| 1131 | if (HINTRAD > 0) { |
| 1132 | draw_circle(dr, rowx+HINTRAD, rowy+HINTRAD, HINTRAD, col, col); |
| 1133 | } else { |
| 1134 | draw_rect(dr, rowx, rowy, HINTSZ, HINTSZ, col); |
| 1135 | } |
| 1136 | } |
| 1137 | if (cursor) { |
| 1138 | int x1,y1,x2,y2; |
| 1139 | x1 = hx + CGAP; y1 = hy + CGAP; |
| 1140 | x2 = hx + hw - CGAP; y2 = hy + hh - CGAP; |
| 1141 | draw_line(dr, x1, y1, x2, y1, COL_CURSOR); |
| 1142 | draw_line(dr, x2, y1, x2, y2, COL_CURSOR); |
| 1143 | draw_line(dr, x2, y2, x1, y2, COL_CURSOR); |
| 1144 | draw_line(dr, x1, y2, x1, y1, COL_CURSOR); |
| 1145 | } |
| 1146 | |
| 1147 | draw_update(dr, hx, hy, hw, hh); |
| 1148 | } |
| 1149 | } |
| 1150 | |
| 1151 | static void currmove_redraw(drawing *dr, game_drawstate *ds, int guess, int col) |
| 1152 | { |
| 1153 | int ox = GUESS_X(guess, 0), oy = GUESS_Y(guess, 0), off = PEGSZ/4; |
| 1154 | |
| 1155 | draw_rect(dr, ox-off-1, oy, 2, PEGSZ, col); |
| 1156 | draw_update(dr, ox-off-1, oy, 2, PEGSZ); |
| 1157 | } |
| 1158 | |
| 1159 | static void game_redraw(drawing *dr, game_drawstate *ds, game_state *oldstate, |
| 1160 | game_state *state, int dir, game_ui *ui, |
| 1161 | float animtime, float flashtime) |
| 1162 | { |
| 1163 | int i, new_move, last_go; |
| 1164 | |
| 1165 | new_move = (state->next_go != ds->next_go) || !ds->started; |
| 1166 | last_go = (state->next_go == state->params.nguesses-1); |
| 1167 | |
| 1168 | if (!ds->started) { |
| 1169 | draw_rect(dr, 0, 0, ds->w, ds->h, COL_BACKGROUND); |
| 1170 | draw_rect(dr, SOLN_OX, SOLN_OY - ds->gapsz - 1, SOLN_W, 2, COL_FRAME); |
| 1171 | draw_update(dr, 0, 0, ds->w, ds->h); |
| 1172 | } |
| 1173 | |
| 1174 | if (ds->drag_col != 0) { |
| 1175 | debug(("Loading from blitter.")); |
| 1176 | blitter_load(dr, ds->blit_peg, ds->blit_ox, ds->blit_oy); |
| 1177 | draw_update(dr, ds->blit_ox, ds->blit_oy, PEGSZ, PEGSZ); |
| 1178 | } |
| 1179 | |
| 1180 | /* draw the colours */ |
| 1181 | for (i = 0; i < state->params.ncolours; i++) { |
| 1182 | int val = i+1; |
| 1183 | if (ui->display_cur && ui->colour_cur == i) |
| 1184 | val |= 0x1000; |
| 1185 | if (ds->colours->pegs[i] != val) { |
| 1186 | draw_peg(dr, ds, COL_X(i), COL_Y(i), FALSE, i+1); |
| 1187 | if (val & 0x1000) |
| 1188 | draw_cursor(dr, ds, COL_X(i), COL_Y(i)); |
| 1189 | ds->colours->pegs[i] = val; |
| 1190 | } |
| 1191 | } |
| 1192 | |
| 1193 | /* draw the guesses (so far) and the hints */ |
| 1194 | for (i = 0; i < state->params.nguesses; i++) { |
| 1195 | if (state->next_go > i || state->solved) { |
| 1196 | /* this info is stored in the game_state already */ |
| 1197 | guess_redraw(dr, ds, i, state->guesses[i], NULL, -1, 0); |
| 1198 | hint_redraw(dr, ds, i, state->guesses[i], |
| 1199 | i == (state->next_go-1) ? 1 : 0, FALSE, FALSE); |
| 1200 | } else if (state->next_go == i) { |
| 1201 | /* this is the one we're on; the (incomplete) guess is |
| 1202 | * stored in the game_ui. */ |
| 1203 | guess_redraw(dr, ds, i, ui->curr_pegs, |
| 1204 | ui->holds, ui->display_cur ? ui->peg_cur : -1, 0); |
| 1205 | hint_redraw(dr, ds, i, NULL, 1, |
| 1206 | ui->display_cur && ui->peg_cur == state->params.npegs, |
| 1207 | ui->markable); |
| 1208 | } else { |
| 1209 | /* we've not got here yet; it's blank. */ |
| 1210 | guess_redraw(dr, ds, i, NULL, NULL, -1, 0); |
| 1211 | hint_redraw(dr, ds, i, NULL, 0, FALSE, FALSE); |
| 1212 | } |
| 1213 | } |
| 1214 | |
| 1215 | /* draw the 'current move' and 'able to mark' sign. */ |
| 1216 | if (new_move) |
| 1217 | currmove_redraw(dr, ds, ds->next_go, COL_BACKGROUND); |
| 1218 | if (!state->solved) |
| 1219 | currmove_redraw(dr, ds, state->next_go, COL_HOLD); |
| 1220 | |
| 1221 | /* draw the solution (or the big rectangle) */ |
| 1222 | if ((state->solved != ds->solved) || !ds->started) { |
| 1223 | draw_rect(dr, SOLN_OX, SOLN_OY, SOLN_W, SOLN_H, |
| 1224 | state->solved ? COL_BACKGROUND : COL_EMPTY); |
| 1225 | draw_update(dr, SOLN_OX, SOLN_OY, SOLN_W, SOLN_H); |
| 1226 | } |
| 1227 | if (state->solved) |
| 1228 | guess_redraw(dr, ds, -1, state->solution, NULL, -1, !ds->solved); |
| 1229 | ds->solved = state->solved; |
| 1230 | |
| 1231 | ds->next_go = state->next_go; |
| 1232 | |
| 1233 | /* if ui->drag_col != 0, save the screen to the blitter, |
| 1234 | * draw the peg where we saved, and set ds->drag_* == ui->drag_*. */ |
| 1235 | if (ui->drag_col != 0) { |
| 1236 | int ox = ui->drag_x - (PEGSZ/2); |
| 1237 | int oy = ui->drag_y - (PEGSZ/2); |
| 1238 | debug(("Saving to blitter at (%d,%d)", ox, oy)); |
| 1239 | blitter_save(dr, ds->blit_peg, ox, oy); |
| 1240 | draw_peg(dr, ds, ox, oy, TRUE, ui->drag_col); |
| 1241 | |
| 1242 | ds->blit_ox = ox; ds->blit_oy = oy; |
| 1243 | } |
| 1244 | ds->drag_col = ui->drag_col; |
| 1245 | |
| 1246 | ds->started = 1; |
| 1247 | } |
| 1248 | |
| 1249 | static float game_anim_length(game_state *oldstate, game_state *newstate, |
| 1250 | int dir, game_ui *ui) |
| 1251 | { |
| 1252 | return 0.0F; |
| 1253 | } |
| 1254 | |
| 1255 | static float game_flash_length(game_state *oldstate, game_state *newstate, |
| 1256 | int dir, game_ui *ui) |
| 1257 | { |
| 1258 | return 0.0F; |
| 1259 | } |
| 1260 | |
| 1261 | static int game_wants_statusbar(void) |
| 1262 | { |
| 1263 | return FALSE; |
| 1264 | } |
| 1265 | |
| 1266 | static int game_timing_state(game_state *state, game_ui *ui) |
| 1267 | { |
| 1268 | return TRUE; |
| 1269 | } |
| 1270 | |
| 1271 | static void game_print_size(game_params *params, float *x, float *y) |
| 1272 | { |
| 1273 | } |
| 1274 | |
| 1275 | static void game_print(drawing *dr, game_state *state, int tilesize) |
| 1276 | { |
| 1277 | } |
| 1278 | |
| 1279 | #ifdef COMBINED |
| 1280 | #define thegame guess |
| 1281 | #endif |
| 1282 | |
| 1283 | const struct game thegame = { |
| 1284 | "Guess", "games.guess", |
| 1285 | default_params, |
| 1286 | game_fetch_preset, |
| 1287 | decode_params, |
| 1288 | encode_params, |
| 1289 | free_params, |
| 1290 | dup_params, |
| 1291 | TRUE, game_configure, custom_params, |
| 1292 | validate_params, |
| 1293 | new_game_desc, |
| 1294 | validate_desc, |
| 1295 | new_game, |
| 1296 | dup_game, |
| 1297 | free_game, |
| 1298 | TRUE, solve_game, |
| 1299 | FALSE, game_text_format, |
| 1300 | new_ui, |
| 1301 | free_ui, |
| 1302 | encode_ui, |
| 1303 | decode_ui, |
| 1304 | game_changed_state, |
| 1305 | interpret_move, |
| 1306 | execute_move, |
| 1307 | PEG_PREFER_SZ, game_compute_size, game_set_size, |
| 1308 | game_colours, |
| 1309 | game_new_drawstate, |
| 1310 | game_free_drawstate, |
| 1311 | game_redraw, |
| 1312 | game_anim_length, |
| 1313 | game_flash_length, |
| 1314 | FALSE, FALSE, game_print_size, game_print, |
| 1315 | game_wants_statusbar, |
| 1316 | FALSE, game_timing_state, |
| 1317 | 0, /* flags */ |
| 1318 | }; |
| 1319 | |
| 1320 | /* vim: set shiftwidth=4 tabstop=8: */ |