2022-08-12 14:59:08 +01:00
|
|
|
#include <iostream>
|
2022-08-26 13:58:55 +01:00
|
|
|
#include <random>
|
|
|
|
#include <chrono>
|
2022-08-13 00:34:25 +01:00
|
|
|
|
2022-08-26 11:55:54 +01:00
|
|
|
using string = std::string;
|
|
|
|
void ignoreTillLineEnd();
|
|
|
|
string promptfs(string);
|
2022-08-26 12:10:43 +01:00
|
|
|
int promptfi(string);
|
2022-08-26 15:09:27 +01:00
|
|
|
string guessRank(int);
|
2022-08-15 12:48:53 +01:00
|
|
|
|
2022-08-12 14:59:08 +01:00
|
|
|
int main()
|
|
|
|
{
|
2022-08-26 13:58:55 +01:00
|
|
|
std::mt19937 rand {std::random_device{}()};
|
2022-08-26 15:09:27 +01:00
|
|
|
std::uniform_int_distribution clamp {1, 100};
|
2022-08-26 13:58:55 +01:00
|
|
|
string name {promptfs("Enter your name:")};
|
|
|
|
std::cout << "Hello " << name << ", welcome to guess the number.\n";
|
2022-08-26 15:09:27 +01:00
|
|
|
int guesses {};
|
2022-08-26 13:58:55 +01:00
|
|
|
int num {clamp(rand)}, ent {};
|
|
|
|
do {
|
2022-08-26 15:09:27 +01:00
|
|
|
if (guesses++ > 0) {
|
2022-08-26 14:32:00 +01:00
|
|
|
if (ent > num)
|
|
|
|
std::cout << "You Guessed Too High!\n";
|
|
|
|
else
|
|
|
|
std::cout << "You Guessed Too Low!\n";
|
|
|
|
}
|
2022-08-26 13:58:55 +01:00
|
|
|
ent = promptfi("Enter your guess:");
|
2022-08-26 12:10:43 +01:00
|
|
|
}
|
2022-08-26 13:58:55 +01:00
|
|
|
while (num != ent);
|
|
|
|
std::cout << "Well done " << name << ", you guessed correctly!\n";
|
2022-08-26 15:09:27 +01:00
|
|
|
std::cout << "You took " << guesses << " guesses, rank " << guessRank(guesses) << " .\n";
|
2022-08-26 11:55:54 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ignoreTillLineEnd() {
|
|
|
|
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
|
|
|
|
}
|
|
|
|
|
|
|
|
string promptfs(string msg) {
|
2022-08-26 13:58:55 +01:00
|
|
|
bool once {};
|
2022-08-26 11:55:54 +01:00
|
|
|
string toret {};
|
|
|
|
do {
|
|
|
|
if (once) {
|
|
|
|
std::cin.clear();
|
2022-08-26 12:10:43 +01:00
|
|
|
ignoreTillLineEnd();
|
|
|
|
std::cout << "Input Failure!\n";
|
|
|
|
}
|
|
|
|
std::cout << msg << "\n";
|
|
|
|
std::cin >> toret;
|
|
|
|
once = true;
|
|
|
|
}
|
|
|
|
while (std::cin.fail());
|
|
|
|
ignoreTillLineEnd();
|
|
|
|
return toret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int promptfi(string msg) {
|
2022-08-26 13:58:55 +01:00
|
|
|
bool once {};
|
2022-08-26 12:10:43 +01:00
|
|
|
int toret {};
|
|
|
|
do {
|
|
|
|
if (once) {
|
|
|
|
std::cin.clear();
|
|
|
|
ignoreTillLineEnd();
|
2022-08-26 11:55:54 +01:00
|
|
|
std::cout << "Input Failure!\n";
|
|
|
|
}
|
|
|
|
std::cout << msg << "\n";
|
|
|
|
std::cin >> toret;
|
|
|
|
once = true;
|
|
|
|
}
|
|
|
|
while (std::cin.fail());
|
2022-08-26 12:10:43 +01:00
|
|
|
ignoreTillLineEnd();
|
2022-08-26 11:55:54 +01:00
|
|
|
return toret;
|
2022-08-12 14:59:08 +01:00
|
|
|
}
|
2022-08-26 15:09:27 +01:00
|
|
|
|
|
|
|
string guessRank(int g) {
|
|
|
|
switch (g) {
|
|
|
|
case 0:
|
|
|
|
return "U";
|
|
|
|
case 1:
|
|
|
|
return "A^";
|
|
|
|
case 2:
|
|
|
|
return "A*";
|
|
|
|
case 3:
|
|
|
|
return "A";
|
|
|
|
case 4:
|
|
|
|
case 5:
|
|
|
|
return "B";
|
|
|
|
case 6:
|
|
|
|
case 7:
|
|
|
|
return "C";
|
|
|
|
case 8:
|
|
|
|
case 9:
|
|
|
|
case 10:
|
|
|
|
return "D";
|
|
|
|
case 11:
|
|
|
|
case 12:
|
|
|
|
case 13:
|
|
|
|
return "E";
|
|
|
|
default:
|
|
|
|
return "F";
|
|
|
|
}
|
|
|
|
}
|