Program for hangman game problem, C/C++ Programming

Assignment Help:

Program for Hangman Problem

  #include

  #include

  #include

  #include

  #include

  static void playGame();

  static void printMistakes(int n);

  static int const MAXGUESS=8;

  static int const MAXLETTER= 'z'-'a'+1;

  static char String[][15]= {"apple", "banana", "cat", "dog", "elephant", "fish", "giraffe", "hippopotamus", "ice cream","jelly", "kangaroo", "lion", "monkey", "noodles", "octopus", "parrot", "queen", "rabbit", "sun", "tree", "umbrella", "van", "window", "x-ray", "yellow", "zebra"};

  enum boolean {false, true};

  void main()

  {

  boolean again;

  char answer;

  clrscr();

  cout<<"\n\n\tThis is the word guessing game called Hangman.";

  cout<<"\n\tThe computer will pick-up a word and you must try to";

  cout<<"\n\tguess the lettrs in the word. If you guess the";

  cout<<"\n\tword with less than"<

  do

  {

  playGame();

  cout<<"\n\nWould you like to play another game?(Y or N)";

  cin>>answer;

  if (answer == 'y'|| answer == 'Y')

  again=true;

  else if (answer == 'n'|| answer == 'N')

  again=false;

  else

  {

  again=false;

  cout<<"\n Your answer\""<

  }

  }

  while (again);

  cout<<"\nThank you for playing Hangman!";

  }

  static void playGame()

  {

  boolean*already;

  char c;

  boolean*display;

  int found;

  int i;

  char inputChar;

  char inputString[15];

  int length;

  int letters;

  int mistakes,score,chanscor;

  boolean playing;

  char word[15];

  mistakes=0;

  score=0;

  randomize();

  strcpy(word,String[(int)(rand()%26)]);

  length= strlen(word);

  display= new boolean[length];

  letters=0;

  for (i=0; i

  {

  c=word[i];

  if ((c>='a')&&(c<='z'))

  {

  display[i]=false;

  letters++;

  }

  else

  display[i]=true;

  }

  cout<<"\n\nI am thinking of a word. ";

  if(letters==1)

  cout<<"the word has one letter.";

  else

  cout<<"the word has"<

  already= new boolean[MAXLETTER];

  for (i=0;i

  already[i]=false;

 

  playing = true;

  while(playing)

  { chanscor=100/(MAXGUESS-mistakes);

  cout<<"\nThe word is:";

  for (i=0; i

  {

  cout<<" ";

  if (display[i])

  cout<

  else

  cout<<"*";

  }

  cout<<"\nWhat is your guess? ";

  if ((MAXGUESS-mistakes) <= 1 )

  cout<<"this is your last guess?";

  else

  cout<<"you have"<<(MAXGUESS-mistakes)<<"guessing remaining.";

  gets(inputString);

  strlwr(inputString);

  if (strlen(inputString)== length)

  {

  if(strcmp(word,inputString)==0)

  {

  cout<<"Yes the word is\""<

  printMistakes(mistakes);

  playing=false;

  }

  else

  {

  cout<<"Sorry, the word is not\""<

  <<"\".";

  mistakes++;

  }

  }

  else if (strlen(inputString)!=1)

  {

  cout<<"Your guess\""<

  }

  else

  {

  inputChar=inputString[0];

  if((inputChar<'a')||(inputChar>'z'))

  {

  cout<<"Your guess\""<

  }

  else if (already[inputChar-'a'])

  {

  cout<<"You have already guessed the following letters:";

  for(i=0; i

 

 cout<<" "<<(char)(i+'a');

  cout<<"\n";

  }

  else

  {

  already[inputChar-'a']= true;

  found=0;

  for (i=0; i

  if (word[i]==inputChar)

  { display[i]= true;

  found++;

  }

  if (found <= 0)

  { cout<<"Sorry, the letter\""<

  mistakes++;

  }

  else

  {

  score=score+chanscor*(MAXGUESS-mistakes);

  cout<<"\n\t Your score now is"<

  if (found==1)

  cout<<"there is one letter\""<

  <<"\"in the word.";

  else

  cout<<"yes there are"<

  playing= false;

 for(i=0; i< length; i++)

 playing=playing || !display[i];

if (!playing)

 {

 cout<<"You found all the letters of the word\""<

printMistakes(mistakes);

}

}

}

}

 if (mistakes>= MAXGUESS)

{

cout<<"the word was\"" << word << "\".";

playing= false;

 }

 }

}

 static void printMistakes(int n)

{

 if (n<= 0)

 cout<<"You did not make any mistake. Excellent!";

 else if (n==1)

  cout<<"you made only one mistake. Great!";

 else

 cout<<"you made only"<

 getch();

}

 


Related Discussions:- Program for hangman game problem

What are the differences among a struct in c & in c++?, A: In C++ a struct ...

A: In C++ a struct is alike to a class except for the default access specifier( refer to other question in the document). In C we ought to include the struct keyword while declarin

Area of curve, Write a program to find the area under the curve y = f(x) be...

Write a program to find the area under the curve y = f(x) between x = a and x = b, integrate y = f(x) between the limits of a and b. The area under a curve between two points can b

Assignment, Classify computer systems according to capacity. How they are d...

Classify computer systems according to capacity. How they are different from computers according to the classification of technology. Provide comparative study also.

C program to allocate memory dynamically for 2-d array, Aim: To implement ...

Aim: To implement a program to allocate memory dynamically for 2 dimensional array (accept and print matrix) using pointers. Code:                       #include #inc

When does a name clash occur in c++, A name clash happens when a name is de...

A name clash happens when a name is described in more than one place. For example, two dissimilar class libraries could give two different classes the similar name. If you try to u

How many non-boundary pits and peaks are on the map, Armed with your functi...

Armed with your function from above, we can do some interesting things. For instance, any pixel where the offsets are both zero is a pit (lower than all surrounding points) .

Object tracking project, Identify 2 to 3 existing algorithms commonly used ...

Identify 2 to 3 existing algorithms commonly used for object tracking. Algorithms should be in C or written in MATLAB language. Document these algorithms in flowcharts and run thes

C program for count the no which you want , # include stdio.h> # include...

# include stdio.h> # include string.h> # include conio.h> void main() {           int i=0,j=0,b=0,count=0;           int a[100];           for(i=0;i

Write a program that calculates points along a curve, write a program that ...

write a program that calculates points along a rhodonea curve.... ? int fillArray( double data[ ], int nValues, double min, double max ); o Used to fill in the theta array. Return

Luminous Jewels - The Polishing Game, Byteland county is very famous for lu...

Byteland county is very famous for luminous jewels. Luminous jewels are used in making beautiful necklaces. A necklace consists of various luminous jewels of particular colour. Nec

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd