Number Guessing Game

Hi, I've been working on a little number guessing game, I started off by making it a number I have to guess then wanted to implement a function that interacts with itself and guesses it on its own but ran into a bunch of problems.

Since this idea, my modifications have turned my code into a horrible view.

The following things don't work,

1. When it asks to play again, it used to loop back to the question of guessing the number as I had a boolean function instead of void, now it goes back to the main menu the same as if I'd type 'n'

2. The AutoPlayer function doesn't seem to guess more than once, I am not sure on how to make it keep guessing until it's correct, the function written for guessing it doesn't really matter for now as I can work on that, but I'm stuck with making it loop to keep guessing.

Here's the patient:

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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <Windows.h>

using namespace std;

char choice = 'n';
int counter = 1;
int yourGuess = 0;
int lastGuess = -1;
int compgen = 0;
int compGuess = 0;

void MainMenu();
int PlayGame();
bool GameOver();
void HumanPlayer();
void AutoPlayer();
int randGen(int low, int high);

int computerGuess();

int main()
{
	MainMenu();

return 0;
}

void MainMenu()
{
	while(true)
	{
		system("cls");
		cout<<"Main Menu\n\n";
		cout<<":: Enter an option ::\n\n";
		cout<<"1. Human Player\n";
		cout<<"2. Computer Player\n";
		cout<<"3. Exit\n\n";
		cout<<"> ";
		cin>>choice;

		if (choice == '1' || choice == '2') PlayGame();
		else if (choice == '3') break;
	}
}

int PlayGame()
{
	if (choice == '1')
	{
		HumanPlayer();
	}

	if (choice == '2')
	{
		AutoPlayer();
	}

	return GameOver();
}

void HumanPlayer()
{
	choice = 'n';

	compgen = randGen(1,100);
	system("cls");
	cout<<"Generated number is: "<<compgen<<endl;
	cout<<"The computer has thought of a number between 1 and 100\n\n";
	cout<<"Enter your guess: ";
	cin>>yourGuess;
	lastGuess = yourGuess;

	while (yourGuess != compgen)
	{
		++counter;
		if (yourGuess < compgen)
		{
			system("cls");
			cout<<""<<lastGuess<<" is incorrect!\n\n";
			cout<<"The number I thought of was higher!\n\n";
			cout<<"Enter another guess: ";
			cin>>yourGuess;
			lastGuess = yourGuess;
		}
		if (yourGuess > compgen)
		{
			system("cls");
			cout<<""<<lastGuess<<" is incorrect!\n\n";
			cout<<"The number I thought of was lower!\n\n";
			cout<<"Enter another guess: ";
			cin>>yourGuess;
			lastGuess = yourGuess;
		}
	}
	system("cls");
	cout<<"You guessed it!\n\n"<<yourGuess<<" was the number I thought of!\n\n";
	Sleep(2000);
}

void AutoPlayer()
{
	choice = 'n';

	compgen = randGen(1,100);
	system("cls");
	cout<<"Generated number is: "<<compgen<<endl;
	cout<<"The computer has thought of a number between 1 and 100\n\n";
	cout<<"Enter your guess: ";
	//I have no idea how to make the computerGuess() function input its guess here in a loop until it guesses it
	
	/*compGuess = computerGuess();
	yourGuess = compGuess;

	lastGuess = compGuess;
*/
	while (yourGuess != compgen)
	{
		++counter;
		if (yourGuess < compgen)
		{
			system("cls");
			cout<<""<<lastGuess<<" is incorrect!\n\n";
			cout<<"The number I thought of was higher!\n\n";
			cout<<"Enter another guess: ";
			cin>>yourGuess;
			lastGuess = yourGuess;
		}
		if (yourGuess > compgen)
		{
			system("cls");
			cout<<""<<lastGuess<<" is incorrect!\n\n";
			cout<<"The number I thought of was lower!\n\n";
			cout<<"Enter another guess: ";
			cin>>yourGuess;
			lastGuess = yourGuess;
		}
	}
	system("cls");
	cout<<"You guessed it!\n\n"<<yourGuess<<" was the number I thought of!\n\n";
	Sleep(2000);
}

