Computer guesses the number

Hello,
Well my teacher gave us an assignment and I have to do it by only while, for, if-else loop. I cannot even use do loop. Also we can't use "Rand" command too. So, the choxie is pretty limited. Can someone guide me a bit about it?

Question 1:
Write a C++ program to do the following:
The user thinks of a number between 1 and 100 in his head. The computer tries to guess the number in as many guesses as it takes. The computer prints the value of a guess and the user tells the computer (by pressing one of two keys) if the number displayed is ‘too low’ or ‘too high’. This goes on till the number is correctly guessed by the computer. A typical session of the game will look like the following:

Please think of a number between 1 and 100.
(Suppose the user thinks of the number 40)
Is it 50?
Press ‘Y’ or ‘y’ if the number is correct.
Press ‘H’ or ‘h’ for ‘too high’ and press ‘L’ or ‘l’ for ‘too low’
(input) H
Is it 25?
Press ‘Y’ or ‘y’ if the number is correct.
Press ‘H’ or ‘h’ for ‘too high’ and press ‘L’ or ‘l’ for ‘too low’
(input) L
Is it 37?
Press ‘Y’ or ‘y’ if the number is correct.
Press ‘H’ or ‘h’ for ‘too high’ and press ‘L’ or ‘l’ for ‘too low’
(input) L
Is it 43?
Press ‘Y’ or ‘y’ if the number is correct.
Press ‘H’ or ‘h’ for ‘too high’ and press ‘L’ or ‘l’ for ‘too low’
(input) H
Is it 40?
Press ‘Y’ or ‘y’ if the number is correct.
Press ‘H’ or ‘h’ for ‘too high’ and press ‘L’ or ‘l’ for ‘too low’
(input) Y
Ha!
I guessed the number in 5 guesses.
Thank you for playing the game.

The program should maintain two values which represent the interval in which the number could lie. The middle value of the interval is presented as the guess and user informs if the guess is below or above the value he has in his mind. Depending on the input from the user, the interval is halved and the next guess is presented. This is done till the number is guessed correctly.
For example, in the above session, the computer maintains: [1..100] as first interval.
Middle value 50 is first guess.
User presses H. So 50 is high and number must be less than middle value. Hence the lower half of the previous interval is now made the new interval which is [1..50].
Middle value 25 is second guess.
User presses L. So 25 is low and the number must be greater than middle value. Hence the upper half of the previous interval is now made the new interval which is [25..50]
And so
Last edited on
What have you tried so far?

For help on loop/control flow, see: http://www.cplusplus.com/doc/tutorial/control/

the user tells the computer (by pressing one of two keys) if the number displayed is ‘too low’ or ‘too high’
Tutorial on user input: http://www.cplusplus.com/doc/tutorial/basic_io/
Last edited on
Here are some hints:

A for loop uses a counter.

The conditional in a for loop can use anything. You have a certain ending conditional.

Binary search.

Good luck.
a predictable, optimal algorithm is boring, though. You may care to slightly randomize the computer, such that it may not be optimal. No one wants to play the computer at chess where it always moves e4, or tic-tac-toe where it always goes move 1 take center square. I realize its not the most exciting game ever, but this idea is important in game design.
Last edited on
Well, this is what I have tried so far. Another condition is, we can't use rand or srand function, we have to do it by for, while or if-else loops. I did got this code somehow. But the value of
int guess=0;
is messing up, and I am not sure what is wrong with it, I have tried dry running this code.


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
#include <iostream>
#include <math.h>
using namespace std;

int main()
{
    const int max_value =100;
    const int min_value = 1;
    int guess=0, high=max_value, low=min_value;
    char choice;

    cout<< "Think about a number between " <<min_value<<" and " <<max_value<<endl;
    while((high-low)!=1)
    {
        cout<<"is your number less than or equal to " << guess << "?\n Enter H/h or L/l"<<endl;
        cin>>choice;
        if(choice=='h' || choice=='H')
        {

            guess-=(high-low)/2; //C+=A  ==> C=C+A
            high=guess;

        }
        else if (choice == 'l' || choice== 'L')
        {
            low=guess;
        }
        cout<<"Your number is: " <<high<<endl;
    }


    return 0;
}
why is
guess -= (high-low) /2 ??

