-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
words.cpp
38 lines (30 loc) · 956 Bytes
/
words.cpp
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
#include "words.h"
#include "fstream"
#include "string"
#include <random>
#include "QString"
#include "QDir"
#include "QCoreApplication"
Words::Words(const std::string &wordListPath) {
std::fstream myfile;
// open file
//myfile.open("C:/Users/Johannes/Downloads/Hangman/wortliste.txt");
QString appDir = QCoreApplication::applicationDirPath();
// Build the full path to the wordlist file
QString fullPath = QDir(appDir).filePath(QString::fromStdString(wordListPath));
// Open the file using the constructed full path
myfile.open(fullPath.toStdString());
if (myfile.is_open()) {
std::string str;
while (getline(myfile, str)) {
wordlist.push_back(str);
}
myfile.close();
}
}
std::string Words::getRandomWord(){
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dist(0, wordlist.size() - 1);
return wordlist[dist(gen)];
}