From: Nick Bowler Date: Sat, 12 Mar 2022 21:31:12 +0000 (-0500) Subject: Report changed positions from game_do_move. X-Git-Url: http://git.draconx.ca/gitweb/rrace.git/commitdiff_plain/48cf657b615567153e5ed786b6034f9b46733f9a Report changed positions from game_do_move. Instead of manually comparing positions before and after the move, we can just alter the definition of game_do_move to return this information, since it has already done most of the work internally. --- diff --git a/src/game.c b/src/game.c index bdd8f2b..999d526 100644 --- a/src/game.c +++ b/src/game.c @@ -170,14 +170,14 @@ void game_reset(struct board *board) } } -int game_do_move(struct board *board, int x, int y) +uint_fast32_t game_do_move(struct board *board, int x, int y) { int bx = board->x, by = board->y; - uint_least32_t mask, val[4]; + uint_least32_t ret = 0, mask, val[4]; int i, shl, shr; if ((bx != x) == (by != y)) - return -1; + return 0; if (bx == x) { mask = board_mask_v(x, by, y); @@ -192,11 +192,13 @@ int game_do_move(struct board *board, int x, int y) for (i = 0; i < 4; i++) { board->game[i] ^= (val[i] = board->game[i] & mask); board->game[i] |= val[i] << shl >> shr; + ret |= board->game[i] ^ val[i]; } board->x = x; board->y = y; - return 0; + + return mask & ret; } uint_fast32_t game_check_goal(struct board *board) diff --git a/src/game.h b/src/game.h index a734e22..176cb36 100644 --- a/src/game.h +++ b/src/game.h @@ -161,9 +161,10 @@ static inline uint_fast32_t board_right(int x) * and update the location of the empty position which, if the move was valid * is now (x, y). * - * Returns 0 if the move was valid (and board has been updated), -1 otherwise. + * Returns the board bitmap indicating which positions changed. A return + * value of 0 therefore indicates an invalid move. */ -int game_do_move(struct board *board, int x, int y); +uint_fast32_t game_do_move(struct board *board, int x, int y); /* * Returns the board bitmap setting game locations that differ from the goal. diff --git a/src/motif_ui.c b/src/motif_ui.c index dfe4e82..85241d8 100644 --- a/src/motif_ui.c +++ b/src/motif_ui.c @@ -217,12 +217,9 @@ static void goal_resize(Widget w, void *data, void *cb_data) static void do_input_move(struct app_state *state, int x, int y) { - uint_least32_t *gp = state->board.game, prev[4]; - - memcpy(prev, gp, sizeof prev); - if (game_do_move(&state->board, x, y) == 0) { - uint_least32_t mask; + uint_fast32_t mask; + if ((mask = game_do_move(&state->board, x, y)) != 0) { if (game_check_goal(&state->board) == 0) { int_fast32_t ms = game_finish(&state->board); unsigned min, sec; @@ -236,14 +233,9 @@ static void do_input_move(struct app_state *state, int x, int y) min = sec / 60, sec %= 60; printf("You won! Time was %u:%.2u:%.3u\n", min, sec, (unsigned)ms); + mask |= ~GOAL_MASK; } - /* Figure out which tiles changed */ - prev[0] ^= gp[0]; - prev[1] ^= gp[1]; - prev[2] ^= gp[2]; - mask = prev[0] | prev[1] | prev[2]; - x11_redraw_game(state, mask); } }