-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.cpp
More file actions
172 lines (154 loc) · 7.32 KB
/
Main.cpp
File metadata and controls
172 lines (154 loc) · 7.32 KB
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
//
// Created by max on 29.04.21.
//
#include "Main.h"
#include "Colors.h"
#include "Runner.h"
#include "SpellChecker.h"
#include "Combinations.h"
#include <iostream>
#include <vector>
#include <fstream>
#include <thread>
using std::cout;
using std::cin;
using std::string;
using std::vector;
using std::thread;
int main(const int argc, const char* argv[]) {
// Ask the user for the word to unscramble.
cout << "Please enter the word to unscramble: ";
cin >> Variables::scrambledWord;
cout << Colors::CYAN << ":: " << Colors::RESET << Colors::EFFECT_BOLD << "Now checking word combinations for \""
<< Colors::DARK_GRAY << Variables::scrambledWord << Colors::RESET << Colors::EFFECT_BOLD << "\"...\n";
cout << Colors::CYAN << ":: " << Colors::RESET << Colors::EFFECT_BOLD << "Reading in words.txt file...\n" << Colors::RESET;
// Read the file words.txt in to the vector "english words".
std::ifstream wordsFile;
// Change the commenting of the next two lines, when developing in CLion.
wordsFile.open("./words.txt", std::ios::out);
// wordsFile.open("../words.txt", std::ios::out);
if (wordsFile.is_open()) {
string line;
while (getline(wordsFile, line)) {
englishWords.push_back(line);
}
} else {
std::cerr << "FileError: Could not open the file scrambled_word.txt!";
return 1;
}
// Display information about the words in the vector "english words".
cout << Colors::GREEN << ":: " << Colors::LIGHT_GRAY << "words.txt " << Colors::RESET << "read in!\n"
<< Colors::YELLOW << ":: " << Colors::RESET << "words.txt contains "
<< Colors::EFFECT_UNDERLINED << Colors::LIGHT_GRAY << englishWords.capacity()
<< Colors::RESET_UNDERLINED << Colors::RESET << " words!"
<< "\n"
<< "Please enter the number of threads to work with: ";
// Ask the user for the number of threads he/she wants to use: currently not available!
int numberOfThreads = 1;
cin >> numberOfThreads;
cout << "\n"
<< Colors::YELLOW << ":: " << Colors::RESET << "Using "
<< Colors::LIGHT_GRAY << numberOfThreads
<< Colors::RESET << " threads."
<< "\n\n";
// Creates all possible combinations.
auto combinations = new Combinations{};
auto allCombinations = combinations->allCombinations(Variables::scrambledWord);
// Initialises the progress bar, used in the runner class.
int combinationsNumber = allCombinations.size();
processed = 0;
// does some preparation work for the splitting of work in the threads.
int wordsPerThread = combinationsNumber / numberOfThreads;
if (wordsPerThread == 0) {
numberOfThreads = 0;
wordsPerThread = combinationsNumber;
}
// Initializes and starts the threads.
vector<Runner *> runners{};
if (numberOfThreads <= 1) {
// Creates a runner with this list (handles the threads).
auto *runner = new Runner(englishWords, allCombinations, combinationsNumber, 1);
// Start the prepared thread.
runner->start();
// Add this thread to the vector of thread pointers.
runners.push_back(runner);
} else {
auto wordStart = allCombinations.begin();
auto wordEnd = wordStart + wordsPerThread;
auto wordStartInt = 0;
auto wordEndInt = wordStartInt + wordsPerThread;
for (int threadNumber = 1; threadNumber <= numberOfThreads; ++threadNumber) {
if (threadNumber == 1) {
// Creates a sublist of combinations of the words, each individual thread should handle.
vector<string> wordsCombinations{allCombinations.begin(), wordEnd};
// Creates a runner with this list (handles the threads).
auto *runner = new Runner(englishWords, wordsCombinations, combinationsNumber, numberOfThreads);
// Start the prepared thread.
runner->start();
// Add this thread to the vector of thread pointers.
runners.push_back(runner);
wordStart += wordsPerThread;
wordEnd += wordsPerThread;
wordStartInt += wordsPerThread;
wordEndInt += wordsPerThread;
} else if (threadNumber == numberOfThreads) {
// Creates a sublist of combinations of the words, each individual thread should handle.
vector<string> wordsCombinations{wordStart, allCombinations.end()};
// Creates a runner with this list (handles the threads).
auto *runner = new Runner(englishWords, wordsCombinations);
// Start the prepared thread.
runner->start();
// Add this thread to the vector of thread pointers.
runners.push_back(runner);
} else {
// Creates a sublist of combinations of the words, each individual thread should handle.
vector<string> wordsCombinations{wordStart, wordEnd};
// Creates a runner with this list (handles the threads).
auto *runner = new Runner(englishWords, wordsCombinations);
// Start the prepared thread.
runner->start();
// Add this thread to the vector of thread pointers.
runners.push_back(runner);
wordStart += wordsPerThread;
wordEnd += wordsPerThread;
wordStartInt += wordsPerThread;
wordEndInt += wordsPerThread;
}
}
}
// Initializes the vector of correct english words, each thread has gathered.
vector<string> correctEnglishWordFromScrambled{};
// Wait for all threads to finish.
for (auto runnerItem = runners.begin(); runnerItem < runners.end(); ++runnerItem) {
// Waits for the current thread to finish.
auto r = *runnerItem;
r->waitForFinish();
// When the thread has finished, get all its correct words and add them to the list of correct words.
auto correctWordsFromThread = r->getCorrectWords();
for (auto wordItem = correctWordsFromThread.begin(); wordItem < correctWordsFromThread.end(); ++wordItem) {
correctEnglishWordFromScrambled.push_back(*wordItem);
}
}
// Print the results
cout << "\n"
<< Colors::GREEN << ":: " << Colors::RESET
<< Colors::EFFECT_BOLD << "Done!" << Colors::RESET_BOLD << "\n"
<< Colors::GREEN << ":: " << Colors::RESET
<< Colors::EFFECT_BOLD << "All possible matches found!" << Colors::RESET_BOLD
<< "\n";
for (auto item = correctEnglishWordFromScrambled.begin(); item < correctEnglishWordFromScrambled.end(); ++item) {
cout << Colors::LIGHT_BLUE << *item << Colors::RESET;
// If the item is not the last in its line, print a semicolon.
if (item != correctEnglishWordFromScrambled.end() - 1) {
cout << "; ";
}
}
cout << "\n"
<< Colors::GREEN << ":: " << Colors::RESET
<< Colors::EFFECT_BOLD << "Exiting" << Colors::EFFECT_BLINK << "...\n" << Colors::RESET_BOLD << Colors::RESET_BLINK
<< Colors::LIGHT_GRAY << "Unscrambler made by MaFeLP (https://mafelp.githu.io/)!"
<< "\nThanks for using it!"
<< Colors::RESET
<< std::endl;
return 0;
}