Page 1 of 1

Hangman C++

#1 computerfox  Icon User is offline

  • straight vegetarian kid

Reputation: 50
  • View blog
  • Posts: 3,772
  • Joined: 29-January 09

Posted 25 August 2009 - 06:03 AM

Hey guys! Today we will be taking a look at how to make hangman in C++.

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:

Attached Image

Second case:

Attached Image



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...

Is This A Good Question/Topic? 1
  • +

Replies To: Hangman C++

#2 Guest_kaizer*


Reputation:

Posted 16 March 2010 - 01:15 AM

it has many errors!?
Was This Post Helpful? 0

#3 rondressler  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 10-August 10

Posted 10 August 2010 - 12:42 PM

View Postcomputerfox, on 25 August 2009 - 05:03 AM, said:

Hey guys! Today we will be taking a look at how to make hangman in C++.

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:

Attachment attachment

Second case:

Attachment attachment



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...


This Code Does not work and is Completely Full of Errors.
Was This Post Helpful? 0
  • +
  • -

#4 Guest_chariz*


Reputation:

Posted 23 September 2010 - 12:44 PM

hmm... did this one never been correct?

Is the <iostream> header file wasnecessarily neede? how about a progaram without it?
Was This Post Helpful? -1

#5 Derek. M  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 17-November 10

Posted 24 November 2010 - 04:10 PM

View Postchariz, on 23 September 2010 - 11:44 AM, said:

hmm... did this one never been correct?

Is the <iostream> header file wasnecessarily neede? how about a progaram without it?


Any C++ program requires <iostream>
Was This Post Helpful? 0
  • +
  • -

#6 JulianNeill  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 13
  • Joined: 15-May 12

Posted 16 May 2012 - 09:23 AM

View PostDerek. M, on 24 November 2010 - 04:10 PM, said:

View Postchariz, on 23 September 2010 - 11:44 AM, said:

hmm... did this one never been correct?

Is the <iostream> header file wasnecessarily neede? how about a progaram without it?


Any C++ program requires <iostream>


If using Visua Studo as your IDE it automatically puts <iostream>
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1