473,379 Members | 1,235 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

bash shell scripting

keyvanrahmadi
Sorry if this post is rather long but hopefully you wont get bored half way through. I have a project in hand which i have started and have a deadline of 1 week. Basically what i need to do is to create a shell script in Bash for the game Hangman. I am sure everyone is well aware of the game. The down side is that i can not use "sed" or "awk". FUN FUN

I am at my ends wit now and truley utterly lost, so any help would go a long way to stop my pulling my wife's hair out ( i ran out of my own hair a long time ago :)

The game has to have the option for 1 player or 2 player, which needs to be stated at the started, when the script is run. This is what i have done to remedy this option:

Expand|Select|Wrap|Line Numbers
  1.  
  2. #!/bin/bash
  3.  
  4. echo "Choose number of Players:"
  5. read players
  6. if [ $players -eq 1 ]
  7. then
  8. echo "The script for thsi hasnt been written."
  9. elif [ $players -eq 2 ]
  10. then
  11. echo "choose a word for player two to guess:"
  12. read player2word
  13. else
  14. echo "incorrect option.. choose again"
  15. fi
  16.  
I am sure there is a better way of doing this, so if any suggestions would be great.

If a 1 player option is choosen, then the following which i havent even coded should happen:

1- A random word from a different file needs to be chosen.
2- The player should be told the word has been chosen and the display should be - - - - (number of dashes should be = to length of word.
3- player is prompted to guess a letter. If a correct letter is chosen then display the message: You have chosen <letter>, then echo the letter and place the chosen letter in correct field.



now 2 player is same as above except that player 1 has to input the word for player 2 to guess. So far i have done this:

Expand|Select|Wrap|Line Numbers
  1.  
  2. life=10
  3. blank="......................"
  4. word=$player2word
  5. letters=$(echo $word | wc -c)
  6. letters=$(( $letters - 1 ))
  7. template="$(echo $blank | cut -c1-$letters)"
  8. remaining=$letters
  9. guessed=""
  10. guesses=0
  11. badguesses=0
  12. echo " ************** The word you are trying to guess has $letters letters **************"
  13. echo ""
  14. echo ""
  15.  
  16. while [ $remaining != $word ] ; do
  17.  
  18. echo -n "Word is: $template Please choose a Letter: " ;
  19. read guess
  20. guesses=$(( $guesses + 1 ))
  21. echo " you have chosen $guess"
  22.  
  23. if echo $guessed | grep -i $guess > .temp1 ; then
  24.  
  25. echo "You've already guessed that letter. Try again!"
  26.  
  27. elif ! echo $word | grep -i $guess > .temp1 ; then
  28.  
  29. echo "Sorry, the letter \"$guess\" is not in the word."
  30. guessed="$guessed$guess"
  31. badguess=$(( $badguess + 1))
  32. life=$(( $life -1 ))
  33. echo " you have $life left"
  34. else
  35.  
  36. echo "Good going! The letter $guess is in the word!"
  37. echo $letter
  38.  
  39. fi
  40. done
  41.  
  42. echo -n "Congratulations! You guessed $word in $guesses guesses"
  43.  
  44. echo " with $badguesses bad guesses"
  45.  
  46.  
problems with above script is as follow:

1- Not finished
2- when a letter is chosen the script excepts the fact its a correct or an incorrect letter but dosent insert it into ..... location.
3- i am sure there is more, best thing i can suggest if you can run it and see the result then you will have a better understading of it.

I know this is a tall order, but any help would be greatly appriciated and in my part main thing is to learn and understand from all you guys out there who have far more knowledge than me in shell scripting.

thank you

K
May 15 '07 #1
14 4863
arne
315 Expert 100+
Expand|Select|Wrap|Line Numbers
  1.       while [ $remaining != $word ] ; do
  2.       echo -n "Word is: $template Please choose a Letter: " ;
  3.       read guess
  4.       guesses=$(( $guesses + 1 ))
  5.       echo " you have chosen $guess"
  6.       if echo $guessed | grep -i $guess > .temp1 ; then
  7.           echo "You've already guessed that letter. Try again!"
  8.       elif ! echo $word | grep -i $guess > .temp1 ; then
  9.           echo "Sorry, the letter \"$guess\" is not in the word."
  10.           guessed="$guessed$guess"
  11.           badguess=$(( $badguess + 1))
  12.           life=$(( $life -1 ))
  13.           echo " you have $life left"
  14.       else
  15.           echo "Good going! The letter $guess is in the word!"
  16.           guessed="$guessed$guess"
  17.           echo $letter
  18.       fi
  19.  
  20.       ndx=0
  21.       template2=""
  22.       while [ $ndx != $(( $letters )) ] ; do
  23.           val1=${word:ndx:1}
  24.           val2=${template:ndx:1}
  25.           if [ "$val1" == "$guess" ] ; then
  26.           template2="$template2$guess"
  27.           elif [ "$val2" != '.' ] ; then
  28.           template2="$template2$val2"
  29.           else
  30.           template2="$template2."
  31.           fi
  32.           ndx=$(( $ndx + 1 ))
  33.       done
  34.       template=$template2
  35.       done
  36.  
The relevant additions are from line 20 onwards (I also added line 16 for convenience).
The idea is to assemble the template every time the player has guessed a letter.
For this, I am using a second template, which I copy over at the end.
Extracting letter by letter from the searched word and comparing with the guess
and already identified letters we assemble the new template to be displayed.

This should work (except that the code regards 'a' and 'A' as different letters,
while your code does not - I left that for you :) ). If it does not work in all cases,
it may give you an idea for a possible solution.
May 16 '07 #2
I have pulled the script apart, figuring out what you have done. thank you for that. it has helped me alot and getting me closer towards getting it finished.

