Free games for graphing calculators: NumWorks, TI-Nspire CX, Casio fx-CG50 and soon TI-84 Plus CE. Balatro, Celeste, Hollow Knight, Minecraft and more.
C calculatorcalculator-gamescasiochesscrossy-roaddrive-madfx-cg50gamesgeometry-dashgraphing-calculatorndlessnumworksnumworks-appsnumworks-calculatornumworks-gamenumworks-gamestetristi-84-plus-ceti-nspire
Level Devil: walks for unsolved rooms, two more solved
Mason363d59dad8374da
diff --git a/games/leveldevil/src/main.c b/games/leveldevil/src/main.cindex 54659191..6a3e98d0 100644--- a/games/leveldevil/src/main.c+++ b/games/leveldevil/src/main.c@@ -249,15 +249,17 @@ static int step_axis(int32_t *p, int32_t d, int axis) {return 0;}/* out of what it is in, a pixel at a time, the nearest way; false when it cannot get out */-static int unstick(void) {+static int unstick(void) { /* the nearest free place, up, down, left or right first */for (int d = 0; d < 64; d++)- for (int i = 0; i < 4; i++) {- int x = (hx >> 8) + (i == 2 ? -d : i == 3 ? d : 0), y = (hy >> 8) + (i == 0 ? -d : i == 1 ? d : 0);- if (!blocked(x, y, -1)) {- hx = x * 256 + (hx & 255), hy = y * 256 + (hy & 255);- return 1;+ for (int j = 0; j <= d; j++)+ for (int i = 0; i < 8; i++) {+ int a = i & 4 ? -j : j, b = i & 1 ? d : -d;+ int x = (hx >> 8) + (i & 2 ? b : a), y = (hy >> 8) + (i & 2 ? a : b);+ if (!blocked(x, y, -1)) {+ hx = x * 256 + (hx & 255), hy = y * 256 + (hy & 255);+ return 1;+ }}- }return 0;}diff --git a/games/leveldevil/tests/play.c b/games/leveldevil/tests/play.cindex 136726c5..a8716690 100644--- a/games/leveldevil/tests/play.c+++ b/games/leveldevil/tests/play.c@@ -1,8 +1,10 @@/* Level Devil on a computer, for tests and pictures (make host, make test).** play solve every room played through with its keys (tests/solutions.txt): each must- * reach the door without dying- * play search L R finds keys through a room, for tests/solutions.txt+ * reach the door without dying; a room with a walk instead ("L-R~ keys", where+ * no keys were found) must set off its traps without dying+ * play search L R finds keys through a room, for tests/solutions.txt (or, failing that, a walk:+ * the way that sets off the most of its traps)* play trace L R [KEYS] where the runner goes* play rooms DIR a picture of every room as it starts (DIR/room_LL_R.ppm)* play film L R DIR [KEYS] the room played with its keys (or these), a picture every 2 steps@@ -33,8 +35,8 @@ uint32_t eadk_random(void) { return 0x9E3779B9u; }void *np_alloc(uint32_t size) { return malloc(size); }/* tests/solutions.txt: a line a room, "LEVEL-ROOM: keys"; "R40 RJ12 N30": the keys held (L R J D, or- N for none) for so many steps */-static char *solutions[ROOM_COUNT];+ N for none) for so many steps; "LEVEL-ROOM~ keys" for a walk */+static char *solutions[ROOM_COUNT], walk[ROOM_COUNT];static void read_solutions(void) {static char text[1 << 17];FILE *f = fopen("tests/solutions.txt", "r");@@ -42,10 +44,11 @@ static void read_solutions(void) {if (f) fclose(f);text[n] = 0;for (char *line = strtok(text, "\n"); line; line = strtok(0, "\n")) {- int l, r, at;- if (line[0] == '#' || sscanf(line, "%d-%d:%n", &l, &r, &at) < 2) continue;+ int l, r, at = 0;+ char c = 0;+ if (line[0] == '#' || sscanf(line, "%d-%d%c%n", &l, &r, &c, &at) < 3 || (c != ':' && c != '~')) continue;int i = (l - 1) * ROOMS + r - 1;- if (i >= 0 && i < ROOM_COUNT) solutions[i] = line + at;+ if (i >= 0 && i < ROOM_COUNT) solutions[i] = line + at, walk[i] = c == '~';}for (int i = 0; i < ROOM_COUNT; i++)if (!solutions[i]) solutions[i] = "";@@ -92,7 +95,7 @@ static int play(const char *s, const char *film, int trace) {}while (n--) {step(in), t++;- if (trace && (!(t % 4) || dead || won))+ if (trace && (!(t % trace) || dead || won))printf("%4d x %6.1f y %6.1f vx %5.1f vy %5.1f%s%s%s\n", t, hx / 256.0, hy / 256.0, vx * 60 / 256.0,vy * 60 / 256.0, ground ? " ground" : "", dead ? " DEAD" : "", won ? " DOOR" : "");if (film && !(t & 1)) {@@ -141,8 +144,19 @@ static void give(const uint8_t *s) {}static int goal_x, goal_y, have_goal;+static int to_traps; /* (searching for a walk: towards the triggers not yet touched) */static int score(void) { /* lower is better: the distance to the door (or the waypoint), in pixels */int gxp = goal_x, gyp = goal_y, best = 1 << 30;+ if (to_traps) {+ int n = 0;+ for (int e = 1; e < EV_MOVE; e++) n += cnt[e] > 0;+ for (int k = 0; k < no; k++)+ if ((ob[k].kind & 15) == K_TRIG && !(ob[k].f & (F_USED | F_GHOST))) {+ int d = iabs((ob[k].x >> 8) - (hx >> 8) - hw / 2) + iabs((ob[k].y >> 8) - (hy >> 8) - hh / 2);+ if (d < best) best = d;+ }+ if (best < 1 << 30) return best - n * 1000 + 8000;+ }if (!have_goal)for (int k = 0; k < no; k++)if ((ob[k].kind & 15) == K_DOOR && !(ob[k].f & (F_OFF | F_GHOST))) {@@ -164,6 +178,23 @@ static int by_score(const void *a, const void *b) {#define DEPTH 900#define CELL_MAX 4static const char *prefix; /* keys played before the search */+static void write_keys(char *out, const uint8_t *p, int d, const uint8_t *moves, int chunk) {+ char *o = out + sprintf(out, "%s%s", prefix ? prefix : "", prefix ? " " : "");+ for (int a = 0; a <= d;) {+ int b = a;+ while (b <= d && p[b] == p[a]) b++;+ int in = moves[p[a]];+ o += sprintf(o, "%s%s%s%s%s%d ", in & I_LEFT ? "L" : "", in & I_RIGHT ? "R" : "", in & I_UP ? "J" : "",+ in & I_DOWN ? "D" : "", in ? "" : "N", chunk * (b - a));+ a = b;+ }+ o[-1] = 0;+}+static int events_set_off(void) { /* (the triggers, and the jump key once armed) */+ int n = 0;+ for (int e = 1; e < EV_MOVE; e++) n += cnt[e] > 0;+ return n;+}static int search(int i, int nmoves, const uint8_t *moves, int chunk, char *out, int depth_max) {size_t sz = snap_size();uint8_t *cur = malloc(sz * BEAM), *nxt = malloc(sz * BEAM * 9), *path = malloc((size_t)BEAM * DEPTH),@@ -171,7 +202,8 @@ static int search(int i, int nmoves, const uint8_t *moves, int chunk, char *out,int *sc = malloc(sizeof(int) * BEAM * 9), *ord = malloc(sizeof(int) * BEAM * 9);uint64_t *keyv = malloc(8 * BEAM * 9);uint16_t *cellv = malloc(2 * BEAM * 9);- int ncur = 1, found = 0;+ int ncur = 1, found = 0, best_q = -(1 << 30), best_d = -1;+ static uint8_t best[DEPTH]; /* (the walk: the way that sets off the most events, then nearest the door) */setup(i);if (prefix) exact = 1, play(prefix, 0, 0), exact = 0;take(cur);@@ -185,20 +217,12 @@ static int search(int i, int nmoves, const uint8_t *moves, int chunk, char *out,memcpy(npath + (size_t)nn * DEPTH, path + (size_t)c * DEPTH, (size_t)d);npath[(size_t)nn * DEPTH + d] = (uint8_t)m;if (won || (have_goal && score() < 6 && (ground || frozen))) { /* the keys (to the door, or the waypoint) */- char *o = out + sprintf(out, "%s%s", prefix ? prefix : "", prefix ? " " : "");- const uint8_t *p = npath + (size_t)nn * DEPTH;- for (int a = 0; a <= d;) {- int b = a;- while (b <= d && p[b] == p[a]) b++;- int in = moves[p[a]];- o += sprintf(o, "%s%s%s%s%s%d ", in & I_LEFT ? "L" : "", in & I_RIGHT ? "R" : "", in & I_UP ? "J" : "",- in & I_DOWN ? "D" : "", in ? "" : "N", chunk * (b - a));- a = b;- }- o[-1] = 0;+ write_keys(out, npath + (size_t)nn * DEPTH, d, moves, chunk);found = 1;break;}+ int q = events_set_off() * 100000 - score();+ if (q > best_q) best_q = q, best_d = d, memcpy(best, npath + (size_t)nn * DEPTH, (size_t)d + 1);take(nxt + sz * nn);uint64_t key = (uint64_t)(hx >> 9) * 1000003u ^ (uint64_t)(hy >> 9) * 9176u ^ (uint64_t)((vx >> 6) + 512) * 131u ^(uint64_t)((vy >> 6) + 512) * 7u ^ (uint64_t)ground << 40 ^ (uint64_t)(gy < 0) << 41;@@ -247,7 +271,8 @@ static int search(int i, int nmoves, const uint8_t *moves, int chunk, char *out,}}free(cur), free(nxt), free(path), free(npath), free(sc), free(ord), free(keyv), free(cellv);- return found;+ if (!found && best_d >= 0) write_keys(out, best, best_d, moves, chunk);+ return found ? 1 : best_d >= 0 ? -1 : 0;}int main(int argc, char **argv) {@@ -263,16 +288,15 @@ int main(int argc, char **argv) {int n = argc > 4 && strchr(argv[4], 'D') ? 9 : 6, chunk = argc > 5 ? atoi(argv[5]) : 4;if (argc > 6) have_goal = sscanf(argv[6], "%d,%d", &goal_x, &goal_y) == 2;if (argc > 7) prefix = argv[7];- if (search(i, n, moves, chunk, out, 900)) {- printf("%d-%d: %s\n", i / ROOMS + 1, i % ROOMS + 1, out);- return 0;- }- printf("no way found\n");- return 1;+ to_traps = getenv("PLAY_TRAPS") != 0;+ int got = search(i, n, moves, chunk, out, getenv("PLAY_DEPTH") ? atoi(getenv("PLAY_DEPTH")) : 900);+ if (got) printf("%d-%d%c %s\n", i / ROOMS + 1, i % ROOMS + 1, got > 0 ? ':' : '~', out);+ else printf("no way found\n");+ return got > 0 ? 0 : 1;}if (!strcmp(cmd, "trace") && argc > 3) {setup(i);- printf("%d\n", play(argc > 4 ? argv[4] : solutions[i], 0, 1));+ printf("%d\n", play(argc > 4 ? argv[4] : solutions[i], 0, getenv("PLAY_EVERY") ? atoi(getenv("PLAY_EVERY")) : 4));return 0;}if (!strcmp(cmd, "film") && argc > 4) {@@ -308,17 +332,24 @@ int main(int argc, char **argv) {return 0;}if (!strcmp(cmd, "solve")) {- int bad = 0;+ int bad = 0, walks = 0;for (int r = 0; r < ROOM_COUNT; r++) {setup(r);+ exact = walk[r];int got = play(solutions[r], 0, 0);- if (got != 1) {+ exact = 0;+ if (walk[r] && got == -1 && events_set_off()) { /* (walked: its traps set off, not dead) */+ printf("level %d room %d: walked, %d of its events set off (no keys to the door yet)\n", r / ROOMS + 1,+ r % ROOMS + 1, events_set_off());+ walks++;+ } else if (got != 1) {printf("level %d room %d: %s at x %d y %d\n", r / ROOMS + 1, r % ROOMS + 1, got ? "stuck" : "dead",(int)(hx >> 8), (int)(hy >> 8));bad++;}}if (bad) printf("%d rooms fail\n", bad);+ else if (walks) printf("%d rooms cleared, %d walked\n", ROOM_COUNT - walks, walks);else printf("all %d rooms cleared\n", ROOM_COUNT);return !!bad;}diff --git a/games/leveldevil/tests/solutions.txt b/games/leveldevil/tests/solutions.txtindex e04794d3..5c3c30a4 100644--- a/games/leveldevil/tests/solutions.txt+++ b/games/leveldevil/tests/solutions.txt@@ -41,13 +41,13 @@8-4: R300 N4 R84 N4 R76 R56 N4 J4 N64 R4 R8 L8 N12 J4 L12 N4 L4 R1248-5: R20 RJ4 R20 L4 R64 L8 N8 L4 R4 L4 N24 R4 L4 R92 L4 N4 L4 R44 N4 R8 L4 R49-1: R16 N4 L16 R80 RJ4 R200-9-2:-9-3:-9-4:+9-2~ N20 L60 N20 R16 J4 L24 N60 R8 RJ4 R32 R12 RJ4 R76+9-3~ R20 RJ4 R20 RJ4 R16 RJ4 R16 RJ4 R4 L4 N12 R4 N8 L8 R16 RJ4 R28 RJ4 R28 RJ4 R36 RJ4 R28 RJ4 R16 L4 N4 L4 R4 N8 J4 N24 J4 N24 J4 N24 J4 N24 J4 N24 J4 N24 J4 N24 J4 N24 J4 N24 J4 N24 R4 RJ4 R16+9-4~ R16 L4 LJ4 L28 LJ4 L28 LJ4 L36 LJ4 L169-5: R16 N4 L4 R52 J4 R12 N28 L4 J4 L4 R12 N12 J4 N16 R4 N4 R3210-1: R88 RJ4 R28 RJ4 R2410-2: R20 L4 N4 J4 N4 L52 LJ4 L20 LJ4 L28 LJ4 R4 L12 N4 L4 J4 L16 N4 L4 N4 L8 LJ4 N4 L4 N8 L12 LJ4 N4 L4 N8 L4 N4 L4 LJ4 L4 N248 R4-10-3:+10-3~ R24 L8 N32810-4: R72 RJ4 R4 N4 R20 L4 N8 R4 L16 N36 L4 R4 N8 R4 N8 R4 N4 R12 N16 R4 N8 R4 N4 R4 N8 R12 N8 L4 R8 N8 R8 N4 L4 R12 N4 R8 L4 N76 J4 N16 L4 R4 N4 L4 R4410-5: R16 L56 LJ4 L15211-1: R124 RJ4 R92 RJ4 R20@@ -59,24 +59,24 @@12-2: R40 RJ4 R32 RJ4 R20 RJ4 R12 RJ4 R4 RJ4 R28 RJ4 R12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L4 R4 L4 R4 L4 R4 L4 R4 L4 R4 L4 R4 L4 R4 L4 R4 L4 R4 L4 R4 L4 R4 L4 R4 L4 R4 L4 R4 L4 R4 L4 R4 L4 N4 L4 N48 L4 N4 L4 N4 R8 N4 L4 R4 L4 R4 L4 R4 L4 R4 L4 R4 L4 R4 L4 R4 L4 R4 L4 R4 L4 R4+14-5~ R32 RJ4 R4+15-1~ N20 J4 N4 R4 N4 R4 N4 L4 N8 L12 N5215-2: R16 L4 LJ4 L8 N12 L8 N8 L140 N4 L4 N4 L4 N16 L88 N4 L4 N4 L4 N20 R4-15-3:-15-4:-15-5:-16-1:+15-3~ R36+15-4~ R56 L4 N8 L8 R4 N12 L24 N4 R8 RJ4 R76 RJ4 R44 N4 L12 J4 L12 LJ4 L4 R64 RJ4 R56+15-5~ R16 L4 LJ4 L12 LJ4 L4 LJ4 L4 LJ4 L4 LJ4 L4 LJ4 L4 LJ4 L4 LJ4 L4 J4 L4 J4+16-1~ R256 RJ416-2: R20 RJ4 R24 RJ4 R80 N4 R4 N8 R16 LJ4 R32 RJ4 R12 L4 N8 LJ4 N4 R100 N4 R4 N8 J4 N4 R64 N8 L4 N4 R4 L4 R4 L4 R4 L4 R4 L4 R4 L8 R12 J4 N16 R4 N4 R4 N4 L8 R4 N24 R4 RJ4 R48 J4 R2016-3: N892 J4 N16 R8 J4 N4 R12 L4 R4 N12 LJ4 R12 N48 R4 RJ4 R4 N228 R4 N28 J4 R28 L8 R4 N4 R4 L4 N4 R68 RJ4 R12 N4 J4 N4 R556 RJ4 R88 L4 N4 R268-16-4:-16-5:+16-4~ R32 J4 N8 R152 RJ4 R96+16-5~ R124 RJ4 R236 N4 R4diff --git a/games/leveldevil/tools/rooms.py b/games/leveldevil/tools/rooms.pyindex 1fc57d0b..7cd9f9f0 100644--- a/games/leveldevil/tools/rooms.py+++ b/games/leveldevil/tools/rooms.py@@ -10,12 +10,15 @@ This writes OUT/rooms.inc, the rooms packed for src/main.c.A room, packed:its modes (R_*) and where the runner starts (x, y: the corner of its 16 x 16 picture),- its ground: rectangles of tiles (x, y, w, h),- its objects: kind (K_*) and turn, flags (F_*), the event a trigger sets off, centre x and y,- half its width and height (pixels),- its script: steps (op, object, event and which time of it, frame, three numbers), in order of- event then time. Event 0 is the room starting; 1 to 8 its triggers (the runner touching a+ its ground: 33 columns of 15 tiles, a bit a tile,+ its objects, in spans (so many alike in a row, 16 pixels apart, across or down): kind (K_*) and+ turn, flags (F_*), the event a trigger sets off, the first one's centre x and y, half its width+ and height (pixels) and how many,+ its script: steps (event, frame, op, the first object and how many, which times of the event,+ three numbers, and how many 256ths of a step later each next object goes), in order of event+ then time. Event 0 is the room starting; 1 to 8 its triggers (the runner touching aTrigger_N); EV_KEY the jump key (once a step has armed it), EV_MOVE the first step taken.+All of it is written column by column: each field of all the rooms, then the next.Usage: rooms.py OUT"""