help with hangman


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers help with hangman
# 1  
Old 12-16-2009
help with hangman

i want to create the known game hangman

Code:
wordlist=./hangmanword

//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 '.'"

Code:
word=$( awk -v l=randmo 'BEGIN {RS="";
    FS="\n"} {print $1}'$wordlist)

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

Code:
awk -v var1=$word 'BEGIN {FS="';split (var1,T); (for i=1;i<=length(var1);i++) {print i,T[i]}}' /dev/null

i use this code to make the - - - - -
Code:
awk -v var1=$word 'BEGIN {for (i=1;i<=length(var1);i++) {printf("%s","-")};print}' /dev/null

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

Last edited by sainis; 12-16-2009 at 07:50 PM..
# 2  
Old 12-16-2009
Hi, there should be a space between ' and $wordlist

Code:
{print $1}' $wordlist

As to the ---
You means something like this?
Code:
awk -v var1=$word 'BEGIN {gsub(/./,"-",var1);print var1}' /dev/null

Alternatively:
Code:
echo $word|sed 's/./-/g'

and if you use bash or ksh you could also use:
Code:
echo ${word//?/-}

I am not sure what you mean with "the right character"

Last edited by Scrutinizer; 12-16-2009 at 07:54 PM..
# 3  
Old 12-16-2009
thanks friend for help

i have read many books and i read yet the last i try to study is The Complete Reference Unix" from Obserne

one thought is wrote this code to replace the '-' with right character is using the substr
is using this code to calculate the length
Code:
wordlength=${#word}
awk -v var1=$charact 'BEGIN {for (i=1;i<=wordlength;i++){A[i]=substr(var1,i,1)};for (i=1;i<=wordlength;i++){print i,T[i]}}' /dev/null

do you believe that may work or what you recommend to chance in this code to run correctly

---------- Post updated at 07:05 PM ---------- Previous update was at 06:58 PM ----------

using this code i split the word to a table

Code:
awk -v var1=$word 'BEGIN {FS="';split (var1,T); (for i=1;i<=length(var1);i++) {print i,T[i]}}' /dev/null

using this code i replace the characters with - - -
for example if the word was bedroom with this code appear - - - - - - -
Code:
awk -v var1=$word 'BEGIN {for (i=1;i<=length(var1);i++) {printf("%s","-")};print}' /dev/null

i want to read a character using this command
Code:
read charact

and if this character included for example to bedroom display this character for example

the word is :bedroom
script appear: ------- 7- depends on word length
while i read charact: press e
i want the script appear this -e----m

i think i can do that with substr

i use bash
# 4  
Old 12-16-2009
How about:
Code:
$ word=bedroom
$ guessed=em
$ echo $word|sed "s/[^$guessed]/-/g"
-e----m
$ guessed=erm
$ echo $word|sed "s/[^$guessed]/-/g"
-e-r--m

# 5  
Old 12-16-2009
many thanks friend
finally it was more simple using sed to using awk

---------- Post updated at 07:51 PM ---------- Previous update was at 07:28 PM ----------

last question any idea how to decrease the lifes?
if i uze this code believe that works
Code:
while [ ${lifes} -gt "0" ]
do
read guessed
if [ -z "$guessed" ]
lifes=`expr lifes - 1`
fi
done


Last edited by sainis; 12-16-2009 at 09:11 PM..
# 6  
Old 12-16-2009
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

# 7  
Old 12-16-2009
Quote:
Originally Posted by Scrutinizer
How about:
Code:
$ word=bedroom
$ guessed=em
$ echo $word|sed "s/[^$guessed]/-/g"
-e----m
$ guessed=erm
$ echo $word|sed "s/[^$guessed]/-/g"
-e-r--m

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--;
}

does anyone how to make it with bash

Last edited by sainis; 12-16-2009 at 10:01 PM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

3 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Hangman Script

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)
Discussion started by: immyakram
1 Replies

2. Shell Programming and Scripting

Script for Hangman Game

Any ideas on whats the best way to form a hangman game via bash shell. (0 Replies)
Discussion started by: immyakram
0 Replies

3. Shell Programming and Scripting

Hangman written in Bash. Suggestions please...

qwertyuiop (1 Reply)
Discussion started by: rorey_breaker
1 Replies
Login or Register to Ask a Question