Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream> //Input/Output objects
- #include <string> //String objects
- #include <fstream> //File objects
- #include <cstdlib> //For rand and srand
- #include <ctime> //For time function
- using namespace std; //Name-space used in the System Library
- //User Libraries
- //Global Constants
- //Function prototypes
- void onePlayer();
- void twoPlayer();
- //Execution Begins Here!
- int main(int argc, char** argv) {
- //Declaration of Variables
- unsigned char select;
- //Input values
- //Main menu selection with input validation
- cout << " , , _ , , _, , , _ , , \n"
- " |_|,'|\\ |\\ | / _ |\\/|'|\\ |\\ | \n"
- "'| | |-\\ |'\\|'\\_|`| `| |-\\ |'\\| \n"
- " ' ` ' `' ` _| ' ` ' `' ` \n"
- " ' " << endl;
- cout << "Welcome to Hangman!";
- cout << "\n 1. One Player!"
- "\n 2. Two Player!!"
- "\n X. Quit :( " << endl;
- do {
- cin >> select; cin.ignore();//Player input
- } while(!(select == '1' || select == '2' || select == 'X' || select == 'x'));
- switch (select) {
- case '1': onePlayer(); break;
- case '2': twoPlayer(); break;
- case 'x':
- case 'X': ;
- }
- cout << "\nThanks for playing!\n";
- //Exit Program
- return 0;
- }
- void onePlayer(){
- //Variable declaration
- unsigned char sizeLst= 0; //Size of word list
- char guess; //Player's guess
- string diff, //Difficulty
- word, //Word used for hangman
- blnk = "", //Blank lines for unknown letters
- guessed = ""; //Already guessed letters
- unsigned seed = time(0); //Create seed from system time
- unsigned short lives = 9; //Player's life
- bool loop = true, //Used as condition for while loop
- correct = true, //If guess was wrong, take a life away
- repeat = false; //A check if letter has been guessed
- srand(seed); //Seed the random number generator
- ifstream inDiff; //Input file for difficulty
- diff:
- //Player chooses difficulty with input validation
- do {
- cout << "\nWhich difficulty would you like to play, easy(1), moderate(2) "
- "or hard(3)?" << endl;
- cin >> diff; cin.ignore();
- } while (!(diff == "easy" || diff == "1" || diff == "moderate" || diff == "2" || diff == "hard" || diff == "3"));
- cout << endl;
- //Depending on player's choice of difficulty, load text file containing custom # of words
- //If file couldn't load, return to difficulty menu
- if (diff == "easy" || diff == "1") {
- inDiff.open("easy.txt");
- sizeLst = 200;
- } else if (diff == "moderate" || diff == "2") {
- inDiff.open("moderate.txt");
- sizeLst = 97;
- } else if (diff == "hard" || diff == "3") {
- inDiff.open("hard.txt");
- sizeLst = 100;
- } else cout << "Error." << endl;
- string wordLst[sizeLst];
- if (inDiff) {
- //cout << "\nFile successfully loaded.\n\n";
- for (int i = 0; i < sizeLst; i++) {
- inDiff >> wordLst[i];
- }
- } else {
- cout << "File couldn't load. :(" << endl;
- goto diff;
- }
- word = wordLst[rand() % sizeLst]; //Assign random word from wordLst to variable 'word'
- cout << "Word: " << word << endl;
- cout << "Length of word: " << word.length() << "\n\n";
- //For every letter in word, add another underscore _ to variable 'blnk'
- for (int i = 0; i < word.length(); i++) {
- blnk += "_";
- }
- //GAME MECHANICS
- //While loops as long as word hasn't been guessed OR player's live is not 0
- //It shows mystery word and player inputs guess with input validation
- //If guess was incorrect, display deducted lives count
- //
- while (loop) {
- correct = true;
- cout << blnk << " Guessed: " << guessed << "\n";
- //Enter guess
- do {
- repeat = false;
- cout << "Guess: ";
- cin >> guess; cin.ignore();
- //If you guessed an already used letter
- for (int i = 0; i < guessed.length(); i++) {
- if (guess == guessed[i]) {
- repeat = true; //if the guess is already guessed then set to true
- cout << "You've already guessed this letter.\n";
- }
- }
- //Validation - Check if guess is a lowercase letter that hasn't yet been guessed
- } while ((guess < 96 || guess > 123) && !repeat);
- //Only add player's guess in variable 'guessed' if it hasn't been guessed yet
- if (guessed.find(guess) == string::npos) guessed += guess;
- //If you didn't guess right
- for (int i = 0; i < word.length(); i++) {
- if (word.find(guess) == string::npos) correct = false;
- }
- if (correct == false) cout << "Your guess was wrong. You have " << --lives << " lives left.\n\n";
- if (lives == 0){
- cout << "You lose. The word was " << word << ".\n\n";
- loop = false;
- }
- //If you guessed right
- for (int i = 0; i < word.length(); i++) {
- if (guess == word[i]) {
- blnk[i] = guess;
- }
- }
- if (word == blnk) {
- cout << "You win! The word was " << word << ".\n\n";
- }
- }
- }
- void twoPlayer() {
- //Variable declaration
- char guess; //Player's guess
- string word = "", //Word used for hangman
- blnk = "", //Blank lines for unknown letters
- guessed = ""; //Already guessed letters
- unsigned short lives = 9; //Player's life
- bool loop = true, //Used as condition for while loop
- correct = true, //If guess was wrong, take a life away
- repeat = false; //A check if letter has been guessed
- //Player Two inputs word for Player One to guess
- cout << "\nInput the word, Player Two! ";
- cin >> word; cin.ignore();
- //Converts user's capitalized word to all lowercase
- for (int i=0; i< word.length(); i++) {
- word[i] = tolower(word[i]);
- }
- cout << string( 100, '\n' ); //Clear screen
- //cout << "Word: " << word << endl;
- cout << "Length of word: " << word.length() << "\n\n";
- //For every letter in word, add another underscore _ to variable 'blnk'
- for (int i = 0; i < word.length(); i++) {
- blnk += "_";
- }
- //GAME MECHANICS
- //While loops as long as word hasn't been guessed OR player's live is not 0
- //It shows mystery word and player inputs guess with input validation
- //If guess was incorrect, display deducted lives count
- //
- while (loop) {
- correct = true;
- cout << blnk << " Guessed: " << guessed << "\n";
- //Enter guess
- do {
- repeat = false;
- cout << "Guess: ";
- cin >> guess; cin.ignore();
- //If you guessed an already used letter
- for (int i = 0; i < guessed.length(); i++) {
- if (guess == guessed[i]) {
- repeat = true; //if the guess is already guessed then set to true;
- cout << "You've already guessed this letter.\n";
- }
- }
- //Validation - Check if guess is a lowercase letter that hasn't yet been guessed
- } while ((guess < 96 || guess > 123) && !repeat);
- //Only add player's guess in variable 'guessed' if it hasn't been guessed yet
- if (guessed.find(guess) == string::npos) guessed += guess;
- //If you didn't guess right
- for (int i = 0; i < word.length(); i++) {
- if (word.find(guess) == string::npos) correct = false;
- }
- if (correct == false) cout << "Your guess was wrong. You have " << --lives << " lives left.\n\n";
- if (lives == 0){
- cout << "You lose. The word was " << word << ".\n\n";
- loop = false;
- }
- //If you guessed right
- for (int i = 0; i < word.length(); i++) {
- if (guess == word[i]) {
- blnk[i] = guess;
- }
- }
- if (word == blnk) {
- cout << "You win! The word was " << word << ".\n";
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement