save progress after every guess

Progress was only saved at the start of a game and on Ctrl-C, so a
dropped connection lost every guess made so far. Ctrl-C during the
reveal also saved the scored row without advancing ROW, which gave the
player that guess back on resume.

Save straight after each guess is scored (before the reveal) with the
next row, record the result before the final reveal, and make the
SIGINT trap only tidy up the terminal.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
adamandClaude Opus 5.5 committed 2026-09-29 17:17:20 +00:00
1 parent 1f6a5b4a44
commit 73f403693a
1 file changed
+15 -11
+15 -11
View File
@@ -51,7 +51,7 @@ function main() {
init_board
fi
[[ "${ONLINE}" == 1 ]] && save_game "NO_EXIT"
[[ "${ONLINE}" == 1 ]] && save_game "${ROW}"
render_board
@@ -61,10 +61,16 @@ function main() {
GUESS=$(retrieve_guess)
check_guess "${GUESS}" "${ROW}" ; WIN=$?
# Save before the reveal so quitting or disconnecting can't undo a guess
if [[ $WIN == 5 ]] || (( ROW+1 == HEIGHT )); then
save_result
elif [[ "${ONLINE}" == 1 ]]; then
save_game "$(( ROW+1 ))"
fi
reveal_row "${ROW}"
if [[ $WIN == 5 ]]; then
save_result
echo -e "${CLEAR_DOWN}YOU WIN"
if [[ "${ONLINE}" == 1 && "${DAILY_DONE}" != 1 ]]; then
echo "Share/view your game with -> curl \"${URL_BASE}/${RESULT_FILE}\""
@@ -84,8 +90,7 @@ function main() {
done
echo -en "${NEWLINE}${RESET}"
save_result
if [[ "${ONLINE}" == 1 && "${DAILY_DONE}" != 1 ]]; then
if [[ "${ONLINE}" == 1 && "${DAILY_DONE}" != 1 ]]; then
echo "Share/view your game with -> curl \"${URL_BASE}/${RESULT_FILE}\""
echo "Share/view your stats with -> curl \"${URL_BASE}/stats/${USER}\""
echo "Check out the new homepage in your browser -> ${URL_BASE}"
@@ -182,7 +187,8 @@ function set_options() {
fi
fi
trap save_game SIGINT
# Progress is saved after every guess so Ctrl-C only needs to tidy the terminal
trap 'echo -en "${SHOW_CURSOR}${RESET}"; exit 0' SIGINT
;;
d)
DELAY=0
@@ -386,8 +392,9 @@ function load_game() {
source "${PROGRESS_FILE}"
}
# shellcheck disable=SC2317 # Don't warn about unreachable commands in this function
# Takes the row the game should resume from
function save_game() {
declare -r row=$1
: > "${PROGRESS_FILE}"
for key in "${!BOARD[@]}"; do
@@ -403,18 +410,15 @@ function save_game() {
done
{
echo "ROW=${ROW}"
echo "ROW=${row}"
echo "POOL=${POOL}"
echo "HARD_MODE=${HARD_MODE}"
echo "STRICT_HARD_MODE=${STRICT_HARD_MODE}"
echo "WIN=${WIN}"
echo "ANSWER=${ANSWER}"
echo "IN_PROGRESS=1"
[[ "${PLAYED}" == 1 ]] && echo 'PLAYED=1'
[[ "${PLAYED}" == 1 ]] && echo 'PLAYED=1'
} >> "${PROGRESS_FILE}"
echo -en "${RESET}"
[[ -z $1 ]] && exit 0
}
function save_result() {