Wt examples: hangman/Dictionary.C Source File

Wt examples 4.10.4

Loading...
Searching...
No Matches
Dictionary.C
Go to the documentation of this file.
1/*
2 * Copyright (C) 2011 Emweb bv, Herent, Belgium
3 *
4 * See the LICENSE file for terms of use.
5 */
6#include "Dictionary.h"
7
8#include <Wt/WApplication.h>
9
10#include <fstream>
11#include <iostream>
12#include <random>
13
14std::string randomWord(Dictionary dictionary)
15{
16 std::ifstream dict;
17 if (dictionary == Dictionary::Dutch) {
18 dict.open(Wt::WApplication::appRoot() + "dict-nl.txt");
19 } else { // english is default
20 dict.open(Wt::WApplication::appRoot() + "dict.txt");
21 }
22
23 std::string retval;
24 int numwords = 0;
25 while (dict) {
26 std::getline(dict, retval);
27 numwords++;
28 }
29 dict.clear();
30 dict.seekg(0);
31
32 std::random_device rd;
33 std::mt19937 gen(rd());
34 std::uniform_int_distribution<> distribution(0, numwords);
35 int selection = distribution(gen);
36
37 while (selection--) {
38 std::getline(dict, retval);
39 }
40 std::getline(dict, retval);
41 for (unsigned int i = 0; i < retval.size(); ++i) {
42 if (retval[i] < 'A' || retval[i] > 'Z') {
43 std::cerr << "word " << retval << " contains illegal data at pos " << i << '\n';
44 }
45 }
46
47 return retval;
48}
randomWord
std::string randomWord(Dictionary dictionary)
Definition Dictionary.C:14
Dictionary.h
Dictionary
Dictionary
Definition Dictionary.h:13
Dictionary::Dutch
@ Dutch