I had couple of more questions, which i was hoping you or anyone else can clarify.

1- the user input as $guess gets compared to $letter, but surely when $guess = $word it should kick the program into echo the last message, but it dosent. any suggestions on that?

2- I have also added echo $guess, to show what letter have been guessed. How can i keep a record of all the letters guessed?

3- I am using a variable called $blank"....", whole idea was to replace the letters with ....., dose unix have a built in function which allows me to do that?

thx again

K
May 16 '07 #3
arne
315 Expert 100+
1- the user input as $guess gets compared to $letter, but surely when $guess = $word it should kick the program into echo the last message, but it dosent. any suggestions on that?
Don't understand what you mean here. Can you elaborate?

2- I have also added echo $guess, to show what letter have been guessed. How can i keep a record of all the letters guessed?
That's why I added line 16 in the code posted. I don't make no difference if the letter is in the word or not. So that should be fixed already :)

3- I am using a variable called $blank"....", whole idea was to replace the letters with ....., dose unix have a built in function which allows me to do that?
One solution would be to use the length of the word to be guessed in order to create the '....' word. But bash also knows a string replacement function:

Expand|Select|Wrap|Line Numbers
  1. ${string/substring/replacement}
  2.  
which will replace 'substring' in 'string' with 'replacement'. Should work if 'substring' is equal to 'string' ...

HTH,
arne
May 17 '07 #4
Don't understand what you mean here. Can you elaborate?
Sorry what i meant to say was, as it stands if the player is trying to guess a letter from a word, the game will continue asking for letter to be guessed even when the whole word has already been guessed, this dose not allow the game to finish and will keep on asking for letters.


That's why I added line 16 in the code posted. I don't make no difference if the letter is in the word or not. So that should be fixed already :)
Thats what i thought line 16 was for :) how can i keep count of all the letters guessed? as it stands it will replace the previous guess

One solution would be to use the length of the word to be guessed in order to create the '....' word. But bash also knows a string replacement function:

Expand|Select|Wrap|Line Numbers
  1. ${string/substring/replacement}
  2.  
which will replace 'substring' in 'string' with 'replacement'. Should work if 'substring' is equal to 'string' ...

HTH,
arne
ok i will look into it and if you dont mind i will questions regarding it later on.

thx alot mate

Keyvan
May 17 '07 #5
arne
315 Expert 100+
Sorry what i meant to say was, as it stands if the player is trying to guess a letter from a word, the game will continue asking for letter to be guessed even when the whole word has already been guessed, this dose not allow the game to finish and will keep on asking for letters.
Change
Expand|Select|Wrap|Line Numbers
  1. while [ $remaining != $word ] ; do
  2.  
to
Expand|Select|Wrap|Line Numbers
  1. while [ $template != $word ] ; do
  2.  
and the code will exit the while loop if the word is complete.

Thats what i thought line 16 was for :) how can i keep count of all the letters guessed? as it stands it will replace the previous guess
No, it won't, $guessed is increasing, I think.
May 17 '07 #6
Change
Expand|Select|Wrap|Line Numbers
  1. while [ $remaining != $word ] ; do
  2.  
to
Expand|Select|Wrap|Line Numbers
  1. while [ $template != $word ] ; do
  2.  
and the code will exit the while loop if the word is complete.


No, it won't, $guessed is increasing, I think.
the exit is as you suggested. $guessed is not increasing as far as i can tell but again what do i know.. i have only been learning shell scripting for about 2 weeks, i will double check it and play around with it. I also had a though about getting rid of the $blank..

Expand|Select|Wrap|Line Numbers
  1. blank="......................"
  2. #word1=getword
  3. word=$player2word
  4. letters=$(echo $word | wc -c)
  5. letters=$(( $letters - 1 ))
  6. template="$(echo $blank | cut -c1-$letters)"
  7. remaining=$letters
  8.  
