Tuesday 23 April 2013

GAME OF CHANCE USING RANDOM FUNCTION

Description:

This is a simple game of chance which is made using random function of c++.

CODE:


#include<iostream>
#include<conio.h>
#include<cstdlib>
#include<ctime>
using namespace std;
int rolldice(void);
int main()
{
    enum status{Continue,won,lost};
    int sum;
    int mypoint;
    char ch3;
    cout<<"Welcom to this exciting game of chance"<<endl;
    cout<<"The rules of the game are as follows"<<endl;
    cout<<"in the first round if your points are 7 or 11 you will win"<<endl;
    cout<<"and if your points are 2 , 3 or 12 you will loose"<<endl;
    cout<<"if your points are other than these you will go to the second round"<<endl;
    cout<<"in second round if your points become equal to your first round points then you will win"<<endl;
    cout<<"and if your second round points become equal to 7 then you will loose"<<endl;
    status gamestatus;
    srand(time(0));
    sum=rolldice();
    switch(sum)
    {
       case 7:
       case 11:
       gamestatus=won;
       break;
       case 2:
       case 3:
       case 12:
       gamestatus=lost;
       break;
       default:
       gamestatus=Continue;
       mypoint=sum;
       cout<<"your first round points are"<<mypoint<<endl;
       cout<<"your decesion can not be made on rolling"<<endl<<"If you want to continue pay 1$"<<endl;
       cout<<"if you have paid then press Y"<<endl;
       break;
               }
       cin>>ch3;
       if(ch3=='y'||ch3=='Y')
       {
       while(gamestatus==Continue)
       {
          sum=rolldice();
          if(sum==mypoint)
          gamestatus=won;
          else if(sum==7)
          gamestatus=lost;
                                  }
          if(gamestatus==won)
          cout<<"player wins"<<endl;
          else if(gamestatus==lost)
          cout<<"player loses"<<endl;
          getche();
          return 0;
    }
}
    int rolldice(void)
    {
        int die1,die2,worksum;
        char ch1,ch2;
        cout<<"press A for your first roll of dice"<<endl;
        cin>>ch1;
        if(ch1=='a'||ch1=='A')
        die1=1+rand()%6;
        cout<<"player rolled "<<endl<<die1<<endl;
        cout<<"press B for your second roll of dice"<<endl;
        cin>>ch2;
        if(ch2=='b'||ch2=='B')
        die2=1+rand()%6;
        worksum=die1+die2;

        cout<<"player rolled "<<endl<<die2<<endl<<"your total points are"<<worksum<<endl;
        return worksum;
     
        }


OUTPUT:


Welcom to this exciting game of chance
The rules of the game are as follows
in the first round if your points are 7 or 11 you will win
and if your points are 2 , 3 or 12 you will loose
if your points are other than these you will go to the second round
in second round if your points become equal to your first round points then you
will win
and if your second round points become equal to 7 then you will loose
press A for your first roll of dice
A
player rolled
2
press B for your second roll of dice
B
player rolled
6
your total points are8
your first round points are8
your decesion can not be made on rolling
If you want to continue pay 1$
if you have paid then press Y
Y
press A for your first roll of dice
A
player rolled
1
press B for your second roll of dice
B
player rolled
4
your total points are5
press A for your first roll of dice
A
player rolled
3
press B for your second roll of dice
B
player rolled
4
your total points are7
player loses

No comments:

Post a Comment