bool GameOver()
{
	system("cls");
	cout<<"You guessed the secret number in "<<counter<<" guesses.\n\n";
	cout<<"Very good!\n\n";
	cout<<"Play again (y/n)? ";
	cin>>choice;

	counter = 1;
	if (choice == 'y') return true;
	else return false;
}

int randGen(int low, int high)
{
	srand(time(NULL));
	return rand()%(high-low)+low;
}

int computerGuess()
{
	return rand()%100/2;
}
Why are you so overcomplicating this? You already have the code for a random computer guess, then just print out the computer guess and add a loop while ( computerGuess != compgen so the computer would keep guessing until it gets it right.

Also you might want to add cin.get() after each computer guess so the player has to press enter everytime before the new computer guess is generated, this way it wouldn't instantly fill up the screen :)

Instead of while(true) loop at the top and then breaking it if input is 3, do while(choice != 3)
Last edited on
This is my number guessing game against the computer, poke around with it if you like. But if you use it or use parts of the code, please give credit :)

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
#include <iostream>
#include <cstdlib> // For rand and srand
#include <ctime> // For the time function
using namespace std;

int main(void)
{
	srand(time(0));
	int number = rand() % 1000 + 1;
	int guess;
	char choice = 'y';
	while (choice != 'n')
	{	
		cout << "I'm thinking of a number between 1-1000. Take a guess: ";
		cin >> guess;

		while (guess != number)
		{
			if (guess > number)
			{
				cout << "Too high, try again: ";
				cin >> guess;
			}
			else if (guess < number)
			{
				cout << "Too low, try again: ";
				cin >> guess;
			}
		}
			if (guess == number) //Thanks to Vidminas
			{
				cout << "Congrats! That's the number! ";
			}

		do
		{
			cout << "Play again? y/n: ";
			cin >> choice;
		}	while (choice != 'y' && choice != 'n');
	}
}
Last edited on
@BinaryGeek You do realise this person has already done such a game that can do the same thing as yours, and now he wants to add the possibility of the computer itself guessing the randomly generated number.

And anyway your code is quite incorrect, which I can see just by looking at it, because the play again function won't work and you made a mistake by writing if (guess = number) as it has to be if (guess == number)
Last edited on
Thanks Vidminas, I was able to get it working with your suggestions, I feel stupid for not seeing something so simple, it must be all the hours I spent thinking on how to do this that blocked my mind. I remember waking up early this morning knowing the solution, didn't write it down, went back to sleep and forgot when I woke up again.

One problem I'm still having is as mentioned in OP, when I'm done guessing the number, I get a prompt to play again or not, if I say no ('n') it goes back to the main menu as it should, but it does the same thing for yes ('y').

I want it to go back and start the game again without going back to the main menu, would I need to give the game 2 GameOver() functions or can I somehow make the same function give me two endings?
Ugh thanks Vidminas, I sometimes confuse = and ==..thanks for pointing that out.
What would be a good way to make the computerGuess function workout a faster way to guess the number instead of just random numbers?

I was playing around with some code but it's not to useful:

EDIT:

1
2
3
4
5
6
7
8
9
int computerGuess(int compGuess)
{
	system("pause");
	compGuess = rand()%(100-1);
	cout<<compGuess<<endl;
	if (compGuess > lastGuess) compGuess += compGuess*100/50;
	if (compGuess < lastGuess) compGuess -= compGuess*100/50;
	return compGuess;
}


I want to use an algorithm where it starts off with a guess of say.. 50, if higher then add 50% and try again (75), if lower then 50% of 50 (25) and so on until it gets it, I think that might be a pretty quick way to get it but I'm not sure how to represent it in syntax.

Anyone have an idea?
Last edited on
Well your generated number is up to 1000, right?

Then you can add something like:
1
2
3
4
5
if (compGuess < compgen - 100)
compguess += 20;

else if (compGuess > compgen + 100)
compGuess -= 20;

Although I don't think this exact code is a good idea (you'd definitely have to have more statements, because if you only have these two the computer will almost never get it right (because you add 20 or take away 20 - imagine compgen is 876 and compGuess is 875, then it will add 20, then take away 20 infinitely))
Last edited on
It should be a number up to 100 only, the method I want to implement should add or takeaway 50% of the last guess so then it's always getting closer to the actual number, also shouldn't it be if (compGuess < lastGuess) ? otherwise it's comparing to the actual value it should guess

