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

Objective:

Write a simple Jeopardy! game using the Bourne-again shell (BASH) scripting language.

Instructions:

I've written two BASH scripts (see below) to get you started. I've also included a required Jeopardy questions database in JSON format:

The BASH script named jeopardy.sh contains incomplete code to emulate a simple Jeopardy! game. The jeopardy script reads a JSON database containing real questions and answers from the Jeopardy! game show. The jeopardy script randomly selects a question and its appropriate answer from the jeopardy JSON database.

The BASH script named compare_string_similarity.sh demonstrates how to read user input from the keyboard and compare that user input with a string.

Requirements:

  • Modify the jeopardy.sh BASH script to prompt the user to enter an answer for a randomly selected Jeopardy question.
  • Compare the user's answer with the correct answer associated with the randomly selected Jeopardy question. It is OK to conduct EXACT string matches. I know, not very fun in real-world but it will be good enough for this exercise. If the user's answer matches the correct Jeopardy answer (for the given question) then output "Correct answer" (or something to that effect) and increment a variable for number of correct answers. If the user's answer is incorrect then output the correct answer.
  • The program should prompt the user to enter TEN Jeopardy questions. After prompting the user to answer ten Jeopardy questions, display the user's score (number of questions answered correctly).
jeopardy.sh
#!/usr/bin/bash

num_questions=$(jq -r '.[].question' JEOPARDY_QUESTIONS.json | wc -l)
num_correct_answers=0
counter=1
while [ $counter -le 10 ]
do
  question_number=$(shuf -n 1 -i 1-$num_questions)
  jeopardy_category=$(jq -r ".[$question_number].category" JEOPARDY_QUESTIONS.json)
  jeopardy_question=$(jq -r ".[$question_number].question" JEOPARDY_QUESTIONS.json)
  jeopardy_answer=$(jq -r ".[$question_number].answer" JEOPARDY_QUESTIONS.json)
  echo "Category: $jeopardy_category"
  echo "Jeopardy question: $jeopardy_question"
  echo "Answer: $jeopardy_answer"
  echo
  ((counter++))
done
compare_string_similarity.sh
#!/usr/bin/bash

read -r -p "Enter a string or 'x' to quit: " user_input
if [ "$user_input" != "x" ];
then
  echo "You entered: $user_input"
fi
while [ "$user_input" != "x" ];
do
  read -r -p "Enter a string or 'x' to quit: " user_input
  if [ "$user_input" != "x" ];
  then
    echo "You entered: $user_input"
  fi
done

Preparation to make the BASH program executable:

  • Once you've uploaded the program to Blue (blue.cs.sonoma.edu), be sure to make the BASH programs executable: chmod 750 jeopardy.sh compare_string_similarity.sh

Submitting your solution

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