Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <iostream>
- #include <string>
- #include <stdlib.h>
- #include <time.h>
- #include <vector>
- #include <cctype>
- //I did not make this, and have no idea what it does
- template<typename T, size_t N>
- T * end(T (&ra)[N]) {
- return ra + N;
- }
- //word list
- const char * wordArray[] = {"sheep", "balloon", "sled", "jetpack", "airplane", "computer", "television", "word", "hung", "snow", "cookie", "rythym", "moon", "pencil", "zebra", "dictionary", "interesting", "programming", "apple", "microwave", "lightbulb", "science"};
- const std::vector<std::string> wordList(wordArray, end(wordArray));
- //depreciated
- /*int getRandomIndex()
- {
- std::srand(time(0));
- return rand() % words.size();
- }*/
- std::string getBlankWord(std::vector<std::string> words, int currentWordIndex)
- {
- std::string blankString;
- for (int i = 0; i < words[currentWordIndex].size(); ++i)
- blankString.append("_");
- return blankString;
- }
- //gets correct input
- std::string getGuess(std::string guessingWord)
- {
- std::string input;
- while (true)
- {
- std::cout << "Enter your guess\n";
- std::getline(std::cin, input);
- if (input.length() != 1 && input.length() != guessingWord.size())
- {
- std::cout << "\nInvalid answer, try again.\n\n\n";
- continue;
- }
- if (input.length() == 1)
- {
- if (!isalpha(tolower(input[0])))
- {
- std::cout << "\nInvalid answer, try again.\n\n\n";
- continue;
- }
- }
- else
- {
- bool quit = false;
- for (int i = 0; i < input.length(); ++i)
- if (!isalpha(input[i]))
- {
- quit = true;
- break;
- }
- if (quit)
- {
- std::cout << "\nInvalid answer, try again.\n\n\n";
- continue;
- }
- }
- for (int i = 0; i < input.length(); ++i)
- input[i] = tolower(input[i]);
- return input;
- }
- }
- //gets y/n answer
- bool getYesNo()
- {
- std::string input;
- while (true)
- {
- std::cout << "Enter 'y' or 'n'\n";
- std::getline(std::cin, input);
- std::cout << "\n\n";
- if (input.length() != 1)
- {
- std::cout << "Invalid input, try again\n\n";
- continue;
- }
- else if (input.length() == 1)
- if (tolower(input[0]) == 'y')
- return true;
- else if (tolower(input[0]) == 'n')
- return false;
- else
- {
- std::cout << "Invalid input, try again\n\n";
- continue;
- }
- }
- }
- //checks if guess is correct
- bool checkGuess(std::string input, std::vector<std::string> words, int currentWordIndex)
- {
- if (input.length() == 1)
- for (int i = 0; i < words[currentWordIndex].length(); ++i)
- if (words[currentWordIndex][i] == input[0])
- return true;
- if (input.length() > 1)
- {
- if (input == words[currentWordIndex])
- return true;
- else
- return false;
- }
- return false;
- }
- //fills in the new string with letter guess
- void fillInLetters(std::string input, std::vector<std::string> words, int currentWordIndex, std::string & currentGuessingWord)
- {
- for (int i = 0; i < words[currentWordIndex].length(); ++i)
- if (words[currentWordIndex][i] == input[0])
- currentGuessingWord[i] = input[0];
- }
- //checks if the guessing string is equal to the word list string
- bool checkForWin(std::string currentGuessingString, std::vector<std::string> words, int currentWordIndex)
- {
- return currentGuessingString == words[currentWordIndex];
- }
- void printHealthAndWord(int health, std::string currentGuessingWord)
- {
- std::cout << "You have " << health << " attempts left before you are hung. ";
- std::cout << "Here is the word:\n ";
- for (int i = 0; i < currentGuessingWord.length(); ++i)
- std::cout << currentGuessingWord[i] << " ";
- std::cout << "\n\n";
- }
- //returns if input is new, based on past attempts list
- bool newInput(std::string input, std::vector<std::string> inputs)
- {
- for (int i = 0; i < inputs.size(); ++i)
- if (input == inputs[i])
- return false;
- return true;
- }
- //generates a random sequence of words
- std::vector<std::string> getRandomizedWordList()
- {
- std::vector<std::string> words;
- std::vector<std::string> wordListCopy = wordList;
- while (wordListCopy.size() > 0)
- {
- int random = rand() % wordListCopy.size();
- words.push_back(wordListCopy[random]);
- wordListCopy.erase(wordListCopy.begin() + random);
- }
- return words;
- }
- //startup announcements
- void startUpText()
- {
- std::cout << "~~~~~Hangman Test V0.1!~~~~~\n\n";
- std::cout << "For some reason not yet determined...\nyou are going to be hung - but don't worry.\n\n";
- std::cout << "You will not be hung if you can manage to guess a word within a certain amount of tries.\n\n";
- std::cout << "You will guess letters in an attempt to solve the word, or guess the entire word\n\n";
- std::cout << "Words will be randomly selected. To guess, type the letter or entire word, and press enter. It is not case sensitive.\n\n\n\n";
- }
- int main(int argc, const char * argv[])
- {
- startUpText();
- //random
- srand(time(0));
- std::string currentGuess;
- //string edited during guesses, starts out blank and blanks are filled in with actual letters
- std::string currentGuessingString;
- //index of string in words that is the word being guessed
- int index = 0;
- //"health", or whatever counts till hangman dies
- int health;
- //list of inputs, so you do not repeat an input
- std::vector<std::string> inputs;
- //word array everything draws from
- std::vector<std::string> words = getRandomizedWordList();
- //main game loop
- while (true)
- {
- currentGuessingString = getBlankWord(words, index);
- health = 6;
- inputs.clear();
- while (true)
- {
- printHealthAndWord(health, currentGuessingString);
- currentGuess = getGuess(words[index]);
- std::cout << "\n\n";
- if (!newInput(currentGuess, inputs))
- {
- std::cout << "You have tried to guess that before. Try again\n\n";
- continue;
- }
- else
- inputs.push_back(currentGuess);
- if (checkGuess(currentGuess, words, index))
- {
- if (currentGuess.length() > 1)
- {
- std::cout << "You have correctly guessed the word! You will not be hung!\n";
- std::cout << "Do you want to play again?\n\n";
- if (getYesNo())
- break;
- else
- return 0;
- }
- else
- {
- fillInLetters(currentGuess, words, index, currentGuessingString);
- std::cout << "Your have guessed correctly!\n\n";
- }
- }
- else
- {
- --health;
- std::cout << "You have guessed incorrectly!\n\n";
- //death
- if (health == 0)
- {
- std::cout << "You have been hung.\n\n";
- std::cout << "The word was " << words[index] << "\n\n";
- std::cout << "Would you like to play again?\n\n";
- if (getYesNo())
- break;
- else
- return 0;
- }
- }
- if (checkForWin(currentGuessingString, words, index))
- {
- std::cout << "Congratulations, you have solved the word and will not be hung!\n\n";
- std::cout << "Would you like to play again?\n\n";
- if (getYesNo())
- break;
- else
- return 0;
- }
- }
- ++index;
- index %= words.size();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment