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.
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <Windows.h>
usingnamespace 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();
elseif (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') returntrue;
elsereturnfalse;
}
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)
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 :)
@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)
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?
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.
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))
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();
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.
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.
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?
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