Write a guess game with C ++

xiaoxiao2021-03-05  25

Today, we have to write a game of guess letters with C . Don't worry, don't be so difficult to do. First, we must understand the rules of the game. I: I can only guess a letter every time; 2: Players can only guess the wrong finish, otherwise the game failed; three: The wrong letter will be recorded; four: After each guess, the game should show the current guesses The incomplete word, and all the letters of the current guess, and the opportunity to guess the wrong; 5: Suppose players guess the letters in words, all the letters in words will be deemed to have guess, for example The word is apple, we guess P, then the program should show that the current guessed hi-PP is -pp. Six: Do not guess the same letter multiple times, because this is a waste of time.

One: Preparation:

Rule is so much, we should use C code to implement it. First, we should consider how to record words, including the original word, guess the wrong letter, currently guess the income words. If you use a traditional C style array, this is very troublesome, which is very difficult to use C style strings. But C is pre-thought of all this, and C has a powerful String class, which is in the header file string. Note that CString or String.h only contains a function of processing strings, which does not include a String class. To use this class, we must first know its constructor so that you can create a String class object.

Constructor String (const char * s) initials the String object to a string SString (size_type n, char c) initializes the object to have N elements, which are characters CString (const string & str, size_type pos = 0 SIZE_TYPE N = NPOS) Initialize the object to the STR, starting from the N elements from the POS element.

In fact, this type of constructor is far more than this, but we have developed this game only to get these (maybe not so much). We also have to know some knowledge about the String class. It overloads all the relationship operators, you can use the = operator to add strings, String class objects, characters to the end of the object. For example, we can write this code: string a = "butter"; string b = "fly"; A = B; this is very convenient. It also overloads the [] operator, so that we can use it like a regular array. For the String class, we also have to first understand its input options. It has Operator >>, so we can use CIN to input, and it is the same as the usage rules of the CIN in the iStream. It is worth noting that the String class is a smart class, which can automatically adjust the length of the string so that we don't have to worry about waste space or enter the string beads at the end of the object. And what about the getLine () function we use? This is a member function, so it cannot be overloaded. The solution is that the String class has a getLine () non-member function, which accepts two parameters, the first is the ISTREAM class object, the second is the String class object, and removes the length parameters, the reason has been said. . So, suppose TEMP is a String class object, we should use getLine (): getLine (cin, string); it looks a bit unhearted, but practical. Finally, to implement this scrabble, we must find characters in the object. The String class has provided a function of this, please see the following table (not fully listed).

Method Prototype Description Size_TYPE FIND (CHAR Ch, SIZE_TYPE POS = 0) Const; start from the POS position of the string, find the character ch, if it is found, return the index where the first CH is located, otherwise, return string :: npos. SIZE_TYPE FIND (const string & str, size_type pos = 0) const; starts from the POS location of the string, finds the string Str, if you find it, return the index where the STR's first letter is located, otherwise, return string :: npos. SIZE_TYPE FIND (SIZE_TYPE POS = 0) const; start from the POS position of the string, find the string S, if it is found, return to the index where the first letter is located, otherwise, return string :: npos.

For example, if Temp is a String object, the content is "apple", then TEMP.FIND ('p') will return 1, that is, the index corresponding to the first character P.

Two: Game source code:

#include #include #include #include #include using namespace std; const amount = 26; const string wordlist [num] = {"Alabama", "choice" , "USUALLY", "Dangerous", "Deer", "Panda", "Love", "Health", "Exciting", "Interesting", "Administrator", "Professional", "Manage", "Non", "" OnSet "," TypeID "," Quarter "," Remote "," Lovely "," Car "," Keeper "," Valid "," Where "," IMPORTANT "," Last "}; // For the game's word library INT main () {srand (time (0)); char play; cout << "Will You Play A Word Game? "; CIN >> Play; Play = Tolower (Play) WHILE (play == 'y') {string first (WordList [Rand ()% NUM]); // Random selection word int length = first.length (); string player (length, '-'); // Players guess words string badguess; // Guess wrong letters set int guest = 10; // Gue guess chance Char guess; cout << "You have" << Guesses << "chance ./n" COUT << "Your word:" << Player << '/ n'; while (Guesses> 0 && Player! = First) {cout << "Please guess!"; Cin >> Guess; if (Badguess. Find (guess)! = String :: npos || Player.Find (guess)! = String :: npos) {cout << "Sorry, this letter you have already guess."; Continu e;} // Judgment if Int Temp = first.find (Guess); if (Temp == String :: Npos) {cout << "! guessed wrong.

