You are on page 1of 11

Dice Game Coding

#include <iostream.h>

#include <conio.h>

#include <stdlib.h>

class dice

private:

intnumber,i,j;

char a;

public:

intbalance;

dice()

balance=100;

number=0;

randomize();

intchoose_number()

// randomize();

number=random(6);

if(number==0)

choose_number();

returnnumber;

}
void dice_show(int)

char array[3][3]={ {' ',' ',' '},

{' ',' ',' '},

{' ',' ',' '} };

a=' ';

switch(number)

case1:

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;

case2:

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;

case3:

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;

case4:

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;

case5:

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;

case6:

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;

};

intmain(void)

cout.flush();

dice d1,d2;

intnumber1,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<<"Pressany key to start the game

again\n";

input=getch();

if(input)

d1.balance=100;

d2.balance=100;

gotobegin;

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+number

4))

cout<<"\nplayer two is the winner\n";

d1.balance=d1.balance-bet;
d2.balance=d2.balance+bet;

cout<<"Theleft 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();

gotobegin;

cout.flush();

//getch();

You might also like