//hangman word is my text file which include words
i use this code to generate a random number corresponding to a word in our file
and works fine calculate and generate a right number
Code:
wordline=$( wc -l ${wordlist} | awk '{print $1}')
randmo=$( awk -v l=$wordline 'BEGIN {srand();
r=rand()*l;
printf("%d",( r + l ))}');
the problem is here when i want to select the appropriate word from our file
i wrote this code and appear this wrong message "awk: 2: unexpected character '.'"
Finally i want a help how to replace a character who read from keyboard with -
i Forgot to write this code how to split the word to table using this code
any idea how to replace the '-' with the right character with command substr or something else
i use this code to read one character
Code:
read charact
Code:
while [ ${lifes} -gt "0" ]
do
echo -e "\n\n You have ${lifes} remaining!"
done
if [ ${lifes} -eq "0" ]; then
echo "GAME OVER"
echo "The word was ${word} "
fi
i don't finish the code of while because i didn't know how to replace the '-' with the right character but the most important is to solve the error
with this code the problem is that in
1st loop guessed was e
display that -e----
2nd loop when guessed was d
display that --d---
and the desire sesult was -ed----
any idea???
---------- Post updated at 08:28 PM ---------- Previous update was at 08:18 PM ----------
Quote:
Originally Posted by Scrutinizer
Something like this:
Code:
guessed=""
lives=7
word=bedroom
while [ $lives -gt 0 ]
do
read char
if [ -n "$char" ]
then
lives=$((lives -1))
guessed="$guessed$char"
fi
echo $word|sed "s/[^$guessed]/-/g"
done
can you explain me this 2 lines what are you doing?
using this code if [ -n "$char" ] any char press the lifes decrease we want to decrease when the guessed is wrong any idea
and why use this guessed="$guessed$char"
can i ignore this line guessed="$guessed$char"
and last line wrote this echo $word|sed "s/[^$char]/-/g"
why use 2 (( here $((lives -1)) and why $ is out;
sorry for my question but i want to learn i think stupid to write somenthing don't understanding maybe i need to use many of commands in the future
In Addition the decrease must doing when i press wrong guess
---------- Post updated at 10:31 PM ---------- Previous update was at 08:28 PM ----------
one though is to split the word to a table an with for i check if char is not included to table decrease the lifes
for example in c language i can write this
for (i=1;i<=sizeof(tablep);i++)
{
if (char != tablep[i])
lives--;
}
Hello everyone, im just having some problem forming a script for a hangman script. The question states:
The object this excercise is for u to write a script that emulate the classic game hangman. The object of this game is for a user to try and guess a word which has been generated by the... (1 Reply)