guess = 0.
is your number higher than 0? yes.
guess -= (100-0)/2. guess now = -50.

if you want to do the optimal BS, then
say you picked 33.
0, 100 .. average = 50, so ask is it H/L 50.
its lower.
0-50, average 25, ask if H/L 25? Its higher.
25-50 average is 37. lower.
and so on.

no offense, but this is a classic problem of coding before understanding. If you play with the problem on paper and solve it the way you would if YOU were the computer playing the game against your 7 year old niece, how would you solve it? Turn that process into code.
Last edited on
Yes, I have tried it on paper, everytime we have to divide it by 2, like taking average. This is what I have done so far. But, now a question arrives if it is 33, and I want it to go higher after printing "25: , what will I do then. This game is going to 200+ lines of code.

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
#include <iostream>
#include <math.h>
using namespace std;

int main()
{
    int low=0;
    int high=100;
    int mid=0;
    char choice;
    while (choice!='Y' || choice!='y')
    {
        mid=(high+low)/2;
        cout<<mid;
        cin>>choice;
        if(choice=='L')
        {
            mid=(mid+low)/2;
            cout<<mid;
            cin>>choice;
            if(choice=='L')
            {
                mid=(mid+low)/2;
                cout<<mid;
                cin>>choice;
                if(choice=='L')
                {
                    mid=(mid+low)/2;
                    cout<<mid;
                    cin>>choice;
                }
            }
        }
    }
    
    

    return 0;
}
You keep nesting and repeating the logic. That's what loops are for.

First, you shouldn't start off your loop with a condition check, because you haven't set the value of choice yet.
A do-while loop may be suitable here. Or, getting the input could be factored out into its own function and used as the while condition, but that's not strictly necessary.

Every iteration, you have three choices, Y, L, or H.

What I would do, is something along the lines of...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
do
{
    int mid = (high + low) / 2;
    cout << mid;
    cin >> choice;
    if (choice == 'L')
    {
        // ... logic for CPU guessed too low
    }
    else if (choice == 'H')
    {
        // ... logic for CPU guessed too high
    }
    else if (choice == 'Y')
    {
        // ... logic for the CPU guessed correct
        // could just break here, because the game is over.
        break;
    }
} while (true);

cout << "You guessed it!\n";
Last edited on
Well, this is the problem, I can't use do loop. I have to solve it by "While", "If-else" or "for" loop. I can't even use srand or rand.
Last edited on
Right, missed that.

But it's a minor restriction. My advice is still the same, just do
1
2
3
4
while (true)
{
    // my previous code
}


or, set an initial dummy value, like
1
2
3
4
5
choice = 'x';
while (choice != 'Y')
{
     // ...
}
Last edited on
Nope, still stuck at it. This goes to 25, and again shows 50 or incase of higher number, 75 and again 50. I don't know why I can't figure out this code. Sorry all for being such a dunce.
This worked!!!
Thanks alot. But somehow it is not working for numbers like 26 and 53, it takes so so many tries, because it doesn't take a specific interval
High+low/2
but it worked.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
while(num<=100)
{
    if(choice=='Y')
    {
        cout<<"guessed it!";
        break;
    }
    else if(choice=='H')
    {
        num+=num/2;
        cout<<num;
        cin>>choice;
    }
    else if(choice=='L')
    {
        num-=num/2;
        
        cinn>>choice;
    }
  }
26
the size is 100, so lg(100)+1 is the max # of tries.
or, 7 max guesses, as you know 2^7 is 128 and 2^6 is 64
or formally: lg(100) is 6.6, integered is 6 + 1 is 7, as above.

1 50 is first go, its less
2 25, its higher.
3 37, its less
4 31, its less
5 28, its less
6 26 got it.

which comes from (0+100)/2, (0+50)/2 50 is new high, (25+50)/2 25 is new low from before, … etc.
if its taking more guesses than this, your logic is off somewhere.
Last edited on
Topic archived. No new replies allowed.