Jump to content
  • Advertisement

ICanC

Member
  • Content Count

    39
  • Joined

  • Last visited

Community Reputation

147 Neutral

About ICanC

  • Rank
    Member

Personal Information

  • Interests
    Programming

Recent Profile Visitors

The recent visitors block is disabled and is not being shown to other users.

  1. thanks all, slightly different to fallout, I just wanted to get the number of letters that exist in the answer, irrespective of where they are. I came up with this, which seems to work, but is probably over complicated : void computer::terminal::get_chars_correct(std::string guess) { int count{ 0 }; std::vector<char> pass; for (char c : answer) { pass.push_back(c); } auto check_match = [](const char pc, const char c) {return pc == c; }; std::vector<char> counted{}; std::vector<char>::iterator it; for (char c : guess) { it = pass.begin(); while (it != pass.end()) { bool b = check_match(*it, c); if (b) { if (std::find(counted.begin(), counted.end(), c) == counted.end()) //check that this letter hasn't already been accounted for { ++count; counted.push_back(c); } } ++it; } } chars_correct = count; }
  2. thanks Zakwayda, I appreciate the feedback very much. Regarding my question, there is no code actually doing the check yet, as I'm unsure as to how to go about doing it. Basiaclly if the user input is 'bars' and the correct answer is 'mars' I need the program to say 3 characters match, but I don't know how best to write a function to count this, although thinking about it again now, possibly I'll have a look at passing a function the above in 2 vectors of chars, iterate through the answer for each character in the user input, and count the matches... I've probably just answered my own question
  3. just functions to clear the screen and print characters slower like an old terminal #define CLEAR system("cls") void print(std::string s) { for (auto& c : s) { std::cout << c; std::this_thread::sleep_for(std::chrono::milliseconds(5)); } }
  4. 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)); }
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!