How to do the Hangman game using Shell Scripts
 
This is the first of a full serie of tutorials that I will be doing every Tuesday of making videogames using Shell Scripts. I will suppose that you know what is Shell Scripts and how to execute scripts in a Linux terminal so i will skip that part. If you dont know how to do it, just search it on Google or send me a message. (You will need some knowledge of Shell Script commands, or ask me the commands that you don't understand)

The first thing that must come to your mind when you are going to do a script is the algorithm that it will have: 

For the Hangman I thought that the first method that I need was one that creates a word to play:

There are several ways to do it for example using a database of words, but in this case I choose the easiest for me that is to write the word. The first function of the program will be like this: (read all the code carefully)

WORD(){
echo "Welcome to the hangman!" #echo displays the characters in the terminal
echo -n "Write a word: "
read word #read allows us to write something and save it in the variable $word
letters=`echo "$word" | grep -o .` #The grep -o . separates the word in letters so we can acces every word in a "for" loop and saves the result in the variable $letters
numberLetters=`echo "$word" | wc -m` #counts the number of letters the word has
let numberLetters=$numberLetters-1 #we need to substract 1 because this method doesn't counts well
echo "The word $word has $numberLetters letters"
}

Now that we have the word, we must create 2 arrays, 1 with empty slots so we can fill the slots with letters when we guess the letter and another array with the word so we can check the letters and the position we must fill in the empty slots.

(Please read carefully and try to understand what it does.)


SLOTS(){
counter=0
while [ $counter -lt $numberLetters ] #Does this loop till the counter is equal to the number of letters 
do
slot[$counter]="_" #Fills the array slot in the position $counter with a "_"
let counter=$counter+1 #let allows to make mathematical operations
done 
echo ${slot[*]} #shows all the array
}


ARRAYWORD(){
counter=0
for eachLetter in $letters #this loop "for" navigates all the letters of the word and $eachLetter becomes that one letter every time it does the loop. 
do
word[$counter]="$eachLetter" #Fills the array word in the position $counter with $eachLetter
let counter=$counter+1
done 
echo ${word[*]} 
}



Now that we got that, we just need a loop that keeps looping till we filled all the array slots with all the letters.



WRITELETTER(){
arraySlot=`echo ${slot[*]}`  #saves all the arrays in this variables
arrayWord=`echo ${word[*]}`  #so then we can check out if the array of slots is equal to the array of words
while [ "$arraySlot" != "$arrayWord" ] #and in that case we end up the loop and the script
do
echo -n "Write one letter: "
read chosenLetter
counter=0
for eachLetter in $letters #does this loop for all the letters in the word
do
if [ "$eachLetter" = "$chosenLetter" ] #if the letter written is equal to the actual letter in the "for" loop in the word 
then
slot[$counter]="$chosenLetter" #fills the slot in the same position ($counter) with the $chosenLetter
fi
let counter=$counter+1 #we use $counter because is necessary to fill the slot in the same position as it is in the actual word
done
echo ${slot[*]}
arraySlot=`echo ${slot[*]}`
arrayWord=`echo ${word[*]}`
done


And combining all of that, the main program will be like this:


WORD
SLOTS
ARRAYWORD
WRITELETTER


Finally, the entire script:


#!/bin/bash

#The Hangman!

##FUNCTIONS##

WORD(){

echo "Welcome to the hangman!"

echo -n "Write a word: "

read word

letters=`echo "$word" | grep -o .`

numberLetters=`echo "$word" | wc -m`

let numberLetters=$numberLetters-1

echo "The word $word has $numberLetters letters"

}


SLOTS(){

counter=0

while [ $counter -lt $numberLetters ]

do

slot[$counter]="_"

let counter=$counter+1

done 

echo ${slot[*]}

}


ARRAYWORD(){

counter=0

for eachLetter in $letters

do

word[$counter]="$eachLetter"

let counter=$counter+1

done 

echo ${word[*]}

}


WRITELETTER(){

arraySlot=`echo ${slot[*]}`

arrayWord=`echo ${word[*]}`

while [ "$arraySlot" != "$arrayWord" ]

do

echo -n "Write one letter: "

read chosenLetter

counter=0

for eachLetter in $letters

do

if [ "$eachLetter" = "$chosenLetter" ]

then

slot[$counter]="$chosenLetter"

fi

let counter=$counter+1

done

echo ${slot[*]}

arraySlot=`echo ${slot[*]}`

arrayWord=`echo ${word[*]}`

done

}

##MAIN PROGRAM##

WORD

SLOTS

ARRAYWORD

WRITELETTER

Now its your turn, try to do it by yourself or just improve this script. I did it in exactly 48 minutes so you can see that many things are missing like counting the number of tries, the hanged or correct some errors like you cant intruduce numbers or you cant introduce a letter that you have already wrote. I will say the name of the one who does the best Hangman the next week. And also, have fun playing with your friends!