bash shell scripting

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • keyvanrahmadi
    New Member
    • Feb 2007
    • 57

    bash shell scripting

    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:

    Code:
    #!/bin/bash
    
    echo "Choose number of Players:"
    read players
    if [ $players -eq 1 ]
    then
    echo "The script for thsi hasnt been written."
    elif [ $players -eq 2 ]
    then
    echo "choose a word for player two to guess:"
    read player2word
    else
    echo "incorrect option.. choose again"
    fi
    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:

    Code:
    life=10
    blank="......................"
    word=$player2word
    letters=$(echo $word | wc -c)
    letters=$(( $letters - 1 ))
    template="$(echo $blank | cut -c1-$letters)"
    remaining=$letters
    guessed=""
    guesses=0
    badguesses=0
    echo " ************** The word you are trying to guess has $letters letters **************"
    echo ""
    echo ""
    
    while [ $remaining != $word ] ; do
    
    echo -n "Word is: $template Please choose a Letter: " ;
    read guess
    guesses=$(( $guesses + 1 ))
    echo " you have chosen $guess"
    
    if echo $guessed | grep -i $guess > .temp1 ; then
    
    echo "You've already guessed that letter. Try again!"
    
    elif ! echo $word | grep -i $guess > .temp1 ; then
    
    echo "Sorry, the letter \"$guess\" is not in the word."
    guessed="$guessed$guess"
    badguess=$(( $badguess + 1))
    life=$(( $life -1 ))
    echo " you have $life left"
    else
    
    echo "Good going! The letter $guess is in the word!"
    echo $letter
    
    fi
    done
    
    echo -n "Congratulations! You guessed $word in $guesses guesses"
    
    echo " with $badguesses bad guesses"
    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
  • arne
    Recognized Expert Contributor
    • Oct 2006
    • 315

    #2
    Code:
          while [ $remaining != $word ] ; do
    	  echo -n "Word is: $template Please choose a Letter: " ;
    	  read guess
    	  guesses=$(( $guesses + 1 ))
    	  echo " you have chosen $guess"
    	  if echo $guessed | grep -i $guess > .temp1 ; then
    	      echo "You've already guessed that letter. Try again!"
    	  elif ! echo $word | grep -i $guess > .temp1 ; then
    	      echo "Sorry, the letter \"$guess\" is not in the word."
    	      guessed="$guessed$guess"
    	      badguess=$(( $badguess + 1))
    	      life=$(( $life -1 ))
    	      echo " you have $life left"
    	  else
    	      echo "Good going! The letter $guess is in the word!"
    	      guessed="$guessed$guess"
    	      echo $letter
    	  fi
    	  
    	  ndx=0
    	  template2=""
    	  while [ $ndx != $(( $letters )) ] ; do
    	      val1=${word:ndx:1}
    	      val2=${template:ndx:1}
    	      if [ "$val1" == "$guess" ] ; then
    		  template2="$template2$guess"
    	      elif [ "$val2" != '.' ] ; then
    		  template2="$template2$val2"
    	      else
    		  template2="$template2."
    	      fi
    	      ndx=$(( $ndx + 1 ))
    	  done
    	  template=$template2
          done
    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.

    Comment

    • keyvanrahmadi
      New Member
      • Feb 2007
      • 57

      #3
      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

      Comment

      • arne
        Recognized Expert Contributor
        • Oct 2006
        • 315

        #4
        Originally posted by keyvanrahmadi
        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?

        Originally posted by keyvanrahmadi
        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 :)

        Originally posted by keyvanrahmadi
        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:

        Code:
        ${string/substring/replacement}
        which will replace 'substring' in 'string' with 'replacement'. Should work if 'substring' is equal to 'string' ...

        HTH,
        arne

        Comment

        • keyvanrahmadi
          New Member
          • Feb 2007
          • 57

          #5
          Originally posted by arne
          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:

          Code:
          ${string/substring/replacement}
          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

          Comment

          • arne
            Recognized Expert Contributor
            • Oct 2006
            • 315

            #6
            Originally posted by keyvanrahmadi
            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
            Code:
            while [ $remaining != $word ] ; do
            to
            Code:
            while [ $template != $word ] ; do
            and the code will exit the while loop if the word is complete.

            Originally posted by keyvanrahmadi
            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.

            Comment

            • keyvanrahmadi
              New Member
              • Feb 2007
              • 57

              #7
              Originally posted by arne
              Change
              Code:
              while [ $remaining != $word ] ; do
              to
              Code:
              while [ $template != $word ] ; do
              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..

              Code:
              blank="......................"
              #word1=getword
              word=$player2word
              letters=$(echo $word | wc -c)
              letters=$(( $letters - 1 ))
              template="$(echo $blank | cut -c1-$letters)"
              remaining=$letters
              i was going to change the template line to:
              Code:
              template1="$(echo $word | tr '[a-z A-Z 0-9]' '_')"
              what do you think?

              Thx

              Keyvan

              Comment

              • arne
                Recognized Expert Contributor
                • Oct 2006
                • 315

                #8
                Originally posted by keyvanrahmadi
                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.

                Originally posted by keyvanrahmadi
                i was going to change the template line to:
                Code:
                template1="$(echo $word | tr '[a-z A-Z 0-9]' '_')"
                what do you think?
                Looks good to me (and it's more elegant than using the length as I proposed :-) ).

                Comment

                • keyvanrahmadi
                  New Member
                  • Feb 2007
                  • 57

                  #9
                  Originally posted by arne
                  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.

                  Comment

                  • arne
                    Recognized Expert Contributor
                    • Oct 2006
                    • 315

                    #10
                    Originally posted by keyvanrahmadi
                    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.

                    Comment

                    • keyvanrahmadi
                      New Member
                      • Feb 2007
                      • 57

                      #11
                      Originally posted by arne
                      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

                      Comment

                      • arne
                        Recognized Expert Contributor
                        • Oct 2006
                        • 315

                        #12
                        Originally posted by keyvanrahmadi
                        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? ;-)

                        Comment

                        • keyvanrahmadi
                          New Member
                          • Feb 2007
                          • 57

                          #13
                          Originally posted by arne
                          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

                          Comment

                          • arne
                            Recognized Expert Contributor
                            • Oct 2006
                            • 315

                            #14
                            Originally posted by keyvanrahmadi
                            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 ...

                            Comment

                            • keyvanrahmadi
                              New Member
                              • Feb 2007
                              • 57

                              #15
                              Originally posted by arne
                              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.

                              Comment

                              Working...