Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Problem Description You will be creating a Hangman program. In this game, the co

ID: 673107 • Letter: P

Question

Problem Description

You will be creating a Hangman program. In this game, the computer will have a hard coded word and the user will enter one letter at a time until he or she has guesses the correct word. A score will be tallied that will be the number of incorrect guesses the user has. So a smaller score is better.

Initially the word will be displayed as a list of special characters such as '*' or '-'. The user will input one letter at a time and each input will be compared with the word. If the guess is correct the letter will be displayed rather than the special character. Each incorrect guess will increment the score. When the user guesses the correct word the game will be over.

Programming Standards

Each increment of the program will adhere to the following standards:

Each source file will have a header describing the program, programmers name, date, purpose of the program, and increment.

Each source file will adhere to the coding standards provided in the lecture, or given by the Professor.

Each program will have an introductory output message introducing the program to the user and providing directions.

All program output will be well structured, easy to read, and appropriately formatted.

Modify the design and program to allow for iteration. Increase the number of guesses to 10 to solve the word. Display the word to the user with each letter as a special character such as ********. Create an array of correct letters guessed such as: char[] guessed = new char[26];

You will need counter also to keep track of how many letters are in the guessed array. You do not need to keep track of incorrectly guessed letters.

Explanation / Answer

I have taken some random word as "ABSORBITES" (which has no meaning you can change the word) and have used random number generator to generate a 5-10 letter word from above array of letters.
The program then finds if the given input letter by user is in our hardcoded array, prints if its present in array and increments the score if it is not.
The user is given chances equal to number of letters generated by random number generator to guess the correct word. You can hardcode the number of chances as per your need. Just change the value of the variable 'chance'.
The program and sample output is given below.

#include<stdio.h>
#include<conio.h>
#include <stdlib.h>
#include<time.h>

int main(){

   int count=0,n=0,i,chance=0;
   time_t t;
   bool flag=false;
   char input;
   char word[10]={'A','B','S','O','R','B','I','T','E','S'}; //hardcoded number
   char display[10]={'*','*','*','*','*','*','*','*','*','*'}; //display stars intially
   /* Intializes random number generator */
   srand((unsigned) time(&t));
   do
   {
       n=rand() % 10;
   }while(n<5);
   chance=n; //set the number of chances to the number of letters in the word
   printf(" You have %d chances to guess the word ",chance);   
  
   for( i=0;i<n;i++){
   printf("*");
   }

   while(chance>=0){
       printf(" Guess the word--Enter a letter ");
       scanf("%c",&input);
       flushall();
       for(i=0;i<n;i++){ //check if entered letter is in hardcoded word
      
           if(input==word[i]){
               printf(" %c is in the word ",input);
               display[i]=input;
               break;
           }
           else{
               flag=true;
           }
      
       }
       if(flag){
       count++; //increment the score for wrong guesses
       flag=false;
       }
       printf(" You are left with %d chances ",chance);
  
       chance--;
  
   }
      printf(" Number of guesses over ");
  
   printf(" The Guessed letters in word are:: ");
   for(i=0;i<n;i++){
   printf("%c",display[i]);
   }
   printf(" The Actual word was:: ");
   for(i=0;i<n;i++){
   printf("%c",word[i]);
   }

   printf(" And your score is %d ",count);
   getch();
   return 0;
}

output:


You have 8 chances to guess the word
********
Guess the word--Enter a letter
A

A is in the word

You are left with 8 chances

Guess the word--Enter a letter
G

You are left with 7 chances

Guess the word--Enter a letter
B

B is in the word

You are left with 6 chances

Guess the word--Enter a letter
Q

You are left with 5 chances

Guess the word--Enter a letter
S

S is in the word

You are left with 4 chances

Guess the word--Enter a letter
R

R is in the word

You are left with 3 chances

Guess the word--Enter a letter
T

T is in the word

You are left with 2 chances

Guess the word--Enter a letter
E

You are left with 1 chances

Guess the word--Enter a letter
F

You are left with 0 chances

Number of guesses over

The Guessed letters in word are::
ABS*R**T
The Actual word was::
ABSORBIT
And your score is 8

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote