diff options
| author | Franklin Wei <git@fwei.tk> | 2018-12-21 22:13:13 -0500 |
|---|---|---|
| committer | Franklin Wei <git@fwei.tk> | 2018-12-21 22:13:33 -0500 |
| commit | b3356e3aff34a4ab94778e7f6a8db43f9135296c (patch) | |
| tree | 9119f850f2138db65da93461505b0a9b08e56d32 /apps/plugins/puzzles/src/rect.c | |
| parent | f08d218e676f9ee8b5c26e755088671baf3a70b7 (diff) | |
| download | rockbox-b3356e3aff34a4ab94778e7f6a8db43f9135296c.zip rockbox-b3356e3aff34a4ab94778e7f6a8db43f9135296c.tar.gz rockbox-b3356e3aff34a4ab94778e7f6a8db43f9135296c.tar.bz2 rockbox-b3356e3aff34a4ab94778e7f6a8db43f9135296c.tar.xz | |
puzzles: resync with upstream
This brings the code to upstream commit 3ece3d6 (I've made my own Rockbox-
specific changes on top of that).
Changes include using C99 `bool' throughout, and minor logic fixes for some
puzzles.
Change-Id: Ie823e73ae49a8ee1de411d6d406df2ba835af541
Diffstat (limited to 'apps/plugins/puzzles/src/rect.c')
| -rw-r--r-- | apps/plugins/puzzles/src/rect.c | 182 |
1 files changed, 94 insertions, 88 deletions
diff --git a/apps/plugins/puzzles/src/rect.c b/apps/plugins/puzzles/src/rect.c index 3d84f04..2cb01bf 100644 --- a/apps/plugins/puzzles/src/rect.c +++ b/apps/plugins/puzzles/src/rect.c @@ -44,7 +44,7 @@ enum { struct game_params { int w, h; float expandfactor; - int unique; + bool unique; }; #define INDEX(state, x, y) (((y) * (state)->w) + (x)) @@ -80,7 +80,7 @@ struct game_state { int *grid; /* contains the numbers */ unsigned char *vedge; /* (w+1) x h */ unsigned char *hedge; /* w x (h+1) */ - int completed, cheated; + bool completed, cheated; unsigned char *correct; }; @@ -90,12 +90,12 @@ static game_params *default_params(void) ret->w = ret->h = 7; ret->expandfactor = 0.0F; - ret->unique = TRUE; + ret->unique = true; return ret; } -static int game_fetch_preset(int i, char **name, game_params **params) +static bool game_fetch_preset(int i, char **name, game_params **params) { game_params *ret; int w, h; @@ -111,7 +111,7 @@ static int game_fetch_preset(int i, char **name, game_params **params) case 5: w = 17, h = 17; break; case 6: w = 19, h = 19; break; #endif - default: return FALSE; + default: return false; } sprintf(buf, "%dx%d", w, h); @@ -120,8 +120,8 @@ static int game_fetch_preset(int i, char **name, game_params **params) ret->w = w; ret->h = h; ret->expandfactor = 0.0F; - ret->unique = TRUE; - return TRUE; + ret->unique = true; + return true; } static void free_params(game_params *params) @@ -153,11 +153,11 @@ static void decode_params(game_params *ret, char const *string) } if (*string == 'a') { string++; - ret->unique = FALSE; + ret->unique = false; } } -static char *encode_params(const game_params *params, int full) +static char *encode_params(const game_params *params, bool full) { char data[256]; @@ -217,7 +217,7 @@ static game_params *custom_params(const config_item *cfg) return ret; } -static const char *validate_params(const game_params *params, int full) +static const char *validate_params(const game_params *params, bool full) { if (params->w <= 0 || params->h <= 0) return "Width and height must both be greater than zero"; @@ -496,7 +496,7 @@ static int rect_solver(int w, int h, int nrects, struct numberdata *numbers, * Now run the actual deduction loop. */ while (1) { - int done_something = FALSE; + bool done_something = false; #ifdef SOLVER_DIAGNOSTICS printf("starting deduction loop\n"); @@ -609,7 +609,7 @@ static int rect_solver(int w, int h, int nrects, struct numberdata *numbers, for (j = 0; j < rectpositions[i].n; j++) { int xx, yy, k; - int del = FALSE; + bool del = false; for (k = 0; k < nrects; k++) workspace[k] = 0; @@ -635,7 +635,7 @@ static int rect_solver(int w, int h, int nrects, struct numberdata *numbers, rectpositions[i].rects[j].h, x, y); #endif - del = TRUE; + del = true; } if (rectbyplace[y * w + x] != -1) { @@ -668,7 +668,7 @@ static int rect_solver(int w, int h, int nrects, struct numberdata *numbers, rectpositions[i].rects[j].h, k); #endif - del = TRUE; + del = true; break; } @@ -689,7 +689,7 @@ static int rect_solver(int w, int h, int nrects, struct numberdata *numbers, rectpositions[i].rects[j].w, rectpositions[i].rects[j].h); #endif - del = TRUE; + del = true; } } @@ -698,7 +698,7 @@ static int rect_solver(int w, int h, int nrects, struct numberdata *numbers, j--; /* don't skip over next placement */ - done_something = TRUE; + done_something = true; } } } @@ -742,7 +742,7 @@ static int rect_solver(int w, int h, int nrects, struct numberdata *numbers, remove_rect_placement(w, h, rectpositions, overlaps, index, j); j--; /* don't skip over next placement */ - done_something = TRUE; + done_something = true; } } } @@ -850,7 +850,7 @@ static int rect_solver(int w, int h, int nrects, struct numberdata *numbers, remove_number_placement(w, h, &numbers[k], m, rectbyplace); m--; /* don't skip the next one */ - done_something = TRUE; + done_something = true; } } } @@ -1144,7 +1144,7 @@ static void display_grid(game_params *params, int *grid, int *numbers, int all) #endif static char *new_game_desc(const game_params *params_in, random_state *rs, - char **aux, int interactive) + char **aux, bool interactive) { game_params params_copy = *params_in; /* structure copy */ game_params *params = ¶ms_copy; @@ -1270,7 +1270,7 @@ static char *new_game_desc(const game_params *params_in, random_state *rs, int dirs[4], ndirs; #ifdef GENERATION_DIAGNOSTICS - display_grid(params2, grid, NULL, FALSE); + display_grid(params2, grid, NULL, false); printf("singleton at %d,%d\n", x, y); #endif @@ -1448,7 +1448,7 @@ static char *new_game_desc(const game_params *params_in, random_state *rs, #ifdef GENERATION_DIAGNOSTICS printf("before expansion:\n"); - display_grid(params2, grid, NULL, TRUE); + display_grid(params2, grid, NULL, true); #endif /* @@ -1576,7 +1576,7 @@ static char *new_game_desc(const game_params *params_in, random_state *rs, #ifdef GENERATION_DIAGNOSTICS printf("after expansion:\n"); - display_grid(params3, grid2, NULL, TRUE); + display_grid(params3, grid2, NULL, true); #endif /* * Transpose. @@ -1607,7 +1607,7 @@ static char *new_game_desc(const game_params *params_in, random_state *rs, #ifdef GENERATION_DIAGNOSTICS printf("after transposition:\n"); - display_grid(params2, grid, NULL, TRUE); + display_grid(params2, grid, NULL, true); #endif } @@ -1734,7 +1734,7 @@ static char *new_game_desc(const game_params *params_in, random_state *rs, } #ifdef GENERATION_DIAGNOSTICS - display_grid(params, grid, numbers, FALSE); + display_grid(params, grid, numbers, false); #endif desc = snewn(11 * params->w * params->h, char); @@ -1817,7 +1817,8 @@ static unsigned char *get_correct(game_state *state) if (index(state,ret,x,y) == 0xFF) { int rw, rh; int xx, yy; - int num, area, valid; + int num, area; + bool valid; /* * Find a rectangle starting at this point. @@ -1834,14 +1835,14 @@ static unsigned char *get_correct(game_state *state) * should be if it's there at all. Find out if we * really have a valid rectangle. */ - valid = TRUE; + valid = true; /* Check the horizontal edges. */ for (xx = x; xx < x+rw; xx++) { for (yy = y; yy <= y+rh; yy++) { int e = !HRANGE(state,xx,yy) || hedge(state,xx,yy); int ec = (yy == y || yy == y+rh); if (e != ec) - valid = FALSE; + valid = false; } } /* Check the vertical edges. */ @@ -1850,7 +1851,7 @@ static unsigned char *get_correct(game_state *state) int e = !VRANGE(state,xx,yy) || vedge(state,xx,yy); int ec = (xx == x || xx == x+rw); if (e != ec) - valid = FALSE; + valid = false; } } @@ -1875,13 +1876,13 @@ static unsigned char *get_correct(game_state *state) area++; if (grid(state,xx,yy)) { if (num > 0) - valid = FALSE; /* two numbers */ + valid = false; /* two numbers */ num = grid(state,xx,yy); } } } if (num != area) - valid = FALSE; + valid = false; /* * Now fill in the whole rectangle based on the @@ -1911,7 +1912,8 @@ static game_state *new_game(midend *me, const game_params *params, state->grid = snewn(area, int); state->vedge = snewn(area, unsigned char); state->hedge = snewn(area, unsigned char); - state->completed = state->cheated = FALSE; + state->completed = false; + state->cheated = false; i = 0; while (*desc) { @@ -2045,9 +2047,9 @@ static char *solve_game(const game_state *state, const game_state *currstate, return ret; } -static int game_can_format_as_text_now(const game_params *params) +static bool game_can_format_as_text_now(const game_params *params) { - return TRUE; + return true; } static char *game_text_format(const game_state *state) @@ -2164,12 +2166,12 @@ struct game_ui { * the pointer _returns_ to its starting point the action is * treated as a small drag rather than a click. */ - int dragged; + bool dragged; /* This flag is set if we're doing an erase operation (i.e. * removing edges in the centre of the rectangle without altering * the outlines). */ - int erasing; + bool erasing; /* * These are the co-ordinates of the top-left and bottom-right squares * in the drag box, respectively, or -1 otherwise. @@ -2182,7 +2184,8 @@ struct game_ui { * These are the coordinates of a cursor, whether it's visible, and * whether it was used to start a drag. */ - int cur_x, cur_y, cur_visible, cur_dragging; + int cur_x, cur_y; + bool cur_visible, cur_dragging; }; static void reset_ui(game_ui *ui) @@ -2195,15 +2198,17 @@ static void reset_ui(game_ui *ui) ui->y1 = -1; ui->x2 = -1; ui->y2 = -1; - ui->dragged = FALSE; + ui->dragged = false; } static game_ui *new_ui(const game_state *state) { game_ui *ui = snew(game_ui); reset_ui(ui); - ui->erasing = FALSE; - ui->cur_x = ui->cur_y = ui->cur_visible = ui->cur_dragging = 0; + ui->erasing = false; + ui->cur_x = ui->cur_y = 0; + ui->cur_visible = false; + ui->cur_dragging = false; return ui; } @@ -2306,15 +2311,15 @@ static void coord_round(float x, float y, int *xr, int *yr) } /* - * Returns TRUE if it has made any change to the grid. + * Returns true if it has made any change to the grid. */ -static int grid_draw_rect(const game_state *state, +static bool grid_draw_rect(const game_state *state, unsigned char *hedge, unsigned char *vedge, - int c, int really, int outline, + int c, bool really, bool outline, int x1, int y1, int x2, int y2) { int x, y; - int changed = FALSE; + bool changed = false; /* * Draw horizontal edges of rectangles. @@ -2353,9 +2358,9 @@ static int grid_draw_rect(const game_state *state, return changed; } -static int ui_draw_rect(const game_state *state, const game_ui *ui, +static bool ui_draw_rect(const game_state *state, const game_ui *ui, unsigned char *hedge, unsigned char *vedge, int c, - int really, int outline) + bool really, bool outline) { return grid_draw_rect(state, hedge, vedge, c, really, outline, ui->x1, ui->y1, ui->x2, ui->y2); @@ -2367,7 +2372,7 @@ static void game_changed_state(game_ui *ui, const game_state *oldstate, } struct game_drawstate { - int started; + bool started; int w, h, tilesize; unsigned long *visible; }; @@ -2377,7 +2382,7 @@ static char *interpret_move(const game_state *from, game_ui *ui, int x, int y, int button) { int xc, yc; - int startdrag = FALSE, enddrag = FALSE, active = FALSE, erasing = FALSE; + bool startdrag = false, enddrag = false, active = false, erasing = false; char buf[80], *ret; button &= ~MOD_MASK; @@ -2387,23 +2392,23 @@ static char *interpret_move(const game_state *from, game_ui *ui, if (button == LEFT_BUTTON || button == RIGHT_BUTTON) { if (ui->drag_start_x >= 0 && ui->cur_dragging) reset_ui(ui); /* cancel keyboard dragging */ - startdrag = TRUE; - ui->cur_visible = ui->cur_dragging = FALSE; - active = TRUE; + startdrag = true; + ui->cur_visible = ui->cur_dragging = false; + active = true; erasing = (button == RIGHT_BUTTON); } else if (button == LEFT_RELEASE || button == RIGHT_RELEASE) { /* We assert we should have had a LEFT_BUTTON first. */ if (ui->cur_visible) { - ui->cur_visible = FALSE; - active = TRUE; + ui->cur_visible = false; + active = true; } assert(!ui->cur_dragging); - enddrag = TRUE; + enddrag = true; erasing = (button == RIGHT_RELEASE); } else if (IS_CURSOR_MOVE(button)) { - move_cursor(button, &ui->cur_x, &ui->cur_y, from->w, from->h, 0); - ui->cur_visible = TRUE; - active = TRUE; + move_cursor(button, &ui->cur_x, &ui->cur_y, from->w, from->h, false); + ui->cur_visible = true; + active = true; if (!ui->cur_dragging) return UI_UPDATE; coord_round((float)ui->cur_x + 0.5F, (float)ui->cur_y + 0.5F, &xc, &yc); } else if (IS_CURSOR_SELECT(button)) { @@ -2416,27 +2421,27 @@ static char *interpret_move(const game_state *from, game_ui *ui, } if (!ui->cur_visible) { assert(!ui->cur_dragging); - ui->cur_visible = TRUE; + ui->cur_visible = true; return UI_UPDATE; } coord_round((float)ui->cur_x + 0.5F, (float)ui->cur_y + 0.5F, &xc, &yc); erasing = (button == CURSOR_SELECT2); if (ui->cur_dragging) { - ui->cur_dragging = FALSE; - enddrag = TRUE; - active = TRUE; + ui->cur_dragging = false; + enddrag = true; + active = true; } else { - ui->cur_dragging = TRUE; - startdrag = TRUE; - active = TRUE; + ui->cur_dragging = true; + startdrag = true; + active = true; } } else if (button == '\b' || button == 27) { if (!ui->cur_dragging) { - ui->cur_visible = FALSE; + ui->cur_visible = false; } else { assert(ui->cur_visible); reset_ui(ui); /* cancel keyboard dragging */ - ui->cur_dragging = FALSE; + ui->cur_dragging = false; } return UI_UPDATE; } else if (button != LEFT_DRAG && button != RIGHT_DRAG) { @@ -2451,9 +2456,9 @@ static char *interpret_move(const game_state *from, game_ui *ui, ui->drag_start_y = yc; ui->drag_end_x = -1; ui->drag_end_y = -1; - ui->dragged = FALSE; + ui->dragged = false; ui->erasing = erasing; - active = TRUE; + active = true; } if (ui->drag_start_x >= 0 && @@ -2461,10 +2466,10 @@ static char *interpret_move(const game_state *from, game_ui *ui, int t; if (ui->drag_end_x != -1 && ui->drag_end_y != -1) - ui->dragged = TRUE; + ui->dragged = true; ui->drag_end_x = xc; ui->drag_end_y = yc; - active = TRUE; + active = true; if (xc >= 0 && xc <= 2*from->w && yc >= 0 && yc <= 2*from->h) { @@ -2497,7 +2502,7 @@ static char *interpret_move(const game_state *from, game_ui *ui, if (ui->dragged) { if (ui_draw_rect(from, ui, from->hedge, - from->vedge, 1, FALSE, !ui->erasing)) { + from->vedge, 1, false, !ui->erasing)) { sprintf(buf, "%c%d,%d,%d,%d", (int)(ui->erasing ? 'E' : 'R'), ui->x1, ui->y1, ui->x2 - ui->x1, ui->y2 - ui->y1); @@ -2516,7 +2521,7 @@ static char *interpret_move(const game_state *from, game_ui *ui, } reset_ui(ui); - active = TRUE; + active = true; } if (ret) @@ -2537,7 +2542,7 @@ static game_state *execute_move(const game_state *from, const char *move) int x, y; ret = dup_game(from); - ret->cheated = TRUE; + ret->cheated = true; for (y = 0; y < ret->h; y++) for (x = 1; x < ret->w; x++) { @@ -2573,7 +2578,7 @@ static game_state *execute_move(const game_state *from, const char *move) ret = dup_game(from); if (mode == 'R' || mode == 'E') { - grid_draw_rect(ret, ret->hedge, ret->vedge, 1, TRUE, + grid_draw_rect(ret, ret->hedge, ret->vedge, 1, true, mode == 'R', x1, y1, x2, y2); } else if (mode == 'H') { hedge(ret,x1,y1) = !hedge(ret,x1,y1); @@ -2589,16 +2594,17 @@ static game_state *execute_move(const game_state *from, const char *move) * if the game has been completed. */ if (!ret->completed) { - int x, y, ok; + int x, y; + bool ok; - ok = TRUE; + ok = true; for (x = 0; x < ret->w; x++) for (y = 0; y < ret->h; y++) if (!index(ret, ret->correct, x, y)) - ok = FALSE; + ok = false; if (ok) - ret->completed = TRUE; + ret->completed = true; } return ret; @@ -2674,7 +2680,7 @@ static game_drawstate *game_new_drawstate(drawing *dr, const game_state *state) struct game_drawstate *ds = snew(struct game_drawstate); int i; - ds->started = FALSE; + ds->started = false; ds->w = state->w; ds->h = state->h; ds->visible = snewn(ds->w * ds->h, unsigned long); @@ -2761,7 +2767,7 @@ static void game_redraw(drawing *dr, game_drawstate *ds, vedge = snewn(state->w*state->h, unsigned char); memcpy(hedge, state->hedge, state->w*state->h); memcpy(vedge, state->vedge, state->w*state->h); - ui_draw_rect(state, ui, hedge, vedge, ui->erasing ? 3 : 2, TRUE, TRUE); + ui_draw_rect(state, ui, hedge, vedge, ui->erasing ? 3 : 2, true, true); } else { hedge = state->hedge; vedge = state->vedge; @@ -2795,7 +2801,7 @@ static void game_redraw(drawing *dr, game_drawstate *ds, state->h * TILE_SIZE + 2*BORDER + 1, COL_BACKGROUND); draw_rect(dr, COORD(0)-1, COORD(0)-1, ds->w*TILE_SIZE+3, ds->h*TILE_SIZE+3, COL_LINE); - ds->started = TRUE; + ds->started = true; draw_update(dr, 0, 0, state->w * TILE_SIZE + 2*BORDER + 1, state->h * TILE_SIZE + 2*BORDER + 1); @@ -2882,9 +2888,9 @@ static int game_status(const game_state *state) return state->completed ? +1 : 0; } -static int game_timing_state(const game_state *state, game_ui *ui) +static bool game_timing_state(const game_state *state, game_ui *ui) { - return TRUE; + return true; } static void game_print_size(const game_params *params, float *x, float *y) @@ -2964,15 +2970,15 @@ const struct game thegame = { encode_params, free_params, dup_params, - TRUE, game_configure, custom_params, + true, game_configure, custom_params, validate_params, new_game_desc, validate_desc, new_game, dup_game, free_game, - TRUE, solve_game, - TRUE, game_can_format_as_text_now, game_text_format, + true, solve_game, + true, game_can_format_as_text_now, game_text_format, new_ui, free_ui, encode_ui, @@ -2989,9 +2995,9 @@ const struct game thegame = { game_anim_length, game_flash_length, game_status, - TRUE, FALSE, game_print_size, game_print, - TRUE, /* wants_statusbar */ - FALSE, game_timing_state, + true, false, game_print_size, game_print, + true, /* wants_statusbar */ + false, game_timing_state, 0, /* flags */ }; |