i was going to change the template line to:
Expand|Select|Wrap|Line Numbers
  1. template1="$(echo $word | tr '[a-z A-Z 0-9]' '_')"
  2.  
what do you think?

Thx

Keyvan
May 17 '07 #7
arne
315 Expert 100+
the exit is as you suggested. $guessed is not increasing as far as i can tell but again what do i know..
Lines 10 and 16 do exactly that, no? Or do I get you wrong here?
Put an additional echo before the ndx=0 line and you will see that
$guessed is getting longer with every letter.

i was going to change the template line to:
Expand|Select|Wrap|Line Numbers
  1. template1="$(echo $word | tr '[a-z A-Z 0-9]' '_')"
  2.  
what do you think?
Looks good to me (and it's more elegant than using the length as I proposed :-) ).
May 17 '07 #8
Lines 10 and 16 do exactly that, no? Or do I get you wrong here?
Put an additional echo before the ndx=0 line and you will see that
$guessed is getting longer with every letter.



Looks good to me (and it's more elegant than using the length as I proposed :-) ).
might be more elegant but it dosent work :(

it should in theory creat - - - depending on number of letters but it only creates 1 dash.
May 17 '07 #9
arne
315 Expert 100+
might be more elegant but it dosent work :(

it should in theory creat - - - depending on number of letters but it only creates 1 dash.
Hmm, it works fine for me ... except that I replaced $template1 by $template in my code.
May 17 '07 #10
Hmm, it works fine for me ... except that I replaced $template1 by $template in my code.
mine creates a continues line as follow:

Word is: ______ Please choose a Letter:

Keyvan
May 17 '07 #11
arne
315 Expert 100+
mine creates a continues line as follow:

Word is: ______ Please choose a Letter:

Keyvan
Hmm, and you would like to have "_ _ _ _"? My shell does that ...

You could introduce blanks, but this will make your code somewhat more complicated. so why not staying with the dots? ;-)
May 17 '07 #12
Hmm, and you would like to have "_ _ _ _"? My shell does that ...

You could introduce blanks, but this will make your code somewhat more complicated. so why not staying with the dots? ;-)
hehehe in that case i play around with it and possibly leave it with dots :)

btw what is "ndx"... i cant find any reference to it

Thx

K
May 17 '07 #13
arne
315 Expert 100+
hehehe in that case i play around with it and possibly leave it with dots :)

btw what is "ndx"... i cant find any reference to it

Thx

K
See line 20 in my code for ndx.

The "_ _ _ _" could also easily be achieved by not echo'ing $template, but printing it letter by letter with blanks in between. Not soo complicated ...
May 17 '07 #14
See line 20 in my code for ndx.

The "_ _ _ _" could also easily be achieved by not echo'ing $template, but printing it letter by letter with blanks in between. Not soo complicated ...
was wondering what stands for :)

K

ps..i will try the to get the - - - sorted and let you know.
May 17 '07 #15

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: John Bowling | last post by:
I have a java (2.0) program with the following lines: String cmdArray1 = {"lp", "-d", "hp4m", "MyFile"}; System.out.println(Runtime.getRuntime().exec(cmdArray1)); It compliles properly, but...
3
by: William Park | last post by:
I'm very excited to announce shell interface to GTK+2 (2.6.1) for Bash. It read XML syntax describing the widget layout, and returns user selection as shell variable or runs shell command as...
11
by: Magnus Jonneryd | last post by:
Hi, I'm planning on writing a program that interactively is fed input via a shell (bash). I also want to be able to write a shell script that executes various commands related to my program. In...
7
by: chakkaradeepcc | last post by:
HI all, How to execute bash scripts from python (other than using os.popen) and get the values that those bash scripts return. I would be happy if someone could help me out in this.. thanks...
4
by: 4zumanga | last post by:
I have a bunch of really horrible hacked-up bash scripts which I would really like to convert to python, so I can extend and neaten them. However, I'm having some trouble mapping some constructs...
16
by: John Salerno | last post by:
Hi all. I just installed Ubuntu and I'm learning how to use the bash shell. Aside from the normal commands you can use, I was wondering if it's possible to use Python from the terminal instead of...
10
by: Isaac Gouy | last post by:
$ ./test_fail 1 Floating point exception - but this leaves 'somefile' zero size $ ./test_fail 1>somefile Floating point exception
1
by: Fredrik Lundh | last post by:
John Lawrence wrote: doesn't exactly work for Python scripts, though: $ cat env.py #!/usr/bin/env python import os os.environ = "hello"
6
by: Frantisek Malina | last post by:
What is the best way to do the regular bash commands in native python? - create directory - create file - make a symlink - copy a file to another directory - move a file - set permissions ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.