Sunday 25 September 2011

Making a simple game using c++

Getting started making simple game with C++


C++ is an excellent language for creating games because of its power and flexibility. By using Visual C++ and DirectX, you can write games in either native or managed code. This flexibility allows you to create your game on the platform that you are most comfortable with.
Creating a good game is a challenge that is outside the scope of this guided tour. If you are up to the challenge, check out the following links for information that will help you create your first game.

This is a very simple game named "Monty Hall" you can easily understand the coding of this game.You see three doors before you. One of them has a new car behind it.You will choose one of the doors. Then, before you get to see which door has the car behind it.Monty will give you the chance to change your choice after showing you that one of the other doors has nothing behind it.


#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>


main()
{
cout << "\n*************>>>>>> This is the Monty Hall Game. <<<<<<************\n\nYou see three doors 
    << "before you. One of them has a new car behind it.\n"
        << "You will choose one of the doors. Then, before you "
        << "get to see which\ndoor has the car behind it.\nMonty"
        << "will give you the chance to change your choice after"
        << "showing\nyou that one of the other doors has nothing"
        << "behind it.\n";
        {
   unsigned seed = time(NULL);
   srand(seed);
   int car, choice, option, open;
   int won = 0, lost = 0, count = 0;
   float percentage;
   car = (rand()%3 + 1);         //random integer from 1 to 3
ag :cout << "\n\nWhich door do you choose (1|2|3) : ";
   cin >> choice;
   count++;
   if (choice > 3 || choice < 1){ cout << "Wrong choice ! Please enter the correct choice as shown."; goto ag; }


   if (car == 1 && choice == 1) {open = 3; option = 2;}
   if (car == 1 && choice == 2) {open = 3; option = 1;}
if (car == 1 && choice == 3) {open = 2; option = 1;}
if (car == 2 && choice == 1) {open = 3; option = 2;}
if (car == 2 && choice == 2) {open = 1; option = 3;}
if (car == 2 && choice == 3) {open = 1; option = 2;}
if (car == 3 && choice == 1) {open = 2; option = 3;}
if (car == 3 && choice == 2) {open = 1; option = 3;}
if (car == 3 && choice == 3) {open = 2; option = 1;}
   cout <<"\nMonty shows that there is no car behind door number"
    << open << ".\n\nDo you want to change choice to door number "
        << option << " ? (y/n): ";
   char ans;
   cin >> ans;
   if (ans == 'y' || ans == 'Y') choice = option;
   cout << "\nDoor number " << car << " has the car behind it.\n"
    << "\nSince your final choice was door number " <<choice;
   if (choice == car) {
    cout << ", You won the car !\n";
      won++;
      cout << "\nDo you want to continue ? (y/n): ";
      char ch;
      cin >> ch;
      if (ch == 'y' || ch == 'Y')
      goto ag;
      else goto result;
      }
   else
   {
   cout << ", you did not win !" << "\n\nDon't worry, try again !\n";
   lost++;
   cout << "\nDo you want to try again ? (y/n): ";
   char ch;
   cin >> ch;
   if (ch == 'y' || ch == 'Y')
    goto ag;
      else goto result;
   }


result:{
cout << "\n\nRESULT :\n\n Total no of attempts = " <<                                                                                   count << " \t Won = " << won << " \t Lost = " << lost;
  percentage = (float(won*100)/count);
   cout << "\n\n Percentage of wins = " << percentage << " % ";
   getch();
   }
  }
}

No comments:

Post a Comment