help with hangman


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers help with hangman
# 8  
Old 12-17-2009
Quote:
Originally Posted by sainis
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???
This is nog my experience:
This is what happens when I run the code:
Code:
e
-e-----
d
-ed----
o
-ed-oo-
a
-ed-oo-
b
bed-oo-
r
bedroo-
m
bedroom

Quote:


---------- Post updated at 08:28 PM ---------- Previous update was at 08:18 PM ----------

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"
Sure it is. I did not want to spoil your fun by writing all the code for you. I just tried to show a basic mechanism. The
Code:
if [ -n "$char" ]

means the if it is non-zero then decrease lives this is so that if a user enters enter by accident without a character lives does not decrease. You could replace that statement with a test to see if the character is part of $word and bob is your uncle.
Quote:
why use 2 (( here $((lives -1)) and why $ is out;
The $(( ... )) construct is used for calculations. Inside the double parentheses you can use variables without the $-sign (you can still use it if you wish. The $(( ... )) is a more modern construct. I hardly use the oldfashioned expr statement anymore.
Quote:
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
Don't be sorry that is what this forum is for Smilie. It is excellent that you really want to understand the code..
Quote:
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
You can use a case ... esac statement to do that or use [[ ... ]] and pattern matching with asterisks or use the pattern matching operator ~= There are many ways... Good luck.

S.

Last edited by Scrutinizer; 12-17-2009 at 03:53 AM..
# 9  
Old 12-17-2009
thanks again friend


do you believe that may work;


Code:
word=bedroom
len=$[#word]
lifes=7

while [ lifes -gt "0" ];do
read a
for i in $( seq 1 $(( $len )) )
if [ a != ${word[i]}
then lives=$(( lives -1))
fi
echo $i
done

in this code it is necessary to create table for example
Code:
word=bedroom
len=$[#word]
lifes=7

for i in $( seq 1 $(( $len )) )
do
t[$i]=$echo "${word}" | cut -c$(($i+1)) )
done

while [ lifes -gt "0" ];do
read a
for i in $( seq 1 $(( $len )) )
if [ a != ${t[i]}
then lives=$(( lives -1))
fi
done



---------- Post updated at 07:24 AM ---------- Previous update was at 05:48 AM ----------

just found the solution i use one var as boolean false or true
if i found in table the character i make the var true and after finidh the loop decrease lifes

Last edited by sainis; 12-17-2009 at 06:58 AM..
# 10  
Old 12-17-2009
I'd do something like this. You can take it from there:
Code:
guessed=""
lives=7
wordlist=./hangmanword
# See if wordfile exists - Elegxos gia to an to wordfile yparxei kai mporei na diavastei
if [ ! -r $wordlist ]; then
  echo "$wordlist den yparxei h den vrethike"
  exit 1
fi
# pick a (kind of) random word - dimiourgia tou generator
nrofwords=$(wc -l<$wordlist)
word=$(sed $((RANDOM%nrofwords+1))!d $wordlist)
# Here we go
while [ $lives -gt 0 ]; do
  echo "you have $lives lives left"
  read char
  case $word in
    *$char*) guessed="$guessed$char";;
    *)       lives=$((lives - 1)) ;;
  esac
  solution=$(echo $word|sed "s/[^$guessed ]/-/g")
  echo $solution
  if [ "$solution" = "$word" ]; then
    echo "You Win"
    exit
  fi
done
echo "You Loose"
echo "The word was $word"


Last edited by Scrutinizer; 12-17-2009 at 06:55 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