calculate average guesses and fix divide by 0 bug
Deploy game to server / deploy (push) Successful in 8s

This commit was merged in pull request #4.
This commit is contained in:
adam committed 2025-02-16 17:05:57 +00:00
1 parent e138f71a62
commit 357c74cf08
1 file changed
+25 -3
+25 -3
View File
@@ -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<HEIGHT; i++ )); do
value="${GAME_HIST[$i]}"
if [[ "${highest}" == 0 ]]; then
bar_length=0
else
bar_length=$(( value * max_width / highest ))
fi
bar='▇'
@@ -484,6 +505,7 @@ function render_stats() {
echo "Losses: ${LOSSES}"
echo "Streak: ${STREAK}"
echo "Longest Streak: ${LONGEST_STREAK}"
echo "Average: ${AVERAGE}"
} >> "${STATS_FILE}"
}