Modify the Trivia Challenge program to allow for multiple players. You should create a Player class with the following members:
void SetNumber(int number) - sets the player's number
int GetScore() - returns the player's current score
void AskQuestion(Question & question) - asks the player the question
In addition, you will need to modify the Game class to have the following data members:
Player m_Players[NUM_PLAYERS] - player objects
int m_Current - current player number
And create the following Member functions:
void SetupPlayer() - sets up the player's objects
void NextPlayer() - sets current player number to next player number
Finally, change the SendScore() member function so it outputs a table:
Player Score
1 3000.00
2 2000.00
question.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
// Trivia Challenge
// Question definition - class represents a single question
#ifndef QUESTION_H
#define QUESTION_H
#include <string>
#include <fstream>
#include <istream>
#include <iostream>
using namespace std;
// Question class definition - for trivia question
class Question
{
public:
static const int NUM_ANSWERS = 4; //number of possible answers
Question(istream& episodeFile); //reads the question from the stream
void Ask(); //displays the question and answers
int ScoreAnswer(int answer); //scores an answer
private:
static const string CORRECT; //text for a correct answer
static const string WRONG; //text for a wrong answer
static const int CORRECT_ANSWER_SCORE = 1000; // score for a correct answer
string m_Category; //name of category
string m_Question; //question text
string m_Answers[NUM_ANSWERS]; //an array of the possible answers
int m_CorrectAnswer; //index of the correct answer
string m_Explanation; //reason why the answer is correct
};
#endif
|
episode.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
// Trivia Challenge
// Episode definition - class represents a single episode
#ifndef EPISODE_H
#define EPISODE_H
#include <string>
#include <fstream>
#include <iostream>
#include "question.h"
// Episode class definition - for trivia episode
class Episode
{
public:
//reads episode data from the file with the given name
Episode(const string& filename);
void Introduce(); //displays episode introduction
bool IsOn(); //tests if the episode is still on
Question NextQuestion(); //returns the next question
private:
string m_Name; //name of the episode
ifstream m_EpisodeFile; //episode data file
};
#endif
|
game.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
// Trivia Challenge
// Game definition - class represents a single game
#ifndef GAME_H
#define GAME_H
#include <istream>
#include <iostream>
#include <iomanip>
#include "episode.h"
// Game class definition - for the game itself
class Game
{
public:
Game();
void DisplayInstructions() const; //displays game instructions
int GetMenuResponse(int numChoices); //receives input from the player
int AskQuestion(Question& question); //asks and scores the question
void SendScore(ostream& os); //sends score to stream
void Play(); //plays a game
private:
Episode m_Episode; //episode for this game
int m_Score; //current score
};
#endif
|
question.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
|
// Trivia Challenge
// Question implementation - class represents a single question
#include "question.h"
//text for a correct answer
const string Question::CORRECT = "Correct!";
//text for a wrong answer
const string Question::WRONG = "Wrong!";
//reads the question from a stream
Question::Question(istream& episodeFile)
{
//read in the 8 lines that form a question
getline(episodeFile,m_Category);
getline(episodeFile,m_Question);
for (int i = 0; i < NUM_ANSWERS; i++)
{
getline(episodeFile,m_Answers[i]);
}
episodeFile >> m_CorrectAnswer;
episodeFile.ignore();
getline(episodeFile,m_Explanation);
//replace any forward slashes in question text with newlines
for (size_t i=0; i<m_Question.length(); ++i)
{
if (m_Question[i] == '/')
{
m_Question[i] = '\n';
}
}
//replace any forward slashes in explanation with newlines
for (size_t i=0; i<m_Explanation.length(); ++i)
{
if (m_Explanation[i] == '/')
{
m_Explanation[i] = '\n';
}
}
}
//displays the question and answers
void Question::Ask()
{
//display question and 4 answers with numbers
cout << m_Category << endl;
cout << m_Question << endl;
for (int i = 0; i < NUM_ANSWERS; i++)
{
cout << i+1 << ") " << m_Answers[i] << endl;
}
}
//scores an answer (and displays response)
int Question::ScoreAnswer(int answer)
{
int score;
// test if the answer is correct and respond appropriately
if (answer == m_CorrectAnswer)
{
cout << CORRECT << endl;
score = CORRECT_ANSWER_SCORE;
}
else
{
cout << WRONG << endl;
score = 0;
}
cout << m_Explanation;
cout << endl << endl;
return score;
}
|
episode.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
// Trivia Challenge
// Episode implementation - class represents a single episode
#include "episode.h"
//reads episode data from the file with the given name
Episode::Episode(const string& filename)
{
//attempt to open the file with the episode
m_EpisodeFile.open(filename.c_str(), ios::in);
if (m_EpisodeFile.fail())
//failed to open file
{
cout << "File " << filename;
cout << " could not be opened for reading." << endl;
exit(1);
}
//read episode name
getline(m_EpisodeFile, m_Name);
}
//prints out introduction to the episode
void Episode::Introduce()
{
cout << "Get ready to play... " << m_Name;
cout << endl << endl;
}
//tests whether there are questions left
bool Episode::IsOn()
{
return m_EpisodeFile.good();
}
//returns the next unasked question
Question Episode::NextQuestion()
{
return Question(m_EpisodeFile);
}
|
game.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
|
// Trivia Challenge
// Game implementation - class represents a single game
#include "game.h"
Game::Game() :
m_Episode("trivia.txt")
{}
//displays game instructions
void Game::DisplayInstructions() const
{
cout << "\tWelcome to Trivia Challenge!";
cout << endl << endl;
cout << "Correctly answer as many questions as possible." << endl;
cout << "You earn 1,000 points for each one you get right.";
cout << endl << endl;
}
//receives input from the player
int Game::GetMenuResponse(int numChoices)
{
int response;
//read the user's choice (must be valid)
do {
cout << "Enter your choice: ";
cin >> response;
} while(cin.good() && (response < 1 || response > numChoices));
if (cin.fail()) //exit if there was a problem
{
cout << endl << "Goodbye!" << endl;
exit(1);
}
cout << endl;
return response;
}
//asks the question and returns the score
int Game::AskQuestion(Question& question)
{
int response;
question.Ask();
response = GetMenuResponse(Question::NUM_ANSWERS);
return question.ScoreAnswer(response);
}
//send the player's score to the given stream
void Game::SendScore(ostream& os)
{
os << "Your final score is " << m_Score << ".";
cout << endl;
}
//plays a game
void Game::Play()
{
m_Score = 0;
m_Episode.Introduce();
//keep asking questions while there are more left
while(m_Episode.IsOn())
{
Question question = m_Episode.NextQuestion();
m_Score += AskQuestion(question);
}
//display score
SendScore(cout);
//write score
ofstream scoreFile("trivia_scores.txt", ios::out | ios::app);
SendScore(scoreFile);
scoreFile.close();
}
|
main.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
// Trivia Challenge
// main program
#include <iostream>
#include "game.h"
using namespace std;
//main function
int main()
{
Game trivia;
trivia.DisplayInstructions();
trivia.Play();
return 0;
}
|