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.

4 Players: the rules of every minigame tested, and a match to a win in each

Mason363a68d62285120
diff --git a/games/fourplayer/Makefile b/games/fourplayer/Makefile
index 88ee629c..bd371332 100644
--- a/games/fourplayer/Makefile
+++ b/games/fourplayer/Makefile
@@ -70,8 +70,15 @@ build/play: $(HOST_SRC) tests/play.c $(HEADERS)
$(Q) cc $(HOST_FLAGS) $(HOST_SAN) $(HOST_SRC) tests/play.c -lm -o $@
@echo "HOST $@"
-test: build/play
- python3 tests/check.py build/play
+# the rules of each minigame, from inside: tests/rules/ takes each game's source in, in place of src/g_*.c
+RULES_SRC = $(filter-out src/g_%.c,$(HOST_SRC)) $(wildcard tests/rules/*.c)
+build/rules: $(RULES_SRC) $(wildcard src/*.c) tests/rules/t.h $(HEADERS)
+ $(Q) mkdir -p $(dir $@)
+ $(Q) cc $(HOST_FLAGS) $(HOST_SAN) $(RULES_SRC) -lm -o $@
+ @echo "HOST $@"
+
+test: build/play build/rules
+ python3 tests/check.py build/play build/rules
clean:
$(Q) rm -rf $(BUILD_DIR) build
diff --git a/games/fourplayer/src/app.c b/games/fourplayer/src/app.c
index 1ccbf453..417dee4f 100644
--- a/games/fourplayer/src/app.c
+++ b/games/fourplayer/src/app.c
@@ -689,6 +689,9 @@ void app_test_step(unsigned held) {
int app_test_winner(void) { return u.decided ? u.winner : -2; } /* a side, -1 a draw, -2 not yet */
uint64_t app_bot_keys(void) {
uint64_t k = 0;
+ static bool press;
+ if (u.screen == S_RULES && u.started && (press = !press)) /* (Mix: everyone ready for the next game) */
+ for (int p = 0; p < np; p++) k |= KEY(PKEY[p]);
if (u.screen == S_PLAY && rt > 0 && !u.decided && u.freeze <= 0)
for (int p = 0; p < np; p++)
if (!gone[p] && G()->bot(p)) k |= KEY(PKEY[p]);
diff --git a/games/fourplayer/src/g_frog.c b/games/fourplayer/src/g_frog.c
index b5af27db..c4b87fbc 100644
--- a/games/fourplayer/src/g_frog.c
+++ b/games/fourplayer/src/g_frog.c
@@ -3,7 +3,7 @@
* across takes the round. */
#include "fp.h"
-#define NPAD 10
+#define NPAD 14
#define MAP 18 /* the map of the race, at the top */
#define FULL 700 /* ms to a full jump */
#define FLY 360
diff --git a/games/fourplayer/src/g_gravity.c b/games/fourplayer/src/g_gravity.c
index 4b5c2072..900535ce 100644
--- a/games/fourplayer/src/g_gravity.c
+++ b/games/fourplayer/src/g_gravity.c
@@ -44,7 +44,7 @@ static void start(void) {
while (next_x < world + 700) add_gap();
}
static void step(void) {
- speed = 105 + (rt < 45000 ? rt : 45000) * 0.0015f;
+ speed = 105 + (rt < 45000 ? rt : 45000) * 0.002f;
world += speed * DT;
while (next_x < world + 700) add_gap();
for (int p = 0; p < np; p++) {
diff --git a/games/fourplayer/src/g_snakes.c b/games/fourplayer/src/g_snakes.c
index 3b995647..e4fbff87 100644
--- a/games/fourplayer/src/g_snakes.c
+++ b/games/fourplayer/src/g_snakes.c
@@ -108,7 +108,7 @@ static void step(void) {
if (s[p].dead_t >= 0 && s[p].len && rt - s[p].dead_t > 900) clear_snake(p);
}
if (rt >= next_t) {
- int period = 130 - (rt < 50000 ? rt / 1000 : 50);
+ int period = 115 - (rt < 55000 ? rt / 1000 : 55);
next_t = rt + period;
if (!over()) move();
}
diff --git a/games/fourplayer/src/g_sumo.c b/games/fourplayer/src/g_sumo.c
index c6bb5ef4..9a2e6400 100644
--- a/games/fourplayer/src/g_sumo.c
+++ b/games/fourplayer/src/g_sumo.c
@@ -8,8 +8,8 @@
#define RING 100.0f /* the ice */
#define R 11.0f /* a wrestler */
#define SPIN 3.4f /* the arrow, radians a second */
-#define DASH 215.0f /* px/s a dash adds */
-#define FAST 330.0f
+#define DASH 185.0f /* px/s a dash adds */
+#define FAST 270.0f
#define COOL 300 /* ms between dashes */
static struct {
body_t b;
@@ -42,7 +42,7 @@ static void step(void) {
if (v > FAST) b->vx *= FAST / v, b->vy *= FAST / v;
w[p].spin = -w[p].spin, w[p].dash_t = rt;
}
- b->vx *= 0.988f, b->vy *= 0.988f; /* ice: barely slows */
+ b->vx *= 0.982f, b->vy *= 0.982f; /* ice: slow to stop */
b->x += b->vx * DT, b->y += b->vy * DT;
}
for (int p = 0; p < np; p++)
diff --git a/games/fourplayer/tests/check.py b/games/fourplayer/tests/check.py
new file mode 100644
index 00000000..664ddbde
--- /dev/null
+++ b/games/fourplayer/tests/check.py
@@ -0,0 +1,272 @@
+#!/usr/bin/env python3
+"""2 3 4 Players played on a computer (build/play and build/rules, with the sanitizers): the rules of every minigame
+from inside, a match to a win in every game and every mode with the computer playing each player, Mix, the menus,
+pause and Home, saves that come back and survive broken files, and random keys.
+
+ python3 tests/check.py build/play build/rules [--fuzz N] [--docs]
+
+--docs also saves the README's screenshots in docs/."""
+import os
+import re
+import shutil
+import struct
+import subprocess
+import sys
+import tempfile
+from concurrent.futures import ThreadPoolExecutor
+
+ARGS = [a for a in sys.argv[1:] if not a.startswith("-")]
+PLAY = os.path.abspath(ARGS[0] if ARGS else "build/play")
+RULES = os.path.abspath(ARGS[1] if len(ARGS) > 1 else "build/rules")
+FUZZ = int(sys.argv[sys.argv.index("--fuzz") + 1]) if "--fuzz" in sys.argv else 300
+WORK = tempfile.mkdtemp(prefix="fourplayer-check-")
+SHOTS = os.path.join(WORK, "shots")
+os.makedirs(SHOTS)
+NG, MODES = 14, 5
+GAMES = ["Sumo", "Racing", "Tanks", "Snakes", "Soccer", "Grab the Fish", "Skate Race", "Jump Rope", "Paint", "Balloons",
+ "Birds", "Gravity Run", "Frog Jump", "Stop the Clock"]
+SIDES = [2, 3, 4, 2, 2]
+failed = []
+
+
+def check(ok, what):
+ print(("ok " if ok else "FAIL ") + what)
+ if not ok:
+ failed.append(what)
+
+
+def run(args, saves):
+ os.makedirs(saves, exist_ok=True)
+ r = subprocess.run([PLAY, "--saves", saves, "--out", SHOTS] + [str(a) for a in args], capture_output=True, text=True)
+ out = r.stdout + r.stderr
+ if r.returncode != 0 or "runtime error" in out or "AddressSanitizer" in out:
+ failed.append("crashed (%d): %s\n%s" % (r.returncode, " ".join(map(str, args))[:300], out[-1500:]))
+ print("FAIL crashed: " + " ".join(map(str, args))[:200])
+ return out
+
+
+def save_bytes(mode=2, game=0, target=3, wins=None):
+ w = wins or [[0] * 6 for _ in range(NG + 1)]
+ return b"FOUR" + bytes([1, mode, game, target]) + b"".join(struct.pack("<6H", *row) for row in w)
+
+
+def fresh(name, data=None):
+ d = os.path.join(WORK, name)
+ shutil.rmtree(d, ignore_errors=True)
+ os.makedirs(d)
+ if data is not None:
+ open(os.path.join(d, "fourplayer.sav"), "wb").write(data)
+ return d
+
+
+def state(out, what="STATE"):
+ lines = [l for l in out.splitlines() if l.startswith(what + " ")]
+ if not lines:
+ return None
+ m = re.match(r"\w+ (\w+) mode (\d+) game (\d+) cur (\d+) target (\d+) round (\d+) sides (\d+) score ([\d,]+) "
+ r"winner (-?\d+) matches (\d+)(?: wins (.*))?(?: frame \d+)?$", lines[-1])
+ s = dict(screen=m[1], mode=int(m[2]), game=int(m[3]), cur=int(m[4]), target=int(m[5]), round=int(m[6]),
+ sides=int(m[7]), score=[int(x) for x in m[8].split(",")], winner=int(m[9]), matches=int(m[10]))
+ if m[11]:
+ s["wins"] = [[int(x) for x in g.split(",")] for g in m[11].split()]
+ return s
+
+
+def screens(out):
+ return [l.split()[1] for l in out.splitlines() if l.startswith("SCREEN ")]
+
+
+def keys(steps, start=10):
+ """'ok down w30 p1 ...': each key pressed 2 frames, 6 frames apart; wN waits N frames. Returns (keys, frame)."""
+ f, ks = start, []
+ for s in steps.split():
+ if s.startswith("w") and s[1:].isdigit():
+ f += int(s[1:])
+ else:
+ ks.append("%d-%d:%s" % (f, f + 1, s))
+ f += 6
+ return ",".join(ks), f
+
+
+# ---- the rules, from inside
+r = subprocess.run([RULES, WORK], capture_output=True, text=True)
+out = r.stdout + r.stderr
+for line in out.splitlines():
+ if line.startswith("FAIL"):
+ failed.append(line)
+ print(line)
+check(r.returncode == 0 and "0 failed" in out and "runtime error" not in out, "rules: " + out.strip().splitlines()[-1])
+
+
+# ---- a match to a win, every game, every mode: the computer plays every player
+def match(g, m, target=3, seed=0):
+ d = fresh("m%d_%d_%d" % (g, m, seed), save_bytes(m, g, target))
+ out = run(["--frames", 40000, "--keys", keys("ok ok ok")[0], "--bots", 40, "--every", 400, "--trace",
+ "--seed", 3 + g * 31 + m * 7 + seed], d)
+ over = [l for l in out.splitlines() if l.startswith("SCREEN over")]
+ return out, d, over
+
+
+jobs = [(g, m) for g in range(NG) for m in range(MODES)]
+with ThreadPoolExecutor(os.cpu_count() or 4) as ex:
+ results = list(ex.map(lambda gm: (gm, match(*gm)), jobs))
+lengths = {}
+for (g, m), (out, d, over) in results:
+ st = state(out)
+ ok = bool(over) and st and st["screen"] == "over"
+ if ok:
+ s = state(over[0], "SCREEN")
+ w = s["winner"]
+ ok = s["score"][w] == 3 and all(x < 3 for i, x in enumerate(s["score"]) if i != w) and s["sides"] == SIDES[m]
+ slot = 4 + w if m >= 3 else w
+ ok = ok and st["wins"][g][slot] == 1 and sum(map(sum, st["wins"])) == 1
+ rounds = len([l for l in out.splitlines() if l.startswith("SCREEN score")]) + 1
+ lengths.setdefault(g, []).append(rounds)
+ check(ok, "%s, %s: a match to a win, the win kept (%d rounds)" % (GAMES[g], ["2 players", "3 players", "4 players",
+ "2 v 2", "3 v 3"][m], rounds))
+
+# ---- Mix: another game every round
+out, d, over = match(NG, 2, 5)
+plays = [int(re.search(r"cur (\d+)", l)[1]) for l in out.splitlines() if l.startswith("SCREEN rules")]
+check(bool(over) and len(set(plays)) >= 4 and all(a != b for a, b in zip(plays, plays[1:])),
+ "Mix: a match to 5, another game every round (%s)" % ",".join(map(str, plays)))
+st = state(out)
+check(st and st["wins"][NG][state(over[0], "SCREEN")["winner"]] == 1, "Mix: its wins kept apart")
+
+# ---- the menus
+d = fresh("menus")
+k, f = keys("down down ok right right down ok")
+out = run(["--frames", f + 20, "--keys", k, "--trace"], d)
+st = state(out)
+check(st and st["screen"] == "rules" and st["mode"] == 4 and st["game"] == 7, "menus: 3 v 3, then the grid to Jump Rope")
+k, f = keys("ok ok w20 right right left back")
+out = run(["--frames", f + 20, "--keys", k, "--trace"], d)
+st = state(out)
+check(st and st["screen"] == "games" and st["target"] == 4 and st["mode"] == 4,
+ "menus: the mode and game kept; first to 4 on the rules; Back to the games")
+k, f = keys("back back up up ok w10 up up right right")
+out = run(["--frames", f + 20, "--keys", k, "--trace"], d)
+st = state(out)
+check(st and st["mode"] == 2 and st["game"] == NG and st["screen"] == "games", "menus: the grid wraps, Mix on top")
+k, f = keys("back down ok")
+out = run(["--frames", f + 20, "--keys", k, "--trace"], d)
+check("QUIT" in out, "menus: Back on the players, Quit game? Yes quits")
+
+# everyone ready: the round starts on its own
+d = fresh("ready", save_bytes(2, 0))
+k, f = keys("ok ok w10 p1 p2 p3")
+out = run(["--frames", f + 10, "--keys", k, "--trace"], d)
+check(state(out)["screen"] == "rules", "rules: three of four ready, still waiting")
+k, f = keys("ok ok w10 p1 p2 p3 p4")
+out = run(["--frames", f + 10, "--keys", k, "--trace"], d)
+check(state(out)["screen"] == "play", "rules: the four ready, the round starts")
+d = fresh("ready2", save_bytes(0, 0))
+k, f = keys("ok ok w10 exe")
+out = run(["--frames", f + 10, "--keys", k, "--trace"], d)
+check(state(out)["screen"] == "play", "rules: two players, EXE starts too")
+d = fresh("ready4", save_bytes(2, 0))
+k, f = keys("ok ok w10 p4")
+out = run(["--frames", f + 10, "--keys", k, "--trace"], d)
+check(state(out)["screen"] == "rules", "rules: four players, EXE is a player's key, not OK")
+
+# pause: Resume, Restart, Other games, Quit game
+d = fresh("pause", save_bytes(2, 6))
+k, f = keys("ok ok ok w200 back")
+out = run(["--frames", f + 10, "--keys", k, "--trace"], d)
+check(state(out)["screen"] == "pause", "pause: Back pauses a round")
+k, f = keys("ok ok ok w200 back w20 back w200")
+out = run(["--frames", f + 10, "--keys", k, "--trace"], d)
+check(state(out)["screen"] == "play", "pause: Back again resumes")
+k, f = keys("ok ok ok w300 back down ok w10")
+out = run(["--frames", f + 10, "--keys", k, "--trace"], d)
+st = state(out)
+check(st["screen"] == "play" and st["round"] == 0 and screens(out).count("play") == 2, "pause: Restart, a new match")
+k, f = keys("ok ok ok w300 back down down ok")
+out = run(["--frames", f + 10, "--keys", k, "--trace"], d)
+check(state(out)["screen"] == "games", "pause: Other games")
+k, f = keys("ok ok ok w300 back down down down ok ok")
+out = run(["--frames", f + 10, "--keys", k, "--trace"], d)
+check(state(out)["screen"] == "pause", "pause: Quit game? No")
+k, f = keys("ok ok ok w300 back up ok down ok")
+out = run(["--frames", f + 10, "--keys", k, "--trace"], d)
+check("QUIT" in out, "pause: Quit game? Yes quits")
+
+# Home in the middle of a match saves the settings and quits; next time they're back
+d = fresh("home", save_bytes(1, 11, 5))
+k, f = keys("ok ok w10 right ok w200 home")
+out = run(["--frames", f + 10, "--keys", k, "--trace"], d)
+check("QUIT" in out, "home: Home quits in a round")
+out = run(["--frames", 20, "--trace"], d)
+st = state(out)
+check(st["mode"] == 1 and st["game"] == 11 and st["target"] == 6, "home: the mode, the game and the target come back")
+
+# ---- saves
+d = fresh("copy", save_bytes(3, 4, 7, [[i + g for i in range(6)] for g in range(NG + 1)]))
+out = run(["--frames", 20, "--keys", "5:ok"], d)
+copy = open(os.path.join(d, "fourplayer_saves.py"), "rb").read()
+check(copy[:1] == b"\0" and b"fourplayer.sav" in copy, "saves: the copy in a Python script")
+os.remove(os.path.join(d, "fourplayer.sav"))
+out = run(["--frames", 20], d)
+st = state(out)
+check(st["mode"] == 3 and st["game"] == 4 and st["target"] == 7 and st["wins"][5] == [5, 6, 7, 8, 9, 10],
+ "saves: the copy brings everything back when the save is gone")
+for name, data in [("empty", b""), ("short", b"FOUR\1\2"), ("version", save_bytes()[:4] + b"\x09" + save_bytes()[5:]),
+ ("garbage", bytes(range(256)) * 2), ("mode", b"FOUR" + bytes([1, 99, 99, 0]) + bytes(180)),
+ ("long", save_bytes() + b"xx")]:
+ d = fresh("bad_" + name, data)
+ open(os.path.join(d, "fourplayer_saves.py"), "wb").write(b"\0# not a copy\n" if name != "mode" else b"\0#>x")
+ out = run(["--frames", 400, "--keys", keys("ok ok ok")[0], "--trace"], d)
+ st = state(out)
+ check(st and st["mode"] in range(5) and st["game"] <= NG and 1 <= st["target"] <= 9 and st["screen"] == "play",
+ "saves: a broken save (%s) starts clean and plays" % name)
+
+# ---- random keys
+def fuzz(i):
+ d = fresh("fuzz%d" % i, save_bytes(i % 5, i % 15) if i % 3 else None)
+ args = ["--frames", 3000, "--fuzz", i, "--every", 5, "--seed", i + 1]
+ if i % 4 == 0:
+ args += ["--bots", 50]
+ out = run(args, d)
+ return state(out) is not None
+
+
+with ThreadPoolExecutor(os.cpu_count() or 4) as ex:
+ ok = list(ex.map(fuzz, range(FUZZ)))
+check(all(ok), "%d random-key sessions, no crash" % FUZZ)
+
+# ---- the README's screenshots
+if "--docs" in sys.argv:
+ docs = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "docs")
+ from PIL import Image
+
+ def shot(name, data, steps, frames, seed=5, bots=True):
+ d = fresh("docs_" + name, data)
+ k, f = keys(steps)
+ args = ["--frames", f + frames + 1, "--keys", k, "--shots", "%d:%s.ppm" % (f + frames, name), "--seed", seed]
+ if bots:
+ args += ["--bots", f]
+ run(args, d)
+ Image.open(os.path.join(SHOTS, name + ".ppm")).save(os.path.join(docs, name + ".png"), optimize=True)
+
+ wins = [[0] * 6 for _ in range(NG + 1)]
+ wins[0] = [4, 2, 5, 3, 1, 2]
+ shot("players", save_bytes(2, 0, 3, wins), "", 5, bots=False)
+ shot("games", save_bytes(2, 0, 3, wins), "ok", 5, bots=False)
+ shot("rules", save_bytes(2, 0, 3, wins), "ok ok w10 p1 p3", 5, bots=False)
+ for name, mode, game, frames, seed in [("sumo", 2, 0, 230, 5), ("racing", 2, 1, 520, 7), ("tanks", 2, 2, 320, 2),
+ ("snakes", 2, 3, 420, 4), ("soccer", 2, 4, 330, 6), ("fish", 2, 5, 390, 3),
+ ("skate", 4, 6, 330, 1), ("rope", 2, 7, 300, 9), ("paint", 2, 8, 700, 5),
+ ("balloons", 2, 9, 520, 8), ("birds", 2, 10, 420, 6), ("gravity", 2, 11, 420, 3),
+ ("frogs", 2, 12, 520, 4), ("clock", 2, 13, 700, 2), ("teams", 4, 4, 300, 3)]:
+ shot(name, save_bytes(mode, game), "ok ok ok", frames, seed)
+ shutil.copy(os.path.join(docs, "sumo.png"), os.path.join(docs, "shot.png"))
+ print("docs: screenshots saved")
+
+print()
+if failed:
+ print("%d FAILED" % len(failed))
+ for f in failed[:40]:
+ print(" " + f[:600])
+ sys.exit(1)
+print("all passed (%s)" % WORK)
+shutil.rmtree(WORK, ignore_errors=True)
diff --git a/games/fourplayer/tests/play.c b/games/fourplayer/tests/play.c
index 85af28ac..d8fbb421 100644
--- a/games/fourplayer/tests/play.c
+++ b/games/fourplayer/tests/play.c
@@ -66,6 +66,7 @@ static void parse_shots(const char *spec) {
}
static const char *const SCREEN[] = {"mode", "games", "rules", "play", "score", "over", "pause", "ask"};
+static int frame_now;
static void print_state(const char *what) {
app_state_t s;
app_state(&s);
@@ -75,6 +76,8 @@ static void print_state(const char *what) {
if (!strcmp(what, "STATE")) {
printf(" wins");
for (int g = 0; g < 15; g++) printf(" %d,%d,%d,%d,%d,%d", s.wins[g][0], s.wins[g][1], s.wins[g][2], s.wins[g][3], s.wins[g][4], s.wins[g][5]);
+ } else {
+ printf(" frame %d", frame_now);
}
printf("\n");
}
@@ -133,6 +136,7 @@ int main(int argc, char **argv) {
}
app_state_t s;
app_state(&s);
+ frame_now = f;
if (trace && s.screen != last_screen) print_state("SCREEN");
last_screen = s.screen;
bool shot = false;
diff --git a/games/fourplayer/tests/rules/balloon.c b/games/fourplayer/tests/rules/balloon.c
new file mode 100644
index 00000000..589ed5db
--- /dev/null
+++ b/games/fourplayer/tests/rules/balloon.c
@@ -0,0 +1,50 @@
+#include "../../src/g_balloon.c"
+#include "t.h"
+
+static void pump(unsigned who, int n) {
+ for (int i = 0; i < n; i++) app_test_step(who), app_test_step(0);
+}
+
+void rules_balloon(void) {
+ app_test_round(M2, G_BALLOON);
+ burst_at[0] = burst_at[1] = 30;
+ pump(1, 5);
+ CHECK(pumps[0] == 5 && pumps[1] == 0, "balloons: a tap, a pump");
+ pump(2, 7);
+ run(0, 1100);
+ CHECK(WINNER == 1, "balloons: after 10 seconds the biggest takes the round");
+
+ app_test_round(M2, G_BALLOON);
+ burst_at[0] = 10, burst_at[1] = 30;
+ pump(2, 4);
+ pump(1, 10);
+ CHECK(burst_t[0] >= 0 && WINNER == 1, "balloons: burst, out; the only one whole takes the round");
+
+ app_test_round(M3, G_BALLOON); /* the same size: the first there */
+ burst_at[0] = burst_at[1] = burst_at[2] = 40;
+ pump(1, 8), pump(2, 8), pump(4, 3);
+ run(0, 1100);
+ CHECK(WINNER == 0, "balloons: the same size, the first to it");
+
+ app_test_round(M2, G_BALLOON); /* everyone bursts: a draw */
+ burst_at[0] = burst_at[1] = 2;
+ pump(3, 2);
+ CHECK(WINNER == -1, "balloons: all burst, a draw");
+
+ app_test_round(M2V2, G_BALLOON); /* teams: the air between you */
+ for (int p = 0; p < 4; p++) burst_at[p] = 40;
+ pump(1, 10), pump(4, 10), pump(2, 12), pump(8, 6);
+ run(0, 1100);
+ CHECK(WINNER == 0, "balloons: teams, the most air between you");
+
+ app_test_round(M4, G_BALLOON);
+ pump(1, 3);
+ run(0, 1100);
+ CHECK(pumps[0] == 3, "balloons: no pumping after 10 seconds");
+ int lo = 99, hi = 0;
+ for (int i = 0; i < 200; i++) {
+ start();
+ lo = burst_at[0] < lo ? burst_at[0] : lo, hi = burst_at[0] > hi ? burst_at[0] : hi;
+ }
+ CHECK(lo == 18 && hi == 36, "balloons: they burst at 18 to 36 pumps");
+}
diff --git a/games/fourplayer/tests/rules/birds.c b/games/fourplayer/tests/rules/birds.c
new file mode 100644
index 00000000..644ba8c9
--- /dev/null
+++ b/games/fourplayer/tests/rules/birds.c
@@ -0,0 +1,19 @@
+#include "../../src/g_birds.c"
+#include "t.h"
+
+void rules_birds(void) {
+ app_test_round(M2, G_BIRDS);
+ app_test_step(1);
+ CHECK(vy[0] < -200, "birds: a tap, a flap");
+ y[1] = y[0], vy[1] = vy[0] = 0;
+ run(0, 200);
+ CHECK(gone[0] && gone[1] && WINNER == -1, "birds: no flaps, both on the ground: a draw");
+
+ app_test_round(M2, G_BIRDS); /* through a gap, and into a column */
+ c[0].x = bx(1) + 30, c[0].mid = 131, c[0].gap = 78;
+ for (int i = 0; i < 120 && WINNER == UNDECIDED; i++) {
+ y[0] = 131, y[1] = 60, vy[0] = vy[1] = 0;
+ app_test_step(0);
+ }
+ CHECK(!gone[0] && gone[1] && WINNER == 0, "birds: through the gap; into a column, out");
+}
diff --git a/games/fourplayer/tests/rules/clock.c b/games/fourplayer/tests/rules/clock.c
new file mode 100644
index 00000000..f950936a
--- /dev/null
+++ b/games/fourplayer/tests/rules/clock.c
@@ -0,0 +1,38 @@
+#include "../../src/g_clock.c"
+#include "t.h"
+
+/* p taps at the round's time ms */
+static void tap_at(unsigned who, int32_t ms) {
+ while (rt + TICK < ms && WINNER == UNDECIDED) app_test_step(0);
+ app_test_step(who), app_test_step(0);
+}
+
+void rules_clock(void) {
+ app_test_round(M2, G_CLOCK);
+ target = 7000;
+ tap_at(1, 6800), tap_at(2, 7100);
+ CHECK(WINNER == 1, "clock: the closest takes the round");
+ CHECK(stopped[0] == 6800 && stopped[1] == 7100, "clock: each one's time, to the hundredth");
+
+ app_test_round(M2, G_CLOCK);
+ target = 5000;
+ tap_at(1, 4000), tap_at(1, 5000), tap_at(2, 5500);
+ CHECK(stopped[0] == 4000 && WINNER == 1, "clock: only the first tap counts");
+
+ app_test_round(M2, G_CLOCK);
+ target = 6000;
+ tap_at(1, 5800), tap_at(2, 6200);
+ CHECK(WINNER == -1, "clock: as close as each other, a draw");
+
+ app_test_round(M3, G_CLOCK);
+ target = 8000;
+ tap_at(4, 7000);
+ run(0, 2000);
+ CHECK(WINNER == 2 && rt <= 12000 + 20, "clock: those who don't stop it, no time");
+ app_test_round(M2, G_CLOCK);
+ run(0, 2000);
+ CHECK(WINNER == -1, "clock: nobody stops it, a draw");
+ int lo = 99999, hi = 0;
+ for (int i = 0; i < 200; i++) start(), lo = target < lo ? target : lo, hi = target > hi ? target : hi;
+ CHECK(lo == 5000 && hi == 10000, "clock: 5 to 10 seconds");
+}
diff --git a/games/fourplayer/tests/rules/fish.c b/games/fourplayer/tests/rules/fish.c
new file mode 100644
index 00000000..7a5fa00e
--- /dev/null
+++ b/games/fourplayer/tests/rules/fish.c
@@ -0,0 +1,48 @@
+#include "../../src/g_fish.c"
+#include "t.h"
+
+/* the next thing, out of the water now */
+static void jumps(int what) {
+ item = what, stage = 0, item_t = rt + 10;
+ app_test_step(0), app_test_step(0);
+ for (int i = 0; i < 9; i++) app_test_step(0); /* (80 ms before a paw can reach it) */
+}
+
+void rules_fish(void) {
+ app_test_round(M2, G_FISH);
+ jumps(FISH);
+ CHECK(stage == 1, "fish: a goldfish out of the water");
+ app_test_step(2), app_test_step(0);
+ CHECK(caught[1] == 1 && caught[0] == 0 && stage == 0, "fish: the first paw on a goldfish keeps it");
+ app_test_step(1), app_test_step(0); /* nothing out */
+ CHECK(dizzy(0), "fish: a swipe at nothing, dizzy");
+ jumps(FISH);
+ app_test_step(1), app_test_step(0);
+ CHECK(caught[0] == 0, "fish: dizzy, no catching");
+
+ app_test_round(M2, G_FISH);
+ jumps(BOOT);
+ app_test_step(1), app_test_step(0);
+ CHECK(caught[0] == 0 && dizzy(0) && dizzy_ms[0] == 1800, "fish: a swipe at a boot, dizzier");
+ for (int k = 0; k < 3; k++) jumps(FISH), app_test_step(2), app_test_step(0);
+ CHECK(caught[1] == 3 && WINNER == 1, "fish: three goldfish take the round");
+
+ app_test_round(M2V2, G_FISH); /* teams: three between you */
+ jumps(FISH), app_test_step(1), app_test_step(0);
+ jumps(FISH), app_test_step(4), app_test_step(0);
+ CHECK(tally[0] == 2 && WINNER == UNDECIDED, "fish: teams count together");
+ jumps(FISH), app_test_step(4), app_test_step(0);
+ CHECK(WINNER == 0, "fish: three for the red team");
+
+ app_test_round(M4, G_FISH); /* things come, then go back in */
+ int fish_ = 0, junk = 0;
+ for (int i = 0; i < 300; i++) {
+ stage = 0, item_t = rt + 1;
+ next_item(0);
+ fish_ += item == FISH, junk += item != FISH;
+ }
+ CHECK(fish_ > 130 && junk > 100, "fish: about half goldfish, half junk");
+ jumps(CAN);
+ run(0, 150);
+ CHECK(stage == 0 && grabber == -1, "fish: nobody swipes, it falls back in");
+}
diff --git a/games/fourplayer/tests/rules/frog.c b/games/fourplayer/tests/rules/frog.c
new file mode 100644
index 00000000..01104701
--- /dev/null
+++ b/games/fourplayer/tests/rules/frog.c
@@ -0,0 +1,43 @@
+#include "../../src/g_frog.c"
+#include "t.h"
+
+/* p holds for ms, then lets go */
+static void jump(int p, int ms) {
+ for (int i = 0; i < ms / TICK; i++) app_test_step(1u << p);
+ app_test_step(0);
+}
+
+void rules_frog(void) {
+ app_test_round(M2, G_FROG);
+ float x0 = f[0].x;
+ jump(0, 300);
+ CHECK(f[0].st == AIR && fabs_(f[0].to - x0 - jump_len(300)) < 2, "frog: held then let go, a jump that long");
+ CHECK(jump_len(0) == 16 && jump_len(700) == 130 && jump_len(5000) == 130, "frog: 16 to 130 pixels, full in 0.7 s");
+ run(0, 40);
+ CHECK(f[0].st != AIR, "frog: it lands");
+
+ app_test_round(M2, G_FROG); /* onto a pad, into the water */
+ int ms = (int)((pad[0] - f[0].x - 16) / 114 * FULL);
+ jump(0, ms);
+ run(0, 40);
+ CHECK(f[0].st == SIT && fabs_(f[0].x - pad[0]) <= 11, "frog: onto the lily pad");
+ float on = f[0].x;
+ ms = (int)(((pad[1] + pad[0]) / 2 - f[0].x - 16) / 114 * FULL); /* between two pads */
+ if (pad[1] - pad[0] < 30) ms = 0;
+ jump(0, ms);
+ run(0, 40);
+ CHECK(f[0].st == SWIM || pad[1] - pad[0] < 30, "frog: into the water");
+ run(0, 80);
+ CHECK(f[0].st == SIT && f[0].x == on, "frog: swum back to the pad");
+
+ app_test_round(M2, G_FROG); /* across */
+ f[1].x = pad[NPAD - 1];
+ ms = (int)((finish + 5 - f[1].x - 16) / 114 * FULL);
+ jump(1, ms);
+ run(0, 40);
+ CHECK(WINNER == 1, "frog: the first across takes the round");
+ CHECK(finish > pad[NPAD - 1] && finish - pad[NPAD - 1] < 120, "frog: the last pad within a jump of the bank");
+ bool reachable = pad[0] - 22 < 120;
+ for (int k = 1; k < NPAD; k++) reachable &= pad[k] - pad[k - 1] < 125;
+ CHECK(reachable, "frog: every pad within a jump");
+}
diff --git a/games/fourplayer/tests/rules/gravity.c b/games/fourplayer/tests/rules/gravity.c
new file mode 100644
index 00000000..ffede290
--- /dev/null
+++ b/games/fourplayer/tests/rules/gravity.c
@@ -0,0 +1,30 @@
+#include "../../src/g_gravity.c"
+#include "t.h"
+
+void rules_gravity(void) {
+ app_test_round(M2, G_GRAVITY);
+ CHECK(r[0].on && r[1].on, "gravity: the runners start on the ground");
+ app_test_step(1), app_test_step(0);
+ CHECK(r[0].g == -1 && !r[0].on, "gravity: a tap flips it");
+ app_test_step(1), app_test_step(0);
+ CHECK(r[0].g == -1, "gravity: no flip in the air");
+ run(0, 100);
+ CHECK(r[0].on && r[0].y == CEIL + R, "gravity: on the ceiling");
+
+ app_test_round(M2, G_GRAVITY); /* a hole */
+ ngap = 1, gap[0].x0 = world + rx(1) + 20, gap[0].x1 = gap[0].x0 + 50, gap[0].up = 0, next_x = 1e9f;
+ run(0, 300);
+ CHECK(gone[1] && WINNER == 0, "gravity: through a hole, out");
+
+ app_test_round(M3V3, G_GRAVITY); /* the holes never face each other */
+ bool fair = true;
+ for (int k = 0; k < 6000; k++) {
+ for (int p = 0; p < np; p++) r[p].y = r[p].g > 0 ? FLOOR - R : CEIL + R, r[p].on = true, r[p].vy = 0; /* (all kept) */
+ app_test_step(0);
+ for (int i = 0; i < ngap; i++)
+ for (int j = 0; j < ngap; j++)
+ if (gap[i].up != gap[j].up && gap[i].x0 < gap[j].x1 + 20 && gap[j].x0 < gap[i].x1 + 20) fair = false;
+ }
+ CHECK(fair, "gravity: a hole in the floor is never over one in the ceiling");
+ CHECK(speed > 150, "gravity: faster and faster");
+}
diff --git a/games/fourplayer/tests/rules/main.c b/games/fourplayer/tests/rules/main.c
new file mode 100644
index 00000000..85a3c6f3
--- /dev/null
+++ b/games/fourplayer/tests/rules/main.c
@@ -0,0 +1,29 @@
+/* The rules of every minigame, checked from inside (each file takes its game's source in).
+ *
+ * rules prints a line a failure, then how many passed */
+#include <stdio.h>
+#include "t.h"
+
+int t_failed, t_passed;
+void rules_sumo(void), rules_race(void), rules_tanks(void), rules_snakes(void), rules_soccer(void), rules_fish(void),
+ rules_skate(void), rules_rope(void), rules_paint(void), rules_balloon(void), rules_birds(void), rules_gravity(void),
+ rules_frog(void), rules_clock(void);
+void host_save_dir(const char *d);
+
+int main(int argc, char **argv) {
+ host_save_dir(argc > 1 ? argv[1] : "build");
+ app_init(1000);
+ rules_sumo(), rules_race(), rules_tanks(), rules_snakes(), rules_soccer(), rules_fish(), rules_skate();
+ rules_rope(), rules_paint(), rules_balloon(), rules_birds(), rules_gravity(), rules_frog(), rules_clock();
+ /* every game, every mode: a round starts with everyone in, and its pictures draw */
+ for (int g = 0; g < NG; g++)
+ for (int m = 0; m < 5; m++) {
+ app_test_round(m, g);
+ int n = alive();
+ CHECK(n == (m == 0 ? 2 : m == 1 ? 3 : m == 4 ? 6 : 4), "every game: everyone in at the start");
+ for (int i = 0; i < 300; i++) app_test_step(i % 37 < 3 ? 0x3F : i % 11 < 2 ? 0x15 : 0);
+ app_draw();
+ }
+ printf("rules: %d passed, %d failed\n", t_passed, t_failed);
+ return t_failed != 0;
+}
diff --git a/games/fourplayer/tests/rules/paint.c b/games/fourplayer/tests/rules/paint.c
new file mode 100644
index 00000000..d8d92ae5
--- /dev/null
+++ b/games/fourplayer/tests/rules/paint.c
@@ -0,0 +1,30 @@
+#include "../../src/g_paint.c"
+#include "t.h"
+
+void rules_paint(void) {
+ app_test_round(M2, G_PAINT);
+ app_test_step(0);
+ CHECK(count[0] > 0 && count[1] > 0, "paint: the rollers paint where they go");
+ int sgn = r[0].sgn;
+ app_test_step(1), app_test_step(0);
+ CHECK(r[0].sgn == -sgn, "paint: a tap curves the other way");
+ int total = 0;
+ for (int y = 0; y < GH; y++)
+ for (int x = 0; x < GW; x++) total += floor_[y][x] != 0;
+ CHECK(total == count[0] + count[1], "paint: the counts are the floor");
+ run(0, 3500);
+ CHECK(WINNER == (count[0] > count[1] ? 0 : count[1] > count[0] ? 1 : -1), "paint: after 30 seconds, the most floor");
+ CHECK(rt >= TIME && rt < TIME + 20, "paint: 30 seconds");
+
+ app_test_round(M2, G_PAINT); /* over the others' paint */
+ r[0].x = r[1].x = 100, r[0].y = 100, r[1].y = 140;
+ app_test_step(0);
+ int before = count[1];
+ r[0].x = r[1].x, r[0].y = r[1].y;
+ roll(0);
+ CHECK(count[1] < before && count[0] > 0, "paint: over the others' colour");
+
+ app_test_round(M2V2, G_PAINT); /* teams paint together */
+ app_test_step(0);
+ CHECK(count[2] == 0 && count[3] == 0 && count[0] > 0 && count[1] > 0, "paint: teams, one colour each");
+}
diff --git a/games/fourplayer/tests/rules/race.c b/games/fourplayer/tests/rules/race.c
new file mode 100644
index 00000000..d52615c7
--- /dev/null
+++ b/games/fourplayer/tests/rules/race.c
@@ -0,0 +1,47 @@
+#include "../../src/g_race.c"
+#include "t.h"
+
+void rules_race(void) {
+ app_test_round(M2, G_RACE);
+ CHECK(c[0].x < MX && c[1].x < MX && c[0].prog < 0, "racing: the cars start behind the line");
+ float h = c[0].h;
+ app_test_step(1);
+ CHECK(fabs_(wrap_pi(h - TURN * DT - c[0].h)) < 0.001f && c[1].h == 0, "racing: holding turns left, letting go goes straight");
+ run(0, 150);
+ CHECK(c[1].s > 100 && c[1].x > 160, "racing: the cars drive on their own");
+ CHECK(c[1].prog > 0 && lap(1) == 1, "racing: over the line, lap 1");
+
+ app_test_round(M2, G_RACE); /* the right way round: three laps take the round */
+ c[1].prog = LAPS * 2 * PI - 0.001f, c[1].last_a = fatan2(c[1].y - MY, c[1].x - MX);
+ run(0, 100);
+ CHECK(WINNER == 1, "racing: three laps, the round");
+
+ app_test_round(M2, G_RACE); /* the wrong way doesn't count */
+ c[0].h = c[0].va = PI, c[0].s = FAST, c[0].x = 200, c[0].y = 212;
+ c[0].last_a = fatan2(c[0].y - MY, c[0].x - MX), c[0].prog = 0;
+ run(0, 60);
+ CHECK(c[0].prog < 0, "racing: driving the wrong way, laps go back");
+
+ app_test_round(M2, G_RACE); /* the island is a wall */
+ c[0].x = IX + IW / 2, c[0].y = IY + IH - 2, c[0].h = c[0].va = -PI / 2, c[0].s = FAST;
+ app_test_step(0);
+ CHECK(island(c[0].x, c[0].y) >= CR - 0.5f, "racing: the island's barrier pushes cars out");
+ CHECK(fabs_(wrap_pi(c[0].va)) < 0.1f || fabs_(wrap_pi(c[0].va - PI)) < 0.1f, "racing: along the barrier");
+
+ app_test_round(M2, G_RACE); /* grass */
+ c[0].x = 10, c[0].y = 30, c[0].h = c[0].va = PI / 4, c[0].s = FAST;
+ app_test_step(0);
+ CHECK(c[0].s < FAST, "racing: grass slows");
+ c[0].x = 10, c[0].y = 30, c[0].s = FAST;
+ for (int i = 0; i < 40; i++) c[0].x = 10, c[0].y = 30, app_test_step(0);
+ CHECK(c[0].s < SLOW + 15, "racing: on grass, little more than a crawl");
+
+ app_test_round(M2, G_RACE); /* oil */
+ c[0].x = oil[0][0], c[0].y = oil[0][1], c[0].h = c[0].va = 0, c[0].s = 40;
+ app_test_step(0);
+ h = c[0].h;
+ app_test_step(1), app_test_step(1);
+ CHECK(c[0].h == h, "racing: on oil, no steering");
+ for (int i = 0; i < 60; i++) app_test_step(1);
+ CHECK(c[0].h != h, "racing: steering again after the oil");
+}
diff --git a/games/fourplayer/tests/rules/rope.c b/games/fourplayer/tests/rules/rope.c
new file mode 100644
index 00000000..65ed6de5
--- /dev/null
+++ b/games/fourplayer/tests/rules/rope.c
@@ -0,0 +1,42 @@
+#include "../../src/g_rope.c"
+#include "t.h"
+
+/* steps until the bar is about to reach p (within ms) */
+static void until_near(int p, float ms) {
+ for (int i = 0; i < 2000; i++) {
+ float d = wrap_pi(seat(p) - beam), dist = w > 0 ? (d >= 0 ? d : d + 2 * PI) : (d <= 0 ? -d : 2 * PI - d);
+ if (fabs_(w) > 0.5f && dist / fabs_(w) * 1000 < ms) return;
+ for (int q = 0; q < np; q++)
+ if (q != p) jump_t[q] = rt - AIR / 2; /* (the others in the air) */
+ app_test_step(0);
+ }
+}
+
+void rules_rope(void) {
+ app_test_round(M2, G_ROPE);
+ flip_at = 1 << 30;
+ until_near(0, 150);
+ app_test_step(1), app_test_step(0); /* a jump */
+ CHECK(height(0) > 0, "rope: a tap jumps");
+ app_test_step(1), app_test_step(0);
+ CHECK(jump_t[0] < rt - 15, "rope: no jump in the air");
+ for (int i = 0; i < 60; i++) jump_t[1] = rt - AIR / 2, app_test_step(0);
+ CHECK(!gone[0], "rope: in the air, the bar passes under");
+ until_near(0, 200);
+ for (int i = 0; i < 40 && !gone[0]; i++) jump_t[1] = rt - AIR / 2, app_test_step(0);
+ CHECK(gone[0] && WINNER == 1, "rope: on the ground, the bar throws you off");
+
+ app_test_round(M4, G_ROPE); /* it turns back now and then, and a second bar comes */
+ int was = dir;
+ for (int i = 0; i < 1300 && dir == was; i++) {
+ for (int p = 0; p < np; p++) jump_t[p] = rt - AIR / 2; /* (all in the air) */
+ app_test_step(0);
+ }
+ CHECK(dir != was, "rope: the bar turns back");
+ for (int i = 0; i < 2300 && !two; i++) {
+ for (int p = 0; p < np; p++) jump_t[p] = rt - AIR / 2;
+ app_test_step(0);
+ }
+ CHECK(two && rt > 22000, "rope: a second bar after 22 seconds");
+ CHECK(speed() > 3, "rope: faster as the round goes on");
+}
diff --git a/games/fourplayer/tests/rules/skate.c b/games/fourplayer/tests/rules/skate.c
new file mode 100644
index 00000000..f5c3e909
--- /dev/null
+++ b/games/fourplayer/tests/rules/skate.c
@@ -0,0 +1,19 @@
+#include "../../src/g_skate.c"
+#include "t.h"
+
+void rules_skate(void) {
+ app_test_round(M2, G_SKATE);
+ run(0, 300);
+ CHECK(x[0] == 30 && x[1] == 30, "skate: no kicks, no rolling");
+ app_test_step(1), app_test_step(0);
+ CHECK(v[0] > 23 && v[0] <= 24 && v[1] == 0, "skate: a kick is speed");
+ run(0, 100);
+ CHECK(v[0] < 24 && x[0] > 30, "skate: rolling slows down");
+ int steps = 0; /* one kicks seven times a second, the other five */
+ while (WINNER == UNDECIDED && steps < 6000) {
+ unsigned k = (steps % 14 < 7 ? 1 : 0) | (steps % 20 < 10 ? 2 : 0);
+ app_test_step(k), steps++;
+ }
+ CHECK(WINNER == 0, "skate: the faster kicker takes the round");
+ CHECK(steps > 600 && steps < 2000, "skate: a race takes 6 to 20 seconds");
+}
diff --git a/games/fourplayer/tests/rules/snakes.c b/games/fourplayer/tests/rules/snakes.c
new file mode 100644
index 00000000..8a427300
--- /dev/null
+++ b/games/fourplayer/tests/rules/snakes.c
@@ -0,0 +1,72 @@
+#include "../../src/g_snakes.c"
+#include "t.h"
+
+static int head_x(int p) { return s[p].cell[s[p].hd] % GW; }
+static int head_y(int p) { return s[p].cell[s[p].hd] / GW; }
+/* steps until the snakes have moved once more */
+static void one_move(unsigned held) {
+ int32_t t0 = next_t;
+ while (next_t == t0 && WINNER == UNDECIDED) app_test_step(held);
+}
+
+void rules_snakes(void) {
+ app_test_round(M2, G_SNAKES);
+ for (int k = 0; k < NSTAR; k++) star[k] = -1;
+ int x = head_x(0), y = head_y(0), d = s[0].dir;
+ one_move(0);
+ CHECK(head_x(0) == x + DX[d] && head_y(0) == y + DY[d] && s[0].len == 4, "snakes: a step a cell, straight on");
+ app_test_step(1);
+ one_move(0);
+ CHECK(s[0].dir == ((d + 1) & 3), "snakes: a tap turns right");
+ for (int i = 0; i < 4; i++) app_test_step(1), app_test_step(0); /* four taps before the next step */
+ CHECK(s[0].turns == 2, "snakes: two turns wait their turn, no more");
+
+ app_test_round(M2, G_SNAKES); /* a star */
+ for (int k = 0; k < NSTAR; k++) star[k] = -1;
+ int h = s[0].cell[s[0].hd];
+ star[0] = (int16_t)at(h % GW + DX[s[0].dir], h / GW + DY[s[0].dir]);
+ one_move(0);
+ for (int i = 0; i < 4; i++) one_move(0);
+ CHECK(s[0].len == 7, "snakes: a star, three cells longer");
+ CHECK(star[0] >= 0 && grid[star[0] / GW][star[0] % GW] == 0, "snakes: a new star, on a free cell");
+
+ app_test_round(M2, G_SNAKES); /* the wall */
+ for (int k = 0; k < NSTAR; k++) star[k] = -1;
+ int steps = 0;
+ while (WINNER == UNDECIDED && steps < 100) one_move(0), steps++;
+ CHECK(gone[0] || gone[1], "snakes: into the wall, out");
+ CHECK(WINNER >= -1, "snakes: the round decided");
+
+ app_test_round(M2, G_SNAKES); /* into another snake */
+ for (int k = 0; k < NSTAR; k++) star[k] = -1;
+ clear_snake(0), clear_snake(1);
+ int a[4] = {at(10, 10), at(11, 10), at(12, 10), at(13, 10)}, b[4] = {at(14, 7), at(14, 8), at(14, 9), at(14, 10)};
+ for (int i = 0; i < 4; i++) { /* 0 goes right into 1's head's cell... 1 goes down past it */
+ s[0].cell[i] = (uint16_t)a[i], grid[10][10 + i] = 1;
+ s[1].cell[i] = (uint16_t)b[i], grid[7 + i][14] = 2;
+ }
+ s[0].hd = s[1].hd = 3, s[0].len = s[1].len = 4, s[0].dir = 0, s[1].dir = 1;
+ one_move(0);
+ CHECK(gone[0] && !gone[1] && WINNER == 1, "snakes: into a snake's body, out");
+
+ app_test_round(M2, G_SNAKES); /* head to head */
+ for (int k = 0; k < NSTAR; k++) star[k] = -1;
+ clear_snake(0), clear_snake(1);
+ for (int i = 0; i < 4; i++) {
+ s[0].cell[i] = (uint16_t)at(5 + i, 10), grid[10][5 + i] = 1;
+ s[1].cell[i] = (uint16_t)at(14 - i, 10), grid[10][14 - i] = 2;
+ }
+ s[0].hd = s[1].hd = 3, s[0].len = s[1].len = 4, s[0].dir = 0, s[1].dir = 2;
+ one_move(0);
+ CHECK(!gone[0] && !gone[1], "snakes: one cell apart, still in");
+ one_move(0);
+ CHECK(gone[0] && gone[1] && WINNER == -1, "snakes: head to head, both out: a draw");
+
+ app_test_round(M3V3, G_SNAKES); /* six snakes on the board */
+ int n = 0;
+ for (int yy = 0; yy < GH; yy++)
+ for (int xx = 0; xx < GW; xx++) n += grid[yy][xx] != 0;
+ CHECK(n == 24, "snakes: six snakes of four, none on another");
+ run(0, 150);
+ CHECK(alive() == 6, "snakes: six snakes start with room to turn");
+}
diff --git a/games/fourplayer/tests/rules/soccer.c b/games/fourplayer/tests/rules/soccer.c
new file mode 100644
index 00000000..2e19b2f5
--- /dev/null
+++ b/games/fourplayer/tests/rules/soccer.c
@@ -0,0 +1,62 @@
+#include "../../src/g_soccer.c"
+#include "t.h"
+
+/* the ball sent at a goal's middle, from just inside */
+static void shoot_at(int k, float speed) {
+ float mx = (seg[k].x0 + seg[k].x1) / 2, my = (seg[k].y0 + seg[k].y1) / 2, dx = mx - CX, dy = my - CY, l = fsqrt(dx * dx + dy * dy);
+ ball = (body_t){mx - dx / l * 20, my - dy / l * 20, dx / l * speed, dy / l * speed};
+}
+static int goal_seg(int side_) {
+ for (int i = 0; i < nseg; i++)
+ if (seg[i].goal == side_) return i;
+ return -1;
+}
+static void players_away(void) { /* (out of the ball's way) */
+ for (int p = 0; p < np; p++) pl[p].b = (body_t){CX + (p - np / 2.0f) * 22, CY + 60, 0, 0};
+}
+
+void rules_soccer(void) {
+ app_test_round(M2, G_SOCCER);
+ CHECK(!corners && nseg == 8 && goal_seg(0) >= 0 && goal_seg(1) >= 0, "soccer: two players, a pitch and two goals");
+ CHECK(seg[goal_seg(0)].x0 < CX && pl[0].b.x < CX, "soccer: player 1's goal on the left, theirs the left half");
+ players_away();
+ shoot_at(goal_seg(0), 300);
+ run(0, 50);
+ CHECK(gone[0] && WINNER == 1, "soccer: a goal in yours, you're out: the other takes the round");
+
+ app_test_round(M2, G_SOCCER); /* a hit sends the ball */
+ ball = (body_t){CX, CY, 0, 0};
+ pl[0].b = (body_t){CX - 30, CY, 0, 0}, pl[0].a = 0;
+ app_test_step(1);
+ run(0, 30);
+ CHECK(ball.vx > 100, "soccer: a dash into the ball sends it flying");
+
+ app_test_round(M2V2, G_SOCCER); /* teams: the first goal */
+ players_away();
+ shoot_at(goal_seg(0), 300);
+ run(0, 50);
+ CHECK(WINNER == 1 && alive() == 4, "soccer: teams, a goal takes the round");
+
+ app_test_round(M4, G_SOCCER); /* four: a goal each, in the corners */
+ CHECK(corners && goal_seg(0) >= 0 && goal_seg(3) >= 0, "soccer: four players, four corner goals");
+ CHECK(seg[goal_seg(0)].x0 < CX && seg[goal_seg(0)].y1 < CY, "soccer: player 1's goal, top left");
+ players_away();
+ shoot_at(goal_seg(2), 300);
+ run(0, 50);
+ CHECK(gone[2] && WINNER == UNDECIDED, "soccer: a goal in yours, you're out, the others play on");
+ run(0, 150); /* the kick-off */
+ CHECK(fabs_(ball.x - CX) < 1 && fabs_(ball.y - CY) < 1, "soccer: the ball back in the middle");
+ players_away();
+ shoot_at(goal_seg(2), 300);
+ run(0, 60);
+ CHECK(past(&seg[goal_seg(2)], ball.x, ball.y) < 0 && alive() == 3, "soccer: the goal of one who's out is a wall");
+
+ app_test_round(M3, G_SOCCER); /* three: the bottom right corner is a wall */
+ players_away();
+ int corner = 3; /* (the fourth diagonal) */
+ for (int i = 0, k = 0; i < nseg; i++)
+ if (seg[i].x0 != seg[i].x1 && seg[i].y0 != seg[i].y1 && k++ == 1) corner = i;
+ shoot_at(corner, 300);
+ run(0, 60);
+ CHECK(alive() == 3 && WINNER == UNDECIDED, "soccer: three players, one corner is a wall");
+}
diff --git a/games/fourplayer/tests/rules/sumo.c b/games/fourplayer/tests/rules/sumo.c
new file mode 100644
index 00000000..8d648ebb
--- /dev/null
+++ b/games/fourplayer/tests/rules/sumo.c
@@ -0,0 +1,47 @@
+#include "../../src/g_sumo.c"
+#include "t.h"
+
+void rules_sumo(void) {
+ app_test_round(M2, G_SUMO);
+ float a0 = w[0].a;
+ run(0, 100); /* a second */
+ CHECK(fabs_(wrap_pi(w[0].a - a0 - SPIN)) < 0.05f, "sumo: the arrow spins 3.4 radians a second");
+ CHECK(alive() == 2 && WINNER == UNDECIDED, "sumo: nobody moves, nobody falls");
+
+ app_test_round(M2, G_SUMO);
+ w[0].b = (body_t){CX - 40, CY, 0, 0}, w[0].a = 0, w[1].b = (body_t){CX, CY + 60, 0, 0};
+ app_test_step(1);
+ CHECK(w[0].b.vx > 150 && fabs_(w[0].b.vy) < 20, "sumo: a tap dashes where the arrow points");
+ CHECK(w[0].spin < 0, "sumo: a dash turns the spin the other way");
+ float v = w[0].b.vx;
+ app_test_step(0), app_test_step(1); /* again at once: too soon */
+ CHECK(w[0].b.vx <= v, "sumo: no second dash within 300 ms");
+
+ app_test_round(M2, G_SUMO); /* one pushes the other off */
+ w[0].b = (body_t){CX - 20, CY, 250, 0}, w[1].b = (body_t){CX + 3, CY, 0, 0};
+ app_test_step(0);
+ CHECK(w[1].b.vx > 100, "sumo: a hit passes the push on");
+ run(0, 300);
+ CHECK(gone[1] && !gone[0] && WINNER == 0, "sumo: pushed into the sea, the other takes the round");
+
+ app_test_round(M3V3, G_SUMO); /* teams: the last team on the ice */
+ w[1].b = (body_t){CX + RING + 5, CY, 0, 0}, w[3].b = (body_t){CX - RING - 5, CY, 0, 0};
+ app_test_step(0);
+ CHECK(gone[1] && gone[3] && WINNER == UNDECIDED, "sumo: a team with one left plays on");
+ w[5].b = (body_t){CX, CY + RING + 5, 0, 0}, w[0].b = (body_t){CX, CY - RING - 5, 0, 0};
+ app_test_step(0);
+ CHECK(WINNER == 0, "sumo: the blue team all in the sea, the red team takes the round");
+
+ app_test_round(M2, G_SUMO); /* all at once: a draw */
+ w[0].b = (body_t){CX + RING + 5, CY, 0, 0}, w[1].b = (body_t){CX - RING - 5, CY, 0, 0};
+ app_test_step(0);
+ CHECK(WINNER == -1, "sumo: both in the sea at once, a draw");
+
+ app_test_round(M4, G_SUMO); /* the ice melts after 20 seconds */
+ for (int p = 0; p < 4; p++) w[p].b = (body_t){CX + (p - 1.5f) * 4, CY, 0, 0}, w[p].fall_t = -1;
+ for (int i = 0; i < 2500 && WINNER == UNDECIDED; i++) {
+ for (int p = 0; p < 4; p++) w[p].b.vx = w[p].b.vy = 0;
+ app_test_step(0);
+ }
+ CHECK(ring < RING - 10, "sumo: the ice melts after 20 seconds");
+}
diff --git a/games/fourplayer/tests/rules/t.h b/games/fourplayer/tests/rules/t.h
new file mode 100644
index 00000000..62eefdc1
--- /dev/null
+++ b/games/fourplayer/tests/rules/t.h
@@ -0,0 +1,23 @@
+/* The rules' tests: each file takes one minigame's source in, to look inside it (tests/rules/main.c runs them). */
+#ifndef FP_T_H
+#define FP_T_H
+#include <stdio.h>
+#include "../../src/app.h"
+#include "../../src/fp.h"
+
+extern int t_failed, t_passed;
+#define CHECK(c, what) \
+ do { \
+ if (c) t_passed++; \
+ else t_failed++, printf("FAIL %s:%d %s\n", __FILE__, __LINE__, what); \
+ } while (0)
+enum { M2, M3, M4, M2V2, M3V3 };
+/* steps with the keys held (a bit a player) until the round is decided or n steps; the steps taken */
+static inline int run(unsigned held, int n) {
+ int i = 0;
+ while (i < n && app_test_winner() == -2) app_test_step(held), i++;
+ return i;
+}
+#define WINNER app_test_winner()
+#define UNDECIDED (-2)
+#endif
diff --git a/games/fourplayer/tests/rules/tanks.c b/games/fourplayer/tests/rules/tanks.c
new file mode 100644
index 00000000..1e38c79d
--- /dev/null
+++ b/games/fourplayer/tests/rules/tanks.c
@@ -0,0 +1,68 @@
+#include "../../src/g_tanks.c"
+#include "t.h"
+
+static int shells(int p) {
+ int n = 0;
+ for (int i = 0; i < NSH; i++) n += sh[i].own == p;
+ return n;
+}
+static void clear_map(void) { nmap = 0; }
+
+void rules_tanks(void) {
+ app_test_round(M2, G_TANKS);
+ clear_map();
+ float a = t[0].a, x = t[0].b.x;
+ app_test_step(0);
+ CHECK(fabs_(wrap_pi(t[0].a - a - TURN * DT)) < 0.001f && t[0].b.x == x, "tanks: alone, a tank turns on the spot");
+ a = t[0].a;
+ run(1, 50);
+ CHECK(t[0].a == a && fabs_(t[0].b.x - x) > 10, "tanks: held, it drives straight");
+ CHECK(shells(0) == 0, "tanks: no shell while driving");
+ app_test_step(0);
+ CHECK(shells(0) == 1, "tanks: let go, it fires");
+ app_test_step(1), app_test_step(0);
+ CHECK(shells(0) == 1, "tanks: 400 ms between shells");
+
+ app_test_round(M2, G_TANKS); /* a hit */
+ clear_map();
+ t[0].b = (body_t){100, 131, 0, 0}, t[0].a = 0, t[1].b = (body_t){200, 131, 0, 0};
+ app_test_step(1), app_test_step(0);
+ run(0, 200);
+ CHECK(gone[1] && WINNER == 0, "tanks: one hit, out, and the last tank takes the round");
+
+ app_test_round(M2V2, G_TANKS); /* teams: through your own */
+ clear_map();
+ t[0].b = (body_t){60, 131, 0, 0}, t[0].a = 0, t[2].b = (body_t){120, 131, 0, 0}, t[1].b = (body_t){220, 131, 0, 0};
+ t[3].b = (body_t){160, 60, 0, 0};
+ app_test_step(1), app_test_step(0);
+ for (int i = 0; i < 200 && !gone[1]; i++) {
+ t[2].b = (body_t){120, 131, 0, 0}; /* (the teammate stays in the way) */
+ app_test_step(0);
+ }
+ CHECK(!gone[2] && gone[1], "tanks: shells pass through your team");
+
+ app_test_round(M2, G_TANKS); /* bounces, then gone: in a corridor 60 pixels wide */
+ static const rect_t CORR[2] = {{100, 50, 20, 150}, {180, 50, 20, 150}};
+ map = CORR, nmap = 2;
+ t[0].b = (body_t){150, 131, 0, 0}, t[0].a = 0, t[1].b = (body_t){40, 40, 0, 0};
+ app_test_step(1), app_test_step(0);
+ int i = 0, bounced = 0;
+ for (; i < 300 && shells(0); i++) {
+ t[0].b = (body_t){150, 60, 0, 0}; /* (out of the way) */
+ for (int k = 0; k < NSH; k++)
+ if (sh[k].own == 0) bounced = sh[k].hits;
+ app_test_step(0);
+ }
+ CHECK(!shells(0) && bounced == 3 && i > 100, "tanks: a shell bounces three times, then it's gone");
+ app_test_round(M2, G_TANKS);
+ clear_map();
+ t[0].b = (body_t){290, 131, 0, 0}, t[0].a = 0, t[1].b = (body_t){40, 40, 0, 0};
+ app_test_step(1), app_test_step(0);
+ run(0, 100);
+ CHECK(gone[0] && WINNER == 1, "tanks: your own shell can hit you");
+
+ app_test_round(M2, G_TANKS); /* the walls */
+ t[0].b = (body_t){map[0].x + map[0].w / 2.0f, map[0].y + map[0].h / 2.0f, 0, 0};
+ app_test_step(0);
+ CHECK(!solid(t[0].b.x, t[0].b.y), "tanks: a tank never stays in a wall");
+}