Today, we are going to use C ++ to write a game that guesses letters. Don't worry. It's not that difficult to do it. First, we must understand the game rules.
1. Players can only guess one letter at a time;
2. Players can only guess a limited number of errors; otherwise, the game fails;
3. Wrong letters will be recorded;
4. After each guess, the game should display the incomplete words that are currently guessed, all the letters that are currently guessed wrong, and the remaining opportunity to guess wrong;
5. Assume that the player guessed the letter in the word, and all the letters in the word will be considered as guessed. For example, if the original word is apple, We guessed P, the program should display the currently guessed incomplete word as-PP --.
6. Do not guess the same letter multiple times, because it is a waste of time.
I. preparations:
There are so many rules that we should use C ++ code to implement it. First, we should consider how to record words, including original words, wrong letters, and incomplete words currently guessed. If you use a traditional C-style array, this is very troublesome, which is difficult to implement with a C-style string. C ++ comes up with a powerful string class in C ++. Its declaration is in the header file string. Note that cstring or string. h only contains some functions that process strings, excluding the string class. To use this class, we first need to know its constructor so that we can create a string class object.
| Constructor |
Description |
| String (const char * s) |
Initialize String object as string s |
| String (size_type N, char C) |
Initialize the object to an object with n elements, all of which are characters C |
| String (const string & STR, size_type Pos = 0, size_type n = NPOs) |
Initialize the object to the n elements starting from the POs element in Str. |
In fact, the constructor of this class is far more than that, but we only use this to develop this game (maybe not so much ). We also need to know some knowledge about the string class. It reloads all Relational operators, and can use the + = Operator to add strings, string class objects, and characters to the end of the object. For example, we can write such code: string a = "butter"; string B = "fly"; A + = B; this is very convenient. It also loads the [] operator so that we can use it like a regular array. For the string class, we also need to have a preliminary understanding of its input options. It has operator>, so we can use CIN for input, and it is the same as the rules for using CIN in istream. It is worth noting that the string class is a smart class that can automatically adjust the length of the string. In this way, we do not have to worry about wasting space or entering a string beyond the end of the object. What about the commonly used Getline () function? This is a member function, so it cannot be overloaded. The solution is that the string class carries a non-member function of Getline (), which accepts two parameters. The first is an istream class object and the second is a string class object, and the length parameter is removed. The reason is already explained. Therefore, if temp is a string class object, we should use Getline (): Getline (CIN, string) for it; it looks a little ugly but practical.
Finally, to implement this pinyin game, we must search for characters in the object. The string class already provides functions in this regard. Please refer to the table below (not fully listed ).
| Method prototype |
Description |
| Size_type find (char CH, size_type Pos = 0) const; |
Search for the CH character starting from the POs position of the string. If it is found, the index of the first ch is returned, Otherwise, string: NPOs is returned. |
| Size_type find (const string & STR, size_type Pos = 0) const; |
Searches for the string 'str' starting from the position of the string's pos. If 'str' is found, returns the index of the first letter of 'str'. Otherwise, returns string: NPOs. |
| Size_type find (const char * s, size_type Pos = 0) const; |
Returns the index of the first letter of string S. Otherwise, returns string: NPOs. |
For example, if temp is a string object whose content is "apple", temp. Find ('P') returns 1, that is, the index corresponding to the first character p.
2. Game source code:
# Include <iostream> # Include <string> # Include <cstdlib> # Include <ctime> # Include <cctype> Using namespace STD; Const int num = 26; Const string wordlist [num] = {"Alabama", "choice", "usually ", "Dangerous", "deer", "Panda", "love", "health", "Exciting ", "Interesting", "Administrator", "professional", "manage", "nonce", "onset ", "Typeid", "quarter", "remote", "lovely", "car", "keeper ", "Valid", "where", "mean", "important", "last"}; // word library for gamesInt main () { Srand (time (0 )); Char play; Cout <"will you play a word game? <Y/n> "; Cin> play; Play = tolower (play ); While (play = 'y ') { String first (wordlist [rand () % num]); // select a random word Int length = first. Length (); String player (length, '-'); // the word that the player guesses. String badguess; // a collection of wrong letters Int guesses = 10; // chance of an error Char guess; Cout <"you have the chance to guess" <guesses <. /N "; Cout <"Your word:" <player <'/N '; While (guesses> 0 & player! = First) { Cout <"Guess it! "; Cin> guess; If (badguess. Find (guess )! = String: NPOs | player. Find (guess )! = String: NPOs) { Cout <"Sorry, you have guessed this letter. "; Continue; } // Determine whether you have guessed it Int temp = first. Find (guess ); If (temp = string: NPOs) { Cout <"Ah! Wrong. /N "; Guesses --; Badguess + = guess; } // Handle the error after guessing Else { Player [temp] = guess; Temp = first. Find (guess, temp + 1 ); While (temp! = String: NPOs) // continue to search for this character to see if there are multiple characters in the word { Player [temp] = guess; Temp = first. Find (guess, temp + 1 ); } } Cout <"you still have the chance to guess the error <guesses <. /N "; Cout <"the word you have guessed:" <player <'/N '; Cout <"a collection of wrong letters you have guessed:" <badguess <'/N '; } If (guesses = 0) Cout <"sorry, you failed. /N "; Else Cout <"You are awesome! /N "; Cout <"correct word:" <first <'/N '; Cout <"will you play again? <Y/n> "; Cin> play; } System ("pause "); Return 0; } |
The running result is as follows:
Will you play a word game? <Y/n> Y
You have 10 chances of making mistakes.
Your word :------------
Guess it! A
You still have 10 chances of making guesses.
The word you have guessed: -----------
A collection of wrong letters:
Guess it! S
You still have 10 chances of making guesses.
The word you have guessed: ----- SS ----
A collection of wrong letters:
Guess it! P
You still have 10 chances of making guesses.
The word you have guessed: P ---- SS ----
A collection of wrong letters:
Guess it! O
You still have 10 chances of making guesses.
The word you have guessed: P-o -- ss-o--
A collection of wrong letters:
Guess it! R
You still have 10 chances of making guesses.
The word you have guessed: Pro -- ss-o--
A collection of wrong letters:
Guess it! F
You still have 10 chances of making guesses.
The word you have guessed: Prof-SS-o--
A collection of wrong letters:
Guess it! E
You still have 10 chances of making guesses.
The word you have guessed: profess-o--
A collection of wrong letters:
Guess it! I
You still have 10 chances of making guesses.
The word that you have guessed: paisio--
A collection of wrong letters:
Guess it! N
You still have 10 chances of making guesses.
The word you have guessed: inclusiona-
A collection of wrong letters:
Guess it! L
You still have 10 chances of making guesses.
The word you have guessed: Professional
A collection of wrong letters:
You are awesome!
The correct word is: Professional
Will you play again? <Y/n> N
Press any key to continue...
3.: Program Analysis
As you can see, the running results of the program comply with the rules of the game, and the randomness is also strong. This is the power of C ++.
Let's determine whether the letter has been guessed, as shown in the following code:
If (badguess. Find (guess )! = String: NPOs | Player. Find (guess )! = String: NPOs) { Cout <"Sorry, you have guessed this letter. "; Continue; } // Determine whether you have guessed it |
If a letter is guessed, it is either in the incomplete word that the player guessed, or in the wrong letter set. We use the find function to search for the two parts separately.
We do the following for the error-based processing:
Int temp = first. Find (guess ); If (temp = string: NPOs) { Cout <"Ah! Wrong. /N "; Guesses --; Badguess + = guess; } // Handle the error after guessing |
First, check whether the character appears in the word. If it does not appear, the find function returns string: NPOs. NPOs is a constant, which is 1 more than the maximum number of elements that string can store. If it does not appear, we first subtract the chance of making a guess, and then use the code badguess + = guess; to add the wrong letter to the badguess object. How hard is it to use a regular character array?
Finally, if temp is not a string: NPOs, we can say the player guessed it. However, this word may contain multiple such letters, so we did this:
Temp = first. Find (guess, temp + 1 ); While (temp! = String: NPOs) // continue to search for this character to see if there are multiple characters in the word { Player [temp] = guess; Temp = first. Find (guess, temp + 1 ); } |
In this way, gradually narrow the interval until the word is confirmed to have no such character. You can also see this in the running results:
The word you have guessed: -----------
A collection of wrong letters:
Guess it! S
You still have 10 chances of making guesses.
The word you have guessed: ----- SS --- A-// both "S" are displayed.
A collection of wrong letters:
If you are interested, you can expand this program, such as adding difficulty options, and finally providing comments on players. Finally, I hope you can use this example to better love C ++!