Skip to content

Instantly share code, notes, and snippets.

@Z41N
Last active July 3, 2020 03:56
Show Gist options
  • Save Z41N/613b5a3a98e83c7d7232b9bd0b01db98 to your computer and use it in GitHub Desktop.
Save Z41N/613b5a3a98e83c7d7232b9bd0b01db98 to your computer and use it in GitHub Desktop.
#include <iostream>
#include "guessing.hpp"
#include <string>
#include <vector>
#include <fstream>
// IN PROGRESS:
// Adding saving feature by having user enter user_name.
// Would pull from file with their save data (score data, etc.).
// Currently only provides username and rank, which is operational.
class User {
std::string rank = "Bronze";
public:
std::string user_name;
std::string get_rank()
{
if (user_name == "Zain" || user_name == "Helen") {
return "Diamond";
} else {
return rank;
}
}
};
int main() {
User user;
std::cout<<"\n>Enter your username: \n";
std::cin>>user.user_name;
// This takes the username they enter and forces uppercase of first character.
user.user_name[0] = toupper(user.user_name[0]);
int user_choice;
// System function to play MP3
system("afplay intro_music.mp3 &>/dev/null &");
// Found this cool set of special string characters that clears the console.
// Perhaps these would work too? TO DO LATER.
// std::cin.clear();
// std::cin.ignore();
std::cout << "\033[2J\033[1;1H";
std::cout<<"--------------------------------------------------------------------------------";
std::cout<<"\nWelcome to Guess-The-Number, "<<user.user_name<<"! Your current rank is: "<<user.get_rank()<<".\n";
do {
std::cout<<"\n---MAIN MENU--- \n";
std::cout<<"\n0. Quit"<<std::endl;
std::cout<<"1. Play Game"<<std::endl;
std::cout<<"2. Instructions"<<std::endl;
std::cout<<"3. Rank Information"<<std::endl;
std::cout<<"\nWhat would you like to do? Type the option number and hit enter: ";
std::cin>>user_choice;
if (user_choice == 0) {
std::cout<<"\n---GAME TERMINATED---\n\n";
system("killall afplay");
exit(-1);
} else if (user_choice == 1) {
std::cout << "\033[2J\033[1;1H";
play_game();
continue;
} else if (user_choice == 2) {
std::cout << "\033[2J\033[1;1H";
std::cout<<"---INSTRUCTIONS---\n";
std::cout<<">You have 10 tries to enter a number between 1 and 10 to guess what the computer outputted\n";
std::cout<<"---END INSTRUCTIONS---\n";
} else if (user_choice == 3) {
std::cout << "\033[2J\033[1;1H";
std::cout<<"---RANK INFORMATION---\n";
std::cout<<">Each player begins at the lowest rank, Bronze. Ranks are broken-down as follows: \n";
std::cout<<">Bronze = 9-10 tries\n";
std::cout<<">Silver = 6-8 tries\n";
std::cout<<">Gold = 2-5 tries\n";
std::cout<<">Diamond = FIRST TRY!\n";
std::cout<<">Your rank is recorded and carries with you each game.\n";
std::cout<<">Promotions and demotions occur depending on how many tries you made vs. your current high score.\n";
std::cout<<"---END RANK INFORMATION---\n";
} else {
std::cout<<"\n---INVALID SELECTION! Please select a valid option.\n";
}
} while(user_choice != 0);
}
void play_game();
#include <iostream>
#include <cmath>
#include <fstream>
void play_game() {
int answer;
int guess;
int misses = 0;
srand (time(NULL));
// A number between 1 and 10. Ignores 0 using +1 to offset.
answer = rand() % 11 + 1;
std::cout<<"---GAME STARTED---\n";
std::cout<<"Enter your guess (0 - 10): ";
// While loop to continue going as long as conditions are not met
// (10 - misses) used because we want to get the remainder # of tries
while (true && misses < 10 && guess != answer) {
std::cin>>guess;
if (guess == answer) {
misses++;
std::cout<<">YOU WIN!!! The answer was "<<answer<<"."<<std::endl;
} else if (guess > answer) {
misses++;
std::cout<<"Too high!"<<" You have "<<(10-misses)<<" tries left.\n";
} else if (guess < answer) {
misses++;
std::cout<<"Too low!"<<" You have "<<(10-misses)<<" tries left.\n";
}
}
std::ifstream input;
input.open("high_score.txt");
int current_score;
input >> current_score;
if (misses < current_score) {
std::cout<<"Nice, you did better";
std::ofstream output;
output.open("high_score.txt");
output << misses;
} else {
std::cout<<"Your score is unchanged";
}
// Rank system START: IN PROGRESS
std::ofstream ranks;
ranks.open("ranks.txt");
if (misses <= 1) {
std::cout<<">You've been promoted to Diamond!!!\n";
ranks<<"Diamond";
} else if (misses >= 2 && misses <= 5) {
std::cout<<">Your rank has been changed to Gold!\n";
ranks<<"Gold";
} else if (misses >= 6 && misses <= 8) {
std::cout<<">Your rank has been changed to Silver!\n";
ranks<<"Silver";
} else if (misses >= 9 || misses == 10) {
std::cout<<">Your rank has been changed to Bronze.\n";
ranks<<"Bronze";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment