Jump to content
  • Advertisement
Sign in to follow this  
Azteroth

got a question

This topic is 4831 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I started writing this program as a sort of game based off of the book beginning c++ game programming but the problem is the computer will start to guess and unless it hits the number by chance will keep guess but some what in order but if it skip it will start over kinda what I need is how can I get it to guess only betweena minimum number and a maximum number heres what I am working with:

// Guess my number 2.0
// Give control of play to user

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;
enum menuMain
{
	cpu = 1,
	user = 2,
	leave = 3
};

enum menuDiff
{
	easy = 1,
	normal = 2,
	hard = 3
};

int d2;

int diffSwitch(const menuDiff& dMenu)
{
	switch(dMenu)
	{
	case easy:
		d2 = 100;
		cout << "\nd2 = " << d2;
		break;
	case normal:
		d2 = 500;
		cout << "\nd2 = " << d2;
		break;
	case hard:
		d2 = 1000;
		cout << "\nd2 = " << d2;
		break;
	}

	return d2;

}

void displayDiff()
{
	int d;

	cout << "\n\nPlease select your difficulty:";
	cout << "\n\t1 - Easy";
	cout << "\n\t2 - Normal";
	cout << "\n\t3 - Hard";
	
	cout << "\n\nEnter your selection here: ";
	cin >> d;

	if((d >= cpu) && (d <= leave))
	{    
		diffSwitch(menuDiff(d));
	}
	else 
	{
		cout << "\nYou made an illegal selection.";
	}
}

void cpuChoose()
{
	displayDiff();
	
	srand(time(0));
	
	int theNumber = rand() % d2 + 1;
	int tries = 0, guess;

	do
	{
		cout << "\nEnter a guess: ";
		cin >> guess;
		++tries;

		if(guess > theNumber)
			cout << "\nToo high!\n";

		if(guess < theNumber)
			cout << "\nToo Low!\n";

	}
	while(guess != theNumber);

	cout << "\nThats it! You got it in " << tries << " guesses!";
	
}

void userChoose()
{
	displayDiff();

	int userNumber;
	int minNum;
	int maxNum = d2;
	int tries = 0, guess;

	cout << "\nEnter a number for the computer to guess(up to " << d2 << "): ";
	cin >> userNumber;

	do
	{
		srand(time(0));
		guess = rand() % maxNum + 1;

		cout << "\nEnter a number: " << guess;
		if((guess != userNumber) && (guess > maxNum) && (guess < minNum))
		{
			guess = rand() % maxNum + 1;


			if(guess > userNumber)
			{
				cout << "\nToo high!";
				maxNum = guess;
			}

			if(guess < userNumber)
			{
				cout << "\nToo low!";
				minNum = guess;
			}

			

		}
		tries++;
	}
	while(guess != userNumber);
	
	cout << "The computer got it in " << tries;

}
void mainSwitch(const menuMain& mMenu)
{
	switch(mMenu)
	{
	case cpu:
		cout << "\nCPU will choose a number.";
		cpuChoose();
		break;
	case user:
		cout << "\nUser will choose a number.";
		userChoose();
		break;
	case leave:
		cout << "\nGame will exit.";
		break;
	}
}

void displayMain()
{
	int m;

	cout << "\n\nPlease make a game selection below:";
	cout << "\n\t1 - User guesses";
	cout << "\n\t2 - CPU guesses";
	cout << "\n\t3 - Quit game";

	cout << "\n\nEnter choice here: ";
	cin >> m;

	if((m >= cpu) && (m <= leave))
	{    
		mainSwitch(menuMain(m));
	}
	else 
	{
		cout << "\nYou made an illegal selection.";
	}
}



int main()
{
	cout << "Welcome to guess my number 2.0.";

	displayMain();

	cout << "\n\nPress ENTER to continue...";
	cin.ignore(cin.rdbuf()->in_avail()+1);

	return 0;
}


thanks ahead, Azteroth [Edited by - Azteroth on August 24, 2005 5:33:57 PM]

Share this post


Link to post
Share on other sites
Advertisement
I haven't read your code but I assume you're writing a "guess my number" kind of game where the computer is the one to guess the number.

The algorithm you want to use is a binary search.

Keep track of the lower and upper bound of the range. Your guess is right in the middle: guess = (min + max)/2. If the reply is that you are too low, your new lower bound is guess+1 (would be just guess in a real binary search, but here, we are working with discrete intervals and we know that guess is not the value we want, so we can directly exclude it). If the reply is that you are too high, your new upper bound is guess-1 (idem).

Loop until either you find the right number, or you end up with an empty range - which means that you have been lied to at some point.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!