]> git.draconx.ca Git - rrace.git/commitdiff
Report changed positions from game_do_move.
authorNick Bowler <nbowler@draconx.ca>
Sat, 12 Mar 2022 21:31:12 +0000 (16:31 -0500)
committerNick Bowler <nbowler@draconx.ca>
Sat, 12 Mar 2022 21:31:12 +0000 (16:31 -0500)
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.

src/game.c
src/game.h
src/motif_ui.c

index bdd8f2b3ac103c6e2974fe138c1b23f342a81673..999d52674abd10007334811ec3a8d8562c40d801 100644 (file)
@@ -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)
index a734e229058f5182f96c28c3512c19c06fec6965..176cb367a51e18671a5a8315961a0cda92658bb2 100644 (file)
@@ -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.
index dfe4e82911434a59695646e14736a0c3546d547c..85241d8803c2de539427e41eea19b79c4d003945 100644 (file)
@@ -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);
        }
 }