Hello there guys,
i think i am at the end of this, which i have to edmit has been a huge learning curve for me, but i still have couple of problems, which i was hoping you can help me. I think tbh its a very simple thing which i am over looking and just require a pair of fresh eyes to look at. I greatly appriciate if you could have a look at the script and let me know what you think. if you have any solution, or advise just email me or pm here..
player 1 option scripts:
The problem is when asked if you want to play again, when you say yes, it kicks out of the script after printing the followig line:
player 2 option scripts:
Thats the only line which is different everything else is the same . The script again kicks out after it has asked you choose a word for player 2 to guess, during replay.
I created 3 function:
1- stored_words: words used for 1-player game.
2- Replay
3- Startup
so to sumerise and stop boring a hell out of you, the problems are:
1- the looping so the players can continue if they decide to play again.
2- combine both scripts into one
K
i think i am at the end of this, which i have to edmit has been a huge learning curve for me, but i still have couple of problems, which i was hoping you can help me. I think tbh its a very simple thing which i am over looking and just require a pair of fresh eyes to look at. I greatly appriciate if you could have a look at the script and let me know what you think. if you have any solution, or advise just email me or pm here..
player 1 option scripts:
Code:
#!/bin/bash
function stored_word()
{
case $(( $$ % 8 )) in
0 ) echo "energy" ;; 1 ) echo "touch" ;;
2 ) echo "climbing" ;; 3 ) echo "declare" ;;
4 ) echo "marry" ;; 5 ) echo "relax" ;;
6 ) echo "bugs" ;; 7 ) echo "inaccessible" ;;
esac
}
function startup() {
echo "Choose number of Players:"
read players
if [ $players -eq 1 ] ; then
echo " A Random word has been chosen... Enjoy the Game"
elif [ $players -eq 2 ] ; then
echo "choose a word for player two to guess:"
read player2word
else
echo "incorrect option..."
replay
fi
}
function replay() {
echo " Do you wish to play again? TYPE \"y\" to play again or \"n\" to quit"
read choice
if [ $choice == y ] ; then
startup
else
echo "Hope you enjoyed the game"
exit 0
fi
}
startup
life=10
word=$(stored_word)
letters=$(echo $word | wc -c)
letters=$(( $letters - 1 ))
template="$(echo $word | tr '[a-z A-Z 0-9]' '.')"
remaining=$letters
echo " ****The word you are trying to guess has \"$letters\" letters ****"
while [ $template != $word ] ; do
echo""
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!"
guessed="$guessed$guess"
echo $letter
elif ! echo $word | grep -i $guess > .temp1 ; then
echo "Sorry, the letter \"$guess\" is not in the word."
guessed="$guessed$guess"
badguesses=$(( $badguesses + 1))
echo "$guessed"
life=$(( $life -1 ))
echo " you have $life life left"
else
echo "Good going! The letter $guess is in the word!"
guessed="$guessed$guess"
echo $letter
fi
if [ $life == 0 ]; then
echo "THE WORD YOU WERE TRYING TO GUESS WAS \"$word\""
replay
exit 0
fi
lettercount=0
template2=""
while [ $lettercount != $(( $letters )) ] ; do
val1=${word:lettercount:1}
val2=${template:lettercount:1}
if [ "$val1" == "$guess" ] ; then
template2="$template2$guess"
elif [ "$val2" != '.' ] ; then
template2="$template2$val2"
else
template2="$template2."
fi
lettercount=$(( $lettercount + 1 ))
done
template=$template2
done
echo -n "Congratulations! You guessed \"$word\" in \"$guesses\" guesses"
echo " with $badguesses incorrect guesses"
replay
exit 0
Code:
echo " A Random word has been chosen... Enjoy the Game"
player 2 option scripts:
Code:
startup life=10 word=$player2word
I created 3 function:
1- stored_words: words used for 1-player game.
2- Replay
3- Startup
so to sumerise and stop boring a hell out of you, the problems are:
1- the looping so the players can continue if they decide to play again.
2- combine both scripts into one
K
Comment