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.

Imposter: tests, screenshots and its README

Mason363b55d06b77430
diff --git a/games/imposter/README.md b/games/imposter/README.md
new file mode 100644
index 00000000..a2796032
--- /dev/null
+++ b/games/imposter/README.md
@@ -0,0 +1,80 @@
+> **Imposter is a [NumPlay](../../README.md) game that comes on its own.** Get `Imposter.nwa` from the [latest release](https://github.com/Mason363/NumPlay/releases/latest).
+
+<p align="center">
+ <img src="src/icon.png" width="84" alt="Imposter icon">
+</p>
+
+<h1 align="center">Imposter</h1>
+
+<p align="center">
+ <b>The party game played by passing one calculator around, on the NumWorks calculator.</b><br>
+ 3 to 12 players, 30 categories, 1695 words.
+</p>
+
+<p align="center">
+ <a href="https://github.com/Mason363/NumPlay/releases/latest/download/Imposter.nwa"><b>Download Imposter.nwa</b></a>
+ &nbsp;·&nbsp; <a href="#controls">Controls</a>
+</p>
+
+<p align="center">
+ <img src="docs/shot.png" width="640" alt="A player's secret word, Giraffe, in the Animals category">
+</p>
+
+## Highlights
+
+* **Everyone by name:** type the players' names with the letters printed on the keys, or pick the letters on the screen.
+* **Passed around:** each player sees their role alone, then hides it. The others get the secret word and its category, the imposter a red screen, with the category as a hint if you want.
+* **Up to 3 imposters:** 2 from 5 players, 3 from 7.
+* **30 categories, 1695 words:** food, animals, countries, cities, jobs, sports, space, fantasy and more. Play them all or tick a few. No word comes back before its category runs out.
+* **Who starts:** the calculator picks who says the first word, and keeps a timer if you want one.
+* **Saved:** the names, the settings and the words already played. Home saves and quits, even in the middle of a round. It's also copied into `imposter_saves.py`, so installing apps again doesn't erase it.
+
+<table>
+ <tr>
+ <td align="center"><img src="docs/setup.png" width="390" alt="The settings: players, categories, imposters, the hint and the timer"><br><sub>The settings</sub></td>
+ <td align="center"><img src="docs/names.png" width="390" alt="A name typed on the screen's letters, laid out as on the keys"><br><sub>A player's name</sub></td>
+ </tr>
+ <tr>
+ <td align="center"><img src="docs/categories.png" width="390" alt="The categories, three of them unticked"><br><sub>The categories</sub></td>
+ <td align="center"><img src="docs/pass.png" width="390" alt="Pass to Ana"><br><sub>Pass it on</sub></td>
+ </tr>
+ <tr>
+ <td align="center"><img src="docs/imposter.png" width="390" alt="You are the Imposter, with the category as a hint"><br><sub>The imposter</sub></td>
+ <td align="center"><img src="docs/reveal.png" width="390" alt="The word was Giraffe, the imposter was Chris"><br><sub>The reveal</sub></td>
+ </tr>
+</table>
+
+## How to play
+
+Everyone gets the same secret word, except the imposter. Pass the calculator around. Each player presses OK to see their role alone, then OK again to hide it before passing it on.
+
+Then the calculator says who starts. In turn, everyone says one word about the secret, enough to show they know it but not enough for the imposter to guess it. Then vote out loud for the imposter, and reveal. Caught, the imposter loses. Not caught, the imposter wins. OK starts the next round, with a new word and new imposters.
+
+## Controls
+
+| Key | What it does |
+| --- | --- |
+| Arrows | Choose |
+| OK | Choose, see your role, hide it |
+| Left and Right | Change a setting |
+| The letter keys | Type a name (alpha or not) |
+| ⌫ | Erase a letter, remove a player |
+| EXE | Done with a name |
+| Back | Go back. In a round, the menu (Resume, New round, Settings, Quit game) |
+| Home | Save and quit |
+
+## Good to know
+
+* **On its own:** NumPlay is full, so Imposter is an app of its own.
+* **Categories:** with none ticked, every category is played.
+* **The words:** everyday words a teenager knows, nothing from a brand or a show.
+
+## Credits
+
+Inspired by the party game Imposter. Made by Mason Chen as part of NumPlay.
+
+* The text uses NumPlay's **Nunito** font (SIL Open Font License). The words and the pictures are the game's own.
+
+## License
+
+The game's code is licensed under the GNU General Public License v3.0: see the `LICENSE` file at the root of the repository.
diff --git a/games/imposter/docs/categories.png b/games/imposter/docs/categories.png
new file mode 100644
index 00000000..aa8acaed
Binary files /dev/null and b/games/imposter/docs/categories.png differ
diff --git a/games/imposter/docs/imposter.png b/games/imposter/docs/imposter.png
new file mode 100644
index 00000000..1b872e2c
Binary files /dev/null and b/games/imposter/docs/imposter.png differ
diff --git a/games/imposter/docs/names.png b/games/imposter/docs/names.png
new file mode 100644
index 00000000..22834ab1
Binary files /dev/null and b/games/imposter/docs/names.png differ
diff --git a/games/imposter/docs/pass.png b/games/imposter/docs/pass.png
new file mode 100644
index 00000000..fc53db4d
Binary files /dev/null and b/games/imposter/docs/pass.png differ
diff --git a/games/imposter/docs/reveal.png b/games/imposter/docs/reveal.png
new file mode 100644
index 00000000..9e361f91
Binary files /dev/null and b/games/imposter/docs/reveal.png differ
diff --git a/games/imposter/docs/setup.png b/games/imposter/docs/setup.png
new file mode 100644
index 00000000..84f8fd3a
Binary files /dev/null and b/games/imposter/docs/setup.png differ
diff --git a/games/imposter/docs/shot.png b/games/imposter/docs/shot.png
new file mode 100644
index 00000000..10f9f3cb
Binary files /dev/null and b/games/imposter/docs/shot.png differ
diff --git a/games/imposter/src/game.c b/games/imposter/src/game.c
index 85e3ad46..30bb7e5a 100644
--- a/games/imposter/src/game.c
+++ b/games/imposter/src/game.c
@@ -14,6 +14,12 @@ const uint8_t timer_minutes[N_TIMERS] = {0, 1, 2, 3, 5};
/* ---- chance */
static uint32_t seed = 1;
void rnd_seed(uint32_t s) { seed = s ? s : 0x9E3779B9u; }
+/* the moment a key is pressed adds to the chance */
+void rnd_stir(uint32_t v) {
+ seed ^= v * 2654435761u;
+ if (!seed) seed = 1;
+ rnd();
+}
uint32_t rnd(void) {
seed ^= seed << 13;
seed ^= seed >> 17;
@@ -52,22 +58,21 @@ int word_text(uint16_t w, char *out) {
}
/* a word of the chosen categories not played yet, every one as likely; a category played through starts again
- * (without the word just played) */
+ * (but not with the word just played) */
static uint16_t pick_word(void) {
int total = 0;
for (int c = 0; c < N_CATS; c++) {
if (!cat_on(c)) continue;
- if (!unused_count(c)) {
- memset(sv.used + c * 8, 0, 8);
- if (sv.last != NO_WORD && sv.last >> 6 == c) use(c, sv.last & 63);
- }
+ if (!unused_count(c)) memset(sv.used + c * 8, 0, 8);
total += unused_count(c);
}
+ uint16_t skip = NO_WORD;
+ if (sv.last != NO_WORD && cat_on(sv.last >> 6) && !used(sv.last >> 6, sv.last & 63) && total > 1) skip = sv.last, total--;
int r = rnd_below(total);
for (int c = 0; c < N_CATS; c++) {
if (!cat_on(c)) continue;
for (int i = 0, k = word_count(c); i < k; i++)
- if (!used(c, i) && r-- == 0) {
+ if (!used(c, i) && (uint16_t)(c << 6 | i) != skip && r-- == 0) {
use(c, i);
return (uint16_t)(c << 6 | i);
}
diff --git a/games/imposter/src/game.h b/games/imposter/src/game.h
index f14533f9..1cf0b11a 100644
--- a/games/imposter/src/game.h
+++ b/games/imposter/src/game.h
@@ -47,4 +47,5 @@ void round_new(void);
void round_end(void);
uint32_t rnd(void);
void rnd_seed(uint32_t s);
+void rnd_stir(uint32_t v);
#endif
diff --git a/games/imposter/src/ui.c b/games/imposter/src/ui.c
index 59a27363..f447b760 100644
--- a/games/imposter/src/ui.c
+++ b/games/imposter/src/ui.c
@@ -686,11 +686,11 @@ static void draw_reveal(void) {
}
gfx_text_center(&np_font_body, 160, 154, k > 1 ? "The imposters were" : "The imposter was", SOFT, 256);
big_text(who, 160, 186, 296, 50, 560, RED_TEXT);
- KEYS("OK", "next round", "Back", "setup");
+ KEYS("OK", "next round", "Back", "settings");
}
/* ------------------------------------------------------------ dialogs */
-static const char *const pause_items[] = {"Resume", "New round", "Setup", "Quit game"};
+static const char *const pause_items[] = {"Resume", "New round", "Settings", "Quit game"};
static const char *const quit_items[] = {"Quit", "Cancel"};
static const char *const reveal_items[] = {"Reveal", "Not yet"};
static const char *const remove_items[] = {"Remove", "Cancel"};
@@ -831,6 +831,7 @@ bool app_step(uint32_t keys, uint32_t letters, uint32_t now) {
hit &= ~(uint32_t)KB_ANY;
if (!hit && !lhit) return true;
u.dirty = true;
+ rnd_stir(now);
if (u.dialog) return dialog_key(hit);
switch (u.screen) {
case SC_HELP: return help_key(hit);
diff --git a/games/imposter/tests/check.py b/games/imposter/tests/check.py
new file mode 100644
index 00000000..7da22e39
--- /dev/null
+++ b/games/imposter/tests/check.py
@@ -0,0 +1,310 @@
+#!/usr/bin/env python3
+"""Imposter played on a computer (build/play, with the sanitizers): the word lists, rounds from the setup to the
+reveal with every role on the right player's screen, words never twice until their category runs out, saves that
+come back and survive broken files, random keys.
+
+ python3 tests/check.py build/play [--fuzz N]"""
+import os
+import random
+import re
+import shutil
+import subprocess
+import sys
+import tempfile
+from concurrent.futures import ThreadPoolExecutor
+
+PLAY = os.path.abspath(sys.argv[1] if len(sys.argv) > 1 and not sys.argv[1].startswith("-") else "build/play")
+FUZZ = int(sys.argv[sys.argv.index("--fuzz") + 1]) if "--fuzz" in sys.argv else 300
+WORK = tempfile.mkdtemp(prefix="imposter-check-")
+SAVES = os.path.join(WORK, "saves")
+SHOTS = os.path.join(WORK, "shots")
+os.makedirs(SHOTS)
+failed = []
+
+
+def check(ok, what):
+ print(("ok " if ok else "FAIL ") + what)
+ if not ok:
+ failed.append(what)
+
+
+def run(args, saves=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 "ERROR" in out 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 fresh(saves=SAVES):
+ shutil.rmtree(saves, ignore_errors=True)
+ os.makedirs(saves)
+
+
+def walk(steps, extra=(), saves=SAVES):
+ """Keys pressed 2 frames each, 6 frames apart: a key name, 'wN' waits N frames, 'shot:NAME' saves the screen.
+ Returns the output and {NAME: picture path}."""
+ f, keys, shots = 10, [], []
+ for s in steps.split():
+ if s.startswith("w") and s[1:].isdigit():
+ f += int(s[1:])
+ elif s.startswith("shot:"):
+ shots.append("%d:%s.ppm" % (f, s[5:]))
+ f += 1
+ else:
+ keys.append("%d-%d:%s" % (f, f + 1, s))
+ f += 6
+ args = ["--trace", "--frames", f + 10, "--keys", ",".join(keys)]
+ if shots:
+ args += ["--shots", ",".join(shots)]
+ out = run(args + list(extra), saves)
+ return out, {s.split(":")[1][:-4]: os.path.join(SHOTS, s.split(":")[1]) for s in shots}
+
+
+def state(out):
+ m = re.findall(r"^STATE n (\d+) names (.*?) imposters (\d+) hint (\d+) timer (\d+) cats (\d+) phase (\d+) turn (\d+) "
+ r"rounds (\d+) word (\d+) flags (\d+)$", out, re.M)
+ if not m:
+ return None
+ n, names, imps, hint, timer, cats, phase, turn, rounds, word, flags = m[-1]
+ return dict(n=int(n), names=names.split(",") if names else [], imposters=int(imps), hint=int(hint),
+ timer=int(timer), cats=int(cats), phase=int(phase), turn=int(turn), rounds=int(rounds), word=int(word))
+
+
+def pixel(path, x, y):
+ with open(path, "rb") as f:
+ data = f.read()
+ head = data.split(b"\n", 3)
+ w = int(head[1].split()[0])
+ px = head[3]
+ i = (y * w + x) * 3
+ return px[i], px[i + 1], px[i + 2]
+
+
+def typed(name):
+ return " ".join("space" if c == " " else c.lower() for c in name)
+
+
+# ---- the words
+out = subprocess.run([PLAY, "--words"], capture_output=True, text=True).stdout
+m = re.search(r"WORDS cats (\d+) total (\d+) min (\d+) max (\d+) bad (\d+)", out)
+check(m and int(m.group(1)) >= 25 and int(m.group(3)) >= 40 and int(m.group(4)) <= 64 and m.group(5) == "0",
+ "words: %s" % (m.group(0) if m else out[-300:]))
+CATS = dict((name, int(n)) for name, n in re.findall(r"^CAT (.+) (\d+)$", out, re.M))
+TOTAL = int(m.group(2)) if m else 0
+
+
+def check_round(out, names, imposters, hint, what):
+ """Every pass screen names the next player in order; the role after it is theirs; the talk and the reveal
+ agree."""
+ out = out[out.index("DRAW pass"):] if "DRAW pass" in out else out # (from the round's first screen)
+ draws = [l for l in out.splitlines() if l.startswith("DRAW ")]
+ seq = [l for l in draws if l.startswith(("DRAW pass", "DRAW role", "DRAW talk", "DRAW reveal"))]
+ passes = [re.match(r"DRAW pass dialog 0 turn (\d+) name (.+)$", l) for l in seq if l.startswith("DRAW pass")]
+ roles = [l for l in seq if l.startswith("DRAW role")]
+ order = []
+ for p in passes:
+ if p and (not order or order[-1] != (int(p.group(1)), p.group(2))):
+ order.append((int(p.group(1)), p.group(2)))
+ check(order == list(enumerate(names)), "%s: the calculator passed to %s" % (what, ", ".join(n for _, n in order)))
+ word = cat = None
+ seen_imps = set()
+ ok = len(roles) >= len(names)
+ for line in roles:
+ m1 = re.match(r"DRAW role dialog 0 turn (\d+) imposter hint (.+)$", line)
+ m2 = re.match(r"DRAW role dialog 0 turn (\d+) word (.+) cat (.+)$", line)
+ if m1:
+ seen_imps.add(int(m1.group(1)))
+ elif m2:
+ word, cat = (m2.group(2), m2.group(3)) if word is None else (word, cat)
+ ok &= (m2.group(2), m2.group(3)) == (word, cat)
+ else:
+ ok = False
+ for line in roles:
+ m1 = re.match(r"DRAW role dialog 0 turn (\d+) imposter hint (.+)$", line)
+ if m1:
+ ok &= m1.group(2) == (cat if hint else "-")
+ check(ok and len(seen_imps) == imposters,
+ "%s: %d imposters (%s) saw their screen%s, the others %s (%s)" %
+ (what, len(seen_imps), ", ".join(names[i] for i in sorted(seen_imps)),
+ " with the category" if hint else " without a hint", word, cat))
+ talk = re.search(r"DRAW talk dialog 0 starter (\d+) (.+) left", out)
+ check(talk and names[int(talk.group(1))] == talk.group(2), "%s: %s starts" % (what, talk.group(2) if talk else "?"))
+ rev = re.search(r"DRAW reveal dialog 0 word (.+) imps (\d+)$", out, re.M)
+ mask = sum(1 << i for i in seen_imps)
+ check(rev and rev.group(1) == word and int(rev.group(2)) == mask,
+ "%s: the reveal shows %s and the imposters" % (what, rev.group(1) if rev else "?"))
+ return word, seen_imps
+
+
+# ---- a first game: the guide, names typed with the letter keys, the settings, a whole round
+fresh()
+NAMES = ["Ana", "Ben", "Chris", "Dana", "Mary Jo"]
+steps = "ok ok ok " + " exe ".join(typed(n) for n in NAMES) + " exe back back"
+steps += " down down down right right down shot:setup up up up up ok"
+for i in range(len(NAMES)):
+ steps += " w4 shot:pass%d ok w32 shot:role%d ok w32" % (i, i)
+steps += " shot:talk ok shot:ask ok shot:reveal"
+out, pics = walk(steps)
+draws = [l for l in out.splitlines() if l.startswith("DRAW")]
+check(draws and draws[0].startswith("DRAW help"), "the first start shows How to play")
+st = state(out)
+check(st and st["names"] == NAMES, "names typed with the letter keys: %s" % (st and st["names"]))
+check(st and st["imposters"] == 2 and st["hint"] == 1, "two imposters, the category as a hint")
+word, imps = check_round(out, NAMES, 2, True, "round 1")
+colours_ok = True
+for i in range(len(NAMES)):
+ r, g, b = pixel(pics["role%d" % i], 4, 120)
+ colours_ok &= (r > 150 and g < 100) if i in imps else (b > 150 and r < 100)
+check(colours_ok, "imposter screens red, the others blue")
+r, g, b = pixel(pics["pass0"], 4, 120)
+check(b > r and r < 100, "the pass screen keeps the secret (the game's own colours)")
+
+# the next round: a new word, new imposters drawn
+out, _ = walk("ok " + " ".join(["ok w32 ok w32"] * len(NAMES)) + " ok ok")
+word2, _ = check_round(out, NAMES, 2, True, "round 2")
+check(word2 != word, "round 2 has a new word: %s" % word2)
+
+# a session later: the same players and settings
+out = run(["--frames", 5])
+st2 = state(out)
+check(st2 and st2["names"] == NAMES and st2["imposters"] == 2 and st2["rounds"] == 2,
+ "the save comes back: %s, %d imposters, round %d" % (st2 and ",".join(st2["names"]), st2 and st2["imposters"],
+ st2 and st2["rounds"]))
+
+# ---- names on the screen's letters (another target's keys carry none), a name already taken, removing one
+fresh()
+run(["--players", "Ana,Ben", "--frames", 2])
+# B: right; O: down down right (rows keep the column as near as they can); then down to Done
+out, _ = walk("ok ok right ok down down right ok z down down down ok back back", ["--no-typing"])
+st = state(out)
+check(st and st["names"] == ["Ana", "Ben", "Bo"], "a name picked on the screen's letters: %s" % (st and st["names"]))
+out, _ = walk("down ok down down down ok " + typed("ben") + " exe w2 back back")
+st = state(out)
+check("DRAW name dialog 0 typed Ben" in out and st and st["names"] == ["Ana", "Ben", "Bo"],
+ "a name already playing is refused: %s" % (st and st["names"]))
+out, _ = walk("down ok down erase ok back")
+st = state(out)
+check(st and st["names"] == ["Ana", "Bo"], "a player removed after the question: %s" % (st and st["names"]))
+out, _ = walk("ok")
+check(out.strip().splitlines()[-2].startswith("DRAW players"), "Start with 2 players opens Players")
+
+# ---- the rules: a word comes again only once its category has run out
+PLAYERS = "Ann,Bob,Cal,Dee"
+fresh()
+out = run(["--players", PLAYERS, "--rounds", 2 * TOTAL])
+rounds = [(int(w), int(c)) for w, c in re.findall(r"^ROUND \d+ word (\d+) .* cat (\d+) ", out, re.M)]
+sizes = list(CATS.values())
+played = [set() for _ in sizes]
+early = 0
+for w, c in rounds:
+ if w in played[c]:
+ early += len(played[c]) < sizes[c]
+ played[c] = set()
+ played[c].add(w)
+first = len({w for w, _ in rounds[:TOTAL * 9 // 10]})
+check(len(rounds) == 2 * TOTAL and early == 0,
+ "%d rounds with every category: no word again before its category ran out (the first %d all different)" %
+ (len(rounds), first))
+idx = list(CATS).index("The sea")
+n = CATS["The sea"]
+fresh()
+out = run(["--players", PLAYERS, "--cats", 1 << idx, "--rounds", 20])
+out2 = run(["--rounds", n - 20])
+w1 = re.findall(r"^ROUND \d+ word (\d+) \S.* cat (\d+) ", out + out2, re.M)
+check(len(w1) == n and len(set(w for w, _ in w1)) == n and {c for _, c in w1} == {str(idx)},
+ "The sea over two sessions: its %d words once each" % n)
+out3 = run(["--rounds", n])
+w2 = re.findall(r"^ROUND \d+ word (\d+) ", out3, re.M)
+check(len(set(w2)) == n and w2[0] != w1[-1][0], "The sea run out: all its words again, not the last one first")
+mask = (1 << 3) | (1 << 17)
+fresh()
+out = run(["--players", PLAYERS, "--cats", mask, "--rounds", 80])
+cats = set(int(c) for c in re.findall(r"^ROUND \d+ word \d+ \S.* cat (\d+) ", out, re.M))
+check(cats == {3, 17}, "only the chosen categories: %s" % sorted(cats))
+for players, setting, want in [("Ann,Bob,Cal", 3, 1), ("Ann,Bob,Cal,Dee,Eve", 3, 2), ("A,B,C,D,E,F,G,H,I,J,K,L", 3, 3),
+ ("Ann,Bob,Cal,Dee,Eve,Fay,Gus", 2, 2)]:
+ fresh()
+ out = run(["--players", players, "--imposters", setting, "--rounds", 300])
+ masks = [int(x) for x in re.findall(r"imps (\d+) starter", out)]
+ starters = [int(x) for x in re.findall(r"starter (\d+)$", out, re.M)]
+ k = len(players.split(","))
+ counts = [sum(m >> p & 1 for m in masks) for p in range(k)]
+ exp = 300 * want / k
+ check(all(bin(m).count("1") == want for m in masks) and min(counts) > exp * 0.6 and max(counts) < exp * 1.4 and
+ set(starters) == set(range(k)),
+ "%d players, %d imposters asked: %d each round, everyone in turn (%d to %d times)" %
+ (k, setting, want, min(counts), max(counts)))
+
+# ---- a round goes on after Home
+fresh()
+run(["--players", ",".join(NAMES), "--frames", 2])
+out, _ = walk("ok w2 ok w32 ok w32 ok w32 ok w32 home")
+st = state(out)
+out = run(["--trace", "--frames", 5])
+st2 = state(out)
+check(st and st["phase"] == 1 and st["turn"] == 2 and st2 and st2["word"] == st["word"] and
+ "DRAW pass dialog 0 turn 2 name Chris" in out, "Home in a round, then back: Chris is next, the same word")
+
+# ---- broken files
+SAV, COPY = os.path.join(SAVES, "imposter.sav"), os.path.join(SAVES, "imposter_saves.py")
+fresh()
+run(["--players", ",".join(NAMES), "--imposters", 2, "--frames", 2])
+good_sav, good_copy = open(SAV, "rb").read(), open(COPY, "rb").read()
+check(good_copy.startswith(b"\0# Imposter's saves: keep this file\n#>imposter.sav:"), "the copy is a Python script")
+
+
+def with_files(sav, copy, what, want_names):
+ fresh()
+ for path, data in ((SAV, sav), (COPY, copy)):
+ if data is not None:
+ open(path, "wb").write(data)
+ out = run(["--frames", 3])
+ st = state(out)
+ check(st is not None and st["names"] == want_names, "%s: %s" % (what, st and (st["names"] or "a new game")))
+
+
+with_files(good_sav[:10], good_copy, "a cut save: back from the copy", NAMES)
+with_files(b"", good_copy, "an empty save: back from the copy", NAMES)
+with_files(None, good_copy, "no save, the copy: back", NAMES)
+with_files(good_sav, None, "the save without its copy", NAMES)
+with_files(bytes(len(good_sav)), b"junk", "both broken: a new game", [])
+with_files(None, good_copy[:60] + b"!!!!" + good_copy[64:], "no save, a broken copy: a new game", [])
+rng = random.Random(7)
+bad = 0
+for k in range(150):
+ sav = bytearray(good_sav)
+ for _ in range(rng.choice([1, 2, 5, 40])):
+ sav[rng.randrange(5, len(sav))] = rng.randrange(256)
+ fresh()
+ open(SAV, "wb").write(bytes(sav))
+ out = run(["--fuzz", k, "--frames", 300])
+ bad += "STATE" not in out
+check(bad == 0 and not [f for f in failed if "crashed" in f], "150 damaged saves: each loads into a game it could be")
+
+# ---- random keys
+def fuzz(k):
+ d = os.path.join(WORK, "fuzz%d" % k)
+ os.makedirs(d, exist_ok=True)
+ if k % 3 == 0: # players already there: rounds sooner
+ subprocess.run([PLAY, "--saves", d, "--players", ",".join(NAMES[: 3 + k % 6]), "--timer", str(k % 5),
+ "--frames", "1"], capture_output=True)
+ return run(["--fuzz", k, "--frames", 3000], saves=d)
+
+
+with ThreadPoolExecutor(max_workers=os.cpu_count() or 4) as ex:
+ outs = list(ex.map(fuzz, range(FUZZ)))
+sessions = sum(int(m) for o in outs for m in re.findall(r"FUZZ sessions (\d+)", o))
+reached = sum("phase 3" in o or "phase 2" in o or "phase 1" in o for o in outs)
+check(not [f for f in failed if "crashed" in f], "%d random-key runs (%d sessions, %d with rounds): no crash" %
+ (FUZZ, sessions, reached))
+
+shutil.rmtree(WORK, ignore_errors=True)
+if failed:
+ print("\n%d FAILED" % len(failed))
+ for f in failed:
+ print(" " + f[:2000])
+ sys.exit(1)
+print("\nall passed")
diff --git a/games/imposter/tests/play.c b/games/imposter/tests/play.c
index dbbba39d..dd6702e0 100644
--- a/games/imposter/tests/play.c
+++ b/games/imposter/tests/play.c
@@ -2,11 +2,13 @@
*
* play [--frames N] [--keys "10:ok,20-30:down,..."] [--shots "40:setup.ppm,..."] [--out DIR] [--saves DIR]
* [--trace] [--no-typing] [--fuzz SEED] [--seed N] [--words] [--rounds N]
+ * [--players Ana,Ben,...] [--cats MASK] [--imposters N] [--hint 0|1] [--timer N] [--round WORD]
*
* A frame is 20 ms. Keys: left right up down ok exe back home erase, a to z (the letter keys), space. "a-b:key"
* holds the key from frame a to b, "a:key" for one frame. --trace prints what each drawing shows (DRAW ...).
* --fuzz presses random keys (Home now and then ends a session, the next one starts from the save).
- * --words checks the word lists; --rounds plays N rounds' draws straight away (ROUND ...).
+ * --words checks the word lists; --rounds plays N rounds' draws straight away (ROUND ...). --players and the
+ * settings after it change the save before the game starts.
* At the end: STATE ... (the save). */
#include <stdio.h>
#include <stdlib.h>
@@ -136,7 +138,9 @@ static int check_words(void) {
}
int main(int argc, char **argv) {
- int frames = 600, fuzz = -1, rounds = 0;
+ int frames = 600, fuzz = -1, rounds = 0, imposters = -1, hint = -1, timer = -1, word = -1;
+ const char *players = NULL;
+ long cats = -1;
uint32_t seed = 1;
char saves[512] = "build/host-saves";
for (int i = 1; i < argc; i++) {
@@ -151,6 +155,12 @@ int main(int argc, char **argv) {
else if (!strcmp(argv[i], "--seed") && i + 1 < argc) seed = (uint32_t)strtoul(argv[++i], 0, 10);
else if (!strcmp(argv[i], "--words")) return check_words();
else if (!strcmp(argv[i], "--rounds") && i + 1 < argc) rounds = atoi(argv[++i]);
+ else if (!strcmp(argv[i], "--players") && i + 1 < argc) players = argv[++i];
+ else if (!strcmp(argv[i], "--cats") && i + 1 < argc) cats = strtol(argv[++i], 0, 0);
+ else if (!strcmp(argv[i], "--imposters") && i + 1 < argc) imposters = atoi(argv[++i]);
+ else if (!strcmp(argv[i], "--hint") && i + 1 < argc) hint = atoi(argv[++i]);
+ else if (!strcmp(argv[i], "--timer") && i + 1 < argc) timer = atoi(argv[++i]);
+ else if (!strcmp(argv[i], "--round") && i + 1 < argc) word = atoi(argv[++i]);
else {
fprintf(stderr, "unknown option %s\n", argv[i]);
return 2;
@@ -160,6 +170,31 @@ int main(int argc, char **argv) {
host_seed = seed ? seed : 1;
host_time = 1000;
app_init(host_time);
+ invariants(-1); /* (whatever the files held) */
+ if (players) { /* (a new game's players: no round on) */
+ char *buf = strdup(players);
+ sv.nplayers = 0, sv.phase = PH_NONE;
+ memset(sv.names, 0, sizeof sv.names);
+ for (char *t = strtok(buf, ","); t && sv.nplayers < MAX_PLAYERS; t = strtok(NULL, ","))
+ snprintf(sv.names[sv.nplayers++], NAME_MAX + 1, "%s", t);
+ free(buf);
+ }
+ if (cats >= 0) sv.cats = (uint32_t)cats;
+ if (imposters >= 0) sv.imposters = (uint8_t)imposters;
+ if (hint >= 0) sv.hint = (uint8_t)hint;
+ if (timer >= 0) sv.timer = (uint8_t)timer;
+ if (players || cats >= 0 || imposters >= 0 || hint >= 0 || timer >= 0) {
+ sv.flags |= 1;
+ game_save();
+ app_init(host_time); /* (from that save) */
+ invariants(-1);
+ }
+ if (word >= 0) { /* a round with this word (screenshots) */
+ round_new();
+ sv.word = sv.last = (uint16_t)word;
+ game_save();
+ app_init(host_time);
+ }
if (rounds) { /* the rules alone: the save's players and categories */
for (int r = 0; r < rounds; r++) {
round_new();
@@ -191,6 +226,7 @@ int main(int argc, char **argv) {
}
k |= fkeys, l |= fletters;
}
+ if (!host_typing) l = 0; /* (the keys carry no letters there) */
host_keys = k, host_letters = l;
if (!app_step(k, l, host_time)) {
printf("QUIT frame %d\n", f);