ModDB Wiki
Register
Advertisement

This is a simple game that is easy to implement in c++. It is called guess the number. It is simple to implement,and you can have some fun taunting the player if they go to low or too high. Let's get started. The first thing we need to do is ask the user for a number then take that number and check against that number if it is too low we keep going until we hit the number then we end the game. First start up whatever ide you use and start a new project, choose console application, and add a c++ file to it. At the top of the page add the following code.


The code[]

#include <iostream>

using namespace std;

#define NUMBER 8

void Game();

int main()
{
	Game();

	char answer;

	cout << "\n\nWant to play again?  Enter 1 to continue  or 2 to quit ";
	cin >> answer;

	switch(answer)
	{
	case 1:
		Game();
	break;

	case 2:
		break;
	}

	cout << "Have a nice day!!!!" << endl;
	return 0;
}

void Game()
{
	int number = 0;
	cout << "Guess what number I am thinking of\n";
		
	while(number != NUMBER)
	{
		cout << "\nNumber:";
	cin >> number;

	
	if(number < NUMBER)
	{
		cout << "Number is too low";
	}
	if(number > NUMBER)
	{
		cout << "Number is too high"; 
	}

	if(number == NUMBER +5)
	{
		cout << "\nCold";
	}
	if(number == NUMBER- 7)
	{	
		cout << "\n In fact your so cold the antarctic is warmer than your guess";
	}
}
	cout << "\nGood Job Mate. The number was: " << NUMBER << endl;
}

Deciphering the code[]

In main the first thing that is called is the Game function. The reason it doesn't exit is because of the while loop inside of the function. Next we ask the user to try and guess what number the computer is thinking off. This could be any number, and that is why I used a define at the top of the code, so that number can have any value. Notice that I use NUMBER in each of the if statements , if I had used the value 8 and then later I wanted a higher value, I would have to go through every place I used it and change it. The meat of this game is the define statement. The cool thing about it is that if you change NUMBER from 8 to any other number , no matter which number you enter the results will be the same. So if you change NUMBER to 54 and the user enters 47, it will say, "your so cold the antarctic is warmer than your guess" It also enables you to add some taunts if the player doesn't get the number.

Well That is all for now. Fool around with the code and see what you come up with. I hope this was helpful. Soon I will update this code for a more engaging game. Peace!!!!

Advertisement