Guessing game

Hi everyone. I'm trying to make a guessing game or well I have made a guessing game between a human and a computer but the computer is very very dumb and it takes her about 10 - 20 thousand guesses to get the right number. So I thought maybe I could make her a little bit more smart by making her look at the last number guessed and if it's too low for example she narrows down the range, so for example if the computer has a random range from 1 - 99 and the guess is 7 and 7 is too low she goes "ah ok so now my range is from 7 - 99"

I might be misunderstanding the rand function but this is what I tried, I'll try to post all the code that is related to this function but the whole code would be to much and is not needed I think.

So first of here is the class for the computerplayer

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
computerplayer::computerplayer()
{
	
	computer_wins = 0;
	first = 99;
	second = 1;
}

int computerplayer::set_first(int f)
{
	first = f;

}

int computerplayer::set_second(int s)
{
	second = s;

}

int computerplayer::getGuess()
{
	srand(time(NULL));
	
	return rand () % first + second;

}


so as you can see at first the range is set from 1 - 99.
I use a bool function in the main program to affect the changes in the range, or at least that what I'm trying to do..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
bool checkForWin(int guess, int answer, int guessNumber)
{
	computerplayer high;
	
    cout << "Guess no. " << guessNumber << ", you guessed " << guess << ". ";
    if (answer == guess)
    {
        cout << "You're right! You win!" << endl;
        return true;
    }
    else if (answer < guess)
	{
		high.set_first(guess);
        cout << "Your guess is too high." << endl;
	}
    else
	{
		high.set_second(guess);
        cout << "Your guess is too low." << endl;
	}
    return false;
}


so here I'm trying to say, if the guess is to high then the next guess will definitely not be higher than this number so we lower the range to the number guessed, the same goes if the guess is to low, then the lower range changes so it won't go lower then that number.
When I run computer vs. computer to see if the values are changing and the range get's narrow it looks like nothing is happening and the range still stays at 1-99, the computer goes back to higher numbers although the range should be set not to go higher.

Any ideas of what I might be doing wrong here?
Last edited on
You are setting the range on a temporary object "high" that get's thrown into the abyss once your function reaches return.

The solution here is to create your computerplayer in main and pass it in by reference (and if any of the people currently in the lounge read this, it doesn't matter if he's passing a pointer or the syntactical construct known as a reference here, because both will achieve exactly the same thing).

Also, you might want to call your range variables "high" and "low" or something the like, because with "first" and "second" I had to look at the usage to figure out which was supposed to be which.
thanks hanst99 for your answer. I created computerplayer in main and passed it by reference into the bool function. I'm still getting the same answers from the computer. Here is my whole program (without the classes though) maybe there is something clumsy in here, ohh and good suggestion, I changed the names to set_high_range and set_low_range so their purpose is more clear now.

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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <iostream>
#include <cstdlib>
#include <ctime>
#include "humanplayer.h"
#include "computerplayer.h"
#include "player.h"
using namespace std;

bool checkForWin(int guess, int answer, int guessNumber,computerplayer &obj);
void play(Player& player1, Player& player2,computerplayer& obj);


int main()
{
	humanplayer one,two; //human player 1, human player 2
	computerplayer ein, zwei; //computer player 1, computer player 2
	computerplayer obj; //for setting guess range of the computer
	int select;
	char ans;
	cout <<"Welcome to the Guessing Game!\n";
	
	do {
		cout <<"*****************************\n";
		cout <<"Select the game type (1-3) from the menu, any other number to quit.\n";
		cout <<"Human vs. Human\n";
		cout <<"Human vs. Computer\n";
		cout <<"Computer vs. Computer\n";
		cout <<"*****************************\n";
		
		cin >> select;
		
		switch(select)
		{
			case 1:
				//human vs. human
				play(one,two, obj);
			break;
			case 2:
				//human vs. computer
				play(one,ein,obj);
			break;
			case 3:
				//computer vs. computer
				play(ein,zwei,obj);
			break;
		}
	
	cout <<"do you want to play again? (y/n)" <<endl;
	cin >> ans;
	
	}while(ans == 'y' || ans == 'Y');

	cout <<endl;
	cout <<"*************Results***************\n";
	cout <<"Human player 1 won " <<one.getWins() <<" times." <<endl;
	cout <<"Human player 2 won " <<two.getWins() <<" times." <<endl;
	cout <<"Computer player 1 won " <<ein.getWins() <<" times." <<endl;
	cout <<"Computer player 2 won " <<zwei.getWins() <<" times." <<endl;
	cout <<"***********************************\n";

	return 0;
}


