Build Game in C++
In this I will tell you how to develop a game using C++ programming language. First of all we have to take a random number through guess variable. Then we will set a limit of number from 1-35 or 1-100 its your choice to set a limit. Then we will take an input from user user will enter two numbers between limit and calculate sum of these numbers. This program will check if sum matches to random number then score will increase 10 times or 10 points and if wrong then it decreases to -10 until it get zero. So let start this program:
Program
#include <cstdlib>
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
srand((unsigned) time(0));
int num1 ;
int num2;
int guess;
int correct=0;
char again;
int incorrect=0;
for (int index = 0; index < 10; index++) {
num1 = (rand() % 35) + 1;
}
for (int index = 0; index < 10; index++) {
num2 = (rand() % 35) + 1;
}
do
{
cout << "Two random numbers have been generated,\n";
cout <<endl<< "Calculate the sum of x+y"<<endl;
cout<<"Chose any number to guess sum:"<<endl;
cin >> guess;
if(guess==num1+num2){
cout <<endl<< "You guessed correctly"<<endl;
correct+=10;
cout<<"score="<<correct<<endl;
cout<<"random number 1 was:"<<num1<<endl;
cout<<"random number 2 was:"<<num2<<endl;
cout << "Would you like to try again? Enter Y/N." << endl;
cin >> again;
}
else if (guess!=num1 + num2)
{
cout << "You guessed incorrectly"<<endl;
incorrect-=10;
cout<<"random number 1 was:"<<num1<<endl;
cout<<"random number 2 was:"<<num2<<endl;
cout << "Would you like to try again? Enter Y/N." << endl;
cin >> again;
}
}
while (again == 'y' || again == 'Y');
{
if (again == 'n' || again == 'N')
{
cout << "The number of correct guesses is: " << correct << "." << endl;
cout << "The number of incorrect guesses is: " << incorrect << "." << endl;
}
}
if (incorrect<0){
return main();
}
return 0;
}
Output of this program
0 Comments