forked from DarthJDG/NumGuess
-
Notifications
You must be signed in to change notification settings - Fork 1
/
num_dos.cpp
79 lines (67 loc) · 1.9 KB
/
num_dos.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <string.h>
#include <math.h>
#include <time.h>
#include <conio.h>
#include <iostream.h>
#include <sstream.h>
using namespace std;
main()
{
int i, num = 0, guess = 0, tries = 0, limit = 0, max_tries = 0;
string name, buffer;
randomize();
cout << "Welcome to NumGuess C++ version!" << endl << endl;
cout << "Enter your name: ";
getline(cin, name);
if(name[0] == '\0') name = "Player";
cout << endl << "Welcome " << name << ", enter upper limit: ";
getline(cin, buffer);
stringstream limitstream(buffer);
if(!(limitstream >> limit)) limit = 10;
if(limit < 10) limit = 10;
max_tries = (int)floor(log2(limit)) + 1;
while(1) {
num = random(limit) + 1;
guess = 0;
tries = 0;
cout << endl << "Guess my number between 1 and " << limit << "!" << endl;
while(num != guess) {
cout << endl << "Guess: ";
getline(cin, buffer);
stringstream guessstream(buffer);
if(guessstream >> guess) {
if((guess < 1) || (guess > limit)) {
cout << "Out of range.";
} else {
tries++;
if(num < guess) {
cout << "Too high!";
} else if(num > guess) {
cout << "Too low!";
}
}
} else {
cout << "That's just plain wrong.";
}
}
cout << endl << "Well done " << name << ", you guessed my number from " << tries << " " << (tries == 1 ? "try" : "tries") << "!" << endl;
if(tries == 1) {
cout << "You're one lucky bastard!";
} else if(tries < max_tries) {
cout << "You are the master of this game!";
} else if(tries == max_tries) {
cout << "You are a machine!";
} else if(tries <= max_tries * 1.1) {
cout << "Very good result!";
} else if(tries <= limit) {
cout << "Try harder, you can do better!";
} else {
cout << "I find your lack of skill disturbing!";
}
cout << endl << "Play again [y/N]? ";
getline(cin, buffer);
if((buffer[0] != 'y') && (buffer[0] != 'Y')) break;
}
cout << endl << "Okay, bye." << endl;
return 0;
}