From 357c74cf08b6c1fd63380139a831453c9b1105f5 Mon Sep 17 00:00:00 2001 From: Adam Date: Sun, 16 Feb 2025 17:05:57 +0000 Subject: [PATCH] calculate average guesses and fix divide by 0 bug --- wordle | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/wordle b/wordle index 4b9a7c2..f99fa91 100755 --- a/wordle +++ b/wordle @@ -23,7 +23,6 @@ declare -gr UP1=$'\e[1F' declare -gr HIDE_CURSOR=$'\e[?25l' declare -gr SHOW_CURSOR=$'\e[?25h' declare -gr GOHOME=$'\e[H' -declare -gr DOWN1=$'\e[1E' declare -gr INDENT=$'\e[1C' function main() { @@ -156,7 +155,6 @@ function set_options() { if [[ "${HM}" =~ ^[Yy]$ ]]; then HARD_MODE=1 - declare SHM echo -en "${NEWLINE}" read -r -p "Do you want to enable strict hard mode [y/N]? " -n 1 HM @@ -352,6 +350,7 @@ function reveal_row() { } function load_game() { + # shellcheck source=/dev/null source "${PROGRESS_FILE}" } @@ -420,6 +419,21 @@ function update_stats() { [[ $WIN != 5 ]] && STREAK=0 [[ $LONGEST_STREAK -lt $STREAK ]] && LONGEST_STREAK=$(( LONGEST_STREAK+1 )) + declare num=0 + declare den=0 + for (( i=0; i<${#GAME_HIST[@]}; i++)); do + ((den += ${GAME_HIST[$i]})) + (( num += ${GAME_HIST[$i]}*(i+1) )) + + done + if [[ "${den}" != 0 ]]; then + result=$(( (num * 100) / den )) + integer_part=$(( result / 100 )) + decimal_part=$(( result % 100 )) + + AVERAGE="${integer_part}.${decimal_part}" + fi + : > "${STATS_SOURCE}" { echo "GAMES_PLAYED=${GAMES_PLAYED}" @@ -428,6 +442,7 @@ function update_stats() { echo "GAME_HIST=(${GAME_HIST[*]})" echo "STREAK=${STREAK}" echo "LONGEST_STREAK=${LONGEST_STREAK}" + echo "AVERAGE=${AVERAGE}" } >> "${STATS_SOURCE}" render_stats @@ -436,10 +451,12 @@ function update_stats() { function create_stats() { { echo "GAMES_PLAYED=0" - echo -e "WINS=0\\nLOSSES=0" + echo "WINS=0" + echo "LOSSES=0" echo "GAME_HIST=(0 0 0 0 0 0)" echo "STREAK=0" echo "LONGEST_STREAK=0" + echo "AVERAGE=0" } >> "${STATS_SOURCE}" } @@ -458,7 +475,11 @@ function render_stats() { for (( i=0; i> "${STATS_FILE}" }