Jump to content

A simple number guessing game (for anyone that would like it :) )

- - - - -

  • Please log in to reply
13 replies to this topic

#1
RuneNova91

RuneNova91

    Learning Programmer

  • Members
  • PipPipPip
  • 74 posts
Hey everyone, I just finished my first program thanks to Flying Dutchman and NullW0rm.
This is for anyone that would like a piece of the code or would just like to see it finished (at least how I did it) in general. I hope this helps anyone who needs or would like to know how to do it and again, thank you so much Flying Dutchman and Nullw0rm :)

Here's the code. On my compiler I have 0 errors and it runs great but please let me know if you get errors or have problems running it:

//This is a very simple text based number guessing game in which the player gets 5 tries to guess the randomly

//generated number that is between 1 and 10. 


#include "stdafx.h"

#include <iostream>

#include <Windows.h>

#include <stdlib.h>

#include <time.h>


using namespace std;


int main()

{

	srand( (unsigned)time( NULL ) );


	int theNumber = (rand() % 10 + 1);

	int playersGuess;

	int tries = 0;


	cout << "Hello and welcome! \n";

	Sleep(3000);

	cout << "Lets play guess my number! \n";

	Sleep(3000);

	

	for(int tries=0;tries<5||theNumber==playersGuess;tries++) 

{

	cout << "Please enter a number between 1 and 10: ";


	cin >> playersGuess;

	if (playersGuess == theNumber) 

{

		cout << "You won! \n";

		Sleep(3000);

		cout << "Thank you for playing! \n";

		Sleep(3000);

}

	else if (playersGuess > theNumber) 

{

		tries++;

		cout << "Lower! \n";

} 

	else if (playersGuess < theNumber) 

{

		tries++;

		cout << "Higher! \n";

}

 

	if (tries == 4) 

		break;

}

{

		cout << "I'm sorry, you lose! \n";

		cout << "The answer is: " << theNumber;

		cout << "  \n";

}

	cin.get ();

	return 0;

}


Feel free to further simplify or enhance it in any way of course :)

Edited by RuneNova91, 30 July 2010 - 10:40 PM.
Code Update

How many programmers does it take to fix a light bulb? ...None, its a hardware problem.

#2
NastyDevil

NastyDevil

    Learning Programmer

  • Members
  • PipPipPip
  • 90 posts
I am not sure if this would work, but wouldnt it be simpler to remove that while and simply use a for loop?

Here is what i mean:

for(int i = 0;i<5||i==PlayerGuess;1++)

{

The if checks

}


So that will basically loop 5 times unless you guess the number right which will break the loop aswell...


Edit: btw this game should have less attemps as you can guess any number 1-10 in 4 tries or less. Of course if you use your brain a little.

#3
TeenChristian

TeenChristian

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 639 posts
Hey there,Cool project! I actually made a number guessing game as one of my first games as well. I also added a mini multi player mode. All you really need to do is ask one player to enter a number and give another player 5 tries to guess it. Good job ^^

PS. Nullworm helped me out with alot of my problems too. YAY FOR NULLWORM
My Personal Blog l Learning C++ l I'll be famous soon enough.

#4
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1

NastyDevil said:

I am not sure if this would work, but wouldnt it be simpler to remove that while and simply use a for loop?

Here is what i mean:

for(int i = 0;i<5||i==PlayerGuess;1++)

{

The if checks

}


So that will basically loop 5 times unless you guess the number right which will break the loop aswell...

How will you store the user input and tries count into one variable?

Congratulations, RuneNova91! :)

Just a small note, this if block must be in a while loop block, otherwise it is infinite loop. Just move closing while brace behind closing if brace

while (!flag) {

    ...

    if (tries == 5) 

    {

	cout << "I'm sorry, you lose! \n";

	cout << "The answer is:" << theNumber;

        flag = true;

    }

} // while loop


And as for number of tries: I think it's a unwritten rule that try count should be log(2) n.

Edited by Flying Dutchman, 30 July 2010 - 02:45 PM.
text size doesn't work or i'm doing it wrong

A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#5
NastyDevil

NastyDevil

    Learning Programmer

  • Members
  • PipPipPip
  • 90 posts

