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.

Chess saves the puzzle rating, streak, and last bot and clock

Mason36367b6811864ba
diff --git a/games/chess/src/main.c b/games/chess/src/main.c
index 51afde58..ba2fda24 100644
--- a/games/chess/src/main.c
+++ b/games/chess/src/main.c
@@ -1,6 +1,7 @@
#include <eadk.h>
#include <string.h>
#include "../../common/epsilon_app.h"
+#include "../../common/epsilon_files.h"
#include "../../common/jump.h"
#include "chess.h"
#include "sprites.h"
@@ -757,6 +758,33 @@ void ui_tick(void) {
}
}
+/* -------------------------------------------------------------- Saving */
+
+/* The puzzle rating and streak, and the last bot, side and clock. */
+static const char SAVE_NAME[] = "numchess.sav";
+
+static void save(void) {
+ uint8_t s[8] = {'C', 1, (uint8_t)prating, (uint8_t)(prating >> 8), (uint8_t)pstreak, (uint8_t)(pstreak >> 8),
+ (uint8_t)bot, (uint8_t)(pside | tc << 2)};
+ ef_write(SAVE_NAME, s, sizeof s);
+}
+
+static void load(void) {
+ uint32_t n;
+ const uint8_t *s = ef_read(SAVE_NAME, &n);
+ if (!s || n != 8 || s[0] != 'C' || s[1] != 1) return;
+ int r = s[2] | s[3] << 8;
+ if (r >= 100 && r <= 4000) prating = r, pstreak = s[4] | s[5] << 8;
+ if (s[6] < NBOTS) bot = s[6];
+ if ((s[7] & 3) < 3) pside = s[7] & 3;
+ tc = s[7] >> 2 & 7;
+}
+
+static int quit(void) {
+ save();
+ return np_app_end();
+}
+
static void rate(int won) {
int e = 16 + (prating - PZ.rating) / 25;
e = e < 2 ? 2 : e > 30 ? 30 : e;
@@ -764,6 +792,7 @@ static void rate(int won) {
prating += pdelta;
if (prating < 100) prating = 100;
pstreak = won ? pstreak + 1 : 0;
+ save();
}
/* Generate the next puzzle while the solved one stays on screen. */
@@ -891,11 +920,12 @@ static int puzzle_menu(int *i) {
int main(void) {
np_app_begin();
- if (np_save_jump(leave)) return np_app_end();
+ if (np_save_jump(leave)) return quit();
ch_init();
+ load();
int m = 0, pk = 0;
for (;;) {
- if (main_menu(&m) < 0) return np_app_end();
+ if (main_menu(&m) < 0) return quit();
if (m == 0 && bot_select()) game(M_BOT);
if (m == 1 && puzzle_menu(&pk) >= 0) puzzles(pk);
if (m == 2 && time_select()) game(M_2P);
diff --git a/games/games.json b/games/games.json
index 3515c884..34791c71 100644
--- a/games/games.json
+++ b/games/games.json
@@ -84,7 +84,9 @@
"top": "0x7FA650",
"bottom": "0x22211E",
"accent": "0x81B64C",
- "records": [],
+ "records": [
+ "numchess.sav"
+ ],
"shots": [
"docs/italian.png",
"docs/bots.png",
diff --git a/tests/test_games.py b/tests/test_games.py
index 9b478a35..819698b3 100644
--- a/tests/test_games.py
+++ b/tests/test_games.py
@@ -43,7 +43,7 @@ def every(t0, t1, step, key, d=80):
GAMES = {
"crossyroad": ("CrossyRoad.nwa", "crossyroad.sav",
every(1500, 9000, 350, "up"), 12000, every(1500, 4000, 400, "up"), 6000),
- "chess": ("NumChess.nwa", None, presses((1500, "ok"), (2500, "ok")), 4000, [], 2500),
+ "chess": ("NumChess.nwa", "numchess.sav", presses((1500, "ok"), (2500, "ok")), 4000, [], 2500),
"numdash": ("NumDash.nwa", "numdash.nds", presses((2500, "ok"), (4000, "ok"), (5500, "ok")), 9000,
presses((2500, "ok")), 5000),
# pause, level list, next level, play: that saves the level to start from
@@ -58,9 +58,12 @@ GAMES = {
SEEDS = {
# the first level done and last played
"numdrive": b"MD" + bytes([1] + [0] * 24) + b"\0\0\0",
+ # puzzle rating 1234, a streak of 3, the bot Hugo, side 1, a 10-minute clock
+ "chess": bytes([ord("C"), 1, 1234 & 255, 1234 >> 8, 3, 0, 7, 1 | 4 << 2]),
}
CHECKS = {
"numdrive": (lambda v: v[27] == 1, lambda v: v[27] == 0), # last played level
+ "chess": (lambda v: v == SEEDS["chess"],) * 2, # read back and kept
}