Tuesday 12 November 2013

Dice Game Coding




Code:-

#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
class dice
{
private:
int number,i,j;
char a;
public:
int balance;
dice()
{
balance=100;
number=0;
randomize();
}
int choose_number()
{
// randomize();
number=random(6);
if(number==0)
{
choose_number();
}
return number;
}

void dice_show(int)
{
char array[3][3]={ {' ',' ',' '},
{' ',' ',' '},
{' ',' ',' '} };

a=' ';
switch(number)
{
case 1:
cout<<"...........";
array[1][1]= char (254);
for(i=0;i<=2;i++)
{
cout<<"\n";
cout<<".";
for(j=0;j<=2;j++)
{
cout<<" ";
cout<<array[i][j];
cout<<" ";
}
cout<<".";
}
cout<<"\n...........";
break;
case 2:
cout<<"...........";
array[1][0]= char (254);
array[1][2]= char (254);
for(i=0;i<=2;i++)
{
cout<<"\n";
cout<<".";
for(j=0;j<=2;j++)
{
cout<<" ";
cout<<array[i][j];
cout<<" ";
}
cout<<".";
}
cout<<"\n...........";
break;
case 3:
cout<<"...........";
array[1][1]= char (254);
array[0][2]= char (254);
array[2][0]= char (254);
for(i=0;i<=2;i++)
{
cout<<"\n";
cout<<".";
for(j=0;j<=2;j++)
{
cout<<" ";
cout<<array[i][j];
cout<<" ";
}
cout<<".";
}
cout<<"\n...........";
break;
case 4:
cout<<"...........";
array[0][0]= char (254);
array[0][2]= char (254);
array[2][0]= char (254);
array[2][2]= char (254);
for(i=0;i<=2;i++)
{
cout<<"\n";
cout<<".";
for(j=0;j<=2;j++)
{
cout<<" ";
cout<<array[i][j];
cout<<" ";
}
cout<<".";
}
cout<<"\n...........";
break;
case 5:
cout<<"...........";
array[0][0]= char (254);
array[0][2]= char (254);
array[2][0]= char (254);
array[2][2]= char (254);
array[1][1]= char (254);
for(i=0;i<=2;i++)
{
cout<<"\n";
cout<<".";
for(j=0;j<=2;j++)
{
cout<<" ";
cout<<array[i][j];
cout<<" ";
}
cout<<".";
}
cout<<"\n...........";
break;
case 6:
cout<<"...........";
array[0][0]= char (254);
array[0][2]= char (254);
array[2][0]= char (254);
array[2][2]= char (254);
array[1][0]= char (254);
array[1][2]= char (254);
for(i=0;i<=2;i++)
{
cout<<"\n";
cout<<".";
for(j=0;j<=2;j++)
{
cout<<" ";
cout<<array[i][j];
cout<<" ";
}
cout<<".";
}
cout<<"\n...........";
break;
}
}
};
int main(void)
{
cout.flush();
dice d1,d2;
int

number1,number2,input,number3,number4,bet;
cout<<" ----------Dice Throw Game---------

\n";
begin:
if(d1.balance <=0 || d2.balance <= 0)
{
cout<<"Sorry you dont have enough credit to

play again\n";
cout<<"Press any key to start the game

again\n";
input=getch();
if(input)
{
d1.balance=100;
d2.balance=100;
goto begin;
}
}
cout<<"Select the bet till 100\n";
cin>>bet;
while(bet>d1.balance && bet>d2.balance)
{
cout<<"Select the bet less than your

balance \n";
cin>>bet;
}
cout<<"player 1: press any key to enter\n";
input=getch();
if(input)
{
number1=d1.choose_number();
d1.dice_show(number1);
cout<<"\n";
number3=d1.choose_number();
d1.dice_show(number3);
cout<<"\n";
}
cout<<"player 2: press any key to enter\n";
input=getch();
if(input)
{
number2=d2.choose_number();
d2.dice_show(number2);
cout<<"\n";
number4=d2.choose_number();
d2.dice_show(number4);
cout<<"\n";
}
cout<<"the randomly selected numbers are

"<<(number1+number3)<<" "<<

(number2+number4);
if((number1+number3) < (number2+number4))
{
cout<<"\nplayer two is the winner\n";
d1.balance=d1.balance-bet;
d2.balance=d2.balance+bet;
cout<<"The left balance of Player 1 is :

"<<d1.balance;
cout<<"\nThe left balance of player 2 is :

"<<d2.balance<<"\n";
}
else if ((number1+number3) ==

(number2+number4))
{
cout<<"\ndraw\n";
}
else
{
cout<<"\nplayer one is the winner\n";
d2.balance=d2.balance-bet;
d1.balance=d1.balance+bet;

if(d1.balance <=0 || d2.balance<=0){
if (d1.balance<=0){
cout<<"Player 1 has lost the game\n";
cout<<"The left balance of Player 1 is :

"<<d1.balance;
cout<<"\nThe left balance of player 2 is :

"<<d2.balance<<"\n";
cout<<"press 'escape' to exit";
cout<<" or Press any other key to play the

game again\n";
input=getch();
if(input==char (27))
{
exit;
}
else
{
clrscr();
goto begin;
}

}else if(d2.balance<=0){
cout<<"Player 2 has lost the game\n";
cout<<"The left balance of Player 1 is :

"<<d1.balance;
cout<<"\nThe left balance of player 2 is :

"<<d2.balance<<"\n";
cout<<"press 'escape' to exit";
cout<<" or Press any other key to play the

game again\n";
input=getch();
if(input==char (27))
{
exit;
}
else
{
clrscr();
goto begin;
}

}
else;}

cout<<"The left balance of Player 1 is :

"<<d1.balance;
cout<<"\nThe left balance of player 2 is :

"<<d2.balance<<"\n";
}
cout<<"press 'escape' to exit";
cout<<" or Press any other key to play the

game again\n";
input=getch();
if(input==char (27))
{
exit;
}
else
{
clrscr();
goto begin;
}

cout.flush();
//getch();
}

Yash

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation.

0 comments:

Post a Comment

 

Copyright @ 2013 H@cking Tricks.