Flying Dutchman said:

How will you store the user input and tries count into one variable?

I would have a variable which will take on the input.... check my for loop:
it has two checks: 1. the i which will go from 0 to 5 2. if i==UserNumber where UserNumber is the variable that takes the user input.

So that will basically eliminate:
bool flag = false;

PS: I am not trying to say his method is incorrect I was just wondering if any of the two are more favorable in some case? I am guessing both would work, but a for loop was the way i was thought for this case.

#6
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
I'm not saying your method is wrong, but I just don't see how it could work. So please enlighten me :) And I apologize if I sounded aggressive earlier.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#7
NastyDevil

NastyDevil

    Learning Programmer

  • Members
  • PipPipPip
  • 90 posts
Here is the complete code with comments...

//This is a very simple text based number guessing game in which the player gets 5 tries to guess the randomly

//generated number that is between 1 and 10. 


#include "stdafx.h"

#include <iostream>

#include <Windows.h>

#include <stdlib.h>

#include <time.h>


using namespace std;


int main()

{

	srand( (unsigned)time( NULL ) );


	int theNumber = (rand() % 10 + 1);

	int playersGuess;


	cout << "Hello and welcome! \n";

	Sleep(3000);

	cout << "Lets play guess my number! \n";

	Sleep(3000);

	

for(int tries=0;tries<5||theNumber==playerGuess;tries++) // Loop will break after 5 attemps or if number is guessed

{

	cout << "Please enter a number between 0 and 10: ";


	cin >> playersGuess;

	if (playersGuess == theNumber) 

{

		cout << "You won! \n";

		Sleep(3000);

		cout << "Thank you for playing! \n";

		Sleep(3000);

		flag = true;

}

	else if (playersGuess > theNumber) 

{

		tries++;

		cout << "Lower! \n";

} 

	else if (playersGuess < theNumber) 

{

		tries++;

		cout << "Higher! \n";

}

	if (tries == 5) 

{

		cout << "I'm sorry, you lose! \n";

		cout << "The answer is:" << theNumber;

		flag = true;

}

}


	char f;

	cin.get ();

	return 0;

}


Logic of the program:
1. A random number is generated (0-10)
2. Loop initialized with int trial
a. It will check if the number is higher/lower/matches IF it is > or < then the generated number it will increment trial (trial++) and loop UNTIL trial reaches 5. IF it matches then it will complete the loop and break because theNumber==playerGuess
3. Loop breaks when 5 attemps are made or the guess matches the generated number
4. the end.

PS: i was too lazy to edit the whole code but obviously the insides of the if's have to changed aswell...

#8
RuneNova91

RuneNova91

    Learning Programmer

  • Members
  • PipPipPip
  • 74 posts
Thanks for the positive comments and also for all the help everyone. There were some small things that I guess I missed that I will look over and correct and I will post the edited and fixed code
How many programmers does it take to fix a light bulb? ...None, its a hardware problem.

#9
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
@NastyDevil - I kind of thought you meant like this, but I was curious if you solution for this
for (int i = 0; i < 5 || i == PlayerGuess; i++)

RuneNova91 said:

There were some small things that I guess I missed that I will look over and correct and I will post the edited and fixed code
Your code works perfecly now, so no need to change it. Instead, you could improve it. Add extra options (chose range, number of tries, etc...)
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#10
RuneNova91

RuneNova91

    Learning Programmer

  • Members
  • PipPipPip
  • 74 posts
Yes that's what I meant haha. Thank you so much everyone. I really appreciate it and I hope somewhere down the road someone learning like I am can use it :)
How many programmers does it take to fix a light bulb? ...None, its a hardware problem.

#11
RuneNova91

RuneNova91

    Learning Programmer

  • Members
  • PipPipPip
  • 74 posts
I updated the code everyone. just FYI. thank you for the help again everyone. I learned a TON and I'm glad I had you guys there to offer suggestions and help
How many programmers does it take to fix a light bulb? ...None, its a hardware problem.

#12
TeenChristian

TeenChristian

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 639 posts
Anytime ^^ can't wait to see your next project!
My Personal Blog l Learning C++ l I'll be famous soon enough.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users