Question

In: Computer Science

Create a game of your choice that involves the following concepts:

C++ PROJECT
Create a game of your choice that involves the following concepts:
STL library. (Maps, Sets, Lists, Stacks and Queues), with Iterators and Algorithms
Show especially Especially Algorithm/Iterators/Containers in the STL
Minimum of 750 lines
Include Full documentation

Solutions

Expert Solution

// File Name: MapQuiz.cpp
#include
#include
#include
#include
#include
#include
#include
using namespace std;

// Function prototypes
void split(const string&, char, vector&);
void getMappings(map &);
void quiz(map, int);

// main function definition
int main()
{
// Creates a constant for 5 questions
const int NUM_QUESTIONS = 5;
// Declares a map for capitals
map capitals;

// Calls the function to build the map.
getMappings(capitals);

// Calls the function to fun the quiz.
quiz(capitals, NUM_QUESTIONS);

system("pause");

return 0;
}// End of main function

//**************************************************************
// The split function splits s into tokens, using delim as the delimiter. *
// The tokens are added to the tokens vector. *
// (See Chapter 10 in your textbook for more information about this function.) *
//**************************************************************
void split(const string& s, char delim, vector& tokens)
{
// To store the city and capital after split
string state = "", capital = "";
// Finds the delimiter in the string and store the delimiter index position
int pos = s.find(delim);
// Extract the city from the string s, from zero index position to the delimiter index position
state = s.substr (0, pos);
// Extract the capital from the string s, from delimiter index position to the end position of the string s
capital = s.substr(pos + 1);
// Adds the city and capital to the vector
tokens.push_back(state);
tokens.push_back(capital);
}// End of function

//**************************************************************
// The getMappings function reads the states and capitals
// from a file named StateCapitals.txt and stores them as
// key-value pairs in the capitals parameter, that is passed by reference.
//**************************************************************
void getMappings(map &capitals)
{
// ifstream object declared
ifstream rFile;
// Declares a vector
vector tokens;
// To store data read from file
string data;
// Counter for city and capital
int c = 0;
// Opens the file for reading
rFile.open("StateCapitals.txt");
// Check that file can be opened or not
// is_open() function returns true if a file is open and associated with this stream object.
// Otherwise returns false.
if(!rFile.is_open())
{
// Displays error message
cout<<"\n Error: Unable to open the file! ";
return;
}// End of if condition

// Loops till end of file
while(!rFile.eof())
{
// Reads one line of data from file and stores it in body
getline(rFile, data);
// Calls the function to split the city and capital by the delimiter ':'
split(data, ':', tokens);
// Adds the city and capital from the vector to map
capitals.insert(pair(tokens[c], tokens[c + 1]));
// Increase the counter by two for pair of city and capital
c+=2;
}// End of while loop
}// End of function

//**************************************************************
// The quiz function to play the quiz for 5 questions.
// Generates a random number and ask the capital name for the random question generated from map.
// Compares the map capital with the state to check for correct answer.
// Keeps the counter for correct and incorrect answer
//**************************************************************
void quiz(map capitals, int numQuestions)
{
// Generates iterator for map
map::iterator it;
// To store the state name entered by the user
string state;
// Lower and higher range of random number
int low = 1, high = 28;
// Counter for random question
int c = 0;
// Counter for question answer
int correct = 0, incorrect = 0;
cout<<"\n I'm going to ask you for the capitals of 5 states."< srand((unsigned)time(0));
// Loops till number of questions (5)
for(int x = 0; x < numQuestions; x++)
{
// Generates a random number
int question = low + (rand() % high);
// Iterates till end of map
for ( it = capitals.begin(); it != capitals.end(); it++ )
{
// Increase the counter by one
c++;
// Checks if the question number generated randomly is equals to the question counter
if(c == question)
// Come out of the loop
break;
}// End of for loop
// Asks the question by taking the state name as the first value of the iterator
cout<<"\n What is the capital of "<first;
getline(cin, state);
state = " " + state;
// Compares the capital entered by the user and the capital stored in map second
if(state.compare(it->second) == 0)
{
// Increase the correct counter by one
correct++;
// Display the message
cout<<"Correct"< }// End of if condition
// Otherwise wrong
else
{
// Displays the message
cout<<"Sorry, that's incorrect."< // Increase the wrong counter by one
incorrect++;
}// End of else
// Reset the question counter to zero for next question
c = 0;
}// End of for loop
// Displays correct and wrong count
cout<<"\n =======================================";
cout<<"\n End of the quiz.";
cout<<"\n Correct answers: "< cout<<"\n Incorrect answers: "< cout<<"\n =======================================\n";
}// End of main function

Sample Output:

I'm going to ask you for the capitals of 5 states.

What is the capital of West Bengal Gangtok
Sorry, that's incorrect.

What is the capital of Rajasthan Jaipur
Correct

What is the capital of Orissa Bhubaneswar
Correct

What is the capital of Punjab Chandigarh
Correct

What is the capital of Uttar Pradesh Dehradun
Sorry, that's incorrect.

=======================================
End of the quiz.
Correct answers: 3
Incorrect answers: 2
=======================================
Press any key to continue . . .

stateCapitals.txt file contents

