fbpx

C++: Guess the Number With Feedback (Vs Player)

Bjarne-stroustrup
 

The task is to write a player for the game that follows the following rules:

The scorer will choose a number between set limits. The computer player will print a guess of the target number. The computer asks for a score of whether its guess is higher than, lower than, or equal to the target. The computer guesses, and the scorer scores, in turn, until the computer correctly guesses the target number.

The computer should guess intelligently based on the accumulated scores given. One way is to use a Binary search based algorithm.

A clever solution that takes advantage of C++’s built-in binary search function lower_bound(). Instead of searching a slice of a container, we search a range of numbers by implementing a specially-designed custom iterator.

#include <iostream>
#include <algorithm>
#include <string>
#include <iterator>

struct GuessNumberIterator : std::iterator<std::random_access_iterator_tag, int> {
	int i;
	GuessNumberIterator() { }
	GuessNumberIterator(int _i) : i(_i) { }
	GuessNumberIterator& operator++() { ++i; return *this; }
	GuessNumberIterator operator++(int) {
		GuessNumberIterator tmp = *this; ++(*this); return tmp; }
	bool operator==(const GuessNumberIterator& y) { return i == y.i; }
	bool operator!=(const GuessNumberIterator& y) { return i != y.i; }
	int operator*() {
		std::cout << "Is your number less than or equal to " << i << "? ";
		std::string s;
		std::cin >> s;
		return (s != "" && (s[0] == 'y' || s[0] == 'Y')) ? 0 : -1;
	}
	GuessNumberIterator& operator--() { --i; return *this; }
	GuessNumberIterator operator--(int) {
		GuessNumberIterator tmp = *this; --(*this); return tmp; }
	GuessNumberIterator& operator+=(int n) { i += n; return *this; }
	GuessNumberIterator& operator-=(int n) { i -= n; return *this; }
	GuessNumberIterator operator+(int n) {
		GuessNumberIterator tmp = *this; return tmp += n; }
	GuessNumberIterator operator-(int n) {
		GuessNumberIterator tmp = *this; return tmp -= n; }
	int operator-(const GuessNumberIterator &y) { return i - y.i; }
	int operator[](int n) { return *(*this + n); }
	bool operator<(const GuessNumberIterator &y) { return i < y.i; }
	bool operator>(const GuessNumberIterator &y) { return i > y.i; }
	bool operator<=(const GuessNumberIterator &y) { return i <= y.i; }
	bool operator>=(const GuessNumberIterator &y) { return i >= y.i; }
};
inline GuessNumberIterator operator+(int n, GuessNumberIterator &i) { return i + n; }

const int lower = 0;
const int upper = 100;

int main() {
	std::cout << "Instructions:\n"
	<< "Think of integer number from " << lower << " (inclusive) to "
	<< upper << " (exclusive) and\n"
	<< "I will guess it. After each guess, I will ask you if it is less than\n"
	<< "or equal to some number, and you will respond with \"yes\" or \"no\".\n";
	int answer = std::lower_bound(GuessNumberIterator(lower), GuessNumberIterator(upper), 0).i;
	std::cout << "Your number is " << answer << ".\n";
	return 0;
}

SOURCE

Content is available under GNU Free Documentation License 1.2.