2 * Slide puzzle core game logic
3 * Copyright © 2022 Nick Bowler
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 * The RNG implementation is adapted from xoshiro256** and splitmix64
19 * by David Blackman and Sebastiano Vigna, originally distributed under
20 * the Creative Commons Zero public domain dedication.
27 #include <gethrxtime.h>
30 #define B64(x) ((x) & 0xffffffffffffffff)
32 /* Rotate val left by n bits. The behaviour is undefined if n is zero. */
33 static unsigned long long rot_left64(unsigned long long val, int n)
35 return B64( (val << n) | (val >> (64 - n)) );
38 static unsigned long long xoshiro256ss(unsigned long long *s)
40 unsigned long long tmp, ret;
42 ret = B64(rot_left64(B64(s[1]*5), 7) * 9);
43 tmp = B64(s[1] << 17);
50 s[3] = rot_left64(s[3], 45);
55 static unsigned long long splitmix64(unsigned long long *state)
59 z = B64(*state += 0x9e3779b97f4a7c15);
60 z = B64((z ^ (z >> 30)) * 0xbf58476d1ce4e5b9);
61 z = B64((z ^ (z >> 27)) * 0x94d049bb133111eb);
66 static unsigned long long rng_state[4];
68 static int rng_is_seeded(void)
70 return rng_state[0] || rng_state[1] || rng_state[2] || rng_state[3];
73 /* Calculate the least power of two greater than val, minus 1. */
74 static unsigned rng_mask(unsigned val)
81 if (UINT_MAX >= 65536)
87 /* Return a random integer uniformly on the closed interval [0, limit-1] */
88 static unsigned rng_uniform_int(unsigned max)
90 unsigned mask = rng_mask(max-1);
91 unsigned long long val;
94 val = ( xoshiro256ss(rng_state) >> 32 ) & mask;
100 static void shuffle(unsigned char *tiles, unsigned n)
104 for (i = 1; i < n; i++) {
107 j = rng_uniform_int(i+1);
114 void game_reseed(unsigned long long seed)
116 rng_state[0] = splitmix64(&seed);
117 rng_state[1] = splitmix64(&seed);
118 rng_state[2] = splitmix64(&seed);
119 rng_state[3] = splitmix64(&seed);
122 void game_reset(struct board *board)
124 unsigned char tiles[25];
127 if (!rng_is_seeded()) {
128 unsigned long long seed;
131 seed += gethrxtime();
137 for (i = 0; i < 24; i++) {
138 tiles[i] = (i%6) + 1;
142 memset(board->goal, 0, sizeof board->goal);
144 for (i = 0; i < 9; i++) {
145 uint_fast32_t position = board_position(i/3+1, i%3+1) >> 6;
147 if (tiles[i] & 1) board->goal[0] |= position;
148 if (tiles[i] & 2) board->goal[1] |= position;
149 if (tiles[i] & 4) board->goal[2] |= position;
152 tiles[24] = TILE_EMPTY;
155 memset(board->game, 0, sizeof board->game);
156 for (i = 0; i < 25; i++) {
157 unsigned x = i/5, y = i%5;
158 uint_fast32_t position;
160 position = board_position(x, y);
161 if (tiles[i] != TILE_EMPTY) {
162 if (tiles[i] & 1) board->game[0] |= position;
163 if (tiles[i] & 2) board->game[1] |= position;
164 if (tiles[i] & 4) board->game[2] |= position;
166 board->game[3] = ~position;
173 uint_fast32_t game_do_move(struct board *board, int x, int y)
175 int bx = board->x, by = board->y;
176 uint_least32_t ret = 0, mask, val[4];
179 if ((bx != x) == (by != y))
183 mask = board_mask_v(x, by, y);
187 mask = board_mask_h(y, bx, x);
192 for (i = 0; i < 4; i++) {
193 board->game[i] ^= (val[i] = board->game[i] & mask);
194 board->game[i] |= val[i] << shl >> shr;
195 ret |= board->game[i] ^ val[i];
204 uint_fast32_t game_check_goal(struct board *board)
206 uint_least32_t *game = board->game;
207 uint_least16_t *goal = board->goal;
208 uint_least32_t mask = 0;
211 for (i = 0; i < 3; i++)
212 mask |= goal[i] ^ (game[i] >> GOAL_SHIFT);
213 return (mask << GOAL_SHIFT) & GOAL_MASK;
216 void game_begin(struct board *board)
218 board->time_start = gethrxtime();
221 static int_fast32_t elapsed(struct board *board)
223 return (gethrxtime() - board->time_start) / 1000000;
226 int_fast32_t game_finish(struct board *board)
228 int_fast32_t t = elapsed(board);
231 for (i = 0; i < 4; i++) {
232 board->game[i] &= GOAL_MASK;
236 * Setting both x and y hole location to a bogus value will effectively
237 * disable the game since there will be no valid moves.
239 board->x = board->y = -1;