Question

Create a new program, WordGuessingGame. Using a while loop, create a game for the user. The...

Create a new program, WordGuessingGame. Using a while loop, create a game for the user. The game requires the user to guess a secret word. The game does not end until the user guesses the word. After they win the game, they are prompted to choose to play again. The secret word that user must guess is "valentine". It is a case-sensitive analysis


With each guess...

  • If the guess does not have an equal number of characters, tell the user if it is too long or too short
  • If the guess has the same number of characters, tell the user how many characters are correct
    • The letter must be in the same position to be considered correct

      Update your Word guessing game to check for how letters are correct regardless of length or position.

      You have all the same requirements as before except with each guess the user it told if any of the letters are in the word and how many are in the correct position.

      Sample output with secret word valentine:

      Welcome to the word guessing game.
      Guess a word: borp
      The word is longer
      Correct letters: 0
      Correct position: 0
      Incorrect. Guess again: turtle
      The word is longer
      Correct letters: 4
      Correct position: 0
      Incorrect. Guess again: val
      The word is longer
      Correct letters: 3
      Correct position: 3
      //program continues from here...
      

      Note that if a letter is in a guess multiple times you are allowed to count them as separate letters (e.g. both 't's in turtle are considered correct even though there's only 1 't' in "valentine")

      **I did the first part. I need help with the CORRECT LETTERS and CORRECT POSITION
    • #include<iostream>

      #include <string>

      int main()

      {

      char quit='y';

      std::string word;

      word = "valentine";

      std::string guess;

        

        

      while (quit=='y')

        

      {

      std::cout<<"Welcome to the word guessing game.\n";

      std::cout<<"Guess a word: ";

      std::cin>>guess;

      if (guess!=word)

      {

      while (guess!=word)

      {

      if (guess.length()<word.length())

      {

         std::cout<<"The word is longer.\n";

      std::cout<<"Incorrect. Guess again: ";

      std::cin>>guess;

      }

        

      else if (guess.length()>word.length())

      {

      std::cout<<"The word is shorter.\n";

      std::cout<<"Incorrect. Guess again: ";

      std::cin>>guess;

      }

      else

      {

        

      int same=0;

      int len=guess.length();

      for (int i=0;i<len;i++)

      {

      if(guess[i] == word[i])

      {

      same++;

      }

      }

      std::cout<<"You have "<<same<<" letters correct.\n";

      std::cout<<"Incorrect. Guess again: ";

      std::cin>>guess;

      }

      }

      }

        

        

         if (guess==word)

      {

      std::cout<<"You win!\n";

      }

        

        

      std::cout<<"Would you like to play again? (y/n)\n";

      std::cin>>quit;

      }

      return(0);

      }

0 0
Add a comment Improve this question Transcribed image text
Answer #1

/***********************game.cpp*********************************/

#include<iostream>

#include <string>

int main()

{

   char quit='y';

   std::string word;

   word = "valentine";

   std::string guess;
  
   while (quit=='y')
   {

       std::cout<<"Welcome to the word guessing game.\n";

       std::cout<<"Guess a word: ";

       std::cin>>guess;

       if (guess!=word)

       {      

           while (guess!=word)

           {

               if (guess.length()<word.length())

               {

                    std::cout<<"The word is longer.\n";
                   int same=0,position=0;

                   int len=guess.length();

                   for (int i=0;i<len;i++)

                   {
                       for (int j=0;j<len;j++)

                       {
                           if(guess[i] == word[j])

                           {              

                               same++;
                               if(i==j){
                               position++;
                               }
                          
                           }
                          

                       }

                   }

               std::cout<<"You have "<<same<<" letters correct.\n";
               std::cout<<"You have "<<position<<" position correct.\n";
               std::cout<<"Incorrect. Guess again: ";

               std::cin>>guess;

          

               }              

               else if (guess.length()>word.length())

               {

                   std::cout<<"The word is shorter.\n";

                   int same=0,position=0;

                   int len=guess.length();

                   for (int i=0;i<len;i++)

                   {
                       for (int j=0;j<len;j++)

                       {
                           if(guess[i] == word[j])

                           {              

                               same++;
                               if(i==j){
                               position++;
                               }
                          
                           }
                          

                       }

                   }
               std::cout<<"You have "<<same<<" letters correct.\n";
               std::cout<<"You have "<<position<<" position correct.\n";
               std::cout<<"Incorrect. Guess again: ";

               std::cin>>guess;


               }

               else

               {

                   int same=0,position=0;

                   int len=guess.length();

                   for (int i=0;i<word.length();i++)

                   {
                       for (int j=0;j<len;j++)

                       {
                           if(guess[i] == word[j])

                           {              

                               same++;
                               if(i==j){
                               position++;
                               }
                          
                           }
                          

                       }

                   }
               std::cout<<"You have "<<same<<" letters correct.\n";
               std::cout<<"You have "<<position<<" position correct.\n";
               std::cout<<"Incorrect. Guess again: ";

               std::cin>>guess;

               }

           }  

       }  

       if (guess==word)

       {

           std::cout<<"You win!\n";

       }

       std::cout<<"Would you like to play again? (y/n)\n";

       std::cin>>quit;

   }

   return(0);

}

/***************************output***********************************8/

Welcome to the word guessing game.
Guess a word: borp
The word is longer.
You have 0 letters correct.
You have 0 position correct.
Incorrect. Guess again: turtle
The word is longer.
You have 4 letters correct.
You have 0 position correct.
Incorrect. Guess again: val
The word is longer.
You have 3 letters correct.
You have 3 position correct.
Incorrect. Guess again: valentine
You win!
Would you like to play again? (y/n)
n

--------------------------------

CAUsersJUGALKISHORNDesktopigame.exe Welcome to the word guessing game. Guess a word: borp The word is longer. You have e let

Thanks a lot, Please let me know if you have any problem..................

Add a comment
Know the answer?
Add Answer to:
Create a new program, WordGuessingGame. Using a while loop, create a game for the user. The...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I am trying to write this code which asks "Write a program that ask the user,...

    I am trying to write this code which asks "Write a program that ask the user, the question: What is a^b? //The program generates two signed integers and gives the user four attempts to get the correct answer //a=- , b = + //a= + , b = - " So far this what I wrote. I am not sure how to do this correctly. #include<iostream> #include<cstdlib> #include<ctime> using namespace std; int main() {    srand(time(0));    int guess,a,ans,b, k;...

  • In this project, you will write a complete program that allows the user to play a...

    In this project, you will write a complete program that allows the user to play a game of Mastermind against the computer. A Mastermind game has the following steps: 1. The codebreaker is prompted to enter two integers: the code length n, and the range of digits m. 2. The codemaker selects a code: a random sequence of n digits, each of which is in the range [0,m-1]. 3. The codebreaker is prompted to enter a guess, an n-digit sequence....

  • Need help with this C program? I cannot get it to compile. I have to use...

    Need help with this C program? I cannot get it to compile. I have to use Microsoft Studio compiler for my course. It's a word game style program and I can't figure out where the issue is. \* Program *\ // Michael Paul Laessig, 07 / 17 / 2019. /*COP2220 Second Large Program (LargeProg2.c).*/ #define _CRT_SECURE_NO_DEPRECATE //Include the following libraries in the preprocessor directives: stdio.h, string.h, ctype.h #include <stdio.h> /*printf, scanf definitions*/ #include <string.h> /*stings definitions*/ #include <ctype.h> /*toupper, tolower...

  • Word Guessing Game Assignment Help Needed. This C++ (I'm using Visual Studio 2015) assignment is actually...

    Word Guessing Game Assignment Help Needed. This C++ (I'm using Visual Studio 2015) assignment is actually kind of confusing to me. I keep getting lost in all of the words even though these are supposed to instruct me as to how to do this. Can I please get some help as to where to start? Word guessing game: Overview: Create a game in which the user has a set number of tries to correctly guess a word. I highly recommend...

  • Hangman using while and fo loops, if's, and functions.

    I would like to preface this by saying this is NOT a current homework assignment; but it is an assignment from 3 years ago before I dropped out of school. I am self teaching and am revisiting an old assignment. I am NOT asking for the entire program, I'm simply looking for help building the skeleton for the initial start of the game.MORE INFO: Player 1 will enter word(of any length / i have been using "Testing") for Player 2...

  • Write a Java application program that plays a number guessing game with the user. In the...

    Write a Java application program that plays a number guessing game with the user. In the starter code that you are given to help you begin the project, you will find the following lines of code: Random generator = args.length == 0 ? new Random() :                    new Random(Integer.parseInt(args[0])); int secret = generator.nextInt(100); You must keep these lines of code in your program. If you delete these lines, or if you change thse lines in any way, then your program...

  • JAVA Develop a letter guessing game in this assignment using Java. Requirements: 1). When the game...

    JAVA Develop a letter guessing game in this assignment using Java. Requirements: 1). When the game is started, generate a random letter between A and Z; 2). Display a message "I have a secret letter (A to Z), can you guess what it is?"; 3). Read the user's answer; 4). Compare the user's answer and the random secret letter generated; 5). If the user answer is before the random secret letter in the alphabet, display "Incorrect. Try something bigger" and...

  • Create a script that presents a word-guessing game. Allow users to guess the word one letter at a...

    Create a script that presents a word-guessing game. Allow users to guess the word one letter at a time by entering a character in a form. Start by assigning a secret word to a variable. After each guess, print the word using asterisks for each remaining letter, but fill in the letters that the user guessed correctly. Store the user’s guess in a form field. For example, if the word you want users to guess is “suspicious” and the user...

  • For a C program hangman game: Create the function int play_game [play_game ( Game *g )]...

    For a C program hangman game: Create the function int play_game [play_game ( Game *g )] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) (Also the link to program files (hangman.h and library file) is below the existing code section. You can use that to check if the code works) What int play_game needs to do mostly involves calling other functions you've already...

  • Write a program that allows a user to play a guessing game. Pick a random number...

    Write a program that allows a user to play a guessing game. Pick a random number in between 1 and 100, and then prompt the user for a guess. For their first guess, if it’s not correct, respond to the user that their guess was “hot.” For all subsequent guesses, respond that the user was “hot”if their new guess is strictly closer to the secret number than their old guess and respond with“cold”, otherwise. Continue getting guesses from the user...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT