首页 > > 详细

C++辅导:CS132 Shall We Play A Game解析C/C++ Assignment

Overview

This activity is designed to have you work with strings as a data type, as well as having you create a class and read from a data file.

Instructions

In this program you will be creating a program to play a trivia game. You will be provided with a data file that contains over 200 questions, and you will need to create a game that allows the user to pick a number of trivia questions and then determine if they are correct or now.

Each question will be stored internally as an object, and it will take control of the ability to determine if the chosen answer is correct or not. It will also have a constructor to parse the input string and break it into its component parts (Questions/correct answer/incorrect answers).

Your main program will take care of reading the file, creating the storage for all the questions, and asking the user how many questions.

The main program

Your main program should do the following:

  • It should open up the data file (“TriviaData.txt”) and take care of the strings in the file.
    • The first line of the file is a single integer that tells how many questions are coming.
    • Each remaining line is a single question with the answers following it.
  • Your program will then create a storage device to keep track of all the possible questions in either an array or a vector. Note that this needs to be an array/vector of TrivaQuestion. NOT an array/vector of strings, making this array is part of the grade for this assignment.
  • Once the data file is read and the questions are created it will ask the user how many questions they want to try.
  • It should then ask the user random questions from the list depending on how many questions they wanted. o Note that the main program only starts the question, the question class will take care of itself. - The main program should record the number of answers they got right and determine the final score.

The Triva Question class

For this assignment, you will create question class that handles all the actions for an individual question. The methods that you will need are the following

TRIVAQUESTION()

You will want a default constructor that doesn’t do anything, but does allow the object to be created.

TRIVAQUESTION(STRING)

You will also want a constructor that allows for the user to create the object with the data string so that it can be initialized if you so desire. This constructor will do the same thing as setup() below, but without having to be called.

SETUP(STRING)

This method is an alternative to the string constructor. After creating the trivaQuestion you can pass a string to it, to set up the question and the possible answer. Details on how to read the string are below.

ASKQUESTION()

This is the method that will print out the question, the possible answer, and wait for the user to type in what they think the answer will be. If they type in the correct answer, it will return a value of 1, or if they get it wrong, it will return a value of zero after telling the user what the correct answer was.

This method is entirely self-contained. It should ask the question and wait for the result. The main program will not deal with the question at all.

Note that the possible answers should be in a random order. So that if I run the program multiple times, the same answer is not always #1.

Values

Your program may have need of various other attributes/fields. But most of the time I would assume that you would have 5 fields

  • The question string
  • The correct answer string
  • The 3 incorrect answer strings.

The data

Each line of the data file except for the first is a single line that looks like the following

Which of the following did not feature in the cartoon apos;Wacky Racesapos;? :: quot;The Dragon Wagonquot; :: quot;The Bouldermobilequot; :: quot;The Crimson Haybailerquot; :: quot;The Compact Pussycatquot; 

You will note that the first part is the question and it ends with :: Following that is the correct answer. (The correct answer is always the first possible answer). That is followed by another :: and then the three wrong answers are also given with :: between them.

  • question = Which of the following did not feature in the cartoon ‘Wacky Races’?
  • correct = “The Dragon W agon”
  • incorrect1 = “The Bouldermobile”
  • incorrect2 = “The Crimson Haybailer”
  • incorrect3 = “The Compact Pussycat”

In your Trivia Question class, you will need to take this single string and break it down into the 5 component parts by using string functions like find, substr, erase, and others.

Because the correct answer is always #1, it is important to rearrange the possible answers when you output them.

Sample Output

Your final program should look something like this:

How many questions would you like to try? gt; 2 For what reason would a spotted hyena quot;laughquot;? 1. quot;Nervousnessquot; 2. quot;Aggressionquot; 3. quot;Exhaustionquot; 4. quot;Excitementquot; Your answer please gt;gt; 1 Nice Job!! Which Russian oblast forms a border with Poland? 1. quot;Nizhny Novgorodquot; 2. quot;Samaraquot; 3. quot;Kaliningradquot; 4. quot;Omskquot; Your answer please gt;gt; 4 Not quite right. The answer was : quot;Kaliningradquot; Your score was 1 correct for 50%. 

Submission

You will be expected to turn in 3 files

  • main.cpp
  • TriviaQuestion.h
  • TriviaQuestion.cpp

Before submitting your assignment, include a comment at the beginning of your program with some basic information and a description of the program in your own words. For example:

Make sure to comment appropriately and document all your major points.

Program Notes

Read from the file first. Even if all you do is print it to the screen, get that working first.

Create the class and get it working in general before trying to parse the string.

The program is slightly easier if you can use and figure out vectors, but it works with arrays as well

Ask help before the due date.

A sample EXE file is provided so you can see what it is supposed to do.

To read the number from the file, followed by the lines, you will need to use a mix of gt;gt;, .ignore, and getline.

联系我们
  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp
热点标签

联系我们 - QQ: 99515681 微信:codinghelp
程序辅导网!