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         srand(time(nullptr));
 7         theWord = wordList[rand()%wordList.size()];
 8         process.append(theWord.size(), '*');
 9         showL(process);
10         step = 0;
11     }
12 
13     int guess(const char guessed) {
14         char c = tolower(guessed);
15         if (checkProcess(c)) {
16             showL("already guessed");
17             return -1;
18         }
19         used.insert(c);
20         vector<int> idx = checkTheWord(c);
21         if (!idx.empty()) {
22             updateProcess(idx);
23             showL(process);
24         } else {
25             step++;
26             updateBody(step);
27             if (step >= maxGuess) {
28                 showL("you lose!");
29                 showL(theWord);
30                 return  0;
31             }
32         }
33         if (checkWin()) {
34             showL("you win");
35             return 1;
36         }
37         showV(used);
38         return -1;
39     }
40 
41 private:
42     vector<string> wordList = {"pizza", "movie", "banana", "hangman"};
43     const int maxGuess = 6;
44     int step;
45     string theWord;
46     string process;
47     set<char> used;
48     void updateBody(int step) {
49         showL("wrong guess ", step, " times.");
50     }
51     void updateProcess(const vector<int>& idx) {
52         for(auto i : idx) {
53             process[i] = theWord[i];
54         }
55     }
56     bool checkProcess(const char c) {
57         if (process.empty()) return false;
58         for(auto s : used) {
59             if (s == c) return true;
60         }
61         return false;
62     }
63     vector<int> checkTheWord(const char c) {
64         vector<int> ret;
65         for(int i = 0; i < theWord.size(); i++) {
66             if (theWord[i] == c) ret.push_back(i);
67         }
68         return ret;
69     }
70     bool checkWin() {
71         return process == theWord;
72     }
73 };
74 
75 int main() {
76     Hangman hm;
77     while(1) {
78         string s;
79         cin >> s;
80         if (hm.guess(s[0]) != -1) break;
81     }
82     showL("Game Over");
83 }

 

 

 

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/

 

posted @ 2017-08-04 05:09  grainy  阅读(472)  评论(0编辑  收藏  举报