Goals

In this assignment, you will practice the following computer science concepts:

Computer Science Principles Curriculum

Common Core Standards

Overview

In this assignment, you will create a program that plays a simple trivia game. This assignment allows you to create your own questions and answers for the game. Download this zip file for a template to help you get started. Open the provided project template and navigate to the main.quorum file.

Activity 1: Write the questions and answers.

This assignment stores the questions and answers for your game in separate ".txt" files. This allows you to update or change your questions/answers without having to modify your program code. Create two ".txt" files, one for the questions and one for the answers. Come up with the questions and answers for your game and put them in the appropriate files, one per line.

Understanding FileReaders

Because your questions and answers are stored in separate ".txt" files, your program needs to read those files in order to access your questions. To do this, you need to use the FileReader object from the Libraries.System.FileReader library. The FileReader object reads a Quorum File object sequentially starting from the beginning of the file. To store the questions and answers during the program you use an object known in Quorum as an Array. An Array is simply a container that stores items in a contiguous block of memory. To use the Array object, you need to include the Libraries.Containers.Array library.

Activity 2: Tell Quorum you want to use the FileReader, File and Array libraries, declare the FileReader and File objects for the questions and answers, and set the paths of the files.

In order to use FileReader objects, you first need to declare both File and FileReader objects. Since you have 2 files, one for questions and one for answers, you need two File objects, one that represents the "questions.txt" file and another that represents the "answers.txt" file. You also need two FileReader objects, one to read the "questions.txt" file and another to read the "answers.txt" file. You also need to set the paths of the File objects to your questions and answers files.

Your program needs a place to store the questions and answers read from the files. To do this, you use arrays.


Example: Declare Array objects to store the questions and answers.

// this line declares an array called questions. The <text> part tells the Array
// that it will store text
Array<text> questions

// this line sets the y position of our birds audio object.
// the sound will be coming from above our heads
birds:SetY(0.9)

// this line declares an array called answers. The <text> part tells the Array
// that it will store text
Array<text> answers

Goal 1: Reading Files

To get your questions and answers from their files into your program, you need to tell your program to read the files. In order to read a file, it must first be opened. In Quorum, you must also tell files what they are being opened for (reading, writing, reading and writing). Since we are going to be reading our file once it has been opened, we use the OpenForRead action on our FileReader objects. This action takes a single parameter that represents the File object we want to open for read.

Activity 3: Open the questions and answers files for read using the OpenForRead action on your FileReader objects.

Now, your file is open and ready to read. There are many options for actions to read your file. The Read action reads and returns the entire contents of the file. The ReadLine action reads and returns a single line from the file. The ReadLines action reads and returns all the remaining lines of the file starting from the current position. You can know if you have read the entire file or not by calling the IsAtEndOfFile action. This action returns true if the end of the file has been reached and false otherwise.


Example: Use the questionsReader object to read the questions file one line at a time into the questions array until the end of the file has been reached.

// This line initially reads the file. This is needed before calling the 
// IsAtEndOfFile action
text question = questionsReader:ReadLine()

// this line adds the question we just read into the questions Array
questions:Add(question)

// this repeat statement reads the rest of the file until the end of the file is
// reached
repeat until questionsReader:IsAtEndOfFile()

   // this line reads in another line from the file
   question = questionsReader:ReadLine()

   // this line adds the question we just read into the questions Array
   questions:Add(question)
end

The answers file is read in the exact same way.

Activity 4: Close the FileReader objects.

Once you have read the contents of your questions and answers files into their respective arrays, you are done with those files and FileReader objects. In Quorum, once you are done using a file that you have opened in your program, you need to tell Quorum that you are done with it and that it is okay to close the file. You can do this by calling the Close action on your FileReader objects.

Goal 2: Using Control Structures

Once the program has read all of the questions and answers, it should start the game. Your program should output all of the questions and ask the user for answers. The user input is captured using the input command. The user input should be checked against the answer, and if the answer is correct, a sound should be played. If the answer is not correct, a different sound should be played. For the convenience of whoever is playing your game, you may want to ignore any whitespace before and after their answer. Calling the Trim action on the text they inputted will accomplish this.


Example: Repeatedly ask the user questions and get their answers, checking the user’s entered answer against the correct answer. If the user is correct, output a message and play a sound. If the user is incorrect, output a different message and play a different sound.

integer counter = 0           // this variable keeps track of where we are in our arrays

// the GetSize action returns the number of elements in an array. We want to
// go through each element in the array in order to ask each question.
repeat questions.GetSize() times
  // the Get action returns the element in the array at the passed integer
   // index. Counter is the variable we use to keep track of where we are in
   // the array
   output questions:Get(counter)

  // this line gets the user’s input (i.e., their guess)
   text guess = input() 

   // this line gets the correct answer to the question from the answers array
  text correctAnswer = answers:Get(counter)

   // this if statement checks the user’s input against the answer to the
   // question. The EqualsIgnoringCase action will return true if the user’s
   // guess is the same as the answer ignoring upper/lowercase.
   if guess:EqualsIgnoringCase(correctAnswer)
      // output correct message
       // play correct sound
   else
      // output incorrect message
       // play incorrect sound
   end

  // this line updates the counter variable so that we can get the next
   // element in the arrays
   counter = counter + 1
end

Once the user has answered all of the questions, you should output a message that informs them that the game is over.

Optionally, you may add more sound events to your game or use the Quorum say keyword to have the program read your questions to the user out loud.

Sample Output

Because the students create their own questions, the output will vary. Here is one example:

What statement in Quorum would you use to execute code only under certain conditions?
if
You are correct!

What is the name of the type in Quorum that stores numbers with decimal points?
number
You are correct!

What is the name of the type in Quorum that stores whole numbers?
integer
You are correct!

What is the name of the type in Quorum that stores words?
text
You are correct!

Which keyword in Quorum allows you to perform tasks over and over again?
repeat
You are correct!

What is the name of the type in Quorum that stores values that are either true or false?
boolean
You are correct!

What do you call a statement that is ignored by the compiler?
comment
You are correct!

What is the keyword that allows you to include a library in your Quorum project?
use
You are correct!

EGame Over. You got 8/8 questions right!
Thanks for playing!

Next Tutorial

In the next tutorial, we will discuss Challenge 3.3, which describes how work Trivia Challenge - Visual in Quorum..