Age guessing game

Feb 19, 2013 at 5:29am
Hey guys,
I need to Write a program that predicts users’ age (0-128 years old) with at most 7 questions. The game starts with asking the user whether he/she is younger or older than G (an initial guess). The user responds with 'O’ for older', ‘Y for 'younger' and ‘X’ for ‘you got it!’
I should use a while loop to ask user whether his/her age is ‘>’, ’<’ or ‘=’ the guess,and update the guess using the binary search method. Note that I'm not allowed to use breaks.

Here's my code so far, I'm not sure how to continue:

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
#include <cstdlib>
using namespace std;
int main ()
{
    int guess1, guess2;
    char userinput;
    srand(time(NULL));
    cout << "Welcome to the age guessing game"
         << endl << endl;
    cout << "This program will initially try and guess your age."
         << endl;
    cout << "If the program guesses your age correctly, please press \"X\"."
         << endl;
    cout << "Otherwise please press \"O\" if your older than the program's"
         << endl;
    cout << "initial guess, and \"Y\" if your younger."
         << endl;
    guess1 = rand() %128+1;
    cout << "Are you " << guess1 << " years old?"
         << endl;
    cin >> userinput;

    if (userinput == 'X')
    {
        cout << "That was lucky!"
             <<endl;
    }

    else while (userinput == 'Y')
    {
        guess2 = rand() %guess1 + 1;
        guess1 = guess2;
        cout << "Are you " << guess2 << " years old?"
             << endl;
        cin >> userinput;
        if (userinput == 'X')
        {
            cout << "That was lucky!"
                 << endl;
        }
        }
    while (userinput == 'O')
    {
        guess2 = rand() %128 + 1;
        guess2 = guess1;
        cout << "Are you " << guess2 << " years old?"
             << endl;
        cin >> userinput;
        if (userinput == 'X')
        {
            cout << "That was lucky!"
                 << endl;
        }
    }


}


So I got lost in my own code. I appreciate any help in advance.
Thanks.
Last edited on Feb 19, 2013 at 5:58am
Feb 19, 2013 at 6:05am
You should not be using random numbers for this. The computer should not guess randomly, but instead should "divide and conquer" to determine the user's age.

That is.. you want to guess halfway between the known minimum and maximum. The user tells you if they are older/younger, which lets you establish a new min/max.

Example:

- Starting min/max is [1,128]
- computer guesses 64 (halfway between 1,128)
- player enters 'Y', you now know the new max is 63. New range: [1,63]
- computer guesses 32 (halfway).
- player enters 'Y'. New range: [1,31]
- computer guesses 16
- player enters 'O'. New range: [17,31]
- computer guesses 24
etc
etc


With that pattern, the computer will be able to correctly determine the user's age in no more than 7 guesses.
Feb 19, 2013 at 6:14am
I see, but it also says I should use binary search. Are you sure that's the way to go?
Feb 19, 2013 at 6:18am
What I described is a binary search. So yes, I'm sure.
Last edited on Feb 19, 2013 at 6:18am
Feb 19, 2013 at 6:23am
Thanks Disch, I'll post my new code here in a bit, so you can check it, if your not busy.
Feb 19, 2013 at 6:24am
I'm going to sleep soon so I probably won't see it tonight.
Feb 19, 2013 at 6:43am
Alright here's my 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
40
41
42
43
44
45
46
47
#include <iostream>
#include <string>
using namespace std;
const int NUMBER_OF_GUESSES = 7;
int main()
{
    int Guess;
    int numGuesses = 1;
    char answer;
    int max;
    int min;
    cout << "This program will try to guess your age in under" <<endl;
    cout << "7 attempts." << endl << endl;
    cout << "Press \'o\' if the number is too high" <<endl;
    cout << " or \'y\' if the number was too low, or \'x\'" << endl;
    cout << " if the number was correct."
         << endl << endl;
    Guess = 64;
    while (numGuesses <= 7)
    {
        cout << "Are you " << Guess << " years old?" << endl;
        cin >> answer;

        if (answer == 'o')
        {
            max = 128;
            min = Guess + 1;
            Guess = (max + min)/2;

            numGuesses ++;
        }
        else if (answer == 'y')
        {
            max = Guess - 1;
            min = 1;
            Guess = (max + 1)/2;

            numGuesses ++;
        }
  if (answer == 'x')
        {
            cout << "Your age is: " << Guess
                 << ". That was lucky!"
                 << endl;
                }
    }
}
Last edited on Feb 19, 2013 at 6:45am
Feb 19, 2013 at 6:49am
very close, but not quite.

1
2
3
4
5
6
7
8
        if (answer == 'o')
        {
            max = 128;   // <- are you sure you want to do this?
            min = Guess + 1;
            Guess = (max + min)/2;

            numGuesses ++;
        }


Think about it:

computer guesses 64
player says Y
computer guesses 32
player says O

With this, your code would reset the max to 128. Is that really what you want?
Last edited on Feb 19, 2013 at 6:49am
Feb 20, 2013 at 5:33am
Alright what should i do to make it work? Also I tried the program.
It's a disaster
Feb 20, 2013 at 5:35am
Here's my new 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
40
41
42
43
44
45
46
47
#include <iostream>
#include <string>
using namespace std;
const int NUMBER_OF_GUESSES = 7;
int main()
{
    int Guess;
    int numGuesses = 1;
    char answer;
    int max;
    int min;
    cout << "This program will try to guess your age in under" <<endl;
    cout << "7 attempts." << endl << endl;
    cout << "Press \'o\' if the number is too high" <<endl;
    cout << " or \'y\' if the number was too low, or \'x\'" << endl;
    cout << " if the number was correct."
         << endl << endl;
    Guess = 64;
    while (numGuesses <= 7)
    {
        cout << "Are you " << Guess << " years old?" << endl;
        cin >> answer;

        if (answer == 'o' || 'O')
        {
            max = 128;
            min = Guess + 1;
            Guess = (max + min)/2;

            numGuesses ++;
        }
        else if (answer == 'y' || 'Y')
        {
            max = Guess - 1;
            min = 1;
            Guess = (max + 1)/2;

            numGuesses ++;
        }
        if (answer == 'x' || 'X')
  {
            cout << "Your age is: " << Guess
                 << ". That was lucky!"
                 << endl;
        }
    }
}
Feb 20, 2013 at 11:41pm
Can anyone help me with this please?
Feb 21, 2013 at 12:45am
closed account (Dy7SLyTq)
line 24: answer == 'o' || answer == 'O' do the same for line 32 and 40
Feb 21, 2013 at 3:22am
Thanks guys, but I'm still having trouble with the program.
Here's an example of what happens:
computer guesses: 64
user enters: y
computer: 32
user enters:o
computer: 80

how do i fix this? i tried setting max= guess, didn't work.
Feb 21, 2013 at 4:04am
This is the problem I mentioned before:

I wrote:
computer guesses 64
player says Y
computer guesses 32
player says O

With this, your code would reset the max to 128. Is that really what you want?


I'm not going to spell out the solution because it's a basic logic issue. You should be able to figure it out on your own.

Just put yourself in the computer's place. If someone tells you they are younger than 64, then says they're older than 32... what would you mentally imagine the min/max to be?

EDIT: to rephrase..

If someone tells you they are older than 32 years old... what does that tell you about their maximum age?
Last edited on Feb 21, 2013 at 4:05am
Feb 21, 2013 at 4:18am
If someone says they are older than 32 years old. That means min=32 +1 or 33.
However it doesn't say anything about thier max age. It should still be 128. I can't seem to figure it out.
Feb 21, 2013 at 4:52am
The answer to the previous question told you they were younger than 64, so your range should now be from 33 to 63.
Feb 21, 2013 at 5:04am
I havent tested it completely but i know it works. you just had a few errors with the || statements and some other stuff

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
40
41
42
43
44
#include <iostream>
#include <string>
using namespace std;
const int NUMBER_OF_GUESSES = 7;
int main()
{
    int Guess;
    int numGuesses = 1;
    char answer;
    int max = 128;
    int min = 0;
    cout << "This program will try to guess your age in under" <<endl;
    cout << "7 attempts." << endl << endl;
    cout << "Press \'o\' if the number is too high" <<endl;
    cout << " or \'y\' if the number was too low, or \'x\'" << endl;
    cout << " if the number was correct."
         << endl << endl;
    Guess = max / 2;
    while (numGuesses <= 7 && answer != 'x')
    {
        cout << "Are you " << Guess << " years old?" << endl;
        cin >> answer;

        if(answer =='o' || answer =='O')
        {
            min = Guess + 1;
            Guess = (max + min)/2;
            numGuesses ++;
        }
        else if(answer == 'y' || answer == 'Y')
        {
            max = Guess - 1;
            Guess = (max + min)/2;
            numGuesses ++;
        }
        else if(answer == 'x' ||answer == 'X')
        {
            cout << "Your age is: " << Guess
                 << ". That was lucky!"
                 << endl;
        }
    }
   return 0;
}


you should also add a catch all else statement in case they enter an invalid choice.
Last edited on Feb 21, 2013 at 5:13am
Feb 21, 2013 at 5:18am
I appreciate your help guys.
Thanks alot for your time, and support.
Topic archived. No new replies allowed.