Sonoma State University
Department of Computer Science
CS-460: Programming Languages
Exercise 14a

Objective:

With your team, enter the the following BASH program in your favourite editor on our Blue Unix server (blue.cs.sonoma.edu). The program should be named "word_scramble.sh".

  • Modify word_scramble.sh by replacing "word1", "word2", "word3", etc. with dictionary words or proper nouns.
word_scramble.sh
#!/bin/bash

# Array of words for the game
words=("word1" "word2" "word3" "word4" "word5" "word6" "word7" "word8" "word9" "word10" "word11" "word12" "word13" "word14" "word15" "word16" "word17" "word18" "word19" "word20")

# Function to shuffle a word
shuffle_word() {
    local word="$1"
    echo $(echo "$word" | fold -w1 | shuf | tr -d '\n')
}

# Select a random word from the array
selected_word=${words[$((RANDOM % ${#words[@]}))]}

# Shuffle the selected word
scrambled_word=$(shuffle_word "$selected_word")

# Game instructions
echo "Welcome to Word Scramble!"
echo "Unscramble the word: $scrambled_word"

# Prompt for user input
read -p "Your guess: " guess

# Check if the guess is correct
if [[ "$guess" == "$selected_word" ]]; then
    echo "Correct! Well done."
else
    echo "Incorrect. The word was: $selected_word"
fi

Preparation to make the BASH program executable:

  1. Once you've uploaded the program to Blue (blue.cs.sonoma.edu), be sure to enter the change mode command to make the program executable: chmod 750 word_scramble.sh

Submitting your solution

  • Please submit your modified BASH script for word_scramble.sh to Canvas.
  • Since this is a group assignment, please include the names of each member of your team along with "Exercise 14a" when submitting your solution.