C++Project

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;
}

Last edited on
trivia.txt
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
An Episode You Can't Refuse
On the Run With a Mammal
Let's say you turn state's evidence and need to "get on the lamb." If you wait /too long, what will happen?
You'll end up on the sheep
You'll end up on the cow
You'll end up on the goat
You'll end up on the emu
1
A lamb is just a young sheep.
The Godfather Will Get Down With You Now
Let's say you have an audience with the Godfather of Soul. How would it be /smart to address him?
Mr. Richard
Mr. Domino
Mr. Brown
Mr. Checker
3
James Brown is the Godfather of Soul.
That's Gonna Cost Ya
If you paid the Mob protection money in rupees, what business would you most /likely be insuring?
Your tulip farm in Holland
Your curry powder factory in India
Your vodka distillery in Russian 
Your army knife warehouse in Switzerland
2
The Rupee is the standard monetary unit of India.
Keeping It the Family
If your mother's father's sister's son was in "The Family," how are you related/to the mob?
By your first cousin once removed
By your first cousin twice removed
By your second cousin once removed
By your second cousin twice removed
1
Your mother's father's sister is her aunt -- and her son is your /mother's first cousin. Since you and your mother are exactly one generation /apart, her first cousin is your first cousin once removed.
A Maid Man
If you were to literally launder your money, but didn't want the green in your /bills to run, what temperature should you use?
Hot
Warm
Tepid
Cold
4
According to my detergent bottle, cold is best for colors that might run.
The finished solution(didn't follow the instructions exactly):

Player.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#pragma once

#include "question.h"

class Player
{
public:
	void SetNumber(int number);
	int GetScore();
	void AskQuestion(Question & question);
private:
	int m_Score;
	int m_Number;

};


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
30
31
32
33
34
35
// Trivia Challenge
// Game definition - class represents a single game

#ifndef GAME_H
#define GAME_H

#include <istream>
#include <iostream>
#include <iomanip>

#include "episode.h"
#include "Player.h"

// Game class definition - for the game itself
class Game
{
public:
	static const int NUM_PLAYERS = 5;
	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
	void SetupPlayers();
	void NextPlayer();

private:
	Player m_Players[NUM_PLAYERS];
	Episode m_Episode;                      //episode for this game
	int m_Score;                            //current score
	int m_Current;
};

#endif 


Player.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include "Player.h"

void Player::SetNumber(int number)
{
	m_Number = number;
}
int Player::GetScore()
{
	return m_Score;
}
void Player::AskQuestion(Question & question)
{
	int ans;
	question.Ask();
    // get answer
    cin >> ans;
    cout << endl;
    m_Score += question.ScoreAnswer(ans);
}


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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// 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 << "Player " << " Score" << endl;
	for (int i = 0; i < NUM_PLAYERS; i++)
	{
		os << i << "  " << m_Players[i].GetScore();//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())
	{
		cout << "For Player " << m_Current << endl;
		Question question = m_Episode.NextQuestion();
		m_Players[m_Current].AskQuestion(question);

		//m_Score += AskQuestion(question);

		NextPlayer();
	}

	//display score
	SendScore(cout);

    //write score
	ofstream scoreFile("trivia_scores.txt", ios::out | ios::app);
	SendScore(scoreFile);
	scoreFile.close();
}

void Game::SetupPlayers()
{
	for (int i = 0; i < NUM_PLAYERS; i++)
	{
		m_Players[i].SetNumber(i);
	}
}
void Game::NextPlayer()
{
	if (m_Current == NUM_PLAYERS - 1)
	{
		m_Current = 0;
	}
	else
	{
		m_Current++;
	}
}
Topic archived. No new replies allowed.