首页 > 代码库 > Design Hangman
Design Hangman
Hangman is a paper and pencil guessing game for two or more players. One player thinks of a word, phrase or sentence and the other tries to guess it by suggesting letters or numbers, within a certain number of guesses.
below is my steps of design hangman:
1. Start game
2. Generate a guess word from Word List
3. Print "_ _ _ _ _ " indicate how many characters in the guess word "pizza"
4. Player type in guess character "z" or "Z" both to lowercase
5. Server return whether the "?" is in the "pizza"
a. Add "z" into the guessed pool.
b. If correct, print all characters in the process word "_ _ z z _"
c. If wrong, draw body, guess cnt++;
d. Repeat 4
6. If player guessed process word == guess word, print "You Win!"
7. If guess cnt > max guesscnt, draw body, print "You Lose…"
my very simple C++ version.
1 #include "leetcode.h" 2 3 class Hangman { 4 public: 5 Hangman() { 6 theWord = wordList[rand()%wordList.size()]; 7 process.append(theWord.size(), ‘*‘); 8 showL(process); 9 showL(theWord); 10 step = 0; 11 } 12 13 void guess(const char guessed) { 14 char c = tolower(guessed); 15 used.insert(c); 16 if (checkProcess(c)) showL("already guessed"); 17 vector<int> idx = checkTheWord(c); 18 if (!idx.empty()) { 19 updateProcess(idx); 20 showL(process); 21 } else { 22 step++; 23 updateBody(step); 24 } 25 if (checkWin()) showL("you win"); 26 showV(used); 27 } 28 29 private: 30 vector<string> wordList = {"pizza", "movie", "banana", "hangman"}; 31 const int maxGuess = 6; 32 int step; 33 string theWord; 34 string process; 35 set<char> used; 36 void updateBody(int step) { 37 showL("wrong guess ", step, " times."); 38 } 39 void updateProcess(const vector<int>& idx) { 40 for(auto i : idx) { 41 process[i] = theWord[i]; 42 } 43 } 44 bool checkProcess(const char c) { 45 if (process.empty()) return false; 46 for(auto s : process) { 47 if (s == c) return true; 48 } 49 return false; 50 } 51 vector<int> checkTheWord(const char c) { 52 vector<int> ret; 53 for(int i = 0; i < theWord.size(); i++) { 54 if (theWord[i] == c) ret.push_back(i); 55 } 56 return ret; 57 } 58 bool checkWin() { 59 return process == theWord; 60 } 61 }; 62 63 int main() { 64 Hangman hm; 65 hm.guess(‘a‘); 66 hm.guess(‘m‘); 67 hm.guess(‘o‘); 68 hm.guess(‘i‘); 69 hm.guess(‘e‘); 70 hm.guess(‘v‘); 71 }
copy from gist python version:
https://gist.github.com/DevDarren/4199441
1 class Hangman(): 2 def __init__(self): 3 print "Welcome to ‘Hangman‘, are you ready to die?" 4 print "(1)Yes, for I am already dead.\n(2)No, get me outta here!" 5 user_choice_1 = raw_input("->") 6 7 if user_choice_1 == ‘1‘: 8 print "Loading nooses, murderers, rapists, thiefs, lunatics..." 9 self.start_game() 10 elif user_choice_1 == ‘2‘: 11 print "Bye bye now..." 12 exit() 13 else: 14 print "I‘m sorry, I‘m hard of hearing, could you repeat that?" 15 self.__init__() 16 17 def start_game(self): 18 print "A crowd begins to gather, they can‘t wait to see some real" 19 print "justice. There‘s just one thing, you aren‘t a real criminal." 20 print "No, no. You‘re the wrong time, wrong place type. You may think" 21 print "you‘re dead, but it‘s not like that at all. Yes, yes. You‘ve" 22 print "got a chance to live. All you‘ve gotta do is guess the right" 23 print "words and you can live to see another day. But don‘t get so" 24 print "happy yet. If you make 6 wrong guess, YOU‘RE TOAST! VAMANOS!" 25 self.core_game() 26 27 def core_game(self): 28 guesses = 0 29 letters_used = "" 30 the_word = "pizza" 31 progress = ["?", "?", "?", "?", "?"] 32 33 while guesses < 6: 34 if self.checkWin(the_word, progress): 35 print "You Win!" 36 print "The word is " + the_word + "!" 37 break 38 guess = raw_input("Guess a letter ->") 39 if (guess in the_word) and (guess not in letters_used): 40 print "As it turns out, your guess was RIGHT!" 41 letters_used += "," + guess 42 self.hangman_graphic(guesses) 43 print "Progress: " + self.progress_updater(guess, the_word, progress) 44 print "Letter used: " + letters_used 45 elif (guess not in the_word) and (guess not in letters_used): 46 guesses += 1 47 print "Things aren‘t looking so good, that guess was WRONG!" 48 print "Oh man, that crowd is getting happy, I thought you" 49 print "wanted to make them mad?" 50 letters_used += "," + guess 51 self.hangman_graphic(guesses) 52 print "Progress: " + "".join(progress) 53 print "Letter used: " + letters_used 54 else: 55 print "That‘s the wrong letter, you wanna be out here all day?" 56 print "Try again!" 57 58 59 60 def hangman_graphic(self, guesses): 61 if guesses == 0: 62 print "________ " 63 print "| | " 64 print "| " 65 print "| " 66 print "| " 67 print "| " 68 elif guesses == 1: 69 print "________ " 70 print "| | " 71 print "| O " 72 print "| " 73 print "| " 74 print "| " 75 elif guesses == 2: 76 print "________ " 77 print "| | " 78 print "| O " 79 print "| / " 80 print "| " 81 print "| " 82 elif guesses == 3: 83 print "________ " 84 print "| | " 85 print "| O " 86 print "| /| " 87 print "| " 88 print "| " 89 elif guesses == 4: 90 print "________ " 91 print "| | " 92 print "| O " 93 print "| /|\ " 94 print "| " 95 print "| " 96 elif guesses == 5: 97 print "________ " 98 print "| | " 99 print "| O " 100 print "| /|\ " 101 print "| / " 102 print "| " 103 elif guesses == 6: 104 print "________ " 105 print "| | " 106 print "| O " 107 print "| /|\ " 108 print "| / \ " 109 print "| " 110 else: 111 print "________ " 112 print "| | " 113 print "| O " 114 print "| " 115 print "| /|\ " 116 print "| / \ " 117 print "| " 118 print "The noose tightens around your neck, and you feel the" 119 print "sudden urge to urinate." 120 print "GAME OVER!" 121 self.__init__() 122 123 def progress_updater(self, guess, the_word, progress): 124 i = 0 125 while i < len(the_word): 126 if guess == the_word[i]: 127 progress[i] = guess 128 i += 1 129 else: 130 i += 1 131 132 return "".join(progress) 133 134 def checkWin(self, the_word, progress): 135 i = 0 136 while i < len(the_word): 137 if progress[i] == the_word[i]: 138 i += 1 139 else: 140 break 141 return i == len(the_word) 142 143 game = Hangman()
some helpful links :
https://www.clear.rice.edu/comp212/08-spring/projects/hangman/
Design Hangman