-
Notifications
You must be signed in to change notification settings - Fork 110
/
dictionary.cpp
55 lines (48 loc) · 1.46 KB
/
dictionary.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include "dictionary.h"
#include <vector>
#include <string>
#include <fstream>
// initiate word dictionary from file .wordler.data and store them as a
// list of upper case letters
dictionary::dictionary(){
dictionary_file = ".wordler.data"; // default file
load_dictionary(dictionary_file);
}
// loads words from file name passed in as long as it includes
// at least one word
void dictionary::load_dictionary(std::string file){
std::ifstream read(file);
std::vector <std::string> temp;
if( read.is_open() ){
std::string word;
while( getline(read,word) ){
//convert every word to UPPER CASE
for(int i=0; i<word.length(); i++){
word[i] = toupper(word[i]);
}
temp.push_back(word);
}
if( temp.size() > 0 ){
words = temp;
dictionary_file = file;
}
read.close();
}
}
// returns the name of the file that was most recently used to
// load the current dictionary
std::string dictionary::file_name(){
return dictionary_file;
}
// select a random word from our dictionary and return it
std::string dictionary::select_word(){
return words.at(rand() % words.size());
}
// return a word from our dictionary given a seed as an index
std::string dictionary::select_word(int seed){
return words.at(seed % words.size());
}
// retrieves the number of words currently in the dictionary
int dictionary::size(){
return words.size();
}