Advertisement
KillianMills

hangman.cpp

Oct 1st, 2014
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.87 KB | None | 0 0
  1. #include "hangman.h"
  2. #include "sneaky.h"
  3.  
  4. #include <iostream>
  5. #include <string>
  6. #include <iterator>
  7. #include <fstream>
  8. #include <vector>
  9. #include <algorithm>
  10. #include<time.h>
  11.  
  12. using namespace std;
  13.  
  14. hangman::hangman(bool b){
  15.             debug = b;
  16. }
  17.  
  18. void hangman::inputGuesses(){ // prompts user to input the amount of guesses allowed
  19.     cout << "\n\n" <<"PLEASE ENTER THE NUMBER OF GUESSES FOR THE USER TO HAVE: " ;
  20.     cin >> guesses;
  21.     while(!(cin.good()) || guesses<=0 || guesses>26){ // limit user to at least the whole alphabet, change in future   
  22.             cin.clear();
  23.             cout<< "INVALID INPUT, PLEASE ENTER A NUMBER BETWEEN 1 AND 26: " ;
  24.             cin >> guesses;
  25.     }
  26. }
  27.  
  28. void hangman::inputWordSize(){  //prompts user to input the word size or difficulty of the word
  29.     cout << "\n" << "PLEASE ENTER THE SIZE OF WORD YOU WOULD LIKE: " ;
  30.     cin >> wordSize;
  31.     while(!(cin.good()) || wordSize<=0 || wordSize == 26 ||  wordSize == 27 || wordSize>29 ){ // no words of length 0,27,26,>29
  32.             cin.clear();
  33.             cout<< "NO WORD OF THAT SIZE, PLEASE INPUT A VALID WORD SIZE: ";
  34.             cin >> wordSize;
  35.         }
  36. }
  37.  
  38. void hangman::dashX(){ // makes a string of "-" of word length
  39.     for(int i=0; i<wordSize; i++){
  40.         dashWord=dashWord + "-"; //fills string with - for as many letters as there is
  41.     }
  42. }
  43.  
  44. void hangman::readFile(){ //read in file to vector holding entire dictionary
  45.     ifstream file("dictionary.txt");
  46.     istream_iterator<string> start(file), end;
  47.     vector<string> myVector(start, end);
  48.     vecsVector = myVector;
  49. }  
  50.  
  51. void hangman::difficulty(){ //make vector holding all words of size wordSize
  52.     for(int i = 0; i < vecsVector.size(); i++)
  53.     {
  54.         string temp = vecsVector[i];
  55.         if(temp.length() == wordSize){
  56.             sliceVector.push_back(temp);
  57.         }
  58.     }
  59. }
  60.  
  61. string hangman::random(){  //selects a word at random from the vector of size wordSize
  62.     srand(time(NULL));
  63.     int randomIndex = rand() % sliceVector.size();
  64.     string random = sliceVector[randomIndex];
  65.     return random;
  66. }
  67.  
  68. void hangman::mainGame(){ // where the user will guess the word by inputting character guesses
  69.     cout<< "\n\n" <<  "                ------LET THE GAMES BEGIN!------ "<< "\n\n";
  70.  
  71.     //keep user guessing while guesses!=0
  72.     while(guesses!=0){
  73.    
  74.         int check = guesses;//stores the value of guesses at the start of each guess
  75.         cout <<"WORD SO FAR: " << dashWord << "\n\n";
  76.         cout << "LETTERS USED: "<< usedLetters << endl;
  77.         cout << "GUESSES LEFT: " << guesses << endl;
  78.        
  79.         currentWord = random(); // selects a random word
  80.        
  81.         if(debug==true){
  82.             cout << "CURRENT WORD: " << currentWord << endl;
  83.         }
  84.        
  85.         cout << "PLEASE ENTER YOUR GUESS: ";   
  86.         cin >> userGuess;
  87.         cout<< "\n";
  88.        
  89.         sneaky s;
  90.         s.makeFamily(sliceVector, userGuess, debug);// makes families based on the dictionary, user input and wordSize
  91.    
  92.         for(int index=0; index<wordSize; index++){  // checks the guess against the first word in the new vector
  93.             string temp = sliceVector[0];
  94.             if(userGuess==temp[index]){ // if it has one of these letter the user will not lose any guesses
  95.                 dashWord[index]=userGuess;
  96.                 guesses++;
  97.             }
  98.         }
  99.        
  100.         if(currentWord==dashWord){ //user guesses the word, break out of loop
  101.             break;
  102.         }  
  103.        
  104.         usedLetters = usedLetters + userGuess + ' '; // updates list of characters used
  105.         guesses--; // decrements guesses each time
  106.        
  107.         if (guesses>check){ // makes sure you do not get lives back for multiple occurences of letters
  108.             guesses=check;
  109.         }
  110.         cout << "\n\n";
  111.     }
  112. }
  113.  
  114. void hangman::ending(){ // messages for the end game
  115.  
  116.     currentWord = random(); // selects a random word for the last time, will select same word if user has won.
  117.    
  118.     //loss condition
  119.     if(dashWord!=currentWord){
  120.         cout << "\n\n" << "SORRY THE WORD WAS: " + currentWord + ", YOU LOSE :( " << "\n\n";
  121.     }
  122.  
  123.     //win condition
  124.     else if(dashWord==currentWord){
  125.         cout << "\n\n" << "CONGRATULATIONS, YOU HAVE WON. THE WORD WAS: " + dashWord << "\n\n";
  126.     }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement