Compare commits

..
Author SHA1 Message Date
adam 39265c3c9d resume on correct row after save during row reveal 2025-05-04 12:33:56 +01:00
+25 -59
View File
@@ -33,7 +33,7 @@ function main() {
declare -g ANSWER
declare -g POOL='QWERTYUIOPASDFGHJKLZXCVBNM'
declare -Ag YELLOWS
declare -Ag KEYS
declare -g REVEALING=0
ANSWER=$(select_random_word)
set_options "$@"
@@ -51,31 +51,24 @@ function main() {
init_board
fi
[[ "${ONLINE}" == 1 ]] && save_game "${ROW}"
[[ "${ONLINE}" == 1 ]] && save_game "NO_EXIT"
render_board
for (( ROW; ROW<HEIGHT; ROW++ )); do
declare GUESS
[[ "${KEYBOARD}" == 1 ]] && render_keyboard
GUESS=$(retrieve_guess)
GUESS=$(retrive_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}"
REVEALING=0
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}\""
echo "Share/view your stats with -> curl \"${URL_BASE}/stats/${USER}\""
echo "Check out the new homepage in your browser -> ${URL_BASE}"
while read -r board_line; do echo "${board_line//[A-Z]/ }"; done < "${RESULT_FILE}"
fi
echo "Guess - $(( ROW+1 ))/6"
@@ -90,10 +83,9 @@ function main() {
done
echo -en "${NEWLINE}${RESET}"
if [[ "${ONLINE}" == 1 && "${DAILY_DONE}" != 1 ]]; then
save_result
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}"
while read -r board_line; do echo "${board_line//[A-Z]/ }"; done < "${RESULT_FILE}"
fi
exit 0
@@ -145,15 +137,9 @@ function set_options() {
echo -en "${CLEAR_SCREEN}"
declare -g USER
read -r -p "Username: " USER || exit 1
read -r -p "Username: " USER
USER="${USER,,}"
# Username is used in file names and URLs so only allow safe characters
while [[ -z "${USER}" || "${USER}" == *[!a-z0-9_]* || ${#USER} -gt 32 ]]; do
read -r -p "${CLEAR_UP1}Username (letters, numbers and _ only): " USER || exit 1
USER="${USER,,}"
done
declare -g DAY
DAY=$(( EPOCHSECONDS / 86400 ))
@@ -187,15 +173,14 @@ function set_options() {
fi
fi
# Progress is saved after every guess so Ctrl-C only needs to tidy the terminal
trap 'echo -en "${SHOW_CURSOR}${RESET}"; exit 0' SIGINT
trap save_game SIGINT
;;
d)
DELAY=0
;;
h)
echo -e \
"Usage: wordle [OPTIONS]
"Usage: <program_name> [OPTIONS]
Play a game for Wordle
Options:
@@ -204,7 +189,6 @@ Options:
-H Enable hard mode
-S Enable strict hard mode (Cannot reuse grey letters)
-k disable on screen keyboard
-o Enable online mode
-d Enable debug mode (no delay during letter reveal)
-h Show this help message and exit"
@@ -243,7 +227,7 @@ function render_board() {
[[ "${BORDER}" == 1 ]] && echo -e "└─────────────────┘"
}
function retrieve_guess() {
function retrive_guess() {
if [[ -n $1 ]]; then
read -r -p "${CLEAR_UP1}$1: " GUESS
else
@@ -256,12 +240,12 @@ function retrieve_guess() {
declare valid_guess
validate_guess "${GUESS}" ; valid_guess=$?
[[ ${valid_guess} == 1 ]] && GUESS=$(retrieve_guess "Not A Valid Word")
[[ ${valid_guess} == 2 ]] && GUESS=$(retrieve_guess "Not A Valid Guess")
[[ ${valid_guess} == 3 ]] && GUESS=$(retrieve_guess "GREEN Issue")
[[ ${valid_guess} == 4 ]] && GUESS=$(retrieve_guess "YELLOW Issue")
[[ ${valid_guess} == 5 ]] && GUESS=$(retrieve_guess "POOL Issue")
[[ ${valid_guess} == 6 ]] && GUESS=$(retrieve_guess "NEW Issue")
[[ ${valid_guess} == 1 ]] && GUESS=$(retrive_guess "Not A Valid Word")
[[ ${valid_guess} == 2 ]] && GUESS=$(retrive_guess "Not A Valid Guess")
[[ ${valid_guess} == 3 ]] && GUESS=$(retrive_guess "GREEN Issue")
[[ ${valid_guess} == 4 ]] && GUESS=$(retrive_guess "YELLOW Issue")
[[ ${valid_guess} == 5 ]] && GUESS=$(retrive_guess "POOL Issue")
[[ ${valid_guess} == 6 ]] && GUESS=$(retrive_guess "NEW Issue")
echo -n "${GUESS^^}"
}
@@ -275,12 +259,7 @@ function render_keyboard() {
elif [[ $i == 19 ]]; then
echo -en "${NEWLINE} "
fi
declare key="${POOL:$i:1}"
if [[ -n "${KEYS[$key]}" ]]; then
echo -n "${KEYS[$key]}${BOLD}${LETTER}${key}${RESET} "
else
echo -n "${key} "
fi
echo -n "${POOL:$i:1} "
done
echo -en "${NEWLINE}"
}
@@ -351,7 +330,6 @@ function check_guess() {
if [[ "${_char}" == "${_answer:$c:1}" ]]; then
greens[c]="${_char}"
KEYS[$_char]="${GREEN}"
BOARD[$_row,$c]="${GREEN}${BOLD}${LETTER} ${_char} ${UNBOLD}"
continue
fi
@@ -360,7 +338,6 @@ function check_guess() {
chars="${chars/${_char}/}"
yellows[c]="${_char}" # volatile - stores previous row yellows
YELLOWS[$_row,$c]="${_char}" # stores all yellow from all rounds
[[ "${KEYS[$_char]}" != "${GREEN}" ]] && KEYS[$_char]="${YELLOW}"
BOARD[$_row,$c]="${YELLOW}${BOLD}${LETTER} ${_char} ${UNBOLD}"
fi
[[ "${ANSWER}" =~ ${_char} ]] && continue
@@ -373,6 +350,7 @@ function check_guess() {
}
function reveal_row() {
REVEALING=1
echo -en "${UP1}${SAVE}"
echo -en "${HIDE_CURSOR}"
# Must increment by 1 Row as 0 == 1 for escape code
@@ -392,9 +370,8 @@ function load_game() {
source "${PROGRESS_FILE}"
}
# Takes the row the game should resume from
# shellcheck disable=SC2317 # Don't warn about unreachable commands in this function
function save_game() {
declare -r row=$1
: > "${PROGRESS_FILE}"
for key in "${!BOARD[@]}"; do
@@ -405,29 +382,19 @@ function save_game() {
echo "YELLOWS[$key]='${YELLOWS[$key]}'" >> "${PROGRESS_FILE}"
done
for key in "${!KEYS[@]}"; do
echo "KEYS[$key]='${KEYS[$key]}'" >> "${PROGRESS_FILE}"
done
# Previous row's hints, needed by hard_mode_check after a resume
for key in "${!greens[@]}"; do
echo "greens[$key]='${greens[$key]}'" >> "${PROGRESS_FILE}"
done
for key in "${!yellows[@]}"; do
echo "yellows[$key]='${yellows[$key]}'" >> "${PROGRESS_FILE}"
done
{
echo "ROW=${row}"
echo "ROW=$(( ROW + REVEALING ))"
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() {
@@ -484,7 +451,6 @@ function update_stats() {
integer_part=$(( result / 100 ))
decimal_part=$(( result % 100 ))
[[ ${decimal_part} -lt 10 ]] && decimal_part="0${decimal_part}"
AVERAGE="${integer_part}.${decimal_part}"
fi