C++ Code for Snake And Ladder
Posted On at by milan#include
#include
#include
#include
#include
#include
#include
enum whoplays{p1,p2};
int counter1=0,counter2=0,counter11=0,counter22=0;
void graph(int,int,int);
void sohaib(void);
void ladder(int,int,int,int);
void cp1(int,int);
void cp2(int,int);
void snake(int,int,int,int);
whoplays setplayer(whoplays);
class dice
{
private:
int dice1;
public:
dice();
int rolldice(void);
operator int()
{
return dice1/1;
}
};
void dice::dice()
{
dice1=0;
}
int dice::rolldice(void)
{
randomize();
dice1=rand()%6+1;
return dice1;
}
class player1
{
private:
int a;
public:
player1();
returna();
reseta(int);
minusa(int);
operator int()
{
return a/1;
}
};
void player1::player1()
{
a=0;
}
int player1::returna()
{
return a;
}
int player1::reseta(int ra)
{
a+=ra;
}
int player1::minusa(int ma)
{
a-=ma;
}
class player2
{
private:
int b;
public:
player2();
returnb();
minusb(int);
resetb(int);
operator int()
{
return b/1;
}
};
void player2::player2()
{
b=0;
}
int player2::returnb()
{
return b;
}
int player2::resetb(int rb)
{
b+=rb;
}
int player2::minusb(int mb)
{
b-=mb;
}
//starting of main
void main()
{
sohaib();
}
//end of main
void sohaib(void)
{
int l=VGA,k=VGAMED;
initgraph(&l,&k,"e:\tc\bgi");
static int x=0;
dice dice1;
player1 obj1;
player2 obj2;
int one=0,two=0,y=0;
y=setplayer(1);
g:if(x==6)
{
if(y==0)
{
y=setplayer(0);
}
else
if(y==1)
{
y=setplayer(1);
}
}
else
{
if(y==0)
{
y=setplayer(1);
}
else
if(y==1)
{
y=setplayer(0);
}
}
x=int(dice1.rolldice());
one=int(obj1.returna());
two=int(obj2.returnb());
graph(y,one,two);
if(x==6&&y==0)
{
counter11=1;
}
if(x==6&&y==1)
{
counter22=1;
}
if(y==0&&counter11==1)
{
counter1=1;
}
if(y==1&&counter22==1)
{
counter2=1;
}
if(y==0&&counter1>=1)
{
obj1.reseta(x);
if(one==10)
{
obj1.reseta(27);
cout<
if(one==55)
{
obj1.reseta(15);
cout<
if(one==79)
{
obj1.reseta(10);
cout<
if(one==99)
{
obj1.minusa(60);
cout<
if(one==56)
{
obj1.minusa(30);
cout<
if(one==77)
{
obj1.minusa(25);
cout<
}
if(y==1&&counter2>=1)
{
obj2.resetb(x);
if(two==24)
{
obj2.resetb(27);
cout<
if(two==43)
{
obj2.resetb(15);
cout<
if(two==88)
{
obj2.resetb(10);
cout<
if(two==28)
{
obj2.minusb(15);
cout<
if(two==69)
{
obj2.minusb(30);
cout<
if(two==99)
{
obj2.minusb(60);
cout<
}
if(one>=100)
{
cout<<"
congratulations player 1 have won";
goto b;
}
if(two>=100)
{
cout<<"
congratulations player 2 have won";
goto b;
}
if((one<101&&two<101)&&(one>=0&&two>=0))
{
goto g;
}
b:getch();
}
whoplays setplayer(whoplays p)
{
whoplays player=p;
return player;
}
void graph(int y,int one,int two)
{
int a=VGA,b=VGAMED;
initgraph(&a,&b,"e:\tc\bgi");
cleardevice();
int count=0,r=21;
if(y==0)
{
rectangle(200,100,400,200);
setfillstyle(1,BLUE);
floodfill(201,101,WHITE);
moveto(210,110);
setcolor(GREEN);
settextstyle(SCRIPT_FONT,HORIZ_DIR,5);
outtext("Player 2");
getch();
cleardevice();
}
if(y==1)
{
rectangle(200,100,400,200);
setfillstyle(1,RED);
floodfill(201,101,WHITE);
moveto(210,110);
setcolor(YELLOW);
settextstyle(SCRIPT_FONT,HORIZ_DIR,5);
outtext("Player 2");
getch();
cleardevice();
}
setcolor(4);
moveto(75,5);
settextstyle(GOTHIC_FONT,HORIZ_DIR,5);
outtext("SNAKE AND LADDER");
rectangle(65,8,535,53);
setcolor(GREEN);
rectangle(15,58,590,298);
for(int e=73;e<590;e=e+58)
{
line(e,58,e,298);
}
for(int d=82;d<298;d=d+24)
{
line(15,d,590,d);
}
setcolor(RED);
moveto(44,286);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("1");
moveto(102,286);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("2");
moveto(160,286);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("3");
moveto(218,286);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("4");
moveto(276,286);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("5");
moveto(334,286);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("6");
moveto(392,286);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("7");
moveto(450,286);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("8");
moveto(508,286);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("9");
moveto(566,286);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("10");
moveto(44,262);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("11");
moveto(102,262);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("12");
moveto(160,262);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("13");
moveto(218,262);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("14");
moveto(276,262);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("15");
moveto(334,262);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("16");
moveto(392,262);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("17");
moveto(450,262);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("18");
moveto(508,262);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("19");
moveto(566,262);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("20");
moveto(44,238);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("21");
moveto(102,238);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("22");
moveto(160,238);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("23");
moveto(218,238);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("24");
moveto(276,238);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("25");
moveto(334,238);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("26");
moveto(392,238);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("27");
moveto(450,238);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("28");
moveto(508,238);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("29");
moveto(566,238);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("30");
moveto(44,214);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("31");
moveto(102,214);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("32");
moveto(160,214);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("33");
moveto(218,214);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("34");
moveto(276,214);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("35");
moveto(334,214);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("36");
moveto(392,214);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("37");
moveto(450,214);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("38");
moveto(508,214);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("39");
moveto(566,214);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("40");
moveto(44,190);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("41");
moveto(102,190);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("42");
moveto(160,190);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("43");
moveto(218,190);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("44");
moveto(276,190);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("45");
moveto(334,190);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("46");
moveto(392,190);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("47");
moveto(450,190);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("48");
moveto(508,190);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("49");
moveto(566,190);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("50");
moveto(44,166);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("51");
moveto(102,166);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("52");
moveto(160,166);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("53");
moveto(218,166);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("54");
moveto(276,166);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("55");
moveto(334,166);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("56");
moveto(392,166);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("57");
moveto(450,166);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("58");
moveto(508,166);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("59");
moveto(566,166);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("60");
moveto(44,142);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("61");
moveto(102,142);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("62");
moveto(160,142);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("63");
moveto(218,142);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("64");
moveto(276,142);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("65");
moveto(334,142);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("66");
moveto(392,142);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("67");
moveto(450,142);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("68");
moveto(508,142);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("69");
moveto(566,142);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("70");
moveto(44,118);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("71");
moveto(102,118);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("72");
moveto(160,118);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("73");
moveto(218,118);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("74");
moveto(276,118);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("75");
moveto(334,118);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("76");
moveto(392,118);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("77");
moveto(450,118);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("78");
moveto(508,118);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("79");
moveto(566,118);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("80");
moveto(44,94);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("81");
moveto(102,94);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("82");
moveto(160,94);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("83");
moveto(218,94);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("84");
moveto(276,94);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("85");
moveto(334,94);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("86");
moveto(392,94);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("87");
moveto(450,94);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("88");
moveto(508,94);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("89");
moveto(566,94);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("90");
moveto(44,70);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("91");
moveto(102,70);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("92");
moveto(160,70);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("93");
moveto(218,70);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("94");
moveto(276,70);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("95");
moveto(334,70);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("96");
moveto(392,70);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("97");
moveto(450,70);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("98");
moveto(508,70);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("99");
moveto(558,70);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtext("100");
settextstyle(SMALL_FONT,HORIZ_DIR,5);
setcolor(GREEN);
rectangle(363,300,590,349);
setcolor(6);
moveto(375,306);
outtext("Dice -> ");
moveto(375,317);
outtext("Player 1 -> ");
moveto(375,328);
outtext("Player 2 -> ");
setcolor(GREEN);
rectangle(15,300,350,349);
setcolor(YELLOW);
moveto(19,302);
outtext("Help : ");
setcolor(7);
settextstyle(SMALL_FONT,HORIZ_DIR,4);
outtext("Just press any key, a number will appear before ");
moveto(72,312);
outtext("dice. The mark of player will automatically move");
moveto(19,322);
outtext("to the accurate position. The player who will reach 100 ");
moveto(19,332);
outtext("will Win the game. ");
ladder(392,214,566,286);
ladder(218,142,276,166);
//ladder(508,94,508,118);
ladder(44,166,218,238);
ladder(160,190,450,166);
ladder(450,70,450,94);
snake(508,70,556,238);
snake(334,166,334,238);
snake(392,118,102,166);
snake(450,238,160,262);
snake(508,142,508,214);
cp1(515,310);
moveto(520,307);
outtext(" Player 1");
cp2(515,330);
moveto(520,327);
outtext(" Player 2");
if(one==1)
{
cp1(44,286);
}
if(one==2)
{
cp1(102,286);
}
if(one==3)
{
cp1(160,286);
}
if(one==4)
{
cp1(218,286);
}
if(one==5)
{
cp1(276,286);
}
if(one==6)
{
cp1(334,286);
}
if(one==7)
{
cp1(392,286);
}
if(one==8)
{
cp1(450,286);
}
if(one==9)
{
cp1(508,286);
}
if(one==10)
{
cp1(566,286);
}
if(one==11)
{
cp1(44,262);
}
if(one==12)
{
cp1(102,262);
}
if(one==13)
{
cp1(160,262);
}
if(one==14)
{
cp1(218,262);
}
if(one==15)
{
cp1(276,262);
}
if(one==16)
{
cp1(334,262);
}
if(one==17)
{
cp1(392,262);
}
if(one==18)
{
cp1(450,262);
}
if(one==19)
{
cp1(508,262);
}
if(one==20)
{
cp1(566,262);
}
if(one==21)
{
cp1(44,238);
}
if(one==22)
{
cp1(102,238);
}
if(one==23)
{
cp1(160,238);
}
if(one==24)
{
cp1(218,238);
}
if(one==25)
{
cp1(276,238);
}
if(one==26)
{
cp1(334,238);
}
if(one==27)
{
cp1(392,238);
}
if(one==28)
{
cp1(450,238);
}
if(one==29)
{
cp1(508,238);
}
if(one==30)
{
cp1(566,238);
}
if(one==31)
{
cp1(44,214);
}
if(one==32)
{
cp1(102,214);
}
if(one==33)
{
cp1(160,214);
}
if(one==34)
{
cp1(218,214);
}
if(one==35)
{
cp1(276,214);
}
if(one==36)
{
cp1(334,214);
}
if(one==37)
{
cp1(392,214);
}
if(one==38)
{
cp1(450,214);
}
if(one==39)
{
cp1(508,214);
}
if(one==40)
{
cp1(566,214);
}
if(one==41)
{
cp1(44,190);
}
if(one==42)
{
cp1(102,190);
}
if(one==43)
{
cp1(160,190);
}
if(one==44)
{
cp1(218,190);
}
if(one==45)
{
cp1(276,190);
}
if(one==46)
{
cp1(334,190);
}
if(one==47)
{
cp1(392,190);
}
if(one==48)
{
cp1(450,190);
}
if(one==49)
{
cp1(508,190);
}
if(one==50)
{
cp1(566,190);
}
if(one==51)
{
cp1(44,166);
}
if(one==52)
{
cp1(102,166);
}
if(one==53)
{
cp1(160,166);
}
if(one==54)
{
cp1(218,166);
}
if(one==55)
{
cp1(276,166);
}
if(one==56)
{
cp1(334,166);
}
if(one==57)
{
cp1(392,166);
}
if(one==58)
{
cp1(450,166);
}
if(one==59)
{
cp1(508,166);
}
if(one==60)
{
cp1(566,166);
}
if(one==61)
{
cp1(44,142);
}
if(one==62)
{
cp1(102,142);
}
if(one==63)
{
cp1(160,142);
}
if(one==64)
{
cp1(218,142);
}
if(one==65)
{
cp1(276,142);
}
if(one==66)
{
cp1(334,142);
}
if(one==67)
{
cp1(392,142);
}
if(one==68)
{
cp1(450,142);
}
if(one==69)
{
cp1(508,142);
}
if(one==70)
{
cp1(566,142);
}
if(one==71)
{
cp1(44,118);
}
if(one==72)
{
cp1(102,118);
}
if(one==73)
{
cp1(160,118);
}
if(one==74)
{
cp1(218,118);
}
if(one==75)
{
cp1(276,118);
}
if(one==76)
{
cp1(334,118);
}
if(one==77)
{
cp1(392,118);
}
if(one==78)
{
cp1(450,118);
}
if(one==79)
{
cp1(508,118);
}
if(one==80)
{
cp1(566,118);
}
if(one==81)
{
cp1(44,94);
}
if(one==82)
{
cp1(102,94);
}
if(one==83)
{
cp1(160,94);
}
if(one==84)
{
cp1(218,94);
}
if(one==85)
{
cp1(276,94);
}
if(one==86)
{
cp1(334,94);
}
if(one==87)
{
cp1(392,94);
}
if(one==88)
{
cp1(450,94);
}
if(one==89)
{
cp1(508,94);
}
if(one==90)
{
cp1(566,94);
}
if(one==91)
{
cp1(44,70);
}
if(one==92)
{
cp1(102,70);
}
if(one==93)
{
cp1(160,70);
}
if(one==70)
{
cp1(218,70);
}
if(one==95)
{
cp1(276,70);
}
if(one==96)
{
cp1(334,70);
}
if(one==97)
{
cp1(392,70);
}
if(one==98)
{
cp1(450,70);
}
if(one==99)
{
cp1(508,70);
}
if(one==100)
{
cp1(566,70);
}
if(two==1)
{
cp2(44,286);
}
if(two==2)
{
cp2(102,286);
}
if(two==3)
{
cp2(160,286);
}
if(two==4)
{
cp2(218,286);
}
if(two==5)
{
cp2(276,286);
}
if(two==6)
{
cp2(334,286);
}
if(two==7)
{
cp2(392,286);
}
if(two==8)
{
cp2(450,286);
}
if(two==9)
{
cp2(508,286);
}
if(two==10)
{
cp2(566,286);
}
if(two==11)
{
cp2(44,262);
}
if(two==12)
{
cp2(102,262);
}
if(two==13)
{
cp2(160,262);
}
if(two==14)
{
cp2(218,262);
}
if(two==15)
{
cp2(276,262);
}
if(two==16)
{
cp2(334,262);
}
if(two==17)
{
cp2(392,262);
}
if(two==18)
{
cp2(450,262);
}
if(two==19)
{
cp2(508,262);
}
if(two==20)
{
cp2(566,262);
}
if(two==21)
{
cp2(44,238);
}
if(two==22)
{
cp2(102,238);
}
if(two==23)
{
cp2(160,238);
}
if(two==24)
{
cp2(218,238);
}
if(two==25)
{
cp2(276,238);
}
if(two==26)
{
cp2(334,238);
}
if(two==27)
{
cp2(392,238);
}
if(two==28)
{
cp2(450,238);
}
if(two==29)
{
cp2(508,238);
}
if(two==30)
{
cp2(566,238);
}
if(two==31)
{
cp2(44,214);
}
if(two==32)
{
cp2(102,214);
}
if(two==33)
{
cp2(160,214);
}
if(two==34)
{
cp2(218,214);
}
if(two==35)
{
cp2(276,214);
}
if(two==36)
{
cp2(334,214);
}
if(two==37)
{
cp2(392,214);
}
if(two==38)
{
cp2(450,214);
}
if(two==39)
{
cp2(508,214);
}
if(two==40)
{
cp2(566,214);
}
if(two==41)
{
cp2(44,190);
}
if(two==42)
{
cp2(102,190);
}
if(two==43)
{
cp2(160,190);
}
if(two==44)
{
cp2(218,190);
}
if(two==45)
{
cp2(276,190);
}
if(two==46)
{
cp2(334,190);
}
if(two==47)
{
cp2(392,190);
}
if(two==48)
{
cp2(450,190);
}
if(two==49)
{
cp2(508,190);
}
if(two==50)
{
cp2(566,190);
}
if(two==51)
{
cp2(44,166);
}
if(two==52)
{
cp2(102,166);
}
if(two==53)
{
cp2(160,166);
}
if(two==54)
{
cp2(218,166);
}
if(two==55)
{
cp2(276,166);
}
if(two==56)
{
cp2(334,166);
}
if(two==57)
{
cp2(392,166);
}
if(two==58)
{
cp2(450,166);
}
if(two==59)
{
cp2(508,166);
}
if(two==60)
{
cp2(566,166);
}
if(two==61)
{
cp2(44,142);
}
if(two==62)
{
cp2(102,142);
}
if(two==63)
{
cp2(160,142);
}
if(two==64)
{
cp2(218,142);
}
if(two==65)
{
cp2(276,142);
}
if(two==66)
{
cp2(334,142);
}
if(two==67)
{
cp2(392,142);
}
if(two==68)
{
cp2(450,142);
}
if(two==69)
{
cp2(508,142);
}
if(two==70)
{
cp2(566,142);
}
if(two==71)
{
cp2(44,118);
}
if(two==72)
{
cp2(102,118);
}
if(two==73)
{
cp2(160,118);
}
if(two==74)
{
cp2(218,118);
}
if(two==75)
{
cp2(276,118);
}
if(two==76)
{
cp2(334,118);
}
if(two==77)
{
cp2(392,118);
}
if(two==78)
{
cp2(450,118);
}
if(two==79)
{
cp2(508,118);
}
if(two==80)
{
cp2(566,118);
}
if(two==81)
{
cp2(44,94);
}
if(two==82)
{
cp2(102,94);
}
if(two==83)
{
cp2(160,94);
}
if(two==84)
{
cp2(218,94);
}
if(two==85)
{
cp2(276,94);
}
if(two==86)
{
cp2(334,94);
}
if(two==87)
{
cp2(392,94);
}
if(two==88)
{
cp2(450,94);
}
if(two==89)
{
cp2(508,94);
}
if(two==90)
{
cp2(566,94);
}
if(two==91)
{
cp2(44,70);
}
if(two==92)
{
cp2(102,70);
}
if(two==93)
{
cp2(160,70);
}
if(two==70)
{
cp2(218,70);
}
if(two==95)
{
cp2(276,70);
}
if(two==96)
{
cp2(334,70);
}
if(two==97)
{
cp2(392,70);
}
if(two==98)
{
cp2(450,70);
}
if(two==99)
{
cp2(508,70);
}
if(two==100)
{
cp2(566,70);
}
getch();
closegraph();
}
void ladder(int s1,int s2,int s3,int s4)
{
setcolor(WHITE);
setlinestyle(1,10,3);
line(s1,s2,s3,s4);
line(s1,s2,s1,s2+10);
line(s1,s2,s1+17,s2);
//circle(s1,s2,3);
}
void cp1(int xx,int yy)
{
setcolor(7);
circle(xx,yy,10);
setfillstyle(1,9);
floodfill(xx,yy,7);
}
void cp2(int xx,int yy)
{
setcolor(7);
circle(xx,yy,10);
setfillstyle(1,4);
floodfill(xx,yy,7);
}
void snake(int s1,int s2,int s3,int s4)
{
setcolor(YELLOW);
setlinestyle(1,10,3);
line(s1,s2,s3,s4);
circle(s1,s2,4);
}
C++ Code for Piano
Posted On at by milan// PIANO saves output in c:piano.txt
#include
#include
#include
void jain(int a)
{
sound((a*8)/2);
delay(300);
nosound();
}
void main(int argc,char *argv[])
{
char anuj;
fstream file1;
file1.open(argv[1],ios::out);
clrscr();
cout<<"PIANO "<<"
Press `q' to exit.
";
while(anuj!='q')
{
jain(anuj);
anuj=getch();
if(anuj!='q')
file1.put(anuj);
}
file1.close();
}
C++ Code for game Card Trick
Posted On at by milan// [ MAGIC CARD TRICK - 1]
// By: Rakesh Juyal //
// I.T.S. Mohan Nagar, Gzb //
// mailme: juyalrules@yahoo.com //
// mailme: rakesh@mindless.com //
#include
#include
#include
#include
#include
#define Col 5
#define Row (Col-1)
void _rectangle ( int left , int top , int right , int bottom );
void Initialize ( int Grid[Row][Col] );
int Any_Zero_Remaining_In_Row ( int Grid[Row][Col] , int i );
void InitializeTrick ( int MagicGrid[Row][Col] );
class Card
{
char _num[ Row * Col ] , _suit[ Row * Col ];
char top;
public:
Card(){ top = 0;}
void show( int i )
{
if ( ( _suit[i] == 3 ) || ( _suit[i] == 4 ) )
textcolor ( RED );
else
textcolor ( DARKGRAY );
cprintf ( "%c%c", _num[i] , _suit[i] );
}
int HaveSame( char num , char suit , int i )
{
if ( ( num == _num[i] ) && ( suit == _suit[i] ) )
return 1;
else
return 0;
}
void set( char num , char suit , int i )
{
_num[i] = num;
_suit[i] =suit;
top = top + 1;
}
int Card_Used ( char num , char suit )
{
for ( int i = 0; i < top; i++ )
{
if ( ( num == _num[i] ) && ( suit == _suit[i] ) )
return 1;
}
return 0;
}
void InitializeCards ( );
};
void DisplayCards ( Card Cards );
void DisplayCardsInGrid ( Card Cards , int MagicGrid[Row][Col] );
void message2( char *msg , int x , int y , int times , int col1 = 8 ,
int
col2 = 7 );
int WhatIsCommon( int MagicGrid[Row][Col] , int i , int j );
void main ()
{
/***** Variables *******/
int MagicGrid[Row][Col];
int i ,j;
int TotalCards;
Card Cards;
/***** Variables *******/
clrscr();
_setcursortype ( 0 );
gotoxy ( 20 , 5 );
textcolor ( RED );
cprintf ( "Dont Press Any Key . . ." );
message2("Magic Created By:- " , 30 , 10 , 1 , CYAN);
message2("Rakesh Juyal" , 40 , 11 , 2 , BLUE );
gotoxy ( 20 , 5 );
textcolor ( GREEN );
message2( " Press Any Key . . ." , 20,5,0 , GREEN);
// cprintf ( " Press Any Key . . ." );
getch ();
clrscr();
TotalCards = Row * Col ;
Cards.InitializeCards ( );
InitializeTrick ( MagicGrid );
textcolor ( GREEN );
_rectangle ( 1 , 1 , 79 , 24 );
DisplayCards ( Cards );
gotoxy ( 30 , 23 );
textcolor ( CYAN + BLINK );
cprintf ( " Choose Any Pair " );
textcolor ( GREEN + BLINK );
gotoxy ( 29 , 24 );
cprintf ( " And Press Any Key " );
getch();
textcolor ( YELLOW );
clrscr ();
DisplayCardsInGrid ( Cards , MagicGrid );
gotoxy ( 1 , 23 );
_setcursortype ( 2 );
textcolor ( CYAN );
cprintf ( "Enter the rows having your Cards [ row1 row2 ]:- " );
scanf ( "%d%d",&i , &j );
if ( ( i <= Row ) && ( i > 0 ) && ( j <= Row ) && ( j > 0 ) )
{
i = WhatIsCommon( MagicGrid , i - 1 , j - 1 );
clrscr();
gotoxy ( 5 , 3 );
textcolor ( GREEN );
cprintf ( "Your Cards Are:- " );
textcolor ( WHITE );
_rectangle ( 12 , 5 , 20 , 13 );
_rectangle ( 25 , 5 , 33 , 13 );
gotoxy ( 13 , 6 );
Cards.show(i * 2 - 2);
gotoxy ( 26 , 6 );
Cards.show(i * 2 - 1);
}
else
{
textcolor ( RED );
cprintf ( "Valid entries are like:- ( 1 1 ) or ( 2 1 ). Dont input braces.");
printf ( "
" );
cprintf ( "InValid entries are like:- ( 0 1 ) or ( 2 0 ) or ( 1,1 ) or ( 2,1 )");
}
getch();
_setcursortype ( 2 );
}
void _rectangle ( int left , int top , int right , int bottom )
{
int i = 0;
/****** Horizontal Lines ******/
for ( i = left; i <= right; i++ )
{
gotoxy ( i , top );
putch ( 'Ä' );
gotoxy ( i , bottom );
putch ( 'Ä' );
}
/****** Vertical Lines ******/
for ( i = top; i <= bottom; i++ )
{
gotoxy ( left , i );
putch ( '³' );
gotoxy ( right , i );
putch ( '³' );
}
/********* Corners ******/
gotoxy ( left , top );
putch ( 'Ú' );
gotoxy ( right , top );
putch ( '¿' );
gotoxy ( left , bottom );
putch ( 'À' );
gotoxy ( right , bottom );
putch ( 'Ù' );
}
void Initialize ( int Grid[Row][Col] )
{
int i , j;
for ( i = 0; i < Row; i++ )
for ( j = 0; j < Col; j++ )
Grid[i][j] = 0;
}
void InitializeTrick ( int MagicGrid[Row][Col] )
{
int i , j , k = 1;
int loc;
Initialize ( MagicGrid );
randomize();
for ( i = 0; i < Row; i++ )
{
MagicGrid[i][ random ( Col ) ] = k;
do
{
loc = random ( Col );
}while ( MagicGrid[i][loc] != 0 );
MagicGrid[i][ loc ] = k;
k = k + 1;
}
for ( i = 0; i < Row ; i++ )
{
j = i + 1;
while ( Any_Zero_Remaining_In_Row ( MagicGrid , i ) )
{
do
{
loc = random ( Col );
}while ( MagicGrid[i][loc] != 0 );
MagicGrid[i][ loc ] = k;
do
{
loc = random ( Col );
}while ( MagicGrid[j][loc] != 0 );
MagicGrid[j][ loc ] = k;
if ( j < Row ) j = j + 1;
k = k + 1;
}
}
}
int Any_Zero_Remaining_In_Row ( int Grid[Row][Col] , int i )
{
int j;
for ( j = 0; j < Col; j++ )
{
if ( Grid[i][j] == 0 )
return 1;
}
return 0;
}
void Card :: InitializeCards ( )
{
char num , suit;
int i , TotalCards;
TotalCards = Row * Col ;
for ( i = 0; i < TotalCards; i = i + 1 )
{
_num[i] = 0;
_suit[i] = 0;
}
randomize();
for ( i = 0; i < TotalCards; i = i + 1 )
{
do
{
num = random ( 12 ) + 1;
suit = random ( 4 ) + 3;
switch ( num )
{
case 1:
num = 'A';
break;
case 10:
num = 'J'; // Surprised why [ J should be 11 ? No]
break;
case 11:
num = 'Q';
break;
case 12:
num = 'K';
break;
default:
num = num + '0';
}
}while ( Card_Used ( num , suit ) );
set ( num , suit , i );
}
}
void DisplayCards ( Card Cards )
{
int i , j , k;
int TotalCards;
int Rand[ Row * Col ] = {0};
TotalCards = Row * Col / 2 ;
randomize();
for ( i = 0; i < TotalCards; i++ )
{
while ( 1 )
{
k = random ( TotalCards );
for ( j = 0; j < i; j++ )
{
if ( Rand[j] == k )
break;
}
if ( j == i ) break;
}
Rand[i] = k;
}
j = 5;
k = 2;
for ( i = 0; i < TotalCards; i = i + 1 )
{
textcolor ( WHITE );
_rectangle ( j , k , j + 8 , k + 2 );
_rectangle ( j , k + 2 , j + 8 , k + 6 );
gotoxy ( j , k + 2 );
putch ( 'Ã' );
gotoxy ( j + 8 , k + 2 );
putch ( '´' );
gotoxy ( j + 1 , k + 1 );
Cards.show( Rand[ i ] * 2 );
gotoxy ( j + 1 , k + 3 );
Cards.show( Rand [ i ] * 2 + 1 );
delay ( 100 );
j = j + 10;
if ( j > 71 )
{
j = 5;
k = k + 7;
}
}
}
void DisplayCardsInGrid ( Card Cards , int MagicGrid[Row][Col] )
{
int i , j;
int x , y , xinc , yinc ;
char first[( Row * Col ) + 1];
for ( i = 0; i < Row * Col; i++ )
first [i] = '0' ;
x = y = 2;
yinc = ( 23 - Row ) / Row;
xinc = yinc + 1;
for ( i = 0; i < Row; i = i + 1 )
{
gotoxy ( 1 , y + 1 );
cprintf ( "%d", i + 1 );
x = 2;
for ( j = 0; j < Col; j = j + 1 )
{
textcolor ( WHITE );
_rectangle ( x , y , x + xinc , y + yinc );
gotoxy ( x + 1 , y + 1 );
if ( first [ MagicGrid[i][j] - 1] == '0' )
Cards.show(MagicGrid[i][j] * 2 - 2);
else
Cards.show( MagicGrid[i][j] * 2 - 1 );
delay ( 100 );
first [ MagicGrid[i][j] - 1 ] = '1';
x = x + xinc + 4;
}
y = y + yinc + 1 ;
}
}
int WhatIsCommon( int MagicGrid[Row][Col] , int i , int j )
{
int k ,l;
if ( i == j )
{
for ( k = 0; k < Col - 1; k++ )
{
for ( l = k + 1; l < Col; l++ )
{
if ( MagicGrid[i][k] == MagicGrid[i][l] )
return MagicGrid[i][k];
}
}
}
else
{
for ( k = 0; k < Col; k++ )
{
for ( l = 0; l < Col; l++ )
{
if ( MagicGrid[i][k] == MagicGrid[j][l] )
return MagicGrid[i][k];
}
}
}
return 0;
}
void message2( char *msg , int x , int y , int times , int col1 , int col2 )
{
int len = 0 , i , j = 0;
char *temp;
temp = msg;
while ( *msg++ != '
C++ Code for game Brainvita
Posted On at by milan/* [[[ Brainvita ]]]
**************************************************************************
_____ _____ _____ _____ _____ ______
_____
| | / | | | | | | | /
| | | | | | | | | | | | | |
|
|_____/ |_____/ |_____| | | | | | | |
|_____|
| | | | | | | | | | | |
|
| | | | | | | | | / | | |
|
|_____| | | | | __|__ | / __|__ | |
|
**************************************************************************
Written & Devloped By : Neeraj Sharma
Email : neeraj_sharma100@rediffmail.com
URL : www.neerajsharma.tk
Last Modified : 10/1/2002
Compiler used : Turbo C++ 3.0
PLAY & HAVE FUN !
Feel free to e-mail me.
*/
#include
#include
#include
#include
#include
#include
#include
#define UP 72
#define DOWN 80
#define LEFT 75
#define RIGHT 77
#define ENTER 13
#define ESC 27
#define YES 1
#define NO 0
int x=320,y=100,marble=32,marble_color=12;
void *p;
size_t area;
// Matrix of board
int board[7][7]=
{
{-1,-1,1,1,1,-1,-1},
{-1,-1,1,1,1,-1,-1},
{ 1, 1,1,1,1, 1, 1},
{ 1, 1,1,1,1, 1, 1},
{ 1, 1,1,1,1, 1, 1},
{-1,-1,1,1,1,-1,-1},
{-1,-1,1,1,1,-1,-1}
};
//------------------------------------------------------------------------
// Function Prototypes
//------------------------------------------------------------------------
void Marble(int x,int y,int c);
void G();
int check();
int GetXY(int X,int Y);
int GetBoard(int X,int Y);
void SetBoard(int X,int Y,int element);
void Blink(int x,int y,int c);
void DrawBoard();
int MakeMove(int X,int Y);
void Init();
int finish();
void win(char *text,int sx,int sy,int ex,int ey,int ck);
void winp(int sx,int sy,int ex,int ey,int state);
void Menu();
void LCD(int left,int top,int NUM);
void Lcd(int x,int y,int n);
void Intro();
void Drawborder(int x,int y);
void Background();
int load_game ();
int save_game();
//------------------------------------------------------------------------
// Main Function
//------------------------------------------------------------------------
void main()
{
int i;
G();
Intro();
Background();
DrawBoard();
Marble(320,220,0);
board[3][3]=0;
Init();
setcolor(0);
for(i=0;i<=220;i++)
{
rectangle(0+i,0+i,640-i,480-i);
delay(10);
}
for(i=0;i<=220;i++)
{
rectangle(100+i,100,540-i,380);
delay(8);
}
closegraph();
printf(" BrainVita
");
printf("By : Neeraj Sharma
");
printf("email : neeraj_sharma100@rediffmail.com
n21@indiatimes.com");
getch();
}
void Marble(int x,int y,int c)
{
setfillstyle(1,c);
setcolor(c);
fillellipse(x,y,8,8);
if(c!=0)
{
if(c==15)c
C++ Code for puzzle game
Posted On at by milanCode :
#include
#include
#include
#include
#include
// GLOBAL VARIABLES
int a[5][5];
int t[16]={0,4,11,12,7,1,15,5,13,6,10,3,2,14,8,9};
int test[16]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
struct pos
{
int h,v;
}p[4][4];
int row=4,col=4;
// FUNCTION PROTOTYPES
void game(int); //MOVEMENT
void rec(); //DRAWING RECTANGLE
void pri(); //PRINTING NUMBERS INITIALLY
int getkey(); // TO TRACE KEY PRESSED
inline void space() { cout<<" "; }
inline void print(int r,int c) { cout<
void init(); //TO STORE CO-ORDINATES
int stop(); // STOPING CRITERION
void gopr(int,int); //TO PRINT NUMBER IN GAME
void main()
{
int gm=DETECT,gd=DETECT;
initgraph(&gm,&gd,"");
int d,cr=1;
init();
rec();
pri();
while(cr!=16)
{
d=getkey();
game(d);
cr=stop();
}
settextstyle(10,0,1);
outtextxy(400,300,"You are winner!");
getch();
}
void rec()
{
setcolor(5);
for(int i=0;i<200;i+=50)
{
for(int j=0;j<240;j+=60)
rectangle(j+100,i+100,j+50,i+60);
}
}
void pri()
{
int k=1;
for(int x=0,i=6;x<4;x++,i+=3)
{
for(int y=0,j=10;y<4&&k<16;y++,j+=7,k++)
{
gotoxy(p[x][y].h,p[x][y].v);
cout< }
}
}
int getkey()
{
union REGS i,o;
while(!kbhit());
i.h.ah=0;
int86(22,&i,&o);
return(o.h.ah);
}
void init()
{
int k=1;
for(int x=0,i=6;x<4;x++,i+=3)
{
for(int y=0,j=10;y<4;y++,j+=7)
{
p[x][y].h=j;
p[x][y].v=i;
a[x][y]=t[k++];
}
}
}
void game(int s)
{
int r=row-1;
int c=col-1;
if(s==77 &&c!=0) //right
{
col--;
a[r][c]=a[r][c-1];
gopr(r,c-1);
space();
gopr(r,c);
print(r,c-1);
}
if(s==80 && r!=0) //down
{
row--;
a[r][c]=a[r-1][c];
gopr(r-1,c);
space();
gopr(r,c);
print(r-1,c);
}
if(s==75 && c!=3) //left
{
a[r][c]=a[r][c+1];
col++;
gopr(r,c+1);
space();
gopr(r,c);
print(r,c+1);
}
if(s==72 &&r!=3) //up
{
a[r][c]=a[r+1][c];
row++;
gopr(r+1,c);
space();
gopr(r,c);
print(r+1,c);
}
}
void gopr(int x, int y)
{
gotoxy(p[x][y].h,p[x][y].v);
}
int stop()
{
int k=0,d=1;
for(int x=0;x<4;x++)
{
for(int y=0;y<4;y++)
{
if(a[x][y]==test[k])
d++;
k++;
}
}
return d;
}
C++ game Code Caught in the Middle
Posted On at by milan#include
#include
#include
#include
#include
#include
enum dir direction;
/*indicates the direction of the shooter*/
int fire(int xco,int yco,int &lr,int &tb,int &rl,int &bt);
/*shoots place check for hit*/
/*The most complex function flashes the green boxes and*/
/*records their coordinates*/
int environment(int xposition,int yposition,int position_array_location);
int match(void);
/*checks the fired location and the locations of the boxes*/
int put_banner(void);
/*banner at the end of game*/
/*structure to record the position of boxes*/
//void refresh();
struct position
{
int x_pos;
int y_pos;
};
struct position p[10];
/*array to record the locations of boxes*/
struct position sh;
/*variable to record the location of the shooter*/
enum dir{EAST,WEST,SOUTH,NORTH};
/*enum to indicate direction*/
unsigned int hitcount=0;
void main()
{
int gdriver = DETECT;
int gmode;
int ch,is_hit;
int x=350,y=200;/*x,y coordinates*/
/*variables used to move around boxes*/
int i=0,j=320,k=400,l=0,m=800,n=200,p=600,o=300;
int iflag,lflag,mflag,pflag;
/*flag to indicate which box has been hit*/
initgraph(&gdriver,&gmode,"c:\tc\bgi");
/*initiating graphics*/
setcolor(YELLOW);
settextstyle(TRIPLEX_FONT,HORIZ_DIR,5);
/*Drawing the initial banners*/
outtextxy((getmaxx()/2)-260,(getmaxy()/2)-20,""CAUGHT IN THE MIDDLE"");
setcolor(WHITE);
outtextxy(100,50,"Use "arrow" key to move");
outtextxy(100,100,"Use "enter" to fire");
/*drawing banner completed*/
getch();
settextstyle(DEFAULT_FONT,HORIZ_DIR,0);
setbkcolor(BLUE);
delay(1000);
cleardevice();
x=getmaxx()/2;y=getmaxy()/2;
moveto(x,y);
do
{
/*draws the shooter*/
setfillstyle(SOLID_FILL,WHITE);
circle(x,y,10);
floodfill(x,y,WHITE);
circle(x,y,20);
line(x,y-20,x,y+20);
line(x-20,y,x+20,y);
/*draws the shooter*/
//refresh();
iflag=0,lflag=0,mflag=0,pflag=0;
while(!kbhit())
/*until the keyboard is hit boxes are flashed*/
{
i=i%800;
l=l%600;
if(m<=0)m+=800;
if(p<=0)p+=600;
environment(i,j,0);
/*moves from left to right*/
environment(k,l,1);
/*moves from top to bottom*/
environment(m,n,2);
/*moves from right to left*/
environment(o,p,3);
/*moves from bottom to top*/
i+=20;
l+=20;
m-=20;
p-=20;
if(i>=800) j=rand()%600;
if(l>=600) k=rand()%800;
if(m<=0) n=rand()%600;
if(p<=0) o=rand()%800;
}
ch =getch();
/*gets key from user*/
/*key board is hit*/
setfillstyle(SOLID_FILL,BLUE);
/*draws a block to erase shooter at previous position*/
bar(x+20,y+20,x-20,y-20);
x=getx();
y=gety();
sh.x_pos=x;
/*gets the shooter position*/
sh.y_pos=y;
setfillstyle(SOLID_FILL,WHITE);
/*draws the shooter*/
circle(x,y,10);
floodfill(x,y,WHITE);
circle(x,y,20);
line(x,y-20,x,y+20);
line(x-20,y,x+20,y);
setfillstyle(SOLID_FILL,BLUE);
bar(x+20,y+20,x-20,y-20);
outtextxy(240,10,"HIT CTRL+Z TO QUIT");
switch(ch)
{
case 72:/*up arrow*/
direction=NORTH;
x=getx();
y=gety();
moveto(x,y-20);
x=getx();
y=gety();
sh.x_pos=x;
sh.y_pos=y;
setfillstyle(SOLID_FILL,WHITE);
circle(x,y,10);
floodfill(x,y,WHITE);
circle(x,y,20);
line(x,y-20,x,y+20);
line(x-20,y,x+20,y);
break;
case 80:/*down arrow*/
direction=SOUTH;
x=getx();
y=gety();
moveto(x,y+20);
x=getx();
y=gety();
sh.x_pos=x;
sh.y_pos=y;
setfillstyle(SOLID_FILL,WHITE);
circle(x,y,10);
floodfill(x,y,WHITE);
circle(x,y,20);
line(x,y-20,x,y+20);
line(x-20,y,x+20,y);
break;
case 75:/*left arrow*/
direction=WEST;
x=getx();
y=gety();
moveto(x-20,y);
x=getx();
y=gety();
sh.x_pos=x;
sh.y_pos=y;
setfillstyle(SOLID_FILL,WHITE);
circle(x,y,10);
floodfill(x,y,WHITE);
circle(x,y,20);
line(x,y-20,x,y+20);
line(x-20,y,x+20,y);
break;
case 77:/*right arrow*/
direction=EAST;
x=getx();
y=gety();
moveto(x+20,y);
x=getx();
y=gety();
sh.x_pos=x;
sh.y_pos=y;
setfillstyle(SOLID_FILL,WHITE);
circle(x,y,10);
floodfill(x,y,WHITE);
circle(x,y,20);
line(x,y-20,x,y+20);
line(x-20,y,x+20,y);
break;
case 13:/*enter key*/
x=getx();
y=gety();
sh.x_pos=x;
sh.y_pos=y;
is_hit=fire(x,y,iflag,lflag,mflag,pflag);
/*flags r passed by reference*/
if(is_hit==1)
{
hitcount++;
if(iflag)i+=800;
else if(lflag)k+=600;
else if(mflag)m=0;
else if(pflag)p=0;
iflag=0;lflag=0;mflag=0;pflag=0; /*flag reset*/
}
x=getx();
y=gety();
setfillstyle(SOLID_FILL,WHITE);
circle(x,y,10);
floodfill(x,y,WHITE);
circle(x,y,20);
line(x,y-20,x,y+20);
line(x-20,y,x+20,y);
break;
}
}while(ch!=26);
closegraph();
restorecrtmode();
gotoxy(30,12);
textcolor(YELLOW);
cprintf("YOU HAVE SCORED :%d",hitcount);/*score is indicated*/
getch();
}
/*function fire draws ans erases a line along the current direction*/
int fire(int x,int y,int <r,int &ttb,int &rtl,int &btt)
{
int i=0,hit=0;
switch(direction)
{
case EAST:/*right*/
if(sh.y_pos>=p[i].y_pos-20&&sh.y_pos<=p[i].y_pos+20&&sh.x_pos {
hit=1;
switch(i)
{
case 0: ltr=1; break;
case 1: ttb=1; break;
case 2: rtl=1; break;
case 3: btt=1; break;
}
}
else
if(sh.y_pos>=p[i+1].y_pos-20&&sh.y_pos<=p[i+1].y_pos+20&&sh.x_pos {
hit=1;
switch(i+1)
{
case 0: ltr=1; break;
case 1: ttb=1; break;
case 2: rtl=1; break;
case 3: btt=1; break;
}
}
else
if(sh.y_pos>=p[i+2].y_pos-20&&sh.y_pos<=p[i+2].y_pos+20&&sh.x_pos {
hit=1;
switch(i+2)
{
case 0: ltr=1; break;
case 1: ttb=1; break;
case 2: rtl=1; break;
case 3: btt=1; break;
}
}
else
if(sh.y_pos>=p[i+3].y_pos-20&&sh.y_pos<=p[i+3].y_pos+20&&sh.x_pos {
hit=1;
switch(i+3)
{
case 0: ltr=1; break;
case 1: ttb=1; break;
case 2: rtl=1; break;
case 3: btt=1; break;
}
}
setcolor(YELLOW);
line(x+20,y,x+800,y);
setcolor(BLUE);
delay(100);
line(x+20,y,x+800,y);
setcolor(WHITE);
break;
case WEST:/*left*/
if(sh.y_pos>=p[i].y_pos-20&&sh.y_pos<=p[i].y_pos+20&&sh.x_pos>p[i].x_pos)
{
hit=1;
switch(i)
{
case 0: ltr=1; break;
case 1: ttb=1; break;
case 2: rtl=1; break;
case 3: btt=1; break;
}
}
else
if(sh.y_pos>=p[i+1].y_pos-20&&sh.y_pos<=p[i+1].y_pos+20&&sh.x_pos>p[i+1].x_pos)
{
hit=1;
switch(i+1)
{
case 0: ltr=1; break;
case 1: ttb=1; break;
case 2: rtl=1; break;
case 3: btt=1; break;
}
}
else
if(sh.y_pos>=p[i+2].y_pos-20&&sh.y_pos<=p[i+2].y_pos+20&&sh.x_pos>p[i+2].x_pos)
{
hit=1;
switch(i+2)
{
case 0: ltr=1; break;
case 1: ttb=1; break;
case 2: rtl=1; break;
case 3: btt=1; break;
}
}
else
if(sh.y_pos>=p[i+3].y_pos-20&&sh.y_pos<=p[i+3].y_pos+20&&sh.x_pos>p[i+3].x_pos)
{
hit=1;
switch(i+3)
{
case 0: ltr=1; break;
case 1: ttb=1; break;
case 2: rtl=1; break;
case 3: btt=1; break;
}
}
setcolor(YELLOW);
line(x-20,y,x-800,y);
setcolor(BLUE);
delay(100);
line(x-20,y,x-800,y);
setcolor(WHITE);
break;
case SOUTH:/*down*/
if(sh.x_pos>=p[i].x_pos-20&&sh.x_pos<=p[i].x_pos+20&&sh.y_pos {
hit=1;
switch(i)
{
case 0: ltr=1; break;
case 1: ttb=1; break;
case 2: rtl=1; break;
case 3: btt=1; break;
}
}
else
if(sh.x_pos>=p[i+1].x_pos-20&&sh.x_pos<=p[i+1].x_pos+20&&sh.y_pos {
hit=1;
switch(i+1)
{
case 0: ltr=1; break;
case 1: ttb=1; break;
case 2: rtl=1; break;
case 3: btt=1; break;
}
}
else
if(sh.x_pos>=p[i+2].x_pos-20&&sh.x_pos<=p[i+2].x_pos+20&&sh.y_pos {
hit=1;
switch(i+2)
{
case 0: ltr=1; break;
case 1: ttb=1; break;
case 2: rtl=1; break;
case 3: btt=1; break;
}
}
else
if(sh.x_pos>=p[i+3].x_pos-20&&sh.x_pos<=p[i+3].x_pos+20&&sh.y_pos {
hit=1;
switch(i+3)
{
case 0: ltr=1; break;
case 1: ttb=1; break;
case 2: rtl=1; break;
case 3: btt=1; break;
}
}
setcolor(YELLOW);
line(x,y+20,x,y+800);
setcolor(BLUE);
delay(100);
line(x,y+20,x,y+800);
setcolor(WHITE);
break;
case NORTH:/*up*/
if(sh.x_pos>=p[i].x_pos-20&&sh.x_pos<=p[i].x_pos+20&&sh.y_pos>p[i].y_pos)
{
hit=1;
switch(i)
{
case 0: ltr=1; break;
case 1: ttb=1; break;
case 2: rtl=1; break;
case 3: btt=1; break;
}
}
else
if(sh.x_pos>=p[i+1].x_pos-20&&sh.x_pos<=p[i+1].x_pos+20&&sh.y_pos>p[i+1].y_pos)
{
hit=1;
switch(i+1)
{
case 0: ltr=1; break;
case 1: ttb=1; break;
case 2: rtl=1; break;
case 3: btt=1; break;
}
}
else
if(sh.x_pos>=p[i+2].x_pos-20&&sh.x_pos<=p[i+2].x_pos+20&&sh.y_pos>p[i+2].y_pos)
{
hit=1;
switch(i+2)
{
case 0: ltr=1; break;
case 1: ttb=1; break;
case 2: rtl=1; break;
case 3: btt=1; break;
}
}
else
if(sh.x_pos>=p[i+3].x_pos-20&&sh.x_pos<=p[i+3].x_pos+20&&sh.y_pos>p[i+3].y_pos)
{
hit=1;
switch(i+3)
{
case 0: ltr=1; break;
case 1: ttb=1; break;
case 2: rtl=1; break;
case 3: btt=1; break;
}
}
setcolor(YELLOW);
line(x,y-20,x,y-800);
setcolor(BLUE);
delay(100);
line(x,y-20,x,y-800);
setcolor(WHITE);
break;
}
for(i=0;i<=3;i++)
{
p[i].x_pos=0;
p[i].y_pos=0;
}
/*refreshing the record*/
return(hit);
/* of a positions*/
}
/*draws the green boxes */
int environment(int x,int y,int pos)
{
int i;
p[pos].x_pos=x;
p[pos].y_pos=y;
/*conditions to check for collision
checks whether the center of box x,y is within the area
(sh_xpos-20,sh_ypos-20)
(sh_xpos+20,sh_ypos+20)*/
if(x>=sh.x_pos-20&&x<=sh.x_pos+20&&y>=sh.y_pos-20&&y<=sh.y_pos+20)
{
put_banner();
getch();
closegraph();
restorecrtmode();
gotoxy(30,12);
textcolor(YELLOW);
cprintf("YOU HAVE SCORED :%d",hitcount);
getch();
exit(0);
}
setfillstyle(SOLID_FILL,LIGHTGREEN);
bar(x-10,y-10,x+10,y+10);
delay(20);
setfillstyle(SOLID_FILL,BLUE);
bar(x-10,y-10,x+10,y+10);
setcolor(WHITE);
return(0);
}
/*puts a banner at the End of Game*/
int put_banner()
{
setcolor(YELLOW);
settextstyle(TRIPLEX_FONT,HORIZ_DIR,5);
outtextxy((getmaxx()/2)-260,(getmaxy()/2)-20,"SORRY YOU`VE BEEN HIT");
return 0;
}
void refresh(void)
{
int i;
for(i=0;i<4;i++)
{
p[i].x_pos=0;
p[i].y_pos=0;
}
}
C++ Code for game LUDO
Posted On at by milanCode :
/* Developed by "akshay kumar srivastava"*/
/* phone 09835457174 oct 2005*/
#include
#include
#include
#include
#include
#include
/* MOUSE FUNCTIONS FOR LUDO GAME */
int DetectMouse(void);
void InitMouse(void);
void ShowMouse(void);
void HideMouse(void);
void WhereMouse(int*,int*);
int ButtClicked(void);
int ButtReleased(void);
int DetectMouse(void){
union REGS in,out;
in.x.ax=0;
int86(0x33,&in,&out);
if(out.x.ax==0)
return(0);
else
return(1);
}
void InitMouse(void)
{
union REGS in,out;
in.x.ax=33;
int86(0x33,&in,&out);
return;
}
void HideMouse(void)
{
union REGS in,out;
in.x.ax=2;
int86(0x33,&in,&out);
return;
}
void ShowMouse(void)
{
union REGS in,out;
in.x.ax=1;
int86(0x33,&in,&out);
return;
}
void WhereMouse(int *x,int *y)
{
union REGS in,out;
in.x.ax=3;
int86(0x33,&in,&out);
*x=(int)out.x.cx;
*y=(int)out.x.dx;
return;
}
int ButtClicked(void){
union REGS imouse,omouse;
int mc,mr;
imouse.x.ax = 3;
int86(0x33,&imouse,&omouse);
mc=omouse.x.bx;
return(mc);
}
int ButtReleased(void){
int br;
br=ButtClicked();
do{}while(ButtClicked()!=0);
return br;
}
/* GLOBAL VARIABLES */
int background=0;
int colorfortext=WHITE;//color used to display text
int rec=MAGENTA;//color for border of rectangles
int flagturn;//players turn=0 computer=1
int for6=0;//for 6 to come at regular intervals
int dice_value_color[4];
/* USER DEFINED FUNCTIONS */
void draw_all(void);
int player_chooses_house_color(void);
void draw_house(int color_of_house,int top_left_x_coordinate,int
top_left_y_coordinate);
void draw_button_with_button_name(int top_left_x_coordinate,int
top_left_y_coordinate,char* button_name);
void show_mouse_click_on_button(int mouse_click_x_coordinate,int
mouse_click_y_coordinate);
int check_if_mouse_click_is_on_any_button (int
mouse_click_x_coordinate,int mouse_click_y_coordinate);
void throw1(int x_coordinate_for_dice ,int y_coordinate_for_dice,int
counter_for_wait_at_last ,char dice_value);
void throw2(int x_coordinate_for_dice ,int y_coordinate_for_dice,char
dice_value);
int throwdice(void);
void messageout(char *message,int refresh_message=1);
void messageclear(void);
void initialise_house_with_ludos(int color_of_house);
void load_path_information_in_ludos(int ludo_house_color);
int check_if_ludo_move_requested_is_valid(int dice_value);
void move_ludo_to_new_position( int dice_value);
void winner(void);
void about(void);
void howtoplay(void);
class ludo{
private:
int x[45];
int y[45];
int pos;
int color;
public:
friend void redrawing(int );
friend int catchclickonludobutton(void) ;
friend int forcomputer1(int);
friend int forcomputer2(int);
friend int forcomputer3(int);
/* ludo1 initialises the ludo class objects */
void ludo1(int col,int *ax,int *ay)
{
int i;
for(i=0;i<=44;i++)
{
x[i]=*(ax);
y[i]=*(ay);
ax++;ay++;
}
color=col;
pos=0;
}//end of ludo1
int check_if_ludo_move_requested_is_valid(int dice)
{
/*return 1 move valid i.e proceed to movetopos,0 if not valid
2 if player cannot move any ludo*/
int i,j=0;
if( (pos<=44-dice && pos!=0) || (pos==0 && dice==6) )
return(1); // ludo can move
for(i=0;i<=3;i++)
{
if( (lp[i].pos+dice<=44 && lp[i].pos!=0) || (lp[i].pos==0 &&
dice==6) )
j=1;//i.e there is atleast one ludo which
//can move with current dice value
}
if(j>0)
return(2);//choose the correct ludo that can move
else
return(3);//no ludo can move at current dice value
//and player has to skip chance
}//end of check_if_ludo_move_requested_is_valid
void move_ludo_to_new_position( int dice )
{
HideMouse();
if(pos==0 && dice==6)//taking out of house on dice givig 6
{ messageclear();dice=1;}
dice=dice-1;
int opos;
opos=pos;
int i;
for(i=opos;i<=dice+opos;i++)
{
setcolor(7);
circle(x[i],y[i],8);
setfillstyle(1,7);
floodfill(x[i],y[i],7);
delay(50);
{pos++; redrawing(this->color);}
setcolor(color);
circle(x[i+1],y[i+1],8);
setfillstyle(1,color);
floodfill(x[i+1],y[i+1],color);
delay(50);
}
/*changing pos to 0 if ludo has been cut before redrawing all ludos*/
for(i=0;i<=3;i++)
{
if(this->x[this->pos]==lp[i].x[lp[i].pos] &&
this->y[this->pos]==lp[i].y[lp[i].pos] )
{
if(this->color!=lp[i].color)
lp[i].pos=0;
}
if(this->x[this->pos]==lc1[i].x[lc1[i].pos] &&
this->y[this->pos]==lc1[i].y[lc1[i].pos] )
{
if(this->color!=lc1[i].color)
lc1[i].pos=0;
}
if(this->x[this->pos]==lc2[i].x[lc2[i].pos] &&
this->y[this->pos]==lc2[i].y[lc2[i].pos] )
{
if(this->color!=lc2[i].color)
lc2[i].pos=0;
}
if(this->x[this->pos]==lc3[i].x[lc3[i].pos] &&
this->y[this->pos]==lc3[i].y[lc3[i].pos] )
{
if(this->color!=lc3[i].color)
lc3[i].pos=0;
}
}// end of for
/* redrawing new positions of all ludos */
redrawing(lp[0].color);
redrawing(lc1[0].color);
redrawing(lc2[0].color);
redrawing(lc3[0].color);
/* to check who is the winner */
static int number_of_player_ludo_home=0;
static int number_of_computer1_ludo_home=0;
static int number_of_computer2_ludo_home=0;
static int number_of_computer3_ludo_home=0;
if(pos==44 && flagturn==0)
number_of_player_ludo_home++;
if(pos==44 && flagturn==1)
number_of_computer1_ludo_home++;
if(pos==44 && flagturn==2)
number_of_computer2_ludo_home++;
if(pos==44 && flagturn==3)
number_of_computer3_ludo_home++;
if(number_of_player_ludo_home==4)
winner();
if(number_of_computer1_ludo_home==4)
winner();
if(number_of_computer2_ludo_home==4)
winner();
if(number_of_computer3_ludo_home==4)
winner();
ShowMouse();
} //end of move_ludo_to_new_position
};//end of ludo class
ludo lp[4],lc1[4],lc2[4],lc3[4];
void main()
{
int colors[4]={RED,BLUE,YELLOW,GREEN};
int i,a,b,dice=0,playercolor;;
draw_all();
InitMouse();
ShowMouse();
playercolor=player_chooses_house_color();
flagturn=0;
dice_value_color[flagturn]=playercolor;
load_path_information_in_ludos(playercolor);//gives life to correct
color
ludo as path depends on color
/* this code helps in making the chance of throwing dice go in a
clockwise
mode what color may the player choose to play*/
i=3;
while(i)
{
if(colors[i]==playercolor)
break;
i--;
}
int loop=i;
while(1)
{
flagturn++;
loop++;
if(loop>=4)
loop=0;
load_path_information_in_ludos(colors[loop]);
dice_value_color[flagturn]=colors[loop];
if(flagturn==3)
break;
}
initialise_house_with_ludos(RED);
initialise_house_with_ludos(BLUE);
initialise_house_with_ludos(YELLOW);
initialise_house_with_ludos(GREEN);
flagturn=0;
char ch;
int check,tmp1=0,tmp2;
enum choose {yes,nomore};
choose choose1=yes;
delay(500);
messageclear();
while(ch!=27)
{
if(kbhit())
ch=getch();
setcolor(RED);
WhereMouse(&a,&b);
if(ButtReleased())
{
check=check_if_mouse_click_is_on_any_button (a,b);
if(check>=1 && check<=4)
show_mouse_click_on_button(a,b);
switch(check)
{
case 1:
{about();break;}
case 4:
{howtoplay(); break;}
case 3:
{exit(0);break;}
case 2:
{dice=throwdice(); break;}
} //end of switch
} //end of if(ButtRelease)
/* players chance*/
if(flagturn==0)
{
messageout("PLAYER throwdice.best of luck. ",0);
if(dice!=0)
{
if(dice==6)
messageout("click on ludo button to take out of house or move");
else
messageout("click on the ludo button to move");
while(choose1==yes)
{
choose1=nomore;
tmp1=catchclickonludobutton();
tmp2=lp[tmp1].check_if_ludo_move_requested_is_valid(dice);
switch(tmp2)
{
case 1:
{
lp[tmp1].move_ludo_to_new_position(dice);
messageout("proceeding");
delay(1000);
break;
}
case 2:
{
messageout("choose correct ludo to proceed");
delay(1000);
choose1=yes;
break;
}
case 3:
{
messageout("sorry,you will have to skip this time");
delay(1000);
break;
}
}//switch ends
}//end of while(choose1=yes)
flagturn=1;
choose1=yes;
}//end of if(dice!=0)
dice=0;
}// end of if(flagturn==0)
/*computer1 chance*/
if(flagturn==1)
{
messageout("now the computer1 will throw dice");
dice=throwdice();
if(dice!=0)
{
tmp1=forcomputer1(dice);
if(tmp1!=5)//forcomputer(dice) returns 5 if no ludo can move
lc1[tmp1].move_ludo_to_new_position(dice);
else
messageout("computer1 has to skip this time.");
}
flagturn=2;
messageclear();
dice=0;
}
/*computer2 chance*/
if(flagturn==2)
{
messageout("now the computer2 will throw dice");
dice=throwdice();
if(dice!=0)
{
tmp1=forcomputer2(dice);
if(tmp1!=5)//forcomputer(dice) returns 5 if no ludo can move
lc2[tmp1].move_ludo_to_new_position(dice);
else
messageout("computer2 has to skip this time.");
}
flagturn=3;
messageclear();
dice=0;
}
/*computer3 chance*/
if(flagturn==3)
{
messageout("now the computer3 will throw dice");
dice=throwdice();
if(dice!=0)
{
tmp1=forcomputer3(dice);
if(tmp1!=5)//forcomputer(dice) returns 5 if no ludo can move
lc3[tmp1].move_ludo_to_new_position(dice);
else
messageout("computer3 has to skip this time.");
}
flagturn=0;
messageclear();
dice=0;
}
dice=0;
}//end of while
}//end of main
void draw_house(int col,int x,int y)
{
setcolor(rec);
rectangle(x,y,x+120,y+120);
setfillstyle(1,col);
floodfill(x+10,y+10,rec);
circle(x+30,y+30,10);
setfillstyle(1,WHITE);
floodfill(x+30,y+30,rec);
circle(x+30,y+70,10);
setfillstyle(1,WHITE);
floodfill(x+30,y+70,rec);
circle(x+90,y+30,10);
setfillstyle(1,WHITE);
floodfill(x+90,y+30,rec);
circle(x+90,y+70,10);
setfillstyle(1,WHITE);
floodfill(x+90,y+70,rec);
}
void draw_button_with_button_name(int x,int y,char *message)
{
setcolor(rec);
rectangle(x,y,x+110,y+30);
setcolor(colorfortext);
outtextxy(x+20,y+15,message);
}
void show_mouse_click_on_button(int x,int y)
{
HideMouse();
setfillstyle(1,7);
floodfill(x,y,rec);
delay(50);
setfillstyle(1,0);
floodfill(x,y,rec);
draw_button_with_button_name(0,55,"about");
draw_button_with_button_name(120,55,"exit");
draw_button_with_button_name(0,55+35,"throw dice");
draw_button_with_button_name(120,55+35,"how to play");
ShowMouse();
}
int check_if_mouse_click_is_on_any_button (int a,int b)
{
if(a>0 && a<110 && b>55 && b<85)
return 1;
else if(a>0 && a<110 && b>90 && b<120)
return 2;
else if(a>120 && a< 230 && b>55 && b<85)
return 3;
else if(a>120 && a< 230 && b>90 && b<120)
return 4;
else if(a>239 && a<359 && b>0 && b<120 )
return 5;
else if(a>239 && a<359 && b>280 && b<400 )
return 6;
else if(a>519 && a<639 && b>0 && b<120 )
return 7;
else if(a>519 && a<659 && b>280 && b<400 )
return 8;
else
return 0;
}
int throwdice()
{
HideMouse();
int rn,count=0;char num;
int x=0,y,flag=0;
randomize();
for(y=150;y<=320;y=y+10,x=x+10)
{
rn=random(6)+1;
/* using for6 to get a 6 on dice every 5th time to make the game a
little faster*/
if(y==320)
{for6++;}
if(for6>=5)
{
rn=6;
for6=0;
}
// cout<
// for6++;
num=rn+48;
if(flag==0)
{
throw2(x,y,num);
flag=1;
}
else
{
count++;
throw1(x,y,count,num);
flag=0;
}
}
settextstyle(0,0,0);
ShowMouse();
return rn;
}
void throw1(int x,int y,int count,char num)
{
settextstyle(0,0,3);
setcolor(WHITE);
rectangle(x,y,x+35,y+35);
//setcolor(RED);
setcolor(dice_value_color[flagturn]);
outtextxy(x+5,y+5,&num);
delay(140);
if(count==9)
{delay(650); }
setcolor(0);
rectangle(x,y,x+35,y+35);
outtextxy(x+5,y+5,&num);
}
void throw2(int x,int y,char num)
{
x=x-17;y=y+10;
settextstyle(0,0,3);
setcolor(WHITE);
line(x,y,x+25,y+25);
line(x,y,x+25,y-25);
line(x+25,y+25,x+50,y);
line(x+25,y-25,x+50,y);
//setcolor(RED);
setcolor(dice_value_color[flagturn]);
outtextxy(x+15,y-10,&num);
delay(140);
setcolor(0);
line(x,y,x+25,y+25);
line(x,y,x+25,y-25);
line(x+25,y+25,x+50,y);
line(x+25,y-25,x+50,y);
outtextxy(x+15,y-10,&num);
}
void messageout(char *message,int refresh)
{
if(refresh)
messageclear();
setcolor(colorfortext);
outtextxy(20,450,message);
}
void messageclear()
{
HideMouse();
setfillstyle(1,0);
floodfill(20,450,rec);
ShowMouse();
}
void initialise_house_with_ludos(int color)
{
int x,y;
switch(color)
{
case RED:
{x=239;y=0;break;}
case BLUE:
{x=519;y=0;break;}
case YELLOW:
{x=639-120;y=280;break;}
case GREEN:
{x=239;y=280;break;}
}
draw_house(color,x,y);
setcolor(rec);
setfillstyle(1,color);
{
circle(x+30,y+30,8);
floodfill(x+30,y+30,rec);
}
{
circle(x+30,y+70,8);
floodfill(x+30,y+70,rec);
}
{
circle(x+90,y+30,8);
floodfill(x+90,y+30,rec);
}
{
circle(x+90,y+70,8);
floodfill(x+90,y+70,rec);
}
}
void load_path_information_in_ludos(int plcol)
{
int arx[45],ary[45];
switch(plcol)
{
case RED:
{ int
arx[45]={269,284,314,344,379,379,379,379,379,439,499,499,499,
499,499,534,564,594,624,624,624,594,564,534,499,
499,499,499,499,439,384,384,384,384,384,344,314,284,
254,254,284,314,344,384,424};
int
ary[45]={30,140,140,140,140,105,75,45,15,15,15,45,75,105,140,140,
140,140,140,200,260,260,260,260,260,295,325,355,385,385,
385,355,325,295,260,260,260,260,260,200,200,200,200,200,
200};
for(int tmp=0;tmp<=3;tmp++)
{
switch(tmp)
{
case 0: {arx[0]=269;ary[0]=30;break;}
case 1: {arx[0]=329;ary[0]=70;break;}
case 2: {arx[0]=329;ary[0]=30;break;}
case 3: {arx[0]=269;ary[0]=70;break;}
}
switch(flagturn)
{
case 0:
{lp[tmp].ludo1(RED,arx,ary);break;}
case 1:
{lc1[tmp].ludo1(RED,arx,ary);break;}
case 2:
{lc2[tmp].ludo1(RED,arx,ary);break;}
case 3:
{lc3[tmp].ludo1(RED,arx,ary);break;}
}
}
break;}//case RED ends
case BLUE:
{ int
arx[45]={549,499,499,499,499,534,564,594,624,624,624,594,564,
534,499,499,499,499,499,439,384,384,384,384,384,344,
314,284,254,254,254,284,314,344,379,379,379,379,379,
439,439,439,439,439,439 };
int ary[45]={30,45,75,105,140,140,140,140,140,200,260,260,260,260,
260,295,325,355,385,385,385,355,325,295,260,260,260,
260,260,200,140,140,140,140,140,105,75,45,15,15,45
,75,105,140,185};
for(int tmp=0;tmp<=3;tmp++)
{
switch(tmp)
{
case 0: {arx[0]=549;ary[0]=30;break;}
case 1: {arx[0]=549;ary[0]=70;break;}
case 2: {arx[0]=609;ary[0]=30;break;}
case 3: {arx[0]=609;ary[0]=70;break;}
}
switch(flagturn)
{
case 0:
{lp[tmp].ludo1(BLUE,arx,ary);break;}
case 1:
{lc1[tmp].ludo1(BLUE,arx,ary);break;}
case 2:
{lc2[tmp].ludo1(BLUE,arx,ary);break;}
case 3:
{lc3[tmp].ludo1(BLUE,arx,ary);break;}
}
}
break;}
case GREEN:
{ int
arx[45]={269,384,384,384,384,344,314,284,254,254,254,284,314,
344,379,379,379,379,379,439,499,499,499,499,499,534,
564,594,624,624,624,594,564,534,499,499,499,499,499,
439,439,439,439,439,439 };
int ary[45]={30,355,325,295,260,260,260,
260,260,200,140,140,140,140,140,105,75,45,15,15,15,45
,75,105,140,140,140,140,140,200,260,260,260,260,
260,295,325,355,385,385,355,325,295,260,215};
for(int tmp=0;tmp<=3;tmp++)
{
switch(tmp)
{
case 0: {arx[0]=269;ary[0]=310;break;}
case 1: {arx[0]=329;ary[0]=350;break;}
case 2: {arx[0]=329;ary[0]=310;break;}
case 3: {arx[0]=269;ary[0]=350;break;}
}
switch(flagturn)
{
case 0:
{lp[tmp].ludo1(GREEN,arx,ary);break;}
case 1:
{lc1[tmp].ludo1(GREEN,arx,ary);break;}
case 2:
{lc2[tmp].ludo1(GREEN,arx,ary);break;}
case 3:
{lc3[tmp].ludo1(GREEN,arx,ary);break;}
}
}
break;}
case YELLOW:
{ int
arx[45]={549,594,564,534,499,499,499,499,499,439,384,384,384,
384,384,344,314,284,254,254,254,284,314,344,379,379,
379,379,379,439,499,499,499,499,499,534,564,594,624,
624,594,564,534,499,454};
int
ary[45]={310,260,260,260,260,295,325,355,385,385,385,355,325,295,
260,260,260,260,260,200,140,140,140,140,140,105,75,45,15,
15,15,45,75,105,140,140,140,140,140,200,200,200,200,200,200};
for(int tmp=0;tmp<=3;tmp++)
{
switch(tmp)
{
case 0: {arx[0]=549;ary[0]=310;break;}
case 1: {arx[0]=549;ary[0]=350;break;}
case 2: {arx[0]=609;ary[0]=310;break;}
case 3: {arx[0]=609;ary[0]=350;break;}
}
switch(flagturn)
{
case 0:
{lp[tmp].ludo1(YELLOW,arx,ary);break;}
case 1:
{lc1[tmp].ludo1(YELLOW,arx,ary);break;}
case 2:
{lc2[tmp].ludo1(YELLOW,arx,ary);break;}
case 3:
{lc3[tmp].ludo1(YELLOW,arx,ary);break;}
}
}
break;}
}//switch ends
}
int catchclickonludobutton()
{
int a,b,i;
while(1)
{
WhereMouse(&a,&b);
if(ButtReleased())
{ for(i=0;i<=3;i++)
{
if( abs(a-(lp[i].x[lp[i].pos])) < 8 &&
abs(b-(lp[i].y[lp[i].pos])) <
8 )
return i;
}
}
}
}
void redrawing(int tmp)
{
int i;
for(i=0;i<=3;i++)
{
if(tmp==lp[0].color)
{
setcolor(lp[i].color);
circle(lp[i].x[lp[i].pos],lp[i].y[lp[i].pos],8);
setfillstyle(1,lp[i].color);
floodfill(lp[i].x[lp[i].pos],lp[i].y[lp[i].pos],lp[i].color);
}
if(tmp==lc1[0].color)
{
setcolor(lc1[i].color);
circle(lc1[i].x[lc1[i].pos],lc1[i].y[lc1[i].pos],8);
setfillstyle(1,lc1[i].color);
floodfill(lc1[i].x[lc1[i].pos],lc1[i].y[lc1[i].pos],lc1[i].color);
}
if(tmp==lc2[0].color)
{
setcolor(lc2[i].color);
circle(lc2[i].x[lc2[i].pos],lc2[i].y[lc2[i].pos],8);
setfillstyle(1,lc2[i].color);
floodfill(lc2[i].x[lc2[i].pos],lc2[i].y[lc2[i].pos],lc2[i].color);
}
if(tmp==lc3[0].color)
{
setcolor(lc3[i].color);
circle(lc3[i].x[lc3[i].pos],lc3[i].y[lc3[i].pos],8);
setfillstyle(1,lc3[i].color);
floodfill(lc3[i].x[lc3[i].pos],lc3[i].y[lc3[i].pos],lc3[i].color);
}
}//for ends
}
/* deciding for computer1 which ludo to move */
int forcomputer1(int dice)
{
int i,j,tmp,poslpforlc[12]={0,0,0,0,0,0,0,0,0,0,0,0},cannotmove=5;
/* checking if atleast one ludo can move at current dice value*/
for(i=0;i<=3;i++)
{
if( (lc1[i].pos<=44-dice && lc1[i].pos!=0) || (lc1[i].pos==0 &&
dice==6) )
cannotmove=0;//cannotmove=5 then no ludo can move
}
if(cannotmove==5)
return 5;
//position of players ludo in terms of computers1 ludo positions
for(i=0;i<=3;i++)
{
for(j=1;j<=40;j++)
{
if( lc1[0].x[j]==lp[i].x[lp[i].pos] &&
lc1[0].y[j]==lp[i].y[lp[i].pos])
{
poslpforlc[i]=j;
}
}
}
//position of computer2 ludo in terms of computer1 ludo positions
for(i=4;i<=7;i++)
{
for(j=1;j<=40;j++)
{
if( lc1[0].x[j]==lc2[i-4].x[lc2[i-4].pos] &&
lc1[0].y[j]==lc2[i-4].y[lc2[i-4].pos])
{
poslpforlc[i]=j;
}
}
}
//position of computer3 ludo in terms of computer1 ludo positions
for(i=8;i<=11;i++)
{
for(j=1;j<=40;j++)
{
if( lc1[0].x[j]==lc3[i-8].x[lc3[i-8].pos] &&
lc1[0].y[j]==lc3[i-8].y[lc3[i-8].pos])
{
poslpforlc[i]=j;
}
}
}//end of i
/*to cut opponents ludo button*/
for(i=0;i<=3;i++)
{
for(j=0;j<=11;j++)
{
if( dice==(poslpforlc[j] - lc1[i].pos) && lc1[i].pos!=0)
return i;
}
}
/* to move those ludos which are near the player ludos and have the
gretest value of pos i.e position . it means saving the threatened
ludo*/
int array[4]={0,0,0,0};
int a1=0,tmp1;
for(i=0;i<=3;i++)
{
for(j=0;j<=11;j++)
{
if( (-poslpforlc[j] + lc1[i].pos)<=6
&& (-poslpforlc[j] + lc1[i].pos)>=1
&& lc1[i].pos+dice<=44
&& poslpforlc[j]!=0
&& lc1[i].pos<=40 )
{array[i]=lc1[i].pos;}
}
if(a1<=array[i])
a1=array[i];/* a1 has the greatest pos value */
}
if(a1!=0) //to see that at least one ludo is threatened */
{
for(i=0;i<=3;i++) //getting the ludo with greatest pos value
{
if(a1==array[i])
return i;
}
}
//to take out ludo button from house
for(i=0;i<=3;i++)
{
if(dice==6)
{
if(lc1[i].pos==0 )
return i;
}
}
/*if ludo buttons are outside and option for cutting opponent or
taking
out
own ludo button are not valid , using round robin
for selecting ludo to move*/
static int ludolast=0;
int firstround=0;
for(i=0;i<=3;i++)
{
if(firstround==0)
{
if(ludolast!=3)
{i=ludolast+1;}
firstround++;
}
if(lc1[i].pos!=0 && lc1[i].pos+dice<=44 )
{ludolast=i;i=4;}
if(i==3)
i=-1;
}
return ludolast;
//if ludo button is
}
/* deciding for computer2 which ludo to move */
int forcomputer2(int dice)
{
int i,j,tmp,poslpforlc[12]={0,0,0,0,0,0,0,0,0,0,0,0},cannotmove=5;
/* checking if atleast one ludo can move at current dice value*/
for(i=0;i<=3;i++)
{
if( (lc2[i].pos<=44-dice && lc2[i].pos!=0) || (lc2[i].pos==0 &&
dice==6) )
cannotmove=0;//cannotmove=5 then no ludo can move
}
if(cannotmove==5)
return 5;
//position of players ludo in terms of computers2 ludo positions
for(i=0;i<=3;i++)
{
for(j=1;j<=40;j++)
{
if( lc2[0].x[j]==lp[i].x[lp[i].pos] &&
lc2[0].y[j]==lp[i].y[lp[i].pos])
{
poslpforlc[i]=j;
}
}
}
//position of computer1 ludo in terms of computer2 ludo positions
for(i=4;i<=7;i++)
{
for(j=1;j<=40;j++)
{
if( lc2[0].x[j]==lc1[i-4].x[lc1[i-4].pos] &&
lc2[0].y[j]==lc1[i-4].y[lc1[i-4].pos])
{
poslpforlc[i]=j;
}
}
}
//position of computer3 ludo in terms of computer2 ludo positions
for(i=8;i<=11;i++)
{
for(j=1;j<=40;j++)
{
if( lc2[0].x[j]==lc3[i-8].x[lc3[i-8].pos] &&
lc2[0].y[j]==lc3[i-8].y[lc3[i-8].pos])
{
poslpforlc[i]=j;
}
}
}//end of i
for(i=0;i<=11;i++)
{
}
/*to cut opponents ludo button*/
for(i=0;i<=3;i++)
{
for(j=0;j<=11;j++)
{
if( dice==(poslpforlc[j] - lc2[i].pos) && lc2[i].pos!=0)
{return i;}
}
}
/* to move those ludos which are near the player ludos and have the
gretest value of pos i.e position . it means saving the threatened
ludo*/
int array[4]={0,0,0,0};
int a1=0,tmp1;
for(i=0;i<=3;i++)
{
for(j=0;j<=11;j++)
{
if( (-poslpforlc[j] + lc2[i].pos)<=6
&& (-poslpforlc[j] + lc2[i].pos)>=1
&& lc2[i].pos+dice<=44
&& poslpforlc[j]!=0
&& lc2[i].pos<=40 )
{array[i]=lc2[i].pos;}
}
if(a1<=array[i])
a1=array[i];/* a1 has the greatest pos value */
}
if(a1!=0) //to see that at least one ludo is threatened */
{
for(i=0;i<=3;i++) //getting the ludo with greatest pos value
{
if(a1==array[i])
{return i;}
}
}
//to take out ludo button from house
for(i=0;i<=3;i++)
{
if(dice==6)
{
if(lc2[i].pos==0 )
{return i;}
}
}
/*if ludo buttons are outside and option for cutting opponent or
taking
out
own ludo button are not valid , using round robin
for selecting ludo to move*/
static int ludolast=0;
int firstround=0;
for(i=0;i<=3;i++)
{
if(firstround==0)
{
if(ludolast!=3)
{i=ludolast+1;}
firstround++;
}
if(lc2[i].pos!=0 && lc2[i].pos+dice<=44 )
{ludolast=i;i=4;}
if(i==3)
i=-1;
}
return ludolast;
//if ludo button is
}
/* deciding for computer3 which ludo to move */
int forcomputer3(int dice)
{
int i,j,tmp,poslpforlc[12]={0,0,0,0,0,0,0,0,0,0,0,0},cannotmove=5;
/* checking if atleast one ludo can move at current dice value*/
for(i=0;i<=3;i++)
{
if( (lc3[i].pos<=44-dice && lc3[i].pos!=0) || (lc3[i].pos==0 &&
dice==6) )
cannotmove=0;//cannotmove=5 then no ludo can move
}
if(cannotmove==5)
return(5);
//position of players ludo in terms of computers1 ludo positions
for(i=0;i<=3;i++)
{
for(j=1;j<=40;j++)
{
if( lc3[0].x[j]==lp[i].x[lp[i].pos] &&
lc3[0].y[j]==lp[i].y[lp[i].pos])
{
poslpforlc[i]=j;
}
}
}
//position of computer2 ludo in terms of computer3 ludo positions
for(i=4;i<=7;i++)
{
for(j=1;j<=40;j++)
{
if( lc3[0].x[j]==lc2[i-4].x[lc2[i-4].pos] &&
lc3[0].y[j]==lc2[i-4].y[lc2[i-4].pos])
{
poslpforlc[i]=j;
}
}
}
//position of computer1 ludo in terms of computer3 ludo positions
for(i=8;i<=11;i++)
{
for(j=1;j<=40;j++)
{
if( lc3[0].x[j]==lc1[i-8].x[lc1[i-8].pos] &&
lc3[0].y[j]==lc1[i-8].y[lc1[i-8].pos])
{
poslpforlc[i]=j;
}
}
}//end of i
/*to cut opponents ludo button*/
for(i=0;i<=3;i++)
{
for(j=0;j<=11;j++)
{
if( dice==(poslpforlc[j] - lc3[i].pos) && lc3[i].pos!=0 )
return i;
}
}
/* to move those ludos which are near the player ludos and have the
gretest value of pos i.e position . it means saving the threatened
ludo*/
int array[4]={0,0,0,0};
int a1=0,tmp1;
for(i=0;i<=3;i++)
{
for(j=0;j<=11;j++)
{
if( (-poslpforlc[j] + lc3[i].pos)<=6
&& (-poslpforlc[j] + lc3[i].pos)>=1
&& lc3[i].pos+dice<=44
&& lc3[i].pos<=40
&& poslpforlc[j]!=0 )
{array[i]=lc3[i].pos;}
}
if(a1<=array[i])
a1=array[i];/* a1 has the greatest pos value */
}
if(a1!=0) //to see that at least one ludo is threatened */
{
for(i=0;i<=3;i++) //getting the ludo with greatest pos value
{
if(a1==array[i])
{return i;}
}
}
//to take out ludo button from house
for(i=0;i<=3;i++)
{
if(dice==6)
{
if(lc3[i].pos==0 )
return i;
}
}
/*if ludo buttons are outside and option for cutting opponent or
taking
out
own ludo button are not valid , using round robin
for selecting ludo to move*/
static int ludolast=0;
int firstround=0;
for(i=0;i<=3;i++)
{
if(firstround==0)
{
if(ludolast!=3)
{i=ludolast+1;}
firstround++;
}
if(lc3[i].pos!=0 && lc3[i].pos+dice<=44 )
{ludolast=i;i=4;}
if(i==3)
i=-1;
}
return ludolast;
//if ludo button is
}
void draw_all()
{
//registerbgidriver(EGAVGA_driver);
//registerbgifont(sansserif_font);
int gdriver = DETECT, gmode;
/* request auto detection */
/* initialize graphics mode */
initgraph(&gdriver, &gmode, "");
/* writing text */
setcolor(background);
settextstyle(0,0,0);
setcolor (colorfortext);
outtextxy(5,10,"(aks_mailin@rediffmail.com)");
outtextxy(5,20,"akshay presents");
outtextxy(5,30,"COMPUTER LUDO");
outtextxy(5,40,"GAME");
/* drawing rectangles */
setcolor(rec);
rectangle(0,410,639,479);
rectangle(0,0,230,50);
setcolor(rec);
rectangle(239,0,639,400);
setfillstyle(1,7);
floodfill(310,10,rec);
draw_house(RED,239,0);
draw_house(GREEN,239,280);
draw_house(BLUE,639-120,0);
draw_house(YELLOW,639-120,280);
draw_button_with_button_name(0,55,"about");
draw_button_with_button_name(120,55,"exit");
draw_button_with_button_name(0,55+35,"throw dice");
draw_button_with_button_name(120,55+35,"how to play");
/*drawing sectors*/
setcolor(0);
line(359,30,519,30 );
line(359,60,519,60 );
line(359,90,519,90 );
line(359,120,519,120 );
line(359,280,519,280);
line(359,310,519,310);
line(359,340,519,340 );
line(359,370,519,370 );
line(239,160,639,160 );
line(239,240,639,240 );
line(269,120,269,280);
line(269+30,120,269+30,280 );
line(269+60,120,269+60,280 );
line(269+90,120,269+90,280 );
line(269+130,0,269+130,400 );
line(269+210,0,269+210,400 );
line(269+250,120,269+250,280 );
line(269+280,120,269+280,280 );
line(269+310,120,269+310,280 );
line(269+340,120,269+340,280 );
setfillstyle(1,WHITE);
floodfill(439,200,background);
setcolor(RED);
outtextxy(420,190,"HOME");
return ;
}
int player_chooses_house_color(void)
{
char ch;
int check;
int a,b,playercolor=0;
messageout("PLAYER,click YOUR lucky COLOR on the board ");
while(ch!=27)
{
if(playercolor!=0)
break;//come out of while
if(kbhit())
ch=getch();
setcolor(RED);
WhereMouse(&a,&b);
if(ButtReleased())
{
check=check_if_mouse_click_is_on_any_button (a,b);
switch(check)
{
case 1:
{show_mouse_click_on_button(a,b);about();break;}
case 4:
{show_mouse_click_on_button(a,b);howtoplay(); break;}
case 3:
{show_mouse_click_on_button(a,b);exit(0);break;}
case 5:
{ messageout( "you have choosen red");
playercolor=RED;
break;}
case 6:
{ messageout( "you have choosen green");
playercolor=GREEN;
break;}
case 7:
{ messageout( "you have choosen blue");
playercolor=BLUE;
break;}
case 8:
{ messageout( "you have choosen yellow");
playercolor=YELLOW;
break;}
} //end of switch
}//end of if(ButtRelease)
}//end of while
delay(500);
return playercolor;
}
void about(void)
{
HideMouse();
setcolor(rec);
rectangle(0,135,230,390);
setcolor(colorfortext);
outtextxy(5,150,"WELCOME to ComputerLudo");
outtextxy(5,160,"This software is developed");
outtextxy(5,170,"by");
outtextxy(5,180,"");
outtextxy(5,190,"");
outtextxy(5,210,"Akshay ");
outtextxy(5,230,"U can send suggestions at");
outtextxy(5,240,"my Email:");
outtextxy(5,250,"aks_mailin@rediffmail.com");
outtextxy(5,260,"thanks");
outtextxy(5,310,"PRESS ANY KEY TO PROCEED ...");
getch();
setfillstyle(1,0);
floodfill(5,300,rec);
setcolor(0);
rectangle(0,135,230,390);
setcolor(rec);
ShowMouse();
return ;
}
void winner()
{
HideMouse();
setcolor(rec);
rectangle(0,135,230,390);
setcolor(colorfortext);
outtextxy(5,150,"welcome to ComputerLudo");
if(flagturn==0)
{
outtextxy(5,160,"CONGRATULATIONS");
outtextxy(5,170,"YOU WON THIS ROUND ");
outtextxy(5,180,"OF");
outtextxy(5,190,"ComputerLudo");
outtextxy(5,210,"akshay thanks ");
outtextxy(5,220,"you for trying this");
outtextxy(5,230,"game.");
outtextxy(5,240,"thanks again.");
outtextxy(5,310,"PRESS ANY KEY TO EXIT..");
}
else
{
switch(flagturn)
{
case 1:
{outtextxy(5,160,"COMPUTER1 WON THIS");break;}
case 2:
{outtextxy(5,160,"COMPUTER2 WON THIS");break;}
case 3:
{outtextxy(5,160,"COMPUTER3 WON THIS");break;}
}
outtextxy(5,170,"ROUND OF");
outtextxy(5,180,"ComputerLudo");
outtextxy(5,200,"BETTER LUCK NEXT TIME");
outtextxy(5,220,"akshay thanks ");
outtextxy(5,230,"you for trying this");
outtextxy(5,240,"game.");
outtextxy(5,250,"thanks again.");
outtextxy(5,310,"PRESS ANY KEY TO EXIT..");
}
getch();
exit(0);
}
void howtoplay()
{
HideMouse();
setcolor(rec);
rectangle(0,135,230,390);
setcolor(colorfortext);
outtextxy(5,150,"This is a game between");
outtextxy(5,160,"COMPUTER and the PLAYER.");
outtextxy(5,170,"Choose HOUSE by ");
outtextxy(5,180,"clicking over the HOUSE of");
outtextxy(5,190,"your fovourite color.");
outtextxy(5,200,"Get 6 on Dice to take a ludo");
outtextxy(5,210,"out of HOUSE.To make a ludo");
outtextxy(5,220,"MOVE click on it.");
outtextxy(5,230,"CUT(send to house) computers");
outtextxy(5,240,"ludos by placing your ludos ");
outtextxy(5,250,"over its ludos.");
outtextxy(5,260,"One who gets all the ludos");
outtextxy(5,270,"HOME wins thE Game.");
outtextxy(5,290,"FOLLOW THE MESSAGE BOX");
outtextxy(5,300,"GIVEN AT BOTTOM");
outtextxy(5,320,"thanks");
outtextxy(2,330,"Akshay.");
outtextxy(5,350,"PRESS ANY KEY TO PROCEED ...");
getch();
setfillstyle(1,0);
floodfill(5,300,rec);
setcolor(0);
rectangle(0,135,230,390);
setcolor(rec);
ShowMouse();
return ;
}