I have a rough idea but I'm not sure how to implent it, something like

1
2
3
4
5
6
7
8
9
10
11
int lowest = 0;
int highest = 100+1; //Initialized as the highest random number generated
int compGuess = 0;
int lastGuess = -1;

compGuess = (lowest + highest)/2; //Where lowest = lowest number left and highest is the highest possible number left

lastGuess = compGuess;

//Then a conditional checking if it's lower then: highest = lastGuess;
//And if it's higher then: lower = lastGuess; 


I know my idea is close to where I want to get, I just don't know how to translate it into something that talks to cin.get();
Last edited on
Lemme think :D

I think you should make 2 pointers, where both of them are assigned to compGuess, one is half that value and one is double that value.

Then have your conditions and either add the value of a pointer to the compGuess or take it away... This will give you the add 50% and take away 50% functionality.

EDIT:

I'm stupid, haha... You only need 1 pointer which is half the value of compGuess and not 2.
Last edited on
Seems like my lovely Windows 7 crashed over sleep last night and lost all the new code I implemented after OP.

Oh well, I guess Ctrl+s also applies to these kinds of things, at least I have it guessing random numbers again.

Just need to work on the above mentioned algorithm.

I was thinking of making the guess in a function without pointers for now as I have not covered that yet in the book I'm reading, but what I will do is keep upgrading the code as I go along with my learning.
If you aren't doing this for a school assignment or something, then you should learn about pointers, at the level you need them, it's quite simple.

Read this: http://www.cplusplus.com/doc/tutorial/pointers/
Yeah it's for nothing in particular, I'm just reading a book on C++ and expanding on the exercises.

I've managed to add the following functionality to the program:

1
2
3
4
5
6
7
8
cout<<"Main Menu\n\n";
cout<<":: Enter an option ::\n\n";
cout<<"1. You Guess\n"; //Working
cout<<"2. Computer Guesses\n"; //Working
cout<<"3. Two Player Game\n"; //Working
cout<<"4. Auto Play\n"; //Working
cout<<"5. Network Game\n"; //Pending
cout<<"6. Exit\n\n";


Thanks for the help Vidminas, I'm sure I'll be back with some tougher questions :D

EDIT: What can I look into to making this little program act as a client/server in order to play against someone else over IP?
Last edited on
I've never done this, but you should look up network sockets as far as I know...
What is the difference between "Computer Guesses" and "Auto Play"?
Last edited on
2. The computer guesses a number you think of
4. The computer guesses a number generated by the computer
Oh lol, that's pretty cool, can I have the source code once you finish? :D
Might use some info about generating numbers in my game...
Last edited on
Sure man that's what we're here for, helping each other.

What do you need to generate?
I'm making a much larger game, where you can walk around and stuff, now I'm working on enemies. I won't get any further in this topic, I just wanted to ask if I can
have the source code once you finish?

:P
Last edited on
Here's the code I have related to generated numbers:

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
int randGen(int low, int high)
{
	srand((unsigned int)time(NULL));
	return rand()%(high-low)+low;
}

bool GenerateGuess()
{
	compGuess = (lowerBound+upperBound)/2;

	if (lastGuess == compGuess)
		return false; //If number was guessed, stop generating

	lastGuess = compGuess;
	return true; //If number is still a mystery, keep generating a new guess
}

void GetInput()
{
	cout<<"\n> ";
	if (compGuess > compNumber)
		gInput = '1';
	if (compGuess < compNumber)
		gInput = '2';
	if (compGuess == compNumber)
		gInput = '3';


	switch(gInput)
	{
	case '1': upperBound = compGuess; break;
	case '2': lowerBound = compGuess; break;
	case '3':
		if (compNumber == compGuess)
			{
				system("cls");
				cout<<"Yay!!"<<endl;
				Sleep(1000);
				break;
			}
		else 
		{
			system("cls");
			cout<<"\nDon't cheat!"<<endl;
			Sleep(1500);
			ShowStory();
		}

	}
//system("pause");
}
Topic archived. No new replies allowed.