From 2577b10640d8484efb08a7981f0f2906a285c92e Mon Sep 17 00:00:00 2001 From: Abiha Naqvi <88877269+Abiha511@users.noreply.github.com> Date: Wed, 19 Oct 2022 10:42:26 +0530 Subject: [PATCH] Added hangman game code --- Cpp/hangman_game.cpp | 432 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 432 insertions(+) create mode 100644 Cpp/hangman_game.cpp diff --git a/Cpp/hangman_game.cpp b/Cpp/hangman_game.cpp new file mode 100644 index 0000000..278cd0d --- /dev/null +++ b/Cpp/hangman_game.cpp @@ -0,0 +1,432 @@ +#include +#include +using namespace std; + + +//A simple attractive multiplayer hangman game using object oriented programming +//This is the structure of the game +/* ++---------------------------------+ +| HANG MAN | ++---------------------------------+ +| | | +| | | +| O | +| /|\ | +| | | +| / \ | +| +----------+ | +| | | | ++---------------------------------+ +| Available letters | ++---------------------------------+ +| A B C D E F G H I J K L M | +| N O P Q R S T U V W X Y Z | ++---------------------------------+ +| Guess the word | ++---------------------------------+ +| _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ | ++---------------------------------+ +> +*/ + + +class hangman +{ +public: + +void PrintMessage(string message, bool printTop = true, bool printBottom = true)//Function to print the lines and the message in a box +{ + if (printTop) + { + cout << "+---------------------------------+" << endl; + cout << "|"; + } + else + { + cout << "|"; + } + bool front = true; + for (int i = message.length(); i < 33; i++) + { + if (front) + { + message = " " + message; + } + else + { + message = message + " "; + } + if(front==true) + { + front=false; + } + else + { + front=true; + } + } + cout << message.c_str(); + + if (printBottom) + { + cout << "|" << endl; + cout << "+---------------------------------+" << endl; + } + else + { + cout << "|" << endl; + } +} + + + +void DrawHangman(int guessCount = 0)// Function to draw the hangman as the game proceeds +{ + if (guessCount >= 1) + PrintMessage("|", false, false); + else + PrintMessage("", false, false); + + if (guessCount >= 2) + PrintMessage("|", false, false); + else + PrintMessage("", false, false); + + if (guessCount >= 3) + PrintMessage("O", false, false); + else + PrintMessage("", false, false); + + if (guessCount == 4) + PrintMessage("/ ", false, false); + + if (guessCount == 5) + PrintMessage("/| ", false, false); + + if (guessCount >= 6) + PrintMessage("/|\\", false, false); + else + PrintMessage("", false, false); + + if (guessCount >= 7) + PrintMessage("|", false, false); + else + PrintMessage("", false, false); + + if (guessCount == 8) + PrintMessage("/", false, false); + + if (guessCount >= 9) + PrintMessage("/ \\", false, false); + else + PrintMessage("", false, false); +} + + + +void PrintLetters(string input, char from, char to)//Function to print the available letters after guessing +{ + string s; + for (char i = from; i <= to; i++) + { + if (input.find(i) == string::npos) + { + s += i; + s += " "; + } + else + s += " "; + } + PrintMessage(s, false, false); +} + + + +void PrintAvailableLetters(string taken) +{ + PrintMessage("Available letters"); + PrintLetters(taken, 'A', 'M'); + PrintLetters(taken, 'N', 'Z'); +} + +bool PrintWordAndCheckWin(string word, string guessed)//To print the updated word as it is being guessed +{ + bool won = true; + string s; + for (int i = 0; i < word.length(); i++) + { + if (guessed.find(word[i]) == string::npos) + { + won = false; + s += "_ "; + } + else + { + s += word[i]; + s += " "; + } + } + PrintMessage(s, false); + return won; +} + + + +string LoadRandomWord(string path)//to load a random word from the text file +{ + int lineCount = 0; + string word; + vector v; + ifstream reader(path); + if (reader.is_open()) + { + while (getline(reader, word)) + v.push_back(word); + + int randomLine = rand() % v.size(); + + word = v.at(randomLine); + reader.close(); + } + return word; +} + + + +bool has_only_alpha(string word) // To check if the word entered by the user is valid and only contain alphabets +{ + char c; + for (int i = 0; i < word.length(); i++) { + c = word.at(i); + + if (! ( ( c >= 'a' && c <= 'z' ) || + ( c >= 'A' && c <= 'Z') ) ) { + return false; + } + } + return true; +} + + + +int TriesLeft(string word, string guessed)//It returns the number of tries left +{ + int error = 0; + for (int i = 0; i < guessed.length(); i++) + { + if (word.find(guessed[i]) == string::npos) + error++; + } + return error; +} + + + +void display() +{ + while(1) + { + + int choice; + system("cls"); + //MENU + cout<<"=======WELCOME TO HANGMAN======="<> wordToGuess; + transform(wordToGuess.begin(), wordToGuess.end(), wordToGuess.begin(), ::toupper); + isValidWord = has_only_alpha(wordToGuess); + if (isValidWord) + { + for (int i = 0; i < 100; i++) + { // clears the terminal + cout << "\n" << endl; + } + break; + } + else + { + cout << "Only letters are allowed in words." << std::endl; + } + } + + cout<<"========="<"; cin >> x; + if(x>='a' && x<='z') + { + x=x-32; + } + + if (guesses.find(x) == string::npos) + guesses += x; + + tries = TriesLeft(wordToGuess, guesses); + + } while (tries < 10); + + if (win) + cout<<"Congratulations "<"; cin >> x; + if(x>='a' && x<='z') + { + x=x-32; + } + + if (guesses.find(x) == string::npos) + guesses += x; + + tries = TriesLeft(wordToGuess, guesses); + + } while (tries < 10); + + if (win) + PrintMessage("YOU WON!"); + else + PrintMessage("GAME OVER"); + + system("pause"); + } + + ///////////////////////////////////////////////////////////////////////////////////////////////// + else if(choice==3) + { + cout<<"Enter words in the randomniser"<>s; + transform(s.begin(), s.end(), s.begin(), ::toupper); + file<>ch; + if(ch=='N'|| ch=='n') + { + break; + } + else if(ch=='Y'||ch=='y') + { + continue; + } + else + { + cout<<"Wrong choice"<