1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
|
#include <iostream>
#include <string>
#include <fstream> //files
#include <iomanip> //setfill, setw etc..
#include <climits> //numeric limits etc..
#include <sstream>// convert text to x, and x to text
#include <cstdlib> //for srand && rand
#include <ctime> //for time
#include <vector>
std::string randWordFromFile;
std::vector<std::string> wordlist;
int menu();
void play();
void addWord(std::vector<std::string>& wordlist);
void readFile();
void randWord(std::vector<std::string>& wordlist, std::string randWordFromFile);
enum MenuChoice
{
playSession = 1,
addWords
};
int main (int argc, char** argv)
{
int menuChoice = 0;
while ( (menuChoice = menu()) > 0 )
{
switch(menuChoice)
{
case playSession:
play();
break;
case addWords:
addWord(wordlist);
break;
default:
std::cout << "Invalid menu choice, please try again!\n\n";
}
}
return 0;
}
int menu ()
{
int choice = 0;
std::cout << std::setfill('/') << std::setw(45) << "/\n";
std::cout << playSession << ".\tPlay Hangman!\n";
std::cout << addWords << ".\tadd a word\n";
std::cout << "0.\tQuit\n\n";
std::cin >> choice;
return choice;
}
void readFile()
{
std::ifstream file("wordlist.txt");
if(!file.is_open())
{
std::cout << "error: file \"wordlist.txt\" could not be opened!\n\n";
std::cout << "have the file \"wordlist.txt\" been created? if not, choose menu \"2\"!\n\n";
return;
}
else if (file.is_open())
{
std::cout << "file \"wordlist.txt\" have been \"opened\"!\n\n\n";
std::string line;
int numberOfLinesInFile = 0;
while (getline(file,line))
{
numberOfLinesInFile++;
wordlist.push_back(line);
}
file.close();
}
file.close();
}
void addWord(std::vector<std::string>& wordlist )
{
std::string wordToAdd;
std::cout << "\nEnter a word, to 'add' it to the game:\n";
std::cin >> std::ws; //ws = Whitesapce ("tabbar" rader)
std::getline(std::cin, wordToAdd);
wordlist.push_back (wordToAdd);/**//**/
std::ofstream file;
file.open("wordlist.txt", std::fstream::app);
for (int i = 0; i < wordlist.size(); i++)
{
file << wordlist[i] << std::endl;
}
file.close();
std::cout << wordToAdd << " was added to the game!" << std::endl;
}
void randWord(std::vector<std::string>& wordlist)
{
srand(time(NULL)); //initializes the random generator
randWordFromFile = wordlist[rand() % wordlist.size()];
}
void play()
{
readFile();
std::cout << std::setfill('/') << std::setw(45) << "/\n";
randWord(wordlist);
std::cout << "Guess the word, by writing a letter!\n\n";
int position;
int guessesLeft = 8;
std::string inputletter;
std::string guessedletters;
int maximumguesses = 0;
std::string rightLetters;
std::string s_variable2;
while(guessesLeft != 0)
{
for(int k=0; k<randWordFromFile.size(); k++)//For printing the blanks and showing the length of the word
{
std::cout << "_ ";
//s_variable2.assign(randWordFromFile.length(), '_');
}
std::cout << "\n\n";
std::cout << "Wrong guesses: " << guessedletters << "\n" << std::endl;
std::cout << "\nGuesses left: " << guessesLeft << "\n" << std::endl;
std::cin >> inputletter;
std::cout << "--> Guess again!\n\n";
guessedletters.insert(maximumguesses, inputletter);
maximumguesses++;
position = randWordFromFile.find(inputletter);
if (randWordFromFile.find(inputletter) != std::string::npos)
{
rightLetters[position] = randWordFromFile[position];
for ( int j = 0; j < randWordFromFile.size(); j++)
{
if (inputletter[0] == randWordFromFile[j])
{
rightLetters[j] = inputletter[0];
}
}
inputletter = "";
if (randWordFromFile.find(rightLetters))
{
std::cout << "You have won the game! The right word is:" << randWordFromFile << std::endl;
}
}
else
{
guessesLeft -= 1;
inputletter = "";
}
if(guessesLeft == 0)
{
std::cout << "You lost the game!\n" << std::endl;
}
}
}
|