Andhra Pradesh : Hyderabad
Arunachal Pradesh : Itanagar
Assam : Dispur
Bihar : Patna
Go : Panaji
Gujarat : Gandhinagar
Haryana : Chandigarh
Himachal Pradesh : Shimla
Karnataka : Bangalooru
Kerala : Thiruvananthapuram
Madhya Pradesh : Bhopal
Maharashtra : Mumbai
Manipur : Imphal
Meghalaya : Shillong
Mizoram : Aizawl
Nagaland : Kohima
Orissa : Bhubaneswar
Punjab : Chandigarh
Rajasthan : Jaipur
Sikkim : Gangtok
Tamil Nadu : Chennai
Tripura : Agartala
Uttar Pradesh : Lucknow
West Bengal : Kolkata
Chhattisgarh : Raipur
Uttarakhand : Dehradun
Jharkhand : Ranchi
Telangana : Hyderabad


Related Solutions

C++ PROJECT Create a sudoku game that involves the following concepts: STL library. (Maps, Sets, Lists,...
C++ PROJECT Create a sudoku game that involves the following concepts: STL library. (Maps, Sets, Lists, Stacks and Queues), with Iterators and Algorithms Show especially Especially Algorithm/Iterators/Containers in the STL Minimum of 900 lines Include Full documentation
OOP involves use of the following concepts: inheritance, polymorphism, and encapsulation. What are your definitions of...
OOP involves use of the following concepts: inheritance, polymorphism, and encapsulation. What are your definitions of these terms? Why are they important? Does a program necessarily suffer if one or more of these items are absent?
Create a project that illustrates your understanding of ONE of the following concepts related to biological...
Create a project that illustrates your understanding of ONE of the following concepts related to biological influences on behavior: -Everyday examples of the different functions of the two hemispheres of the brain -Everyday examples of neuroplasticity—that is, of the way the brain is changed and shaped by experience -Examples of how hormones affect a person’s daily life -The cortical lobes involved in the following behaviors: -Seeing faces in the audience. -Hearing questions from the audience. -Remembering where your car is...
create a website that describes a city of your choice. The website must include the following...
create a website that describes a city of your choice. The website must include the following pages:  Home Page  Arts & Culture  News & Events  Recreation Requirements Write from scratch the HTML and CSS files and show your creativity, aesthetics and imagination. Begin by creating a wireframe diagram that indicates the structure of the web page . Use grammatically correct sentences that provide reasonable flow through each web page. Include the following:  Structural elements (header,...
__________ Involves connecting concepts to measurements operations ?
__________ Involves connecting concepts to measurements operations ?
A prisoner's dilemma is a game that involves no dominant strategies. a game in which players...
A prisoner's dilemma is a game that involves no dominant strategies. a game in which players should work together towards the Nash Equilibrium. a game where if players act in rational and self-interested ways everyone will be worse off than a cooperative outcome. a game in which players collude to outfox authorities.
Use the following transactions to create transactions for a service business of your choice: Al Ain...
Use the following transactions to create transactions for a service business of your choice: Al Ain Services Company had the following transactions for January 2020. January 1 The owner invested in the company cash $70,000 and Equipment $25,000. January 2 The company purchased office supplies for $4,200, on account January 5 Paid $600 for an insurance covering the full year January 7 Provided a service to a client and received $27,500 cash. January 9 The company borrowed $20,000 cash from...
Using a Python environment of your choice – Create the following three dictionaries: Gary, Alice, and...
Using a Python environment of your choice – Create the following three dictionaries: Gary, Alice, and Tyler Gary = { "name": "Gary", "homework": [90.0,97.0,75.0,92.0], "quizzes": [88.0,40.0,94.0], "tests": [75.0,90.0] } Alice = { "name": "Alice", "homework": [100.0, 92.0, 98.0, 100.0], "quizzes": [82.0, 83.0, 91.0], "tests": [89.0, 97.0] } Tyler = { "name": "Tyler", "homework": [0.0, 87.0, 75.0, 22.0], "quizzes": [0.0, 75.0, 78.0], "tests": [100.0, 100.0] } Insert one additional (new) dictionary of your choice Create a new list named students that...
Using a Python environment of your choice – Create the following three dictionaries: Gary, Alice, and...
Using a Python environment of your choice – Create the following three dictionaries: Gary, Alice, and Tyler Gary = { "name": "Gary", "homework": [90.0,97.0,75.0,92.0], "quizzes": [88.0,40.0,94.0], "tests": [75.0,90.0] } Alice = { "name": "Alice", "homework": [100.0, 92.0, 98.0, 100.0], "quizzes": [82.0, 83.0, 91.0], "tests": [89.0, 97.0] } Tyler = { "name": "Tyler", "homework": [0.0, 87.0, 75.0, 22.0], "quizzes": [0.0, 75.0, 78.0], "tests": [100.0, 100.0] } Insert one additional (new) dictionary of your choice Create a new list named students that...
Question 1. Your task is to create a full program for a Restaurant of your choice....
Question 1. Your task is to create a full program for a Restaurant of your choice. You will have the output appear at first saying Welcome to ______ Restaurant where the _____ will be whatever you name your restaurant Today’s choices are 1.__________ 2._________ 3._______ 4.________ NOTE each _____ will say an item and its price for example pizza 2.99 Please choose 1 of the choices. In each case, as the user picks a choice It will calculate the total...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT