Make your very own game using Visual C++ Express Edition (or in any PL)

I’m sure you all have heard of “Guess the number” game. Well who hasn’t. It’s practically in every programming book of every programming languages by every programming authors. The reason perhaphs is you can actually make billions of games from it. All you need to do is make up some weird stories with number guessing involved in there then you are good to go. Here are some examples:

1. Mind Reader Game

Here's the code:

#pragma endregion
  //declare two variables. intrandom will handle the generated random numbers
  //intcounter will handle the number of incorrect guesses
   int intrandom;
   int intcounter;
 private: System::Void guessbutton_Click(System::Object^  sender, System::EventArgs^  e) {
  //create a random object named randgen based on the Random class
     Random^ randgen=gcnew Random();
     //generate a random number from 1 to 31 and store the generated number into a variable named intrandom
     intrandom=randgen->Next(1,31);
     //if the date inputted by the user is lesser than the generated number
     if (int::Parse(this->datetextBox->Text)statuslabel->Text="Sorry. Your guess is wrong. Perhaps you need some crystal ball?";
   //if its lesser than the generated number obviously the user has guessed it wrong so we acummulate the value
   //of the intcounter variable.
     intcounter=intcounter + 1;
     }
   //if its greate than the generated number
     else if (int::Parse(this->datetextBox->Text)>intrandom)
     {
      //display this message
      this->statuslabel->Text="It's not what the audienced had picked.Perhaps you need some chant or something.";
   //if its greater  than the generated number obviously the user has guessed it wrong again so we acummulate the value
   //of the intcounter variable.
      intcounter=intcounter + 1;
     }
     else
     {
    //if the user has guessed it right. display the follwing message and disable the textbox.
      this->statuslabel->Text="You guessed it right. Congratulations! You are now a fullfledge magician.";
      this->datetextBox->Enabled=false;
     }
    //if the user has guessed it wrong thrice 
     if ( intcounter>2)
     {
    //dsiplay this then disbale the textbox.
      this->statuslabel->Text="You have failed the test. Your remaining magic powers will be taken from you.";
      this->datetextBox->Enabled=false;
     }

    }
};
}





2. Oil Magnate Game

Here's the code:

#pragma endregion
//I wont add comments on this one coz it’s practically the same code. we only //added random responses from the user if the answer is the answer is wrong
   int intrandom;
   int intcounter;
 private: System::Void guessbutton_Click(System::Object^  sender, System::EventArgs^  e) {
 
     Random^ randgen=gcnew Random();
   
     intrandom=randgen->Next(20,50);
   
     if (int::Parse(this->agetextBox->Text)Next(1,2);

     if (intrandom==1)
     {
     this->statuslabel->Text="Im flattered but I'm not that young.";
     }
     else
     {
     this->statuslabel->Text="I have a feeling  that you are just making fun of me.";
     }

        intcounter=intcounter + 1;
     }
  
     else if (int::Parse(this->agetextBox->Text)>intrandom)
     {
 
     intrandom=randgen->Next(1,2);

     if (intrandom==1)
     {
     this->statuslabel->Text="That's rude, I'm not that old.";
     }
     else
     {
     this->statuslabel->Text="Are you stupid or something?";
     }
 
      intcounter=intcounter + 1;
     }
     else
     {
   
      this->statuslabel->Text="Congratulations! You have won the 3 billion dollars.";
      this->agetextBox->Enabled=false;
     }
 
     if ( intcounter>2)
     {
 
      this->statuslabel->Text="You have used the maximum number of guesses and won nothing.";
      this->agetextBox->Enabled=false;
     }

    }
};
}

So there...enjoy game programming :)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.