Aug 13, 2015 at 6:58pm UTC
Need reviews on the following hangman code:
#include <iostream>
#include <windows.h> //for clear screen function system("CLS")
#include <string>
#include <cstdlib> // for rand, srand
#include <ctime> // for time function
using namespace std;
void head();
void body();
void left_hand();
void right_hand();
void left_leg();
void right_leg();
int randRange(int hjavascript:tx('code')igh,int low); // range of random numbers
void clues(int a);
int main ()
{
system("color B"); //changing output color
srand(time(0)); // seeding number generator
// initializations:
int i,j=0,right_counter=0,wrong_counter=0,len,get,space,space_counter=0,repeat=0,q,range;
char input,used[100];
bool flag;
string words[]={"MISSION IMPOSSIBLE 4","SAW","THE DARK KNIGHT RISES","LIONEL MESSI","FC PARMA","SUICIDE SQUAD","JURASSIC PARK"};//words used for the game
get=randRange((sizeof(words)/4)-1,0); // giving the range of index numbers of the words to randomly select one
cout << "HANGMAN"<<endl;
cout << "_______"<<endl;
cout << "~ = vowels" << endl;
cout << "n= numbers"<<endl;
clues(get);
cout << "\n\n\n\n\n\n\n";
//putting the chosen word on char
char *ques=const_cast<char*>(words[get].c_str()); //converting string into char
//taking the length of the word;
string length(ques); // converting ques char into length string
len=length.length();
// to prepare the blanks for the chosen word
char *blanks=const_cast<char*>(length.c_str());
for(i=0;i<len;i++)//loop for converting words into blanks
{
q=blanks[i]; //putting asci value in of char in integer
if(q==32) //if space
blanks[i]=' ';
else if(q==65||q==69||q==73||q==79||q==85)// if vowel
blanks[i]='~';
else if(q>=48&&q<=57) // if number
blanks[i]='n';
else
blanks[i]='_';
}
// for initial display:
for(i=0;i<len;i++)
cout<< blanks[i] << " ";
cout<< endl;
// for counting space so that space will not be counted as a character in the main loop
for(i=0;i<len;i++)
{
space=ques[i];
if(space==32)
space_counter++;
}
space_counter=len-space_counter;
//the main loop begins with the game
while(right_counter!=space_counter && wrong_counter!=6)
{
flag=false;
cin >> input;
input=toupper(input); // taking all the inputs to upper case
// to check repetition and avoid them:
for(i=0;i<=(j-1)&&j>0;i++) // loop for checking the array where the used letters are kept
{
if(input==used[i])
{
repeat=1;
break;
}
}
if(repeat==1) // if a letter is repeated
{
cout<<"This letter is already used";
repeat=0;
continue; // the following does not execute
}
system("CLS"); //clearing the screen every time a new input is given
cout << "HANGMAN"<<endl;
cout << "_______"<<endl;
cout << "~ = vowels" << endl;
cout << "n= numbers"<<endl;
clues(get);
cout << "\n\n\n\n\n\n\n\n\n"; //keeping the heading intact
used[j]=input; //putting the given inputs in the used array
j++;
// to check if input is right:
for(i=0;i<len;i++)
{
if(input==ques[i])
{
blanks[i]=ques[i];
right_counter++;
flag=true;
}
cout << blanks[i] << " "; // display the modified state if correct
}
// to check if input is wrong:
cout << endl;
if(flag==false)
wrong_counter++;
if(wrong_counter==1)
head();
else if(wrong_counter==2)
{
head(); cout << endl;
body();
}
else if(wrong_counter==3)
{
head(); cout << endl;
left_hand();
}
else if(wrong_counter==4)
{
head(); cout << endl;
right_hand();
}
else if(wrong_counter==5)
{
head(); cout << endl;
right_hand(); cout << endl;
left_leg();
}
else if(wrong_counter==6)
{
system("color C");
head(); cout << endl;
right_hand(); cout << endl;
right_leg();
cout << "The Word is "<< words[get];
}
cout <<endl;
repeat=0;
if(right_counter==space_counter) //if victory
{
system("color A");
cout << "BINGO!!!";
}
}
cin.get();
cin.ignore();
}
void head()
{
cout << " *******"<< endl;
cout << " * o o *"<< endl;
cout << " * @ *"<<endl;
cout << " *******";
}
void body()
{
cout << " *"<<endl;
cout << " *"<<endl;
cout << " *"<<endl;
cout << " *"<<endl;
cout << " *"<<endl;
cout << " *"<<endl;
cout << " *"<<endl;
}
void left_hand()
{
cout << " *"<<endl;
cout << " *"<<endl;
cout << " *"<<endl;
cout << " X*******|*"<<endl;
cout << " *"<<endl;
cout << " *"<<endl;
cout << " *"<<endl;
}
void right_hand()
{
cout << " *"<<endl;
cout << " *"<<endl;
cout << " *"<<endl;
cout << " X*******|*|*******X"<<endl;
cout << " *"<<endl;
cout << " *"<<endl;
cout << " *"<<endl;
cout << " \\ /";
}
void left_leg()
{
cout << " *"<<endl;
cout << " *"<<endl;
cout << " *"<<endl;
cout << " *"<<endl;
}
void right_leg()
{
cout << " * *"<<endl;
cout << " * *"<<endl;
cout << " * *"<<endl;
cout << " * *"<<endl;
}
int randRange(int high,int low)
{
return rand() % (high-low)+low;
}
void clues(int a)
{
if(a==0)
{
cout << "Clue: Action Movie from 2005-2010(english)" << endl;
}
else if(a==1)
cout << "Clue: Thriller movie" << endl;
else if(a==2)
cout << "Clue: Action movie from 2011-2015(english)" << endl;
else if(a==3)
cout << "Clue: Athlete" << endl;
else if(a==4)
cout << "Football team" << endl;
else if(a==5)
cout << "Clue: Action movie latest(english)" << endl;
else if(a==6)
cout << "Clue: Thriller movie latest(english)" << endl;
}