Compare commits
3
Commits
f43f5216f5
...
f3d2d8d0b8
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f3d2d8d0b8 | ||
|
|
5c8a63ef76 | ||
|
|
f1c214ddd4 |
@@ -24,8 +24,10 @@ function reveal_row() {
|
|||||||
echo -en "${SAVE}"
|
echo -en "${SAVE}"
|
||||||
echo -en "${HIDE_CURSOR}"
|
echo -en "${HIDE_CURSOR}"
|
||||||
# Must increment by 1 Row as 0 == 1 for escape code
|
# Must increment by 1 Row as 0 == 1 for escape code
|
||||||
declare RNUM=$(( $1+1 ))
|
declare RNUM=$(( $1+1+BORDER ))
|
||||||
echo -en "\e[$RNUM;0H"
|
declare OFFSET=$(( BORDER * 3 ))
|
||||||
|
|
||||||
|
echo -en "\e[${RNUM};${OFFSET}H"
|
||||||
for (( C=0; C<WIDTH; C++ )); do
|
for (( C=0; C<WIDTH; C++ )); do
|
||||||
echo -n "${BOARD[$1,$C]}"
|
echo -n "${BOARD[$1,$C]}"
|
||||||
read -r -s -t 0.5
|
read -r -s -t 0.5
|
||||||
@@ -41,7 +43,7 @@ function check_guess() {
|
|||||||
declare green_count=0
|
declare green_count=0
|
||||||
declare chars=""
|
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}
|
[[ "${_answer:i:1}" != "${_guess:i:1}" ]] && chars+=${_answer:i:1}
|
||||||
done
|
done
|
||||||
|
|
||||||
@@ -51,7 +53,7 @@ function check_guess() {
|
|||||||
declare _char=${_guess:$c:1}
|
declare _char=${_guess:$c:1}
|
||||||
|
|
||||||
if [[ "${_char}" == "${_answer:$c:1}" ]]; then
|
if [[ "${_char}" == "${_answer:$c:1}" ]]; then
|
||||||
green_count=$((green_count+1))
|
green_count=$(( green_count+1 ))
|
||||||
BOARD[$_row,$c]="${BOLD}${LETTER}${GREEN} ${_char} "
|
BOARD[$_row,$c]="${BOLD}${LETTER}${GREEN} ${_char} "
|
||||||
[[ $green_count == 5 ]] && return 5
|
[[ $green_count == 5 ]] && return 5
|
||||||
continue
|
continue
|
||||||
@@ -100,7 +102,7 @@ function retrive_guess() {
|
|||||||
|
|
||||||
function select_random_word() {
|
function select_random_word() {
|
||||||
IFS=' ' read -r -a answers <<< "${POSSIBLE_ANSWERS}"
|
IFS=' ' read -r -a answers <<< "${POSSIBLE_ANSWERS}"
|
||||||
declare -r random_index=$((RANDOM % ${#answers[@]}))
|
declare -r random_index=$(( RANDOM % ${#answers[@]} ))
|
||||||
echo "${answers[random_index]^^}"
|
echo "${answers[random_index]^^}"
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -110,12 +112,16 @@ function render_board() {
|
|||||||
declare row
|
declare row
|
||||||
declare char
|
declare char
|
||||||
|
|
||||||
|
[[ "${BORDER}" == 1 ]] && echo -e "┌─────────────────┐"
|
||||||
for (( row=0; row<HEIGHT; row++ )); do
|
for (( row=0; row<HEIGHT; row++ )); do
|
||||||
|
[[ "${BORDER}" == 1 ]] && echo -en "│ "
|
||||||
for (( char=0; char<WIDTH; char++ )); do
|
for (( char=0; char<WIDTH; char++ )); do
|
||||||
echo -n "${BOARD[${row},${char}]}${RESET}"
|
echo -n "${BOARD[${row},${char}]}${RESET}"
|
||||||
done
|
done
|
||||||
|
[[ "${BORDER}" == 1 ]] && echo -en " │ "
|
||||||
echo -en "${DOWN1}"
|
echo -en "${DOWN1}"
|
||||||
done
|
done
|
||||||
|
[[ "${BORDER}" == 1 ]] && echo -e "└─────────────────┘"
|
||||||
}
|
}
|
||||||
|
|
||||||
function init_board() {
|
function init_board() {
|
||||||
@@ -127,11 +133,37 @@ function init_board() {
|
|||||||
done
|
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() {
|
function main() {
|
||||||
echo -en "${CLEAR_SCREEN}"
|
echo -en "${CLEAR_SCREEN}"
|
||||||
declare -g ANSWER
|
declare -g ANSWER
|
||||||
ANSWER=$(select_random_word)
|
ANSWER=$(select_random_word)
|
||||||
[[ -n $1 ]] && ANSWER=${1^^}
|
|
||||||
|
set_options "$@"
|
||||||
|
|
||||||
init_board
|
init_board
|
||||||
|
|
||||||
@@ -147,8 +179,8 @@ function main() {
|
|||||||
reveal_row "${row}"
|
reveal_row "${row}"
|
||||||
[[ $win == 5 ]] && echo -e "\n${CLEAR_LINE}YOU WIN" && exit 0
|
[[ $win == 5 ]] && echo -e "\n${CLEAR_LINE}YOU WIN" && exit 0
|
||||||
done
|
done
|
||||||
IFS=
|
|
||||||
result="${ANSWER//?/ & }"
|
result="${ANSWER//?/& }"
|
||||||
result=${result%}
|
result=${result%}
|
||||||
echo -e "\n${CLEAR_LINE}YOU LOSE\n${result} ${RESET}" && exit 0
|
echo -e "\n${CLEAR_LINE}YOU LOSE\n${result} ${RESET}" && exit 0
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user