Thursday, November 11, 2010 | By: Asram Noordin

My First C++ Lab Test 1!!!!!!

                  Wooooooowwww!!!!.....cannot believe it coz i just finished with my first game in C++.Actually,it is the Lab Test 1 question previously in my Fundamental of Programming course.We need to create the Guess Game.Although it was a very long and tough coding(i think!!)but i just use a simple function.However,I manage to finish it within 1 hour n half...and as a result,i've got full mark..Alhamdulillah...thanks Mr.Hilmi(my current Lecture).If you have any C++ software,you can try it...Suit urself....!!!


#include<iostream>
#include<conio.h>
#include<cstdlib>
#include<ctime> 
#include<iomanip>

using namespace std;

void startGame();
void gameResults();

int a,b,life,mark,maxrand;
char c;


int main()
{
    
     system("TITLE WELCOME TO MY FIRST GAME,(PLEASE ENJOY YOURSELF)!!");
     system("COLOR F1");
    
     cout<<setw(50)<<">*** HIGHLOW GAME ***<\n"<<endl;
     cout<<"The aim of this game is to guess a number. You will be ask to type a number\n";
     cout<<"(You have 6 guess n life)\n";
     cout<<"The system then will tell you if this number is too High of too Low compared to the secret number to find\n\n";
     startGame();
     getch();
     return 0;
}

void startGame()
{
     a=0;
     b=0;
     life=0;
     maxrand=6;

     cout<<"Select difficulty mode:\n"; // the user has to select a difficutly level
     cout<<"1 : Easy (0-30)\n";
     cout<<"2 : Medium (0-50)\n";
     cout<<"3 : Difficult (0-100)\n";
     cout<<"or type another key to quit\n";
     c=30;

     cin>>c;                   // read the user's choice
     cout<<"\n";

     switch (c)
     {
        case '1' : maxrand = 30;  // the random number will be between 0 and maxrand
        break;
        case '2' : maxrand = 50;
        break;
        case '3' : maxrand = 100;
        break;
        default : exit(0);
        break;
     }

     life=6;         // number of lifes of the player
     srand( (unsigned)time( NULL ) ); // init Rand() function
     b=rand()%maxrand;  // j get a random value between 0 and maxrand
     

     gameResults();

}

void gameResults()
{
     if (life <= 0)
        // if player has no more life then he lose
     {
        mark=0;
        cout<<"You lose !\n\n";
        cout<<"Mark :"<<mark<<endl;
        cout<<"The Secret Number is :"<<b<<endl;
        cout<<endl;
        startGame();
     }

     cout<<"Enter Your Guess:";
     cin>>a;          // read user's number

     if ((a>maxrand) || (a<0)) // if the user number isn't correct, restart
     {
        cout<<"Error : Number not between 0 and "<<maxrand;
        gameResults();
     }

     if (a==b)
     {
           if(life==5)
           {
             mark=6;
             cout<<"YOU WIN !\n\n"; // the user found the secret number
             cout<<"Mark :"<<mark<<endl;
             cout<<endl;
             startGame();
           }
           else if(life==4)
           {
             mark=5;
             cout<<"YOU WIN !\n\n"; // the user found the secret number
             cout<<"Mark :"<<mark<<endl;
             cout<<endl;
             startGame();
           }
           else if(life==3)
           {
             mark=4;
             cout<<"YOU WIN!\n\n"; // the user found the secret number
             cout<<"Mark :"<<mark<<endl;
             cout<<endl;
             startGame();
           }
           else if(life==2)
           {
             mark=3;
             cout<<"YOU WIN !\n\n"; // the user found the secret number
             cout<<"Mark :"<<mark<<endl;
             cout<<endl;
             startGame();
           }
           else if(life==1)
           {
             mark=2;
             cout<<"YOU WIN !\n\n"; // the user found the secret number
             cout<<"Mark :"<<mark<<endl;
             cout<<endl;
             startGame();
           }
     }

     else if (a>b)
     {
        cout << "HIGH\n";
        life = life - 1;    // -1 to the user's "life"
        gameResults();
     }

     else if (a<b)
     {
        cout << "LOW\n";
        life = life - 1;
        gameResults();
     }
}
    

0 comments:

Post a Comment