I'm trying to make a terminal hacking game the same as fallout, but I'm struggling to get the number of characters matching between the user input and the correct password. I've tried searching but so far I haven't found any simple solutions. Please can someone suggest a way? I've also included the full class code below for any tips on improving it
void computer::terminal::create_pass()
{
int lower, upper;
switch (diff)
{
case difficulty::easy: lower = 2; upper = 4; break;
case difficulty::medium: lower = 5; upper = 7; break;
case difficulty::hard: lower = 8; upper = 12; break;
}
int a = rand_eng(lower, upper);
fill_vec(a);
this->fill_addresses(a);
this->fill_characters();
int vec_size = passwds.size();
int b = rand_eng(0, vec_size-1);
answer = passwds[b];
return;
}
void computer::terminal::fill_addresses(int amount)
{
int ran{};
std::string text{};
for (int i = 0; i <= amount; i++)
{
ran = rand_eng(100, 999);
text = "0xF" + std::to_string(ran);
bool dupe{ false };
std::vector<std::string>::iterator it = adds.begin();
while (it != adds.end())
{
if (*it == text)
{
dupe = true;
break;
}
++it;
}
if (dupe == false)
{
adds.push_back(text);
}
else
this->fill_addresses(amount);
}
return;
}
void computer::terminal::fill_characters()
{
std::vector<char> temp = { '?', '/', '\\', '-', '_', '[', ']', '$', '%', '&', '*', '"', '<', '#', '>' };
int ran{};
for (int i = 0; i <= 14; i++)
{
ran = rand_eng(0, 14);
chars.push_back(temp[ran]);
}
return;
}
void computer::terminal::fill_vec(int amount)
{
std::vector<std::string> temp{ "biscuit", "jupiter", "mars", "wellington", "statue", "radio", "sponge", "universe", "capture", "penny", "thunder", "hurricane", "knight", "bishop", "queen", "king"};
int r{ 0 };
for (int i = 0; i <= amount; i++)
{
bool dupe{ false };
r = rand_eng(0, 15);
std::string s = temp[r];
auto it = passwds.begin();
while (it != passwds.end())
{
if (*it == s)
{
dupe = true;
break;
}
++it;
}
if (dupe == false)
{
passwds.push_back(s);
}
}
return;
}
void computer::terminal::login(bool first_login)
{
CLEAR;
std::cout << "ROBCO INDUSTRIES (TM) TERMLINK PROTOCOL\nENTER PASSWORD NOW\n\n" << tries << " ATTEMPT(S) LEFT: ";
for (int i = 1; i <= tries; i++) { std::cout << "| "; } std::cout << "\n\n";
std::random_device shuffle_device;
for (int i = 0; i != passwds.size(); ++i)
{
print(adds[i]);
std::cout << '\t';
int before = rand_eng(0, 10);
int end = rand_eng(0, 10);
std::shuffle(chars.begin(), chars.end(), std::mt19937(shuffle_device()));
for (int i = 0; i <= before; i++)
{
std::cout << chars[i];
}
print(passwds[i]);
if (answer == passwds[i])
print("THIS IS THE ANSWER");
std::shuffle(chars.begin(), chars.end(), std::mt19937(shuffle_device()));
for (int i = 0; i <= end; i++)
{
std::cout << chars[i];
}
std::cout << '\n';
}
print("\n\nCHARACTERS MATCH: "); std::cout << chars_correct;
if (first_login)
guess();
else
return;
}
bool computer::terminal::guess_valid(const std::string& guess)
{
auto validity_check = [&guess](const std::string& pw) { return guess == pw; };
return std::any_of(passwds.begin(), passwds.end(), validity_check);
}
void computer::terminal::check_valid(std::string& s)
{
while (!guess_valid(s))
{
print("\n\nINVALID PASSWORD");
std::this_thread::sleep_for(std::chrono::seconds(1));
std::cout << "\n\n> ";
std::cin.clear();
std::getline(std::cin, s);
}
}
void computer::terminal::guess()
{
std::cout << "\n\n> ";
std::string s{};
std::cin.ignore();
std::getline(std::cin, s);
check_valid(s);
while (tries > 0)
{
if (pass_correct(s))
{
print("\n\nPASSWORD ACCEPTED");
std::this_thread::sleep_for(std::chrono::seconds(1));
return;
}
else
{
print("\n\nWRONG PASSWORD");
std::this_thread::sleep_for(std::chrono::seconds(1));
std::vector<std::string>::iterator it = passwds.begin();
while (it != passwds.end())
{
if (*it == s)
{
passwds.erase(it);
break;
}
++it;
}
tries--;
std::cout << "\n\n";
login(false);
}
std::cout << "\n\n> ";
std::cin.clear();
std::getline(std::cin, s);
check_valid(s);
}
CLEAR;
print("\n\nTERMINAL LOCKED. PLEASE CONTACT YOUR SYSTEM ADMINISTRATOR");
std::this_thread::sleep_for(std::chrono::seconds(3));
}