-
Notifications
You must be signed in to change notification settings - Fork 0
/
PasswordDriver.cpp
65 lines (53 loc) · 1.75 KB
/
PasswordDriver.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
56
57
58
59
60
61
62
63
64
65
#include "Password.h"
#include "ReadFile.h"
#include <iostream>
using CSC2110::Keyboard;
using namespace std;
void addWords(Password* fh)
{
Keyboard* kb = Keyboard::getKeyboard();
String* file_str = kb->readString("Enter the file name containing the possible passwords: ");
ReadFile* rf = new ReadFile(file_str->getText());
delete file_str;
String* num_words_str = rf->readLine();
int num_words = num_words_str->a_to_i();
delete num_words_str;
for (int i = 0; i < num_words; i++)
{
String* word = rf->readLine();
fh->addWord(word);
}
delete rf;
fh->displayViableWords();
}
void guessWords(Password* fh)
{
Keyboard* kb = Keyboard::getKeyboard();
int numLeft = fh->getNumberOfPasswordsLeft();
while (numLeft > 1) //terminate if the password has been identified, or there is no valid password (user entry error)
{
int best_guess_index = fh->bestGuess();
String* best_guess_word = fh->getOriginalWord(best_guess_index);
cout << "You should guess \"";
best_guess_word->displayString();
cout << "\" at index " << best_guess_index;
cout << endl << endl;
String* guess_str = kb->readString("Index of word in the original word list to guess (1-based): ");
int guess = guess_str->a_to_i();
delete guess_str;
String* match_str = kb->readString("Number of character matches: ");
int match = match_str->a_to_i();
delete match_str;
cout << endl;
fh->guess(guess, match); //1-based index of the guessed word and the number of character matches
fh->displayViableWords();
numLeft = fh->getNumberOfPasswordsLeft();
}
}
int main()
{
Password* fallout = new Password();
addWords(fallout);
guessWords(fallout);
delete fallout;
}