Coders Packet

The Marvel Game in C++

By Vaishnavi Jaiswal

This packet consists of a fun game called The Marvel Game and it is created in C++ language. So give it a go! Good luck!

Hey everyone,

I have created this interesting game known as The Marvel Game in C++ language, as we all know C++ is one of the world's most popular languages. It is a general-purpose language and is also a high-level language. It is simple and easy to understand which makes it easier for the coders to code in C++.

The Marvel Game is a very interesting game and will serve as a treat to all Marvel fans. It is basically a word-guessing game that can be played by one or more players. It depends on your guessing power and also on your knowledge about the Marvel characters, it's not necessary that you require some deep knowledge of Marvel in order to play this game but a little knowledge is very important and it can be as little as just knowing the names of Marvel characters.

About the Game

So this game is a pure guessing game and in this game, a list of the names of Marvel characters is provided to the computer in the beginning, out of which the computer chooses one name randomly, and then it hides the name and asks the user to guess the name letter by letter in a limited number of guesses. If the user exceeds the maximum number of guesses by guessing the wrong letters and is not able to guess the correct name then he/she will lose otherwise if the user guesses the correct name within the maximum no of guesses provided then he/she will win the game.

There are certain Rules and Regulations in order to play this game and they are as follows:-

Rules and Regulations

1.The hidden marvel character is displayed to you in a format where each of its letters is represented by a star(*).
2.Do not type multiple letters, you have to type only one letter in a single try.
3.You will be given only 5 tries to guess the correct marvel character.

Now we will have a look at the code for better understanding.

#include <bits/stdc++.h>
 #include 
 #include 
 typedef long long ll;
 using namespace std;
 const ll Maximum_no_of_guesses=5;
 /*This function will take one letter guessed by the user and also the name of the marvel character 
 which the user has to guess and then it fills the hidden name.It returns the count of the letters
 matched and will return zero if the letter is already guessed by the user*/
 int Add_letters(char guess ,string character_name, string &hidden_name)
 {
 int count=0;
 int len=character_name.length();
 for (int i = 0; i< len; i++)
 {
 // It will check that we already match this letter in a previous guess or not
 if (guess == hidden_name[i]){
 return 0;
 }
 // It will check the letter guessed is contained in the name of the character or not
 if (guess == character_name[i])
 {
 hidden_name[i]=guess;
 count++;
 }
 }
 return count;
 }

 int main ()
 {
 char letter;
 int num_of_wrong_guesses=0;
 string marvel_character;
 string marvel_characters[] =
 {
 "thor","hulk","thanos",
 "ironman","antman","batman",
 "spiderman","wolverine","deadpool"
 };
 // This will randomly choose and copy one name from the array of names
 srand(time(NULL));
 int n=rand()% 9;
 marvel_character=marvel_characters[n];
 // Now we will initialize the marvel character name with the * character.
 string hidden(marvel_character.length(),'*');
 // Now we will welcome our user
 cout << "\n\t\t\t\t****WELCOME TO THE MARVEL GAME****\t\t\t\t";
 cout<<"\nHere are some Rules and Regulations that you have to follow in order to play this game:";
 cout << "\n1.The hidden marvel character is displayed to you in a format where each of it's letter is represented by a star.";
 cout << "\n2.Do not type multiple letters,you have to type only one letter in a single try.";
 cout << "\n3.You will be given only " << Maximum_no_of_guesses<< " tries to guess the correct marvel character.";
 cout<<"\nLoading.........";
 cout<<"\nSo here the game begins";
 // This loop will run until all the guesses are used up
 while (num_of_wrong_guesses < Maximum_no_of_guesses)
 {
 cout << "\n\n" <<"The hidden marvel character is: "<<hidden;
 cout << "\n\nGuess a letter: ";
 cin >> letter;
 /*Fill the hidden name with letter if the guess is correct,
   otherwise increment the number of wrong guesses.*/
 if (Add_letters(letter, marvel_character, hidden)==0)
 {
 cout << endl << "Oops! You made a wrong guess!" << endl;
 num_of_wrong_guesses++;
 }
 else
 {
 cout << endl << "Woah!You found a letter!" << endl;
 }
 // Now inform the user about the number of guesses left
 cout << "You have " << Maximum_no_of_guesses - num_of_wrong_guesses;
 cout << " guesses left." << endl;
 // Now check if the user guessed the correct name or not
 if (marvel_character==hidden)
 {
 cout << marvel_character << endl;
 cout << "Congratulations!You guessed it right!";
 cout<<"\n Thanks for playing this game,we would love to see you again.";
 break;
 }
 }
 if(num_of_wrong_guesses == Maximum_no_of_guesses)
 {
 cout << "\nSorry,Better luck next time!." << endl;
 cout << "The word was : " << marvel_character << endl;
 cout<<"\n Thanks for playing this game,we would love to see you again.";
 }
 cin.ignore();
 cin.get();
 return 0;
 }
 

About the code

At the beginning of the code, I have defined a constant value for the maximum number of guesses a user can make. Here a user can make at most 5 wrong guesses, whenever the user runs out of guesses the game will end otherwise if they get it correct they will be congratulated.

Then we have the Add_letters function, this function will take one letter guessed by the user and also the name of the Marvel character which the user has to guess, and then it fills the hidden name. It returns the count of the letters matched or will return zero if the letter is already guessed by the user.

Now we have the main function, inside the main function I have declared some variables which will be used in the code ahead then I have also declared an array that contains the list of names of the Marvel characters here I have provided 9 names.

Now on moving ahead we have these lines of code

srand(time(NULL)); 
int n=rand()% 9; 
marvel_character=marvel_characters[n];

The above lines of code will randomly choose and copy one name out of the array containing 9 names of the Marvel characters.

string hidden(marvel_character.length(),'*');

Now this will initialize the name of the Marvel character chosen above with the *character that means it will replace each letter with a * character and will hide the name.

In the rest of the code, we have a while loop that will run until the number of wrong guesses is less than the maximum number of guesses. Further, the code checks if the guess is correct or not, if the guess is correct then the hidden name will be filled with the letter otherwise the number of wrong guesses will be incremented. Then the user will be informed about the total number of guesses left.

Finally in the end we check if the user guessed the correct name or not and display the message accordingly and then the game ends.

Download project

Reviews Report

Submitted by Vaishnavi Jaiswal (Vaishnavi0904)

Download packets of source code on Coders Packet