Skip to content

Instantly share code, notes, and snippets.

@fuck-shithub
Created September 24, 2017 14:37
Show Gist options
  • Save fuck-shithub/85809a1eee819c1349e91e4ea4c038c8 to your computer and use it in GitHub Desktop.
Save fuck-shithub/85809a1eee819c1349e91e4ea4c038c8 to your computer and use it in GitHub Desktop.
CHALLENGE 5 / 20SEP - 25SEP / Mastermind (Fallout's Hacking Game)
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <stdlib.h>
#include <time.h>
using namespace std;
string toupperstring(string s) //function for making all letters in a string uppercase.
{
for(int i = 0; i<s.length(); i++)
{
s[i] = toupper(s[i]);
}
return s;
}
int main(int argc, char* argv[])
{
bool easy = false;
if (argc > 1)
{
if (string(argv[1]) == "-easy") easy = true; //sets easy to true when you type "-easy" as an argument in the console.
}
vector<string> passwords;
ifstream file ("words.txt");
if (file.is_open())
{
string passwordFromFile;
while (file >> passwordFromFile)
{
passwords.push_back(passwordFromFile); //put all passwords in a vector.
}
if (passwords.size() == 0)
{
cout << "The file \"words.txt\" is empty or only contains whitespace characters.";
return 1;
}
srand(time(NULL)); //use time as seed
string password = passwords[rand() % passwords.size()]; //pick a password.
//alternative method: string password = passwords[(int) (passwords.size() * (rand() / (RAND_MAX + 1.0)))];
int maxTries = password.length(); //sets max tries to password length.
int tries;
for (tries = 0; tries < maxTries; tries++)
{
string input;
if (tries == 0) cout << "Guess password: "; else cout << "You have " << maxTries - tries << " tries left. Guess password: "; //tells user to guess the password and shows how many tries they have left.
getline(cin, input); //get user input.
if (toupperstring(input) == toupperstring(password)) //check if input is equal to password (not case sensitive).
{
cout << "You won! You guessed the password!" << endl;
return 0;
}
else
{
if (!input.empty()) //this will stop the program from freezing when input is empty.
{
int correctPositions = 0;
if (password.length() >= input.length())
{
for (int i = 0; i < password.length(); i++)
{
if (i < input.length())
{
if (toupper(password[i]) == toupper(input[i]))
{
if (easy == true) cout << (char) toupper(password[i]) << " ";
correctPositions++;
}
else
{
if (easy == true) cout << "_ ";
}
}
else
{
if (easy == true) cout << "- ";
}
}
if (easy == true) cout << "(";
cout << correctPositions << "/" << password.length();
if (easy == true) cout << ")";
cout << endl;
}
else
{
for (int i = 0; i < input.length(); i++)
{
if (i < password.length())
{
if (toupper(password[i]) == toupper(input[i]))
{
if (easy == true) cout << (char) toupper(password[i]) << " ";
correctPositions++;
}
else
{
if (easy == true) cout << "_ ";
}
}
else
{
if (easy == true) cout << ". ";
}
}
if (easy == true) cout << "(";
cout << correctPositions << "/" << password.length();
if (easy == true) cout << ")";
cout << endl;
}
}
}
}
if (tries == maxTries) cout << "You lost! The password was: " << toupperstring(password);
}
else
{
cout << "Unable to open file. Make sure the file is called \"words.txt\".";
return 1;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment