FIrst we need to do the imports. Even though the list is minimal, I still want to show you step by step how I made this run correctly.
Imports:
#include <iostream> //enables input/output #include <string> //enables you to enter strings using namespace std; //don't have to add std:: before things
Yeah, I know it wasn't worth putting on, but like I said, I wanted to show you step by step.
There will be some public variables for the sole purpose of being able to manipulate easier. The main variable which will be public is line, which will be the whole line that the first user puts in for the second user to figure out. Also, another variable which will be public is hangs, which is the variable that prints out, well, the hangs. You can make them whatever you wish, but I will have a vertical line.
Public Variables:
int hang; //for number of hangs string line; //defaults to "word" string hangs; //prints out the hangs
As usual, I work with a lot of methods. The method we are going to use is called game because we will be putting that method which will run until you tell it to stop. Game will be a void, which means it won't return anything and that's fine. If you wanted to, you can make it into a boolean or String, but I will be showing you how to make it with a void. Another method will be toLowerCase since C++ does not include a method like that, but it does include a method which will change each character in a string to lowercase. That being said, you can figure out what that method does. And the final method we will have is a check, which will, well, check your input. Game will pretty much just loop through check until either you get hangman or you get the word.
string toLowerCase(string word){
string lower;
for(int i=0;i<word.length();i++){
lower+=tolower(word[i]);
}
return lower;
}
void check(char user_word){
string word=toLowerCase(line),final,fine;
int l=line.length();
user_word=tolower(user_word);
final=user_word;
if(final==word){
cout<<"WIN!"<<endl;
}
//initial run through
for(int i=0;i<l;i++){
for(int j=0;j<final.length();j++){
if(final[j]==word[i]){
cout<<line[i];
}
//puts spaces to symbolize you got more than one letter, but there are still letters missing
if(final[j]!=word[i]){
hang++;
cout<<" ";
}
}
}
if(hang==l){
hangs+="|";
}
cout<<"\nHangs: "<<hangs<<endl;
return;
}
void game(char user_word){
string final;
final+=user_word;
check(user_word);
while(final!=line){
cout<<"Next Letter: ";
cin>>user_word;
final+=user_word;
check(user_word);
if(final==line){
cout<<"WIN!\n";
break;
}
}
return;
}
The driver method will look like this:
int main(){
string user_word;
int again;
//header to make it look more professional and nicer
cout<<"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n";
cout<<"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n";
cout<<" WELCOME TO HANGMAN \n";
cout<<"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n";
cout<<"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n";
cout<<"Player 1 enter line: ";
cin>>line;
cout<<"Player 2 enter line: ";
cin>>user_word;
game(user_word); //executes the actual game
cout<<"1-Again 2-No :";
cin>>again;
//loop
if(again==1){
while(again==1){
cout<<"SWITCH!!!!!!!!!!!!!!\n";
cout<<"Player 1 enter word: ";
cin>>line;
cout<<"Player 2 enter word: ";
cin>>user_word;
game(user_word);
cout<<"1-Again 2-No :";
cin>>again;
}
}
cout<<"\nThank you for playing. Please come again soon...\n\n";
}
Now how do we put the code together?
Altogether the code would look something like this:
#include <iostream>
#include <string>
using namespace std;
string line;
string final;
string hangs="";
int hang;
string toLowerCase(string word);
void check(char user_word);
void game(char user_word);
int main(){
char user_word;
int again;
cout<<"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n";
cout<<"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n";
cout<<" WELCOME TO HANGMAN \n";
cout<<"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n";
cout<<"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n";
cout<<"Player 1 enter line: ";
cin>>line;
cout<<"Player 2 enter line: ";
cin>>user_word;
game(user_word);
cout<<"1-Again 2-No :";
cin>>again;
if(again==1){
while(again==1){
hangs="";
hang=0;
cout<<"\nSWITCH!!!!!!!!!!!!!!\n";
cout<<"Player 1 enter word: ";
cin>>line;
cout<<"Player 2 enter word: ";
cin>>user_word;
game(user_word);
cout<<"1-Again 2-No :";
cin>>again;
}
}
cout<<"\nThank you for playing. Please come again soon...\n\n";
}
string toLowerCase(string word){
string lower;
for(int i=0;i<word.length();i++){
lower+=tolower(word[i]);
}
return lower;
}
void check(char user_word){
string word=toLowerCase(line),fine;
int l=line.length();
user_word=tolower(user_word);
final=user_word;
if(final==word){
cout<<"WIN!"<<endl;
}
else
//initial run through
for(int i=0;i<l;i++){
for(int j=0;j<final.length();j++){
if(final[j]==word[i]){
cout<<line[i];
}
else
//puts spaces to symbolize you got more than one letter, but there are still letters missing
if(final[j]!=word[i]){
hang++;
cout<<" ";
}
}
}
if(hang==l){
hangs+="|";
}
cout<<"\nHangs: "<<hangs<<endl;
return;
}
void game(char user_word){
string final;
final+=user_word;
check(user_word);
while(final!=line){
cout<<"Next Letter: ";
cin>>user_word;
final+=user_word;
check(user_word);
if(final==line){
cout<<"WIN!\n";
break;
}
}
return;
}
And when we execute it, going through each situation, it would look like this:
First case:

Second case:

Attached to this tutorial is the cpp file, since I already gave you the code, there would be no point in not giving you the compiled file.
link for the file
Thank you for stopping by. I hope this tutorial helped you in some way to understand about game development. JAVA version soon to come...




MultiQuote






|