Skip to content

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.

Sokoban: within 5 KB in NumPlay, 35 levels there, a step at a time as in the original

Mason3638b383bf6a13b
diff --git a/docs/media/gallery/sokoban.gif b/docs/media/gallery/sokoban.gif
index 425afe4c..9ef84aeb 100644
Binary files a/docs/media/gallery/sokoban.gif and b/docs/media/gallery/sokoban.gif differ
diff --git a/docs/media/sokoban.gif b/docs/media/sokoban.gif
index 8abee717..1920efba 100644
Binary files a/docs/media/sokoban.gif and b/docs/media/sokoban.gif differ
diff --git a/docs/media/thumb_sokoban.png b/docs/media/thumb_sokoban.png
index 267ef49a..cd6ff0e5 100644
Binary files a/docs/media/thumb_sokoban.png and b/docs/media/thumb_sokoban.png differ
diff --git a/games/sokoban/Makefile b/games/sokoban/Makefile
index 17d4fb84..33ca68ce 100644
--- a/games/sokoban/Makefile
+++ b/games/sokoban/Makefile
@@ -15,7 +15,7 @@ NWLINK ?= npx --yes -- [email protected]
BUILD_DIR = output
SRC = src/main.c
LEVELS = $(BUILD_DIR)/levels.inc
-NUMPLAY_LEVELS ?= 50
+NUMPLAY_LEVELS ?= 35
HEADERS = ../common/epsilon_app.h ../common/epsilon_files.h ../common/np_text.h ../../launcher/src/gfx.h \
../../launcher/src/np.h ../../launcher/src/font.h ../../launcher/src/inflate.h
diff --git a/games/sokoban/README.md b/games/sokoban/README.md
index 361d14dc..661a4580 100644
--- a/games/sokoban/README.md
+++ b/games/sokoban/README.md
@@ -4,15 +4,15 @@ Push every box onto a goal. Hiroyuki Imabayashi's warehouse puzzle on the NumWor
<img src="docs/shot.png" width="320" alt="Sokoban's first level: the keeper in his red cap between a crate on its goal and a crate still to push">
-- **Microban's 155 levels**, small ones that each show an idea, the easy ones first. **Sokoban.nwa** has all 155; **inside NumPlay** there's room for the first 50. Both keep the same progress, so a level solved in one is solved in the other.
+- **Microban's 155 levels**, small ones that each show an idea, the easy ones first. **Sokoban.nwa** has all 155; **inside NumPlay** there's room for the first 35. Both keep the same progress, so a level solved in one is solved in the other.
- **The rules of the original:** the keeper pushes the boxes one at a time and can never pull them, so a box pushed into a corner stays there.
- **Undo** takes back your steps one at a time, hundreds of them; **Restart** puts the level back as it began.
- **Moves and pushes** are counted, and the fewest moves you solved each level in are kept.
- **The levels**, ten to a row: the ones you solved are green. A level opens while fewer than three before it are still unsolved, so one that blocks you never stops you for good.
- **The levels as Microban has them**, wall for wall, down to the decorations of The Dungeon; big ones shrink to fit the screen.
-- **The classic look**: brick walls, wooden crates that turn green on their goals, and the warehouse keeper in his red cap, who looks the way he walks. Steps glide from cell to cell.
+- **The classic look**: brick walls, wooden crates that turn green on their goals, and the warehouse keeper in his red cap, who looks the way he walks. The keeper steps a cell at a time, as in the original.
-<img src="docs/levels.png" width="320" alt="The level list: the first sixteen levels solved in green, the next three open">
+<img src="docs/levels.png" width="320" alt="The level list: the first seventeen levels solved in green, the next three open">
## Controls
@@ -27,7 +27,7 @@ Push every box onto a goal. Hiroyuki Imabayashi's warehouse puzzle on the NumWor
**Sokoban** is Hiroyuki Imabayashi's game, first published by Thinking Rabbit in 1982. This version is written in C for NumPlay and drawn from shapes as it goes, with no code or art from any Sokoban.
-The levels are **Microban** by **David W. Skinner** (2000), from [his Sokoban page](http://users.bentonrea.com/~sasquatch/sokoban/) as kept on [Scott Lindhurst's Sokoban levels page](http://www.sneezingtiger.com/sokoban/levels.html); they are his, used with credit to him. `levels/microban.txt` holds them as he wrote them. `tools/levels.py` packs them for the game, which puts back the walls around the floor itself (only the others are stored), and `make test` checks every one against Microban as published, then plays through the 153 that `tools/solve.py` solves.
+The levels are **Microban** by **David W. Skinner** (2000), from [his Sokoban page](http://users.bentonrea.com/~sasquatch/sokoban/) as kept on [sneezingtiger.com's Sokoban levels page](http://www.sneezingtiger.com/sokoban/levels.html); they are his, used with credit to him. `levels/microban.txt` holds them as he wrote them. `tools/levels.py` packs them for the game, which puts back the walls around the floor itself (only the others are stored), and `make test` checks every one against Microban as published, then plays through the 153 that `tools/solve.py` solves.
It comes inside [NumPlay](https://github.com/Mason363/NumPlay), or on its own as `Sokoban.nwa` from the [latest release](https://github.com/Mason363/NumPlay/releases/latest).
diff --git a/games/sokoban/docs/levels.png b/games/sokoban/docs/levels.png
index afe409c9..42d5593c 100644
Binary files a/games/sokoban/docs/levels.png and b/games/sokoban/docs/levels.png differ
diff --git a/games/sokoban/src/main.c b/games/sokoban/src/main.c
index d68a2ff8..b7685328 100644
--- a/games/sokoban/src/main.c
+++ b/games/sokoban/src/main.c
@@ -8,9 +8,10 @@
* The levels are packed by tools/levels.py as Microban has them, but for the walls around the floor,
* which the game puts back (the few that differ are written out), compressed with DEFLATE and unpacked
* into the strip buffer when a level opens. Only the rows a step changes are drawn again, in the launcher's strips
- * (launcher/src/gfx.c). Inside NumPlay it uses the launcher's shapes, fonts, files and inflater; on
+ * (launcher/src/gfx.c); the keeper steps a cell at a time, as in the original. Inside NumPlay it uses the launcher's shapes, fonts, files and inflater; on
* its own it brings them along. */
#include <eadk.h>
+#include <string.h>
#include "../../../launcher/src/gfx.h"
#include "../../../launcher/src/inflate.h"
#include "../../common/np_text.h"
@@ -39,7 +40,6 @@ static uint32_t keys(void) {
return ((uint32_t)s & 0x800201FFu) | ((uint32_t)(s >> 32) & 0x00100870u) << 10;
}
#define HDR 22 /* the band on top */
-#define STEPS 4 /* frames a step slides over */
#define HIST 1024
#define F_FLOOR 1
#define F_GOAL 2
@@ -66,10 +66,10 @@ static struct {
the first and last cells) */
static uint8_t g[MAXW * (MAXH + 2) + 2];
#define G (g + MAXW + 1)
-static const int8_t off[4] = {-MAXW, 1, MAXW, -1}, ddx[4] = {0, 1, 0, -1}, ddy[4] = {-1, 0, 1, 0}; /* up, right... */
+static const int8_t off[4] = {-MAXW, 1, MAXW, -1}, ddx[4] = {0, 1, 0, -1}; /* up, right, down, left */
static uint8_t hist[HIST]; /* the steps taken: the way, and 4 for a push */
static color_t strip[SCREEN_W * STRIP_H];
-static int level, rows, size, ox, oy, keeper, face, left, moves, pushes, nh, anim, moved;
+static int level, rows, size, ox, oy, keeper, face, left, moves, pushes, nh;
static int scr, ov, sel, pick, top, dlo, dhi;
enum { OV_NONE, OV_PAUSE, OV_QUIT, OV_HELP, OV_DONE };
static char line[64], msg[48]; /* any text; what the Solved! panel says */
@@ -108,7 +108,7 @@ static void open_level(int n) {
const uint8_t *t = (const uint8_t *)strip;
np_inflate((uint8_t *)strip, TEXT_BYTES, levels_z, sizeof levels_z, (np_inflate_tables_t *)(void *)tables);
for (int k = n; k; t++) k -= *t == '|';
- for (unsigned i = 0; i < sizeof g; i++) g[i] = 0;
+ memset(g, 0, sizeof g);
int x = 0, y = 0, w = 0;
for (left = 0; *t != '|'; t++) {
int c = *t, i = y * MAXW + x++;
@@ -127,7 +127,7 @@ static void open_level(int n) {
if ((SCREEN_H - HDR - 8) / rows < size) size = (SCREEN_H - HDR - 8) / rows;
if (size > 24) size = 24;
ox = (SCREEN_W - w * size) / 2, oy = HDR + (SCREEN_H - HDR - rows * size) / 2;
- level = sv.level = (uint8_t)n, moves = pushes = nh = anim = ov = 0, face = 2, scr = 1;
+ level = sv.level = (uint8_t)n, moves = pushes = nh = ov = 0, face = 2, scr = 1;
mark(0, SCREEN_H);
save();
}
@@ -139,7 +139,7 @@ static void shove(int from, int to) {
/* the keeper steps one way, pushing a box if there is one and it can go; or a step back */
static void step(int d) {
int o = off[d], to = keeper + o, b = to + o;
- face = d, moved = -1;
+ face = d;
mark_cell(keeper);
if (!(G[to] & F_FLOOR) || (G[to] & F_BOX && (!(G[b] & F_FLOOR) || G[b] & F_BOX))) return;
if (nh == HIST) { /* the oldest steps can't be undone any more */
@@ -147,11 +147,8 @@ static void step(int d) {
nh = HIST / 2;
}
hist[nh++] = (uint8_t)d;
- if (G[to] & F_BOX) {
- shove(to, b);
- pushes++, hist[nh - 1] |= 4, moved = b;
- }
- moves++, keeper = to, anim = STEPS;
+ if (G[to] & F_BOX) shove(to, b), pushes++, hist[nh - 1] |= 4, mark_cell(b);
+ moves++, keeper = to;
}
static void undo(void) {
int h = hist[--nh], o = off[h & 3];
@@ -161,8 +158,8 @@ static void undo(void) {
}
/* ------------------------------------------------------------ drawing */
-static void box(int i, int dx, int dy) {
- int s = size, x = ox + i % MAXW * s + dx, y = oy + i / MAXW * s + dy;
+static void box(int i) {
+ int s = size, x = ox + i % MAXW * s, y = oy + i / MAXW * s;
color_t dark = G[i] & F_GOAL ? HEX(0x3F7A2C) : HEX(0x8A5422);
gfx_rrect(x + 1, y + 1, s - 2, s - 2, s / 5, dark, 256);
gfx_rrect(x + 3, y + 3, s - 6, s - 6, s / 5 - 1, G[i] & F_GOAL ? GREEN : HEX(0xD6943E), 256);
@@ -190,7 +187,7 @@ static void overlay(void) {
}
static void play_scene(void *ctx) {
- int s = size, sh = s * anim / STEPS, ex = -ddx[face] * sh, ey = -ddy[face] * sh;
+ int s = size;
gfx_fill(0, gfx_y0, SCREEN_W, gfx_y1 - gfx_y0, BG);
for (int i = 0; i < rows * MAXW; i++) {
int x = ox + i % MAXW * s, y = oy + i / MAXW * s, f = G[i];
@@ -204,12 +201,11 @@ static void play_scene(void *ctx) {
} else if (f) {
gfx_fill(x, y, s, s, (i ^ i / MAXW) & 1 ? HEX(0xE4D7BC) : HEX(0xDCCDAF));
if (f & F_GOAL) gfx_circle(x + s / 2, y + s / 2, s / 5 + 1, HEX(0xD2452F), 256);
- if (f & F_BOX && (!anim || i != moved)) box(i, 0, 0);
+ if (f & F_BOX) box(i);
}
}
- if (anim && moved >= 0) box(moved, ex, ey);
/* the keeper: a warehouse man in blue overalls and a red cap, looking the way he goes */
- int cx = ox + keeper % MAXW * s + ex + s / 2, cy = oy + keeper / MAXW * s + ey + s / 2;
+ int cx = ox + keeper % MAXW * s + s / 2, cy = oy + keeper / MAXW * s + s / 2;
gfx_rrect(cx - s * 5 / 16, cy - s / 16, s * 5 / 8, s * 7 / 16, s / 6, HEX(0x2F5FC4), 256);
gfx_circle(cx, cy - s / 6, s / 4, HEX(0xF2C29B), 256);
gfx_rrect(cx - s / 4, cy - s * 7 / 16, s / 2, s / 6 + 1, s / 12, HEX(0xC83A2C), 256);
@@ -226,21 +222,17 @@ static void play_scene(void *ctx) {
/* the levels, ten a row, six rows on the screen at a time */
static void hub_scene(void *ctx) {
char s[8];
- int n = 0;
gfx_fill(0, 0, SCREEN_W, SCREEN_H, BG);
gfx_fill(0, 0, SCREEN_W, 36, BAR);
gfx_text(&np_font_title, 12, 27, "SOKOBAN", GOLD, 256);
for (int i = 0; i < LEVELS; i++) {
int x = 12 + 30 * (i % 10), y = 46 + 27 * (i / 10 - top);
- n += sv.best[i] != 0;
if (y < 40 || y > 190) continue;
gfx_rrect(x, y, 26, 22, 6, sv.best[i] ? GREEN : unlocked(i) ? CREAM : HEX(0x57505E), 256);
if (i == pick) gfx_rrect_outline(x - 3, y - 3, 32, 28, 8, 2, GOLD, 256);
put(s, "", (uint32_t)i + 1);
gfx_text_center(&np_font_small, x + 13, y + 15, s, sv.best[i] ? WHITE : BG, 256);
}
- put(cat(put(line, "", (uint32_t)n), " / "), "", LEVELS);
- gfx_text_right(&np_font_body, 308, 24, line, CREAM, 256);
gfx_fill(0, 214, SCREEN_W, 26, BAR);
put(line, T("Your best: "), sv.best[pick]);
if (sv.best[pick]) gfx_text_center(&np_font_body, 160, 232, line, CREAM, 256);
@@ -253,7 +245,7 @@ int main(void) {
gfx_buf = strip;
uint32_t len = 0;
const uint8_t *d = np_file_read(SAVE_NAME, &len);
- for (uint32_t i = 0; d && len == sizeof sv && d[0] == 'S' && d[1] == 1 && i < len; i++) ((uint8_t *)&sv)[i] = d[i];
+ if (d && len == sizeof sv && d[0] == 'S' && d[1] == 1) memcpy(&sv, d, len);
if (sv.level >= LEVELS) sv.level = 0;
pick = sv.level, top = pick / 10 > 3 ? pick / 10 - 3 : 0;
if (!(sv.flags & 1)) ov = OV_HELP;
@@ -296,19 +288,14 @@ int main(void) {
} else if (hit & back) {
ov = OV_PAUSE, sel = 0;
mark(0, SCREEN_H);
- } else if (!anim && way >= 0 && (tap >= 0 || (int32_t)(now - again) >= 0)) { /* held, it goes on */
+ } else if (way >= 0 && (tap >= 0 || (int32_t)(now - again) >= 0)) { /* held, it goes on */
if (way < 4) step(way);
else if (nh) undo();
- again = now + (tap >= 0 ? 240 : 100);
- }
- if (anim) {
- anim--;
- if (moved >= 0) mark_cell(moved);
- mark_cell(keeper);
+ again = now + (tap >= 0 ? 240 : 110);
mark(0, HDR);
- if (!anim && !left) { /* solved */
+ if (!left) { /* solved */
if (!sv.best[level] || moves < sv.best[level]) sv.best[level] = (uint16_t)moves;
- put(cat(put(msg, T("Moves "), (uint32_t)moves), " "), T("Your best: "), sv.best[level]);
+ put(msg, T("Your best: "), sv.best[level]);
ov = OV_DONE;
save();
mark(0, SCREEN_H);
diff --git a/games/sokoban/tests/play.c b/games/sokoban/tests/play.c
index 5e1ebfde..275a0a94 100644
--- a/games/sokoban/tests/play.c
+++ b/games/sokoban/tests/play.c
@@ -98,7 +98,6 @@ static const char *play(const char *m) {
int d = way(*p), n = moves, q = pushes;
if (d < 0) continue;
step(d);
- anim = 0;
if (moves != n + 1 || pushes != q + (*p < 'a')) return "a step didn't go";
}
if (left) return "not solved";
@@ -143,16 +142,16 @@ int main(int argc, char **argv) {
open_level(atoi(argv[2]) - 1);
const char *m = argc > 4 ? argv[4] : "";
for (const char *p = m; *p; p++)
- if (way(*p) >= 0) step(way(*p)), anim = p[1] ? 0 : STEPS / 2;
+ if (way(*p) >= 0) step(way(*p));
if (argc > 5) { /* and a panel on top: 1 paused, 2 quit, 3 how to play, 4 solved */
ov = atoi(argv[5]);
- if (ov == OV_DONE) put(cat(put(msg, T("Moves "), (uint32_t)moves), " "), T("Your best: "), (uint32_t)moves);
+ if (ov == OV_DONE) put(msg, T("Your best: "), (uint32_t)moves);
}
gfx_render(play_scene, 0, 0, SCREEN_H);
picture(argv[3]);
return 0;
}
- if (argc >= 5 && !strcmp(argv[1], "film")) { /* film L DIR MOVES: two pictures a step, then Solved! */
+ if (argc >= 5 && !strcmp(argv[1], "film")) { /* film L DIR MOVES: a picture a step, then Solved! */
char path[256];
int n = 0;
open_level(atoi(argv[2]) - 1);
@@ -162,15 +161,12 @@ int main(int argc, char **argv) {
for (const char *p = argv[4]; *p; p++) {
if (way(*p) < 0) continue;
step(way(*p));
- for (int a = STEPS / 2; a >= 0; a -= STEPS / 2) {
- anim = a;
- gfx_render(play_scene, 0, 0, SCREEN_H);
- snprintf(path, sizeof path, "%s/f_%04d.ppm", argv[3], n++);
- picture(path);
- }
+ gfx_render(play_scene, 0, 0, SCREEN_H);
+ snprintf(path, sizeof path, "%s/f_%04d.ppm", argv[3], n++);
+ picture(path);
}
if (!left) {
- put(cat(put(msg, T("Moves "), (uint32_t)moves), " "), T("Your best: "), (uint32_t)moves);
+ put(msg, T("Your best: "), (uint32_t)moves);
ov = OV_DONE;
gfx_render(play_scene, 0, 0, SCREEN_H);
snprintf(path, sizeof path, "%s/f_%04d.ppm", argv[3], n++);
diff --git a/games/sokoban/tools/fetch.py b/games/sokoban/tools/fetch.py
index 4dc70c77..1a74c62d 100644
--- a/games/sokoban/tools/fetch.py
+++ b/games/sokoban/tools/fetch.py
@@ -1,6 +1,6 @@
#!/usr/bin/env python3
"""Writes levels/microban.txt: David W. Skinner's Microban (155 levels), in the usual Sokoban text
-format, from the copy on Scott Lindhurst's Sokoban levels page. Run once; the file is kept in the
+format, from the copy on sneezingtiger.com's Sokoban levels page. Run once; the file is kept in the
repository, so building needs no network.
# wall @ the keeper $ a box . a goal
diff --git a/games/sokoban/tools/media.py b/games/sokoban/tools/media.py
index 922aeeae..77b98979 100644
--- a/games/sokoban/tools/media.py
+++ b/games/sokoban/tools/media.py
@@ -33,14 +33,15 @@ def film(level):
def gif(path, clips):
- """Clips of frames, each shown 50 ms; a clip's first frame and last (Solved!) are held."""
+ """Clips of frames, a step each 110 ms (as when an arrow is held); a clip's first frame and last
+ (Solved!) are held."""
frames, times = [], []
for c in clips:
for i, f in enumerate(c):
frames.append(f)
- times.append(700 if i == 0 else 1600 if i == len(c) - 1 else 50)
- sheet = Image.new("RGB", (320, 240 * len(frames[::8])))
- for i, f in enumerate(frames[::8]):
+ times.append(700 if i == 0 else 1600 if i == len(c) - 1 else 110)
+ sheet = Image.new("RGB", (320, 240 * len(frames[::4])))
+ for i, f in enumerate(frames[::4]):
sheet.paste(f, (0, 240 * i))
pal = sheet.quantize(64, method=Image.Quantize.MEDIANCUT, dither=Image.Dither.NONE)
out = [f.quantize(palette=pal, dither=Image.Dither.NONE) for f in frames]