GitHub
// hangman.cpp #include #include #include using namespace std; const int MAX_BAD_GUESS = 7; const string WORD_LIST[]= {"angle", "ant", "apple", "arch", "arm", "army", "baby", "bag", "ball", "band", "basin", "basket", "bath", "bed", "bee", "bell", "berry", "bird", "blade", "board", "boat", "bone", "book", "boot", "bottle", "box", "boy", "brain", "brake", "branch", "brick", "bridge", "brush", "bucket", "bulb", "button", "cake", "camera", "card", "cart", "carriage", "cat", "chain", "cheese", "chest", "chin", "church", "circle", "clock", "cloud", "coat", "collar", "comb", "cord", "cow", "cup", "curtain", "cushion", "dog", "door", "drain", "drawer", "dress", "drop", "ear", "egg", "engine", "eye", "face", "farm", "feather", "finger", "fish", "flag", "floor", "fly", "foot", "fork", "fowl", "frame", "garden", "girl", "glove", "goat", "gun", "hair", "hammer", "hand", "hat", "head", "heart", "hook", "horn", "horse", "hospital", "house", "island", "jewel", "kettle", "key", "knee", "knife", "knot", "leaf", "leg", "library", "line", "lip", "lock", "map", "match", "monkey", "moon", "mouth", "muscle", "nail", "neck", "needle", "nerve", "net", "nose", "nut", "office", "orange", "oven", "parcel", "pen", "pencil", "picture", "pig", "pin", "pipe", "plane", "plate", "plow", "pocket", "pot", "potato", "prison", "pump", "rail", "rat", "receipt", "ring", "rod", "roof", "root", "sail", "school", "scissors", "screw", "seed", "sheep", "shelf", "ship", "shirt", "shoe", "skin", "skirt", "snake", "sock", "spade", "sponge", "spoon", "spring", "square", "stamp", "star", "station", "stem", "stick", "stocking", "stomach", "store", "street", "sun", "table", "tail", "thread", "throat", "thumb", "ticket", "toe", "tongue", "tooth", "town", "train", "tray", "tree", "trousers", "umbrella", "wall", "watch", "wheel", "whip", "whistle", "window", "wire", "wing", "worm"}; const int WORD_COUNT = sizeof (WORD_LIST) / sizeof (string) ; const string FIGURE[] = { " ------------------ \n" " | \n" " | \n" " | \n" " | \n" " | \n" " | \n" " | \n" " ----- \n", " ------------------ \n" " | | \n" " | \n" " | \n" " | \n" " | \n" " | \n" " | \n" " ----- \n", " ------------------ \n" " | | \n" " | O \n" " | \n" " | \n" " | \n" " | \n" " | \n" " ----- \n", " ------------------ \n" " | | \n" " | O \n" " | / \n" " | \n" " | \n" " | \n" " | \n" " ----- \n", " ------------------ \n" " | | \n" " | O \n" " | / \\ \n" " | \n" " | \n" " | \n" " | \n" " ----- \n", " ------------------ \n" " | | \n" " | O \n" " | /|\\ \n" " | \n" " | \n" " | \n" " | \n" " ----- \n", " ------------------ \n" " | | \n" " | O \n" " | /|\\ \n" " | / \n" " | \n" " | \n" " | \n" " ----- \n", " ------------------ \n" " | | \n" " | O \n" " | /|\\ \n" " | / \\ \n" " | \n" " | \n" " | \n" " ----- \n", }; string chooseWord(){ int randomIndex = rand() % WORD_COUNT; return WORD_LIST[randomIndex]; } void renderGame (string guessWord , int badGuessCount){ cout << FIGURE[badGuessCount] << endl; cout << "Current guessed word is :" << guessWord << endl; cout << "Number or bad guess is:" << badGuessCount << endl; } char readAGuess(){ char guess; cout << "Your guess ?" << endl; cin >> guess ; return guess; } bool contains (string word , char guess){ return word.find_first_of(guess) != string::npos; } string update(string guessWord, string word, char guess){ for(size_t i=0; i< word.length();i++){ if(word[i] == guess) guessWord[i] = guess; } return guessWord; } int main(){ srand(time(0)); string word = chooseWord(); string guessWord = string (word.length (), '-'); int badGuessCount = 0; do { renderGame (guessWord, badGuessCount); char guess = readAGuess(); if (contains (word, guess)) guessWord = update (guessWord, word, guess); else badGuessCount++; } while (badGuessCount < MAX_BAD_GUESS && word != guessWord); renderGame (guessWord, badGuessCount); if (badGuessCount < MAX_BAD_GUESS) cout << "You win ! The word is " << guessWord << endl; else cout << "Game over ! The correct word is " << word << endl; }
⭐ 2 | 🍴 0