80 lines
1.7 KiB
C++
80 lines
1.7 KiB
C++
#include <iostream>
|
|
#include <cstring>
|
|
#include <cstdlib>
|
|
#include <ctime>
|
|
|
|
using namespace std;
|
|
|
|
class Game {
|
|
private:
|
|
int upper;
|
|
int lower;
|
|
int count;
|
|
int answer;
|
|
public:
|
|
Game(void) {
|
|
this->reset();
|
|
}
|
|
|
|
int get_upper() {
|
|
return this->upper;
|
|
}
|
|
|
|
int get_lower() {
|
|
return this->lower;
|
|
}
|
|
|
|
int get_count() {
|
|
return this->count;
|
|
}
|
|
|
|
int get_answer() {
|
|
return this->answer;
|
|
}
|
|
|
|
void reset() {
|
|
this->upper = 999;
|
|
this->lower = 0;
|
|
this->count = 0;
|
|
this->answer = rand() % 1000;
|
|
}
|
|
|
|
bool guess(int num) {
|
|
this->count++;
|
|
if (num == answer) return true;
|
|
|
|
if (num > answer && num < this->upper) this->upper = num;
|
|
else if (num < answer && num > this->lower) this->lower = num;
|
|
|
|
return false;
|
|
}
|
|
};
|
|
|
|
int main(int argc, char const *argv[]) {
|
|
srand(time(nullptr));
|
|
|
|
Game game;
|
|
|
|
for (int i = 0; i < argc; i++) {
|
|
if (strcmp("-debug", argv[i]) == 0) {
|
|
cout << "[Debug] " << game.get_answer() << endl;
|
|
}
|
|
}
|
|
|
|
while (true) {
|
|
int guess;
|
|
cout << "Guess: ";
|
|
cin >> guess;
|
|
if (game.guess(guess)) {
|
|
cout << "Answer correct. You have guessed for " << game.get_count() << " times." << endl;
|
|
break;
|
|
} else {
|
|
cout << "Answer is fewer than " << game.get_upper() << endl;
|
|
cout << "Answer is more than " << game.get_lower() << endl;
|
|
cout << "------------------------" << endl;
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|