correct formula for computer guessing game

Page 1 of 1

8 Replies - 7412 Views - Last Post: 03 March 2011 - 03:36 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=219857&amp;s=ff1525b637b5f0003bcf4404c78a702a&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 miatech

Reputation: 0
• Posts: 17
• Joined: 18-February 11

correct formula for computer guessing game

Posted 02 March 2011 - 11:56 AM

guys, I'm working on an problem in which the computer is guessing the number the user is thinking. but the guesses are not quite working. And I think is because the implementation of the guessHigh and guessLow methods. Any help appreciated. Here's the problem.
------------------------------------------------------------------------------------------------------

Write a program that calculates a random number 1 through 100. The program then asks the user to guess the number.
If the user guesses too high or too low then the program should output "too high" or "too low" accordingly.
The program must let the user continue to guess until the user correctly guesses the number.

Modify the program so that instead of the user guessing a number the computer came up with, the computer guesses the number that the user has secretly decided. The user must tell the computer whether it guessed too high or too low.

what I have so far.

```#include <iostream>
#include <Windows.h>
using namespace std;
void startGame();
void guessNum(int n);
int guessHigh(int l, int h);
int guessLow(int l, int h);
void guessRight(int n);
int low = 50, high = 100, count = 0, num = 50;
int main()
{
startGame();
system("pause");
return 0;
}

void startGame()
{
cout << "Think a number from 1 - 100. \n";
Sleep(5000);
guessNum(num);
}
void guessNum(int num)
{
char a;
do
{
cout << "Is your number " << num << " ?\n";
cout << "Enter (c) for correct\n";
cout << "Enter (l) if number is low\n";
cout << "Enter (h) if number is too high\n";
cout << "Enter (q) to quit\n";
cin >> a;
if(a == 'c') { guessRight(num); }
else if(a == 'l') { num = guessHigh(high, low); }
else if(a == 'h') { num = guessLow(high, low); }
else if(a == 'q') { cout << "Goodbye!\n"; break; }
else { cout << "Wrong choice try again\n"; }
count++;
}while(a != 'q');
}
int guessHigh(int l, int h)
{
int n = (l + h) / 2;
high = l; low = n;
return n;
}
int guessLow(int l, int h)
{
int n = (l - h) / 2;
high = l; low = n;
return n;
}
void guessRight(int n)
{
cout << "Congrats, you guessed in " << count << " tries!\n";
startGame();
}
```

This post has been edited by miatech: 02 March 2011 - 11:57 AM

Is This A Good Question/Topic? 0

Replies To: correct formula for computer guessing game

#2 chinchang

• Indie Game Developer

Reputation: 192
• Posts: 727
• Joined: 22-December 08

Re: correct formula for computer guessing game

Posted 02 March 2011 - 12:16 PM

If I am right, you always return the number which is between low and high...right ?
In that case, your low shouldn't be 50 in the beginning but 0 so that the first number you return is between 0 and 100.

Suppose if you think 25 and computer says 50, you say its too high and run the guessLow() which should do the following :

initialise high to the last_guess-1 as the number won't be bigger than this and keep low as it is.
And finally it should return the mid value i.e. (high-low)/2 not (low-high)/2.

By the way, you don't need to pass low and high to your functions as they are global variables.

#3 miatech

Reputation: 0
• Posts: 17
• Joined: 18-February 11

Re: correct formula for computer guessing game

Posted 02 March 2011 - 12:44 PM

chinchang, on 02 March 2011 - 12:16 PM, said:

If I am right, you always return the number which is between low and high...right ?
In that case, your low shouldn't be 50 in the beginning but 0 so that the first number you return is between 0 and 100.

Suppose if you think 25 and computer says 50, you say its too high and run the guessLow() which should do the following :

initialise high to the last_guess-1 as the number won't be bigger than this and keep low as it is.
And finally it should return the mid value i.e. (high-low)/2 not (low-high)/2.

By the way, you don't need to pass low and high to your functions as they are global variables.

well, 50 is between 0 and 100. And is the mid point, if like you said, your number is 25; then, you will call guessLow, and the formula is: (100 - 50)/2 which is 25... if your number is 75; then, you will call guessHigh, which is (100 + 50)/2, which is 75.... I get what you're saying.. you're base is to think of it as range(0-100), but if that's your range after you call either method you'll end up with 50 as result anyway... in my opinion the problem is after the formula, when I reassign the variables... that's when it gets tricky

This post has been edited by miatech: 02 March 2011 - 12:47 PM

#4 chinchang

• Indie Game Developer

Reputation: 192
• Posts: 727
• Joined: 22-December 08

Re: correct formula for computer guessing game

Posted 02 March 2011 - 01:06 PM

Reassigning the variables is what I explained.

See suppose this is your range :

|----------------|-------------------|
low | mid | high

The computer always returns mid.

What should your functions do ?

guessLow() should result in this range :

|-----------|-------------|
low | new_mid | new_high=last_mid-1

and guessHigh() in :
|-----------|--------------|
low=last_mid+1 | new_mid | high

And this is called Binary search. I guess this is what you mean to do.

This post has been edited by chinchang: 02 March 2011 - 01:08 PM

#5 miatech

Reputation: 0
• Posts: 17
• Joined: 18-February 11

Re: correct formula for computer guessing game

Posted 03 March 2011 - 02:29 PM

one thing I realize is that variables: low and high are not getting changed.... how's that?

#6 miatech

Reputation: 0
• Posts: 17
• Joined: 18-February 11

Re: correct formula for computer guessing game

Posted 03 March 2011 - 03:14 PM

I was able to get it working by passing the variables by & reference.... but how would it be if I wanted to pass by value instead?

#7 jimblumberg

Reputation: 5568
• Posts: 17,215
• Joined: 25-December 09

Re: correct formula for computer guessing game

Posted 03 March 2011 - 03:20 PM

If you pass variables to a function by value then the function will get a "copy" of the variables passed, and any change made to these variables in the function will be local to that function. The variables in the calling function will remain unchanged.

See these links: Functions I and Functions II.

Jim

#8 miatech

Reputation: 0
• Posts: 17
• Joined: 18-February 11

Re: correct formula for computer guessing game

Posted 03 March 2011 - 03:33 PM

so, in other words, passing by reference is the correct way of passing the variables in this case?

#9 chinchang

• Indie Game Developer

Reputation: 192
• Posts: 727
• Joined: 22-December 08

Re: correct formula for computer guessing game

Posted 03 March 2011 - 03:36 PM

miatech, on 04 March 2011 - 04:03 AM, said:

so, in other words, passing by reference is the correct way of passing the variables in this case?

Exactly. Because you need your variables to change inside the function called.