}
}
-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);
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)
* 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.
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;
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);
}
}