/ N "; Guesses ---; Badguess = Guess;} // Treatment ELSE {Player [Temp] = Guess; Temp = first.Find (Guess, Temp 1); while (Temp! = string :: NPOS) // Continue to search the character, see if there are multiple characters in words {Player [Temp] = Gues; TEMP = first.Find (Gues, Temp 1);}} cout << "You still have" << Guesses << "Opportunities. / N "; COUT <<" You currently guess words: "<< Player << '/ n'; cout <<" Your current guessing letter collection: "<< Badguess << '/ n';} IF (Guesses == 0) cout << "Sorry, you failed. / N "; Else Cout <<" You are awesome! / N "; COUT <<" correct word is: "<< first << '/ n'; cout <<" Will You Play Again? "; CIN >> Play;} System (" Pause " RETURN 0;} The following is the result: Will you play a word game? Y

You have 10 guessment opportunities.

Your words: ------------

Please guess! a

You still have 10 chances of guessing.

You are guessing words: ---------- A-

Your current guessing alphabet collection:

Please guess! s

You still have 10 chances of guessing.

You are currently guessing: ----- SS --- A-

Your current guessing alphabet collection:

Please guess! p

You still have 10 chances of guessing.

You are currently guessing: p ---- ss --- A-

Your current guessing alphabet collection:

Please guess! o

You still have 10 chances of guessing.

You are currently guessing: P-O - SS-O-A-

Your current guessing alphabet collection:

Please guess! r

You still have 10 chances of guessing.

You are currently guessing: Pro - SS-O-A-

Your current guessing alphabet collection:

Please guess! fly

You still have 10 chances of guessing.

You are guessing words: PROF-SS-O-A-

Your current guessing alphabet collection:

Please guess! e

You still have 10 chances of guessing.

You are currently guessing: Profess-O-A-

Your current guessing alphabet collection:

Please guess! i

You still have 10 chances of guessing.

You are currently guessing: Professio-A-

Your current guessing alphabet collection:

Please guess! n

You still have 10 chances of guessing.

You are currently guessing: ProfessionalA-

Your current guessing alphabet collection:

Please guess! l

You still have 10 chances of guessing.

You are guessing words: Professional

Your current guessing alphabet collection:

You are awesome!

Correct word is: ProfessionalWill You Play Again? n

Press any key to continue.

three

: The rules of the program analyzer have seen that in line with the rules of the game, the randomness is also strong, this is the power of C . We judge whether the letters have been guessed, doing this:

IF (Badguess.Find (Guess)! = String :: Npos || Player.Find (Gues)! = String :: Npos) {cout << "Sorry, this letter you have already guess."; Continue;} / / Judgment Have you already guess

Because if the letters have been guessed, then it is either in the wrong word that players guess, or in the wrong alphabet collection, we use the FIND function to search for both.

We do this for dealing with guessment, we do this:

INT TEMP = first.find (guess); if (Temp == String :: npos) {cout << "! Guess ./n"; guests ---; badguess = guess;} // Guess wrong deal with

First, we look for whether this characters appear in words. If there is no appearance, the Find function will return string :: npos. NPOS is a constant, the maximum number of elements that can be stored in String 1. If there is no appearance, let's take a chance of guessing, then use this code: Badguess = Guess; add the wrong letter to your Badguess object. Think about it, is this more difficult to use a regular character array?

Finally, if Temp is not string :: npos, then let's say players guessed. But there may be many such letters in this word, so we did this:

Temp = first.find (Guess, Temp 1); while (Temp! = String :: Npos) // Continue to search the character, see if there are multiple characters in words {Player [Temp] = Gues; TEMP = first .find (Guess, Temp 1);

In this way, the interval is gradually narrowed until this character is not allowed to confirm the word. Everyone can see this from the results:

You are guessing words: ---------- A-

Your current guessing alphabet collection:

Please guess! s

You still have 10 chances of guessing.

You are currently guessing: ----- SS --- A - // Display two "s"

Your current guessing alphabet collection:

If you are interested, you can expand this program, such as adding difficulty choices, and finally gives the player's evaluation. Finally, I hope everyone can pass this example, more love C !

转载请注明原文地址:https://www.9cbs.com/read-37493.html

New Post(0)