Question

In: Computer Science

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

Solutions

Expert Solution

// File Name: MapQuiz.cpp
#include <iostream>
#include <fstream>
#include <locale>
#include <vector>
#include <map>
#include <cstdlib>
#include <ctime>
using namespace std;

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

// main function definition
int main()
{
// Creates a constant for 5 questions
const int NUM_QUESTIONS = 5;
// Declares a map for capitals
map<string, string> 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<string>& 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<string, string> &capitals)
{
// ifstream object declared
ifstream rFile;
// Declares a vector
vector<string> 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<string, string>(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<string, string> capitals, int numQuestions)
{
// Generates iterator for map
map<string, string>::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."<<endl;
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 "<<it->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"<<endl;
}// End of if condition
// Otherwise wrong
else
{
// Displays the message
cout<<"Sorry, that's incorrect."<<endl;
// 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: "<<correct;
cout<<"\n Incorrect answers: "<<incorrect;
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

Create a game of your choice that involves the following concepts:
C++ PROJECTCreate a game of your choice that involves the following concepts:STL library. (Maps, Sets, Lists, Stacks and Queues), with Iterators and AlgorithmsShow especially Especially Algorithm/Iterators/Containers in the STLMinimum of 750 linesInclude Full documentation
I'm having trouble programming connect four board game using linked lists, sets and maps in c++....
I'm having trouble programming connect four board game using linked lists, sets and maps in c++. Can you code connect four game using these concepts.
Please Use the STL library for this question and Should be done in C++ and Provide...
Please Use the STL library for this question and Should be done in C++ and Provide screenshots of the OUTPUT Topic: Stack Infix to postfix conversion         Take Input mathematical expression in infix notation, and convert it into a postfix notation. The infix notation may or may not have round parenthesis. Postfix expression evaluation         Take Input mathematical expression in postfix form, evaluate it, and display the result The mathematical expression can contain Numbers as operands (numbers can be of...
Create a C++ program using native C++ (STL is acceptable) that produces Huffman code for a...
Create a C++ program using native C++ (STL is acceptable) that produces Huffman code for a string of text entered by the user. Must accept all ASCII characters. Provide an explanation for how you got frequencies of characters, how you sorted them, and how you got codes.
Code in C++ Objectives Use STL vector to create a wrapper class. Create Class: Planet Planet...
Code in C++ Objectives Use STL vector to create a wrapper class. Create Class: Planet Planet will be a simple class consisting of three fields: name: string representing the name of a planet, such as “Mars” madeOf: string representing the main element of the planet alienPopulation: int representing if the number of aliens living on the planet Your Planet class should have the following methods: Planet(name, madeOf, alienPopulation) // Constructor getName(): string // Returns a planet’s name getMadeOf(): String //...
Create a Snake game project in C++ using all these which are given below. 1. Classes...
Create a Snake game project in C++ using all these which are given below. 1. Classes 2. structures 3. files ( MULTFILLING AND FILE HANDLING ) 4. loops 5. conditions 6. switch 7. functions 8. array Note: Also upload the zip file.
Can you program Exploding kittens card game in c++ using linked lists and classes! The game...
Can you program Exploding kittens card game in c++ using linked lists and classes! The game is played with between 2 and 4 players. You'll have a deck of cards containing some Exploding Kittens. You play the game by putting the deck face down and taking turns drawing cards until someone draws an Exploding Kitten. When that happens, that person explodes. They are now dead and out of the game. This process continues until there's only one player left, who...
please create a tic tac toe game with python using only graphics.py library
please create a tic tac toe game with python using only graphics.py library
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...
please submit the C code( no third party library). the C code will create output to...
please submit the C code( no third party library). the C code will create output to a file, and iterate in a loop 60 times and each iteration is 1 second, and if any key from your keyboard is pressed will write a 1 in the file, for every second no key is pressed, will write a 0 into the output file.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT