Compare commits

..
3 Commits
Author SHA1 Message Date
adam f3d2d8d0b8 for loop spacing 2025-01-21 21:54:53 +00:00
adam 5c8a63ef76 border and word option 2025-01-21 21:12:00 +00:00
adam f1c214ddd4 add box around board 2025-01-21 19:40:30 +00:00
+40 -8
View File
@@ -24,8 +24,10 @@ function reveal_row() {
echo -en "${SAVE}"
echo -en "${HIDE_CURSOR}"
# Must increment by 1 Row as 0 == 1 for escape code
declare RNUM=$(( $1+1 ))
echo -en "\e[$RNUM;0H"
declare RNUM=$(( $1+1+BORDER ))
declare OFFSET=$(( BORDER * 3 ))
echo -en "\e[${RNUM};${OFFSET}H"
for (( C=0; C<WIDTH; C++ )); do
echo -n "${BOARD[$1,$C]}"
read -r -s -t 0.5
@@ -41,7 +43,7 @@ function check_guess() {
declare green_count=0
declare chars=""
for ((i=0; i<WIDTH; i++)); do
for (( i=0; i<WIDTH; i++ )); do
[[ "${_answer:i:1}" != "${_guess:i:1}" ]] && chars+=${_answer:i:1}
done
@@ -51,7 +53,7 @@ function check_guess() {
declare _char=${_guess:$c:1}
if [[ "${_char}" == "${_answer:$c:1}" ]]; then
green_count=$((green_count+1))
green_count=$(( green_count+1 ))
BOARD[$_row,$c]="${BOLD}${LETTER}${GREEN} ${_char} "
[[ $green_count == 5 ]] && return 5
continue
@@ -100,7 +102,7 @@ function retrive_guess() {
function select_random_word() {
IFS=' ' read -r -a answers <<< "${POSSIBLE_ANSWERS}"
declare -r random_index=$((RANDOM % ${#answers[@]}))
declare -r random_index=$(( RANDOM % ${#answers[@]} ))
echo "${answers[random_index]^^}"
}
@@ -110,12 +112,16 @@ function render_board() {
declare row
declare char
[[ "${BORDER}" == 1 ]] && echo -e "┌─────────────────┐"
for (( row=0; row<HEIGHT; row++ )); do
[[ "${BORDER}" == 1 ]] && echo -en "│ "
for (( char=0; char<WIDTH; char++ )); do
echo -n "${BOARD[${row},${char}]}${RESET}"
done
[[ "${BORDER}" == 1 ]] && echo -en " │ "
echo -en "${DOWN1}"
done
[[ "${BORDER}" == 1 ]] && echo -e "└─────────────────┘"
}
function init_board() {
@@ -127,11 +133,37 @@ function init_board() {
done
}
function set_options() {
declare -g BORDER=1
while getopts "bw:h" OPT; do
case "${OPT}" in
b)
BORDER=0
;;
w)
ANSWER="${OPTARG^^}"
validate_guess "${ANSWER}"
if [[ $? == 1 ]]; then
echo "Not a valid word"
exit 1
fi
;;
h)
exit # add help message
;;
*)
;;
esac
done
}
function main() {
echo -en "${CLEAR_SCREEN}"
declare -g ANSWER
ANSWER=$(select_random_word)
[[ -n $1 ]] && ANSWER=${1^^}
set_options "$@"
init_board
@@ -147,8 +179,8 @@ function main() {
reveal_row "${row}"
[[ $win == 5 ]] && echo -e "\n${CLEAR_LINE}YOU WIN" && exit 0
done
IFS=
result="${ANSWER//?/ & }"
result="${ANSWER//?/& }"
result=${result%}
echo -e "\n${CLEAR_LINE}YOU LOSE\n${result} ${RESET}" && exit 0
}