bool checkForWin(int guess, int answer, int guessNumber,computerplayer &obj)
{	
    cout << "Guess no. " << guessNumber << ", you guessed " << guess << ". ";
    if (answer == guess)
    {
        cout << "You're right! You win!" << endl;
        return true;
    }
    else if (answer < guess)
	{
		obj.set_high_range(guess);
        cout << "Your guess is too high." << endl;
	}
    else
	{
		obj.set_low_range(guess);
        cout << "Your guess is too low." << endl;
	}
    return false;
}

void play(Player& player1, Player& player2,computerplayer& obj)
{
    int answer=0, guess=0, guessNumber=0;
    answer = rand() % 100;
    bool win = false;

    while (!win)
    {
        cout << "Player 1's turn to guess." << endl;
        guess = player1.getGuess();
        guessNumber++;
        win = checkForWin(guess, answer, guessNumber,obj);
        if (win) {
            player1.setWins(player1.getWins()+1);
            return;
        }

        cout << "Player 2's turn to guess." << endl;
        guess = player2.getGuess();
        guessNumber++;
        win = checkForWin(guess, answer, guessNumber,obj);
        if (win)
            player2.setWins(player2.getWins()+1);
    }
}
Well, maybe that's just me being stupid but I don't see where you ever get a guess from the computerplayer.
it's generated in the computerplayer class

1
2
3
4
5
6
7
int computerplayer::getGuess()
{
	srand(time(NULL));
	
	return rand () % first + second;

}


so the play function which is here above get's it by going guess = player1.getGuess(); , or player2.getGuess();

I'm sorry I forgot to mention that computerplayer inherits from player class which is a class based of virtual functions.

so this might be where the problem lies. I have two private integers first and second and I try to change their value with obj.set_high_range and obj.set_low_range but it doesn't seem to be working.

Here is the rest of the computerplayer class if it helps

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
computerplayer::computerplayer()
{
	
	computer_wins = 0;
	first = 99;
	second = 1;
}

int computerplayer::set_high_range(int f)
{
	first = f;

}

int computerplayer::set_low_range(int s)
{
	second = s;

}

int computerplayer::getGuess()
{
	srand(time(NULL));
	
	return rand () % first + second;

}

int computerplayer::setWins(int computer_winning)
{
	computer_wins = computer_winning;
}

int computerplayer::getWins()
{
	return computer_wins;

}

Last edited on
Ah wait, the error is in your calculation. It should be rand () % (high - low) + low.
AHH YES, I got it. I was declaring srand(time(NULL)) in the getGuess function, so everytime the computer made a guess srand was called, this was causing the problem. I got rid of this and added your calculations hanst and now it works perfectly. I'm down to about 60 - 120 guesses between to computers, one time it only took 2 guesses. Perfect. Thank you so much for your help :D
Do you want to keep it random like that? Cause by using binary search, you could essentially let the computer always get the right number after [log n base 2] times.
That would be an interesting thing to add to the program. I really just wanted the computer to be a little bit smarter then when it had to guess 65.700 times but there is alot you could add to this, binary search could maybe be an option in the game where you can pick how hard the computer is suppose to be or how smart, so you could choose a. dummy computer = 30 - 60 thousand guesses, average computer = 20 - 300 guesses and then supercomuter = very smart. Good idea ;)
can u pls give the complete class files of humanplayer.h, computerplayer.h" and include player.h, i'm kind of testing out the program cause i'm interested in learning c++...
Sure. I don't know if I'm the best to be learning from since I'm also quite the beginner but here is a zip file with all the files.

http://dl.dropbox.com/u/2208202/guessingame.zip
Topic archived. No new replies allowed.