You are on page 1of 84

#include <graphics.

h>
#include <iostream.h>
#include <conio.h>
#include <dos.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <malloc.h>

#define MAX 50
#define UP_ARROW 72
#define DOWN_ARROW 80
#define LEFT_ARROW 75
#define RIGHT_ARROW 77

#define WinMinX 40
#define WinMaxX 600
#define WinMinY 40
#define WinMaxY 440
enum Direction {Forward,Backward,Upward,Downward};

void signature();
void drawmainMenu(int,int,int);
void main_Header(void);

int main()
{clrscr();
int g = DETECT , d;
initgraph ( &g , &d , "c:\TURBOC3\bgi" );

int select_option;
int mainmenu(void);
MainStart:
select_option = mainmenu();
int snake(void);
int ludo(void);
int tic_tac_toe(void);
int snakeladder(void);
switch ( select_option )
{
case 1:
snake();
goto MainStart;
case 2:
ludo();
goto MainStart;
case 3:
tic_tac_toe();
goto MainStart;
case 4:
snakeladder();
goto MainStart;
case 5:
return 1;
}return 1;
}

int mainmenu ()
{
int ch;
int selected = 1;
int TotalOptions = 5;

cleardevice();
setbkcolor ( BLACK );
main_Header();
signature();
drawmainMenu ( selected , RED , GREEN );
do
{
ch = getch();
if ( ch == DOWN_ARROW )
{
selected = selected >= TotalOptions ? 1 : selected + 1;
drawmainMenu ( selected , RED , GREEN );
}
else if ( ch == UP_ARROW )
{
selected = selected < 2 ? TotalOptions : selected - 1;
drawmainMenu ( selected , RED , GREEN );
}

}while ( ch != ' ' );

return selected;
}
void drawmainMenu ( int selected , int defCol , int selCol )
{
int x = 250;
int y = 100;
int width = 175;
int height = 30;
int i;
int TotalOptions = 5;
char menu_option[5][20]= {
" SNAKE WAR-I ",
" LUDO ",
" TIC-TAC-TOE ",
" SNAKES & LADDERS ",
" EXIT "
};
setcolor ( WHITE );

for ( i = 1; i <= TotalOptions; i++ )


{
if ( i == selected )
setfillstyle ( 1 , selCol );
else
setfillstyle ( 1 , defCol );
bar ( x , y , x + width , y + height );
rectangle ( x , y , x + width , y + height );
outtextxy ( x + 20 , y + 10 , menu_option[i - 1] );
y = y + height + 30;
}
}
void main_Header()
{
setcolor ( RED );
settextstyle ( 1 , 0 , 4 );
outtextxy ( 270 , 27 , " 4-IN-1-GAME " );
setcolor ( YELLOW );
outtextxy ( 270 , 25 , " 4-IN-1-GAME " );
}

/*snake*/

struct Coord
{
int x , y;
};
class Snake;
class Point
{
int x , y , color ;
public:
Point ( )
{
set ();
}

void set();
void draw( );
int getx() { return x; }
int gety() { return y; }
friend int point_vanished ( Point &p , Snake &s );
};

class Snake
{
Coord *_Snake;
int _CurSize, _color,_MaxSize, _Points;
char _player;
Direction _Direction;
public:
Snake ( int size = 20, int color = RED , char player = 'M' )
{
_Snake = new Coord [ size ];
_CurSize = 3;
if ( player == 'C' )
{
_Snake [0].x = WinMaxX - 10;
_Direction = Backward;
}
else
{
_Snake [0].x = WinMinX + 10;
_Direction = Forward;
}
//_Snake [0].x = WinMinX + 10;
_Snake [0].y = WinMinY + 10;
_color = color;
_MaxSize = size;
_player = player;
_Points = 0;
}
void set( int size = 20, int color = RED , char player = 'M' )
{
delete _Snake;
_Snake = new Coord [ size ];
_CurSize = 3;
if ( player == 'C' )
{
_Direction = Backward;
_Snake [0].x = WinMaxX - 10;
}
else
{
_Snake [0].x = WinMinX + 10;
_Direction = Forward;
}
_Snake [0].x = WinMinX + 10;
//_Snake [0].y = WinMinY + 10;
_color = color;
_MaxSize = size;
_player = player;
_Points = 0;
}

void change_direction ( Direction d);


void increment ();
void inc_disp ();
void shift_all ();
void display ( int color = BLACK );
void com_play ( Point p1 );
friend int point_vanished ( Point &p , Snake &s );
};
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
void Sound ( int s );
void Message_Display ( char msg[30] , char color );
void show_Header();
int menu ();
void drawMenu ( int selected , int defCol , int selCol );
void show_About();
void show_HowTOPlay();
void Play();
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
int snake()
{
int selected_option;

Start:
selected_option = menu();

switch ( selected_option )
{
case 1:
Play();
goto Start;
case 2:
show_HowTOPlay();
goto Start;
case 3:
return 1;
}
return 1;
}

/****************************************

****************************************/
/****************************************

****************************************/
void Snake :: increment ( )
{
//int i;

shift_all();
if ( _Direction == Forward )
{
if ( _Snake[0].x >= WinMaxX )
{
_Snake[0].x = WinMinX ;
}
else
_Snake[0].x = _Snake[0].x + 10;
}
else if ( _Direction == Backward )
{
if ( _Snake[0].x <= WinMinX )
{
_Snake[0].x = WinMaxX ;
}
else
_Snake[0].x = _Snake[0].x - 10;
}
else if ( _Direction == Upward )
{
if ( _Snake[0].y <= WinMinY )
{
_Snake[0].y = WinMaxY ;
}
else
_Snake[0].y = _Snake[0].y - 10;
}
else if ( _Direction == Downward )
{
if ( _Snake[0].y >= WinMaxY )
{
_Snake[0].y = WinMinY ;
}
else
_Snake[0].y = _Snake[0].y + 10;
}
}
/****************************************

****************************************/
void Snake :: shift_all ()
{
int i;
for ( i = _CurSize -1 ; i > 0; i-- )
{
_Snake[i].x = _Snake[i-1].x;
_Snake[i].y = _Snake[i-1].y;
}
}

void Snake :: inc_disp ()


{
display ( BLACK );
increment();
display ( _color );
}
/****************************************

****************************************/
void Snake :: display ( int color)
{
setfillstyle ( 1, color );
if ( color == 0 )
{
setcolor ( 0 );
bar ( _Snake[_CurSize - 1].x - 5 , _Snake[_CurSize - 1].y - 5 , _Snake[_CurSize - 1].x + 5 ,
_Snake[_CurSize - 1].y + 5 );
rectangle ( _Snake[_CurSize - 1].x - 5 , _Snake[_CurSize - 1].y - 5 ,_Snake[_CurSize - 1].x + 5
, _Snake[_CurSize - 1].y + 5 );
//return ;
}
else
{
setcolor ( WHITE );
for ( int i = 0; i< _CurSize; i++ )
{
bar ( _Snake[i].x - 5 , _Snake[i].y - 5 , _Snake[i].x + 5 , _Snake[i].y + 5 );
rectangle ( _Snake[i].x - 5 , _Snake[i].y - 5 , _Snake[i].x + 5 , _Snake[i].y + 5 );
}

/*
//int i = 0;
bar ( _Snake[i].x - 5 , _Snake[i].y - 5 , _Snake[i].x + 5 , _Snake[i].y+ 5 );
rectangle ( _Snake[i].x - 5 , _Snake[i].y - 5 , _Snake[i].x + 5 ,_Snake[i].y );
*/
setfillstyle ( 1 , 0 );
fillellipse ( _Snake[0].x , _Snake[0].y , 2 , 2);

char msg[50];
setcolor ( WHITE );

if ( _player == 'C' )
{
bar ( 250 , 12 , 630 , WinMinY - 10 );
sprintf ( msg , "Com Snake at :- ( %d , %d ) Score:- %d", _Snake[0].x, _Snake[0].y , _Points
);
outtextxy ( 250 , 12 , msg );
}
else
{
bar ( 250 , 1 , 630 , WinMinY - 10 );
sprintf ( msg , "Ur Snake at :- ( %d , %d ) Score:- %d", _Snake[0].x, _Snake[0].y , _Points );
outtextxy ( 250 , 1 , msg );
}
}
}
/****************************************

****************************************/
void Snake :: change_direction ( Direction d)
{
if ( ( _Direction == Forward ) && ( d == Backward ) )
{
Sound ( -1 );
}
else if ( ( _Direction == Backward ) && ( d == Forward ) )
{
Sound ( -1 );
}
else if ( ( _Direction == Upward ) && ( d == Downward ) )
{
Sound ( -1 );
}
else if ( ( _Direction == Downward ) && ( d == Upward ) )
{
Sound ( -1 );
}
else
{
_Direction = d;
Sound ( 1 );
}
}

/******************
/******************/

void Point :: draw ( )


{
char msg[30];
setfillstyle ( 1 , color );
setcolor ( YELLOW );
bar ( x - 4 , y - 4 , x + 4 , y + 4 );
rectangle ( x - 4 , y - 4 , x + 4 , y + 4 );

setfillstyle ( 1 , 0 );
fillellipse ( x , y , 2 , 2 );

bar ( 1 , 1 , 300 , WinMinY - 10 );


sprintf ( msg , "Point at :- ( %d , %d )", x , y );
outtextxy ( 40 , 1 , msg );
}

void Point :: set ( )


{
color = random ( 15 ) + 1;
x = random ( ( ( WinMaxX - WinMinX ) / 10 ) ) ;
y = random ( ( ( WinMaxY - WinMinY ) / 10 ) ) ;
x = ( x * 10 ) + WinMinX;
y = ( y * 10 ) + WinMinY;
draw ( );
}

int point_vanished ( Point &p , Snake &s )


{
if ( ( s._Snake[0].x == p.x ) && ( s._Snake[0].y == p.y ) )
{
s._CurSize++;
if ( s._CurSize == s._MaxSize )
{
return 2;
}
s.increment ();
s.display ( RED );
Sound ( 2 );
delay ( 100 );

s._Points = s._Points + 20 ;
p.set();
return 1;
}
else
{
return -1;
}
}
void Sound ( int s )
{
if ( s == -1 )
{
sound ( 150 );
delay ( 30 );

sound ( 250 );
delay ( 30 );
nosound ();
}
else if ( s == 1 )
{
sound ( 450 );
delay ( 20 );
nosound ();
}
else if ( s == 2 )
{
sound ( 650 );
delay ( 20 );
nosound ();
}

void Snake :: com_play ( Point p1 )


{
if ( p1.getx() < _Snake[0].x )
{
if ( _Direction == Forward )
_Direction = p1.gety() < _Snake[0].y ? Upward : Downward;
else
_Direction = Backward;
}
else if ( p1.getx() > _Snake[0].x )
{
if ( _Direction == Backward )
_Direction = p1.gety() < _Snake[0].y ? Upward : Downward;
else
_Direction = Forward;
}
else
{
if ( p1.gety() < _Snake[0].y )
{
_Direction = Upward;
}
else if ( p1.gety() > _Snake[0].y )
{
_Direction = Downward;
}
}
}

void Message_Display ( char msg[30] , char color )


{
settextstyle ( 1 , 0 , 5 );
setcolor ( 8 );
outtextxy ( 270 , 205 , msg);

settextstyle ( 1 , 0 , 5 );
setcolor ( color );
outtextxy ( 275 , 200 , msg);
delay ( 1000 );
}

int menu ()
{
int ch;
int selected = 1;
int TotalOptions = 3;

cleardevice();
setbkcolor ( BLUE );
show_Header();
signature();

drawMenu ( selected , RED , GREEN );


do
{
ch = getch();
if ( ch == DOWN_ARROW )
{
selected = selected >= TotalOptions ? 1 : selected + 1;
drawMenu ( selected , RED , GREEN );
}
else if ( ch == UP_ARROW )
{
selected = selected < 2 ? TotalOptions : selected - 1;
drawMenu ( selected , RED , GREEN );
}

}while ( ch != ' ' );

return selected;
}

void drawMenu ( int selected , int defCol , int selCol )


{
int x = 250;
int y = 100;
int width = 150;
int height = 30;
int i;
int TotalOptions = 3;
char menu_option[3][14]= {
" PLAY ",
" HOW TO PLAY ",
" EXIT "
};
setcolor ( WHITE );

for ( i = 1; i <= TotalOptions; i++ )


{
if ( i == selected )
setfillstyle ( 1 , selCol );
else
setfillstyle ( 1 , defCol );
bar ( x , y , x + width , y + height );
rectangle ( x , y , x + width , y + height );
outtextxy ( x + 20 , y + 10 , menu_option[i - 1] );
y = y + height + 30;
}
}

void show_HowTOPlay()
{
cleardevice();
setbkcolor ( BLACK );
show_Header();
settextstyle ( 0 , 0 , 0 );
setcolor ( WHITE );
outtextxy ( 20 , 100 , "Objective:" );
outtextxy ( 20 , 150 , "Playing:" );
outtextxy ( 20 , 220 , "Tip:" );

setcolor ( LIGHTGREEN );
outtextxy ( 120 , 120 , "To collect 50 boxes before the computer Snake." );
outtextxy ( 120 , 170 , "1. Use arrow keys to control your Snake." );
outtextxy ( 120 , 180 , "2. To collect the box just come near to the BOX." );
outtextxy ( 120 , 190 , "3. Press <ESC> to QUIT any time." );
outtextxy ( 120 , 240 , "1. Use shortcuts to collect the BOX.");
outtextxy ( 120 , 250 , "P.S The computer never uses shortcuts" );
outtextxy ( 120 , 260 , "2. Computer Snake can't Hurt you, so enjoy moving around." );

signature();

getch();
}

void signature()
{
setcolor ( BROWN );
settextstyle ( 0 , 0 , 0 );
outtextxy ( 350 , 400 , " Programmers: " );

setcolor ( YELLOW );
outtextxy ( 450 , 400 , " Devesh & Arjhuna " );
}

void show_Header()
{
setcolor ( RED );
settextstyle ( 1 , 0 , 4 );
outtextxy ( 270 , 27 , " SNAKE WAR - I " );
setcolor ( YELLOW );
outtextxy ( 270 , 25 , " SNAKE WAR - I " );
}

void Play()
{
Snake s1 ( MAX , GREEN , 'M' );
Snake s2 ( MAX , MAGENTA , 'C' );
char ch , KeyPressed = 0;

cleardevice();
randomize ();

rectangle ( WinMinX - 7, WinMinY - 7, WinMaxX + 7 , WinMaxY + 7 );


Point p1;

setbkcolor ( BLUE );
s1.inc_disp();
s2.inc_disp();

setcolor ( YELLOW );
outtextxy ( 10 , 450 , "> Collect 50 Boxes to WIN. > Use shortcuts to WIN.");
setcolor ( CYAN );
outtextxy ( 10 , 460 , "> Use <ESC> to QUIT anytime. > LEFT , RIGHT , UP , DOWN
Arrow Keys to Play. ");
getch();

KeyPressed = 1;
ch = 'R';
while ( 1 )
{
while ( !kbhit() )
{
s1.inc_disp();
if ( point_vanished ( p1 , s1 ) == 2 )
{
Message_Display ( "YOU WIN " , GREEN );
ch=0x1b;
getch();
break;
}

s2.com_play ( p1 );
s2.inc_disp();
if ( point_vanished ( p1 , s2 ) == 2 )
{
Message_Display ( "YOU LOSE " , GREEN );
ch=0x1b;
getch();
break;
}
delay ( 100 );
if ( KeyPressed == 1 )KeyPressed = 0;
}
if ( ch == 0x1b )
break;

ch = getch();
if ( KeyPressed == 1 )
{
KeyPressed = 0;
continue;
}
if ( ch == 0x1b )
break;
else if ( ch == 0 )
{
ch = getch ();
if ( ch == UP_ARROW )
{
s1.change_direction ( Upward );
KeyPressed = 1;
}
else if ( ch == DOWN_ARROW )
{
s1.change_direction ( Downward );
KeyPressed = 1;
}
else if ( ch == LEFT_ARROW )
{
s1.change_direction ( Backward );
KeyPressed = 1;
}
else if ( ch == RIGHT_ARROW )
{
s1.change_direction ( Forward );
KeyPressed = 1;
}
}
}
}
/*ludo*/
/* 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 winner(void);
void about(void);
void howtoplay(void);
void redrawing(int );
int catchclickonludobutton(void) ;
int forcomputer1(int);
int forcomputer2(int);
int forcomputer3(int);

struct ludo{
int x[45];
int y[45];
int pos;
int color;

void (*ptr_ludo1)(int,int*,int*,ludo *);


int (*ptr_check_if_ludo_move_requested_is_valid)(int ,ludo*);
void (*ptr_move_ludo_to_new_position)( int ,ludo*);

};
struct ludo lp[4],lc1[4],lc2[4],lc3[4];

/* ludo1 initialises the ludo class objects */


void ludo1(int col,int *ax,int *ay,ludo *node)
{
int i;

for(i=0;i<=44;i++)
{
node->x[i]=*(ax);
node->y[i]=*(ay);
ax++;ay++;
}
node->color=col;
node->pos=0;
}//end of ludo1

int check_if_ludo_move_requested_is_valid(int dice,ludo *node)


{
/*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( (node->pos<=44-dice && node->pos!=0) || (node->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 ,ludo *node)


{
HideMouse();

if(node->pos==0 && dice==6)//taking out of house on dice givig 6


{ messageclear();dice=1;}

dice=dice-1;
int opos;
opos=node->pos;
int i;
for(i=opos;i<=dice+opos;i++)
{
setcolor(7);
circle(node->x[i],node->y[i],8);
setfillstyle(1,7);
floodfill(node->x[i],node->y[i],7);
delay(50);
{node->pos++; redrawing(node->color);}
setcolor(node->color);
circle(node->x[i+1],node->y[i+1],8);
setfillstyle(1,node->color);
floodfill(node->x[i+1],node->y[i+1],node->color);
delay(50);
}

/*changing pos to 0 if ludo has been cut before redrawing all ludos*/
for(i=0;i<=3;i++)
{
if(node->x[node->pos]==lp[i].x[lp[i].pos] &&
node->y[node->pos]==lp[i].y[lp[i].pos] )
{
if(node->color!=lp[i].color)
lp[i].pos=0;
}
if(node->x[node->pos]==lc1[i].x[lc1[i].pos] &&
node->y[node->pos]==lc1[i].y[lc1[i].pos] )
{
if(node->color!=lc1[i].color)
lc1[i].pos=0;
}
if(node->x[node->pos]==lc2[i].x[lc2[i].pos] &&
node->y[node->pos]==lc2[i].y[lc2[i].pos] )
{
if(node->color!=lc2[i].color)
lc2[i].pos=0;
}
if(node->x[node->pos]==lc3[i].x[lc3[i].pos] &&
node->y[node->pos]==lc3[i].y[lc3[i].pos] )
{
if(node->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(node->pos==44 && flagturn==0)
number_of_player_ludo_home++;
if(node->pos==44 && flagturn==1)
number_of_computer1_ludo_home++;
if(node->pos==44 && flagturn==2)
number_of_computer2_ludo_home++;
if(node->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

//struct ludo lp[4],lc1[4],lc2[4],lc3[4];


void changingcpptoc(void)
{
//struct ludo lp[4],lc1[4],lc2[4],lc3[4];
int zx;
for( zx=0;zx<4;zx++)
{
lp[zx].ptr_ludo1=ludo1;
lc1[zx].ptr_ludo1=ludo1;
lc2[zx].ptr_ludo1=ludo1;
lc3[zx].ptr_ludo1=ludo1;

lp[zx].ptr_check_if_ludo_move_requested_is_valid=check_if_ludo_move_requested_is_vali
d;

lc1[zx].ptr_check_if_ludo_move_requested_is_valid=check_if_ludo_move_requested_is_val
id;;

lc2[zx].ptr_check_if_ludo_move_requested_is_valid=check_if_ludo_move_requested_is_val
id;;

lc3[zx].ptr_check_if_ludo_move_requested_is_valid=check_if_ludo_move_requested_is_val
id;;

lp[zx].ptr_move_ludo_to_new_position=move_ludo_to_new_position;
lc1[zx].ptr_move_ludo_to_new_position=move_ludo_to_new_position;
lc2[zx].ptr_move_ludo_to_new_position=move_ludo_to_new_position;
lc3[zx].ptr_move_ludo_to_new_position=move_ludo_to_new_position;

//int (*ptr_check_if_ludo_move_requested_is_valid)(int );
//void (*ptr_move_ludo_to_new_position)( int );

}//for zx loop ends


}
int ludo()
{
changingcpptoc();
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:
{return 0;}
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].ptr_check_if_ludo_move_requested_is_valid(dice,&lp[tmp1]);

switch(tmp2)
{
case 1:
{
lp[tmp1].ptr_move_ludo_to_new_position(dice,&lp[tmp1]);
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].ptr_move_ludo_to_new_position(dice,&lc1[tmp1]);
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].ptr_move_ludo_to_new_position(dice,&lc2[tmp1]);
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].ptr_move_ludo_to_new_position(dice,&lc3[tmp1]);
else
messageout("computer3 has to skip this time.");
}
flagturn=0;
messageclear();
dice=0;
}

dice=0;
}//end of while
return 0;
}//end of ludo
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;
//if(y==320)
// 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].ptr_ludo1(RED,arx,ary,&lp[tmp]);break;}
case 1:
{lc1[tmp].ptr_ludo1(RED,arx,ary,&lc1[tmp]);break;}
case 2:
{lc2[tmp].ptr_ludo1(RED,arx,ary,&lc2[tmp]);break;}
case 3:
{lc3[tmp].ptr_ludo1(RED,arx,ary,&lc3[tmp]);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].ptr_ludo1(BLUE,arx,ary,&lp[tmp]);break;}
case 1:
{lc1[tmp].ptr_ludo1(BLUE,arx,ary,&lc1[tmp]);break;}
case 2:
{lc2[tmp].ptr_ludo1(BLUE,arx,ary,&lc2[tmp]);break;}
case 3:
{lc3[tmp].ptr_ludo1(BLUE,arx,ary,&lc3[tmp]);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].ptr_ludo1(GREEN,arx,ary,&lp[tmp]);break;}
case 1:
{lc1[tmp].ptr_ludo1(GREEN,arx,ary,&lc1[tmp]);break;}
case 2:
{lc2[tmp].ptr_ludo1(GREEN,arx,ary,&lc2[tmp]);break;}
case 3:
{lc3[tmp].ptr_ludo1(GREEN,arx,ary,&lc3[tmp]);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].ptr_ludo1(YELLOW,arx,ary,&lp[tmp]);break;}
case 1:
{lc1[tmp].ptr_ludo1(YELLOW,arx,ary,&lc1[tmp]);break;}
case 2:
{lc2[tmp].ptr_ludo1(YELLOW,arx,ary,&lc2[tmp]);break;}
case 3:
{lc3[tmp].ptr_ludo1(YELLOW,arx,ary,&lc3[tmp]);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,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);return 0;}
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,"Devesh & Arjhuna");


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 ;
}
int 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,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,310,"PRESS ANY KEY TO EXIT..");
}
getch();
return 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,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 ;
}

/*Tic_Tac_Toe*/
typedef struct TicTacToe
{
int blockNo;
struct TicTacToe *next;
}Alist;

void draw(void);
int ticplay(void);
void compVSplayer(Alist *[]);
void playerVSplayer(Alist *[]);
void make_list(Alist **);
void printplayer(int );
void printcomp(int );
int compwin(Alist ** ,int []);
void rules(void);
void help(void);
int ticmenu();
void drawticMenu(int,int,int);
void tic_Header();
void drawticplayMenu(int,int,int);
int ticplaymenu();

char name[10];
char name1[10];

int tic_tac_toe()
{
int selected_option;

Start:
selected_option = ticmenu();

switch ( selected_option )
{
case 1:
rules();
goto Start;
case 2:
ticplay();
goto Start;
case 3:
help();
goto Start;
case 4:
return 1;
}
return 1;

int ticmenu ()
{
int ch;
int selected = 1;
int TotalOptions = 4;
cleardevice();
setbkcolor ( BLUE );
tic_Header();
signature();
drawticMenu ( selected , RED , GREEN );
do
{
ch = getch();
if ( ch == DOWN_ARROW )
{
selected = selected >= TotalOptions ? 1 : selected + 1;
drawticMenu ( selected , RED , GREEN );
}
else if ( ch == UP_ARROW )
{
selected = selected < 2 ? TotalOptions : selected - 1;
drawticMenu ( selected , RED , GREEN );
}

}while ( ch != ' ' );

return selected;
}

void drawticMenu ( int selected , int defCol , int selCol )


{
int x = 250;
int y = 100;
int width = 150;
int height = 30;
int i;
int TotalOptions = 4;
char menu_option[4][14]= {
" Rules ",
" Play ",
" Help ",
" QUIT "
};
setcolor ( WHITE );

for ( i = 1; i <= TotalOptions; i++ )


{
if ( i == selected )
setfillstyle ( 1 , selCol );
else
setfillstyle ( 1 , defCol );
bar ( x , y , x + width , y + height );
rectangle ( x , y , x + width , y + height );
outtextxy ( x + 20 , y + 10 , menu_option[i - 1] );
y = y + height + 30;
}
}
void tic_Header()
{
setcolor ( RED );
settextstyle ( 1 , 0 , 4 );
outtextxy ( 270 , 27 , " TIC-TAC-TOE " );
setcolor ( YELLOW );
outtextxy ( 270 , 25 , " TIC-TAC-TOE " );
}

int ticplay(void)
{

int selected_option;
Alist *GRAPH[10];
make_list(GRAPH);
Start:
selected_option = ticplaymenu();

switch ( selected_option )
{
case 1:
cleardevice();
cout<<"\nEnter your Name : ";
cin>>name;
name[0] = toupper(name[0]);
draw();
gotoxy(5,1);
cout<<"ALL THE BEST "<<name;
cout<<"\n\t Plz play First ";
compVSplayer(GRAPH);
break;

case 2:
cleardevice();
cout<<"\nEnter First player Name : ";
cin>>name;
name[0] = toupper(name[0]);
cout<<"\nEnter Second Player Name : ";
cin>>name1;
name1[0] = toupper(name1[0]);
draw();
gotoxy(5,1);
cout<<"\n\nALL THE BEST TO BOTH OF YOU ";
cout<<"\n\t "<<name<<" Plz play First ";
playerVSplayer(GRAPH);
break;

case 3:
return 1;
}
return 1;

int ticplaymenu ()
{
int ch;
int selected = 1;
int TotalOptions = 3;

cleardevice();
setbkcolor ( BLUE );
tic_Header();
signature();
drawticplayMenu ( selected , RED , GREEN );
do
{
ch = getch();
if ( ch == DOWN_ARROW )
{
selected = selected >= TotalOptions ? 1 : selected + 1;
drawticplayMenu ( selected , RED , GREEN );
}
else if ( ch == UP_ARROW )
{
selected = selected < 2 ? TotalOptions : selected - 1;
drawticplayMenu ( selected , RED , GREEN );
}
}while ( ch != ' ' );

return selected;
}

void drawticplayMenu ( int selected , int defCol , int selCol )


{
int x = 250;
int y = 100;
int width = 175;
int height = 30;
int i;
int TotalOptions = 3;
char menu_option[3][21]= {
"COMP Vs PLAYER ",
"PLAYER Vs PLAYER ",
"BACK TO MAIN MENU "
};
setcolor ( WHITE );

for ( i = 1; i <= TotalOptions; i++ )


{
if ( i == selected )
setfillstyle ( 1 , selCol );
else
setfillstyle ( 1 , defCol );
bar ( x , y , x + width , y + height );
rectangle ( x , y , x + width , y + height );
outtextxy ( x + 20 , y + 10 , menu_option[i - 1] );
y = y + height + 30;
}
}

/*****************************************************
void compVSplayer() function to play Game
comp VS player main logic function

CALL BY: play();


CALL'S : printplayer(),printcomp(),compwin();
*****************************************************/

void compVSplayer(Alist *GRAPH[])


{
Alist * node;
int i,flag[10],Num,block1,block2,pwin=0,compNo=0,cwin=0;

Alist *ptr=GRAPH[5];
// TEMPORARY DE-BUGGING STATEMENTS
// DISPLAY LINK LIST
/* for(i=1;i<10;i++)
{
cout<<"\n\n";
node=GRAPH[i];
for(;node!=NULL;node=node->next)
{
cout<<node->blockNo<<",";
}
}
getch(); */
/*
FLAG ARRAY TO INDICATE WHO VISITED THE BLOCK
0 : NO ONE
1 : PLAYER
2 : COMP
*/
for(i=0;i<10;i++)
{
flag[i]=0; //SET ALL FLAG'S TO ZERO
}
gotoxy(1,20);
cout<<"Enter Block No :";

for(i=1;i<5;i++) //PLYER HAS TO ENTER 4 NO.


{
gotoxy(20,20);
cout<<" ";
gotoxy(20,20);
cin>>Num;
if(flag[Num]==0 && Num > 0 && Num < 10 ) // CHECK FOR
VACANCY
{
flag[Num] = 1;
printplayer(Num);

/*
WHEN PLAYER SELECT ANY NO THEN HIS WINING
CHANGES ARE MORE IN IT's
ADJACENCY LIST HENCE CHECK PLAYERS WINING
CONDITION IN THE SAME LIST
here let list plyar select no 7 then :-
we have 7's list 5,3,4,1,8,9
so camper flag[5] & flag[3] with 1
if they are equle then player win
else select next two no 4 & 1 and so on

*/
for(node = GRAPH[Num];node!=NULL;node=node->next->next)
{
block1 = node->blockNo;
block2 = node->next->blockNo;
if(flag[block1]==1&&flag[block2]==1)
{
gotoxy(20,22);
cout<<" "<<name<<" You win the Game !";
pwin=1;
break;
}

/*
IF PLAYER HAS VISITED BLOCK1 THEN TO BREAK
HIS PATH PUT COMP's
NO ON BLOCK2 AND VISE VARSA
*/
if(flag[block1]==1 && flag[block2]==0)
{
compNo = block2;
}
if(flag[block2]==1 && flag[block1]==0)
{
compNo = block1;
}
}

if(pwin==1) //IF PLAREY WIN THEN BREAK


break;

/*
AS 5 IS CENTRAL BLOCK AND MAX. PAYTH ARE GOING
THROUE IT SO
IF PLARER HAS NOT VISITED IT THEN VISIT IT FIRST
*/
if(flag[5]==0)
{
printcomp(5);
flag[5] = 2;
}
else
{
//CHEACK FOR COMP WIN

cwin = compwin(GRAPH,flag);

if(cwin == 1) //IF COMP WIN THEN BREAK


break;

/*
IF PLAYER & COMP ARE NOT IN A POSITION TO WIN
THE GAME THEN
COMP HAS TO PLAY ANY NON VISITED BLOCK
*/
if(compNo==0)
{
while(ptr)
{
if(flag[ptr->blockNo] == 0)
{
compNo = ptr->blockNo;
break;
}
ptr = ptr->next;
}
}

printcomp(compNo);
flag[compNo] = 2;
}
compNo = 0; //RESET COMP NO.

}
/*
IF USER TRY TO REPET THE BLOCK THEN DISPLAY ERROR
MASSEGE
*/
else
{
gotoxy(10,22);

cout<<"Sorry ! "<<name;
cout<<" you are trying to overlod the block ! or Invalid Input";
getch();
gotoxy(10,22);
cout<<" ";
--i;
Num = 0;
}
}

if(i==5 && cwin == 0 && pwin == 0)


{
gotoxy(20,22);
cout<<" Draw Match ! Try Again !";
}
getch();
}

/*****************************************************
int compwin() function to check comp wining condition

FUNCTION: scan throue all graph and check for comp's


wining condition
CALL BY: play()
CALL'S : printcomp();
*****************************************************/

int compwin(Alist **GRAPH ,int flag[])


{
int i,cwin=0,block1,block2;
Alist *node;
for(i=1;i<10;i++)
{
if(flag[i] == 2) //if starting flag is not 2 then there is
{ //no wining chance for comp.
for(node = GRAPH[i]; node!=NULL ; node = node->next->next)
{
block1 = node->blockNo;
block2 = node->next->blockNo;
/*
IF COMP HAS VISITED iTH & ETHER OF BLOCK1 ||
BLOCK 2 & ONE OF THEM
IS NON VISITED THEN COMP CAN WIN BY VISITING
NON VISITED BLOCK
*/
if((flag[block1]== 2 && flag[block2] ==0)||((flag[block2] ==
2)&&flag[block1] == 0))
{
if(flag[block1] == 2)
printcomp(block2);
else
printcomp(block1);

settextstyle( 4 , HORIZ_DIR, 2);


outtextxy(100,400," COMP win's the Game !");
cwin=1;
break;
}
if(cwin == 1)
break;
}
}
}
return cwin;
}
/*****************************************************
void make_list() function to creat adjucency list

CALL BY: play()


CALL'S : NON;
*****************************************************/

void make_list(Alist ** GRAPH)


{
int i,j;
Alist *node;
/*
THIS ARE THE PATH PASSING THROUE EACH BLOCK
A1[0] is the no of node in the list......
*/
int A1[] = {6,5,9,2,3,4,7};
int A2[] = {4,5,8,1,3};
int A3[] = {6,5,7,1,2,6,9};
int A4[] = {4,5,6,1,7};
int A5[] = {8,2,8,4,6,1,9,3,7};
int A6[] = {4,5,4,3,9};
int A7[] = {6,5,3,4,1,8,9};
int A8[] = {4,5,2,7,9};
int A9[] = {6,5,1,6,3,8,7};

for(i=0;i<10;i++) // MAKE ALL ELEMENT OF GRAPH ARRAY TO NULL


{
GRAPH[i] = NULL;
}
// SELECT ONE BY ONE STATIC ARRAY AND CREAT IT's LIST

for(i=A1[0];i>=1;i--)
{
node = new Alist;
node ->blockNo = A1[i];
node->next = GRAPH[1];
GRAPH[1] = node;
}

for(i=A2[0];i>=1;i--)
{
node = new Alist;
node ->blockNo = A2[i];
node->next = GRAPH[2];
GRAPH[2] = node;
}
for(i=A3[0];i>=1;i--)
{
node = new Alist;
node ->blockNo = A3[i];
node->next = GRAPH[3];
GRAPH[3] = node;
}
for(i=A4[0];i>=1;i--)
{
node = new Alist;
node ->blockNo = A4[i];
node->next = GRAPH[4];
GRAPH[4] = node;
}
for(i=A5[0];i>=1;i--)
{
node = new Alist;
node ->blockNo = A5[i];
node->next = GRAPH[5];
GRAPH[5] = node;
}
for(i=A6[0];i>=1;i--)
{
node = new Alist;
node ->blockNo = A6[i];
node->next = GRAPH[6];
GRAPH[6] = node;
}
for(i=A7[0];i>=1;i--)
{
node = new Alist;
node ->blockNo = A7[i];
node->next = GRAPH[7];
GRAPH[7] = node;
}
for(i=A8[0];i>=1;i--)
{
node = new Alist;
node ->blockNo = A8[i];
node->next = GRAPH[8];
GRAPH[8] = node;
}
for(i=A9[0];i>=1;i--)
{
node = new Alist;
node ->blockNo = A9[i];
node->next = GRAPH[9];
GRAPH[9] = node;
}

/*****************************************************
void playerVSplayer() function to play Game
player VS player main logic function

CALL BY: play();


CALL'S : printplayer(),printcomp();
*****************************************************/

void playerVSplayer(Alist *GRAPH[])


{
int i,Num,p1win = 0 ,p2win =0 , flag[10];
int block1,block2,cr = 0;
Alist *node;

/*
FLAG ARRAY TO INDICATE WHO VISITED THE BLOCK
0 : NO ONE
1 : PLAYER1
2 : PLAYER2
*/

for(i=0;i<10;i++)
flag[i] = 0; //SET ALL FLAG TO ZERO

gotoxy(1,20);
cout<<"Enter Block No :";
for(i=0;i<9;)
{
gotoxy(20,20);
cout<<" ";
gotoxy(20,20);
cin>>Num;

if(flag[Num]==0 && Num > 0 && Num < 10 ) // CHECK FOR


VACANCY
{
++i;
flag[Num] = 1;
printplayer(Num);

/*
WHEN PLAYER SELECT ANY NO THEN HIS WINING
CHANGES ARE MORE IN IT's
ADJACENCY LIST HENCE CHECK PLAYERS WINING
CONDITION IN THE SAME LIST
here: let list plyar select no 7 then :-
we have 7's list 5,3,4,1,8,9
so camper flag[5] & flag[3] with 1
if they are equle then player win
else select next two no 4 & 1 and so on

*/
//FOR PLAYER name
for(node = GRAPH[Num];node!=NULL;node=node->next->next)
{
block1 = node->blockNo;
block2 = node->next->blockNo;
if(flag[block1]==1&&flag[block2]==1)
{
gotoxy(20,22);
cout<<"\""<<name<<"\"You win the Game !";
p1win=1;
break;
}
}
if(p1win == 1)
break;
if(i == 9)
break;
do
{
gotoxy(20,20);
cout<<" ";
gotoxy(20,20);
cin>>Num;

if(flag[Num]==0 && Num > 0 && Num < 10 ) // CHECK


FOR VACANCY
{
++i;
flag[Num] = 2;
printcomp(Num);
cr = 1; //if user enters carect no then set cr
//FOR PLAYER name1
for(node = GRAPH[Num];node!=NULL;node=node-
>next->next)
{
block1 = node->blockNo;
block2 = node->next->blockNo;
if(flag[block1]==2 && flag[block2]==2)
{
gotoxy(20,22);
cout<<"\""<<name1<<"\"You win the
Game !";
p2win=1;
break;
}
}
}
else
{
gotoxy(10,22);

cout<<"Sorry ! "<<name1;
cout<<" you are trying to overlod the block ! or Invalid
Input";
getch();
gotoxy(10,22);
cout<<"
";
cr = 0; //if user enters incarect no then Reset cr
Num = 0;
}
}while(cr == 0); //repet till player 2 enters carect No.
if(p2win == 1)
break;
}
else
{
gotoxy(10,22);

cout<<"Sorry ! "<<name;
cout<<" you are trying to overlod the block ! or Invalid Input";
getch();
gotoxy(10,22);
cout<<" ";
Num = 0;
}
}
if(i == 9 && p1win == 0 && p2win == 0) //check for Draw match
{
gotoxy(20,22);
cout<<" Draw Match ! Try Again !";
}
getch();
}
/*************************************************************
void draw() to Draw the Game box
by seting cursor position and printing = sigen

CALL BY: main();


*************************************************************/
void draw(void)
{
int i,j; //Looping variables
int num=1; //Number displaying function
char h=205; //Horizontal lines
char v=186; //Vertical lines
char c=206; //center symbol like +
cleardevice();
for(i=6;i<12;i+=5)
{
gotoxy(25,i+3);
for(j=0;j<=30;j++)
cout<<h;
}
for(j=10;j<=20;j+=10)
{
for(i=2;i<17;i++)
{
gotoxy(25+j,i+3);
cout<<v;
}
}
for(i=35;i<=45;i+=10)
for(j=9;j<=14;j+=5)
{
gotoxy(i,j);
cout<<c;
}
for(j=0;j<=10;j+=5)
for(i=0;i<=20;i+=10)
{
gotoxy(34+i,8+j);
cout<<num++;
}
}
/*****************************************************
void printplayer() function to display the player's
visited block

CALL BY: play()


CALL'S : NON;
*****************************************************/

void printplayer(int place)


{
if(place==1)
{
gotoxy(30,6);
cout<<"X";
gotoxy(10,20);
}
else if(place==2)
{
gotoxy(40,6);
cout<<"X";
gotoxy(10,20);
}
else if(place==3)
{
gotoxy(50,6);
cout<<"X";
gotoxy(10,20);
}
else if(place==4)
{
gotoxy(30,11);
cout<<"X";
gotoxy(10,20);
}
else if(place==5)
{
gotoxy(40,11);
cout<<"X";
gotoxy(10,20);
}
else if(place==6)
{
gotoxy(50,11);
cout<<"X";
gotoxy(10,20);
}
else if(place==7)
{
gotoxy(30,16);
cout<<"X";
gotoxy(10,20);
}
else if(place==8)
{
gotoxy(40,16);
cout<<"X";
gotoxy(10,20);
}
else if(place==9)
{
gotoxy(50,16);
cout<<"X";
gotoxy(10,20);
}
}

/*****************************************************
void printcomp() function to display the comp's
visited block

CALL BY: play()


CALL'S : NON;
*****************************************************/

void printcomp(int place)


{
if(place==1)
{
gotoxy(30,6);
cout<<"O";
gotoxy(10,20);
}
else if(place==2)
{
gotoxy(40,6);
cout<<"O";
gotoxy(10,20);
}
else if(place==3)
{
gotoxy(50,6);
cout<<"O";
gotoxy(10,20);
}
else if(place==4)
{
gotoxy(30,11);
cout<<"O";
gotoxy(10,20);
}
else if(place==5)
{
gotoxy(40,11);
cout<<"O";
gotoxy(10,20);
}
else if(place==6)
{
gotoxy(50,11);
cout<<"O";
gotoxy(10,20);
}
else if(place==7)
{
gotoxy(30,16);
cout<<"O";
gotoxy(10,20);
}
else if(place==8)
{
gotoxy(40,16);
cout<<"O";
gotoxy(10,20);
}
else if(place==9)
{
gotoxy(50,16);
cout<<"O";
gotoxy(10,20);
}
}
/*************************************************************
void rules() to show the rules of the Game

CALL BY: main();


*************************************************************/

void rules(void)
{cleardevice();
outtextxy(0,25,"The rules of the game are as follows:-");
outtextxy(0,35,"Press the number indicated near the playing box ");
outtextxy(0,45,"to put the symbol X in the playing box.");
outtextxy(0,55,"Do not press a number in which a symbol such as");
outtextxy(0,65,"'X' or 'O' already exist. If you do so the");
outtextxy(0,75,"program will give error due to illegal inputs");
outtextxy(0,85,"Your aim is to get a line containing 3 'X'");
outtextxy(0,95,"either horizontally or vertically or diagonally");
outtextxy(0,105,"with alternative inputs by you and computer");
settextstyle( 5 , HORIZ_DIR, 2);
outtextxy(0,145,"press any key to continue...........");
getch();
}
/*************************************************************
void help() to show the help for the Game

CALL BY: main();


*************************************************************/

void help(void)
{cleardevice();
outtextxy(0,25,"As the rule of the Game get a line containing 3 'X'");
outtextxy(0,35,"either hrizontally or vertically or diagonally");
outtextxy(0,45,"with alternative inputs by you and computer.");
outtextxy(0,55,"Always try to occupy the middle box and");
outtextxy(0,65,"then the corners for confusing the computer");
settextstyle( 5 , HORIZ_DIR, 2);
outtextxy(0,105,"Press any key to continue..........");
getch();
}
/*SNAKES AND LADDERS*/

int k=1, i, user=0, dice=0, x1=50, y1=410, x2=70, y2=410, dir1=0,


dir2=0,
ch;
int cnt1=1, cnt2=1;
void *obj1, *obj2, *o1, *o2, *dot, *back, *turn, *ready;
unsigned int size;

void ladder1()
{
int m,n;
for(m=0;m<=250;m+=250)
for(n=0;n<=m;n+=250)
{
setcolor(DARKGRAY);
line(53+m,57+n,55+m,55+n);
line(53+m,57+n,133+m,137+n);
line(55+m,55+n,135+m,135+n);
line(133+m,137+n,135+m,135+n);
setfillstyle(SOLID_FILL, YELLOW);
floodfill(55+m,58+n,DARKGRAY);
line(68+m,42+n,70+m,40+n);
line(68+m,42+n,148+m,122+n);
line(70+m,40+n,150+m,120+n);
line(148+m,122+n,150+m,120+n);
floodfill(70+m,43+n,DARKGRAY);
line(65+m,65+n,78+m,52+n);
line(68+m,68+n,81+m,55+n);
floodfill(79+m,54+n,DARKGRAY);
line(75+m,75+n,88+m,62+n);
line(78+m,78+n,91+m,65+n);
floodfill(89+m,64+n,DARKGRAY);
line(85+m,85+n,98+m,72+n);
line(88+m,88+n,101+m,75+n);
floodfill(99+m,74+n,DARKGRAY);
line(95+m,95+n,108+m,82+n);
line(98+m,98+n,111+m,85+n);
floodfill(109+m,84+n,DARKGRAY);
line(105+m,105+n,118+m,92+n);
line(108+m,108+n,121+m,95+n);
floodfill(119+m,94+n,DARKGRAY);
line(115+m,115+n,128+m,102+n);
line(118+m,118+n,131+m,105+n);
floodfill(129+m,104+n,DARKGRAY);
line(125+m,125+n,138+m,112+n);
line(128+m,128+n,141+m,115+n);
floodfill(139+m,114+n,DARKGRAY);
}
}

void ladder2()
{
int p,q=0;
for(p=0;p<=180;p+=155)
{
line(100+p,330-q,140+p,290-q);
line(100+p,330-q,102+p,332-q);
line(102+p,332-q,142+p,292-q);
line(142+p,292-q,140+p,290-q);
floodfill(141+p,292-q,8);
line(115+p,345-q,155+p,305-q);
line(115+p,345-q,117+p,347-q);
line(117+p,347-q,157+p,307-q);
line(157+p,307-q,155+p,305-q);
floodfill(155+p,307-q,8);
line(112+p,322-q,125+p,335-q);
line(114+p,320-q,127+p,333-q);
floodfill(125+p,334-q,8);
line(122+p,312-q,135+p,325-q);
line(124+p,310-q,137+p,323-q);
floodfill(135+p,324-q,8);
line(132+p,302-q,145+p,315-q);
line(134+p,300-q,147+p,313-q);
floodfill(145+p,314-q,8);
q+=95;
}
}

void snake1()
{
int x,y=0,h=2;
for(x=0;x<=200;x+=125)
{
arc(120+x,110+y,10,85,60);
arc(258+x,85+y,190,240,80);
arc(112+x,118+y,10,80,60);
arc(250+x,93+y,190,290,80);
arc(275+x,100+y,250,270,70);
line(250+x,170+y,250+x,165+y);
line(250+x,165+y,230+x,160+y);
line(230+x,160+y,218+x,155+y);
line(130+x,50+y,115+x,47+y);
line(121+x,59+y,106+x,52+y);
line(106+x,52+y,114+x,48+y);
circle(114+x,52+y,1);
setfillstyle(1,h);
floodfill(116+x,52+y,8);
y+=230; h+=8;
}
}

void snake2()
{
arc(130,220,150,180,40);
arc(130,220,180,253,40);
arc(105,328,273,80,70);
arc(143,220,150,180,40);
arc(143,215,180,230,40);
arc(112,328,265,50,75);
arc(80,354,45,72,115);
line(102,400,104,402);
line(102,400,107,399);
line(95,200,110,185);
line(110,185,109,200);
line(110,185,111,182);
circle(104,198,1);
setfillstyle(1,12);
floodfill(103,199,8);
}

void snake3()
{
arc(255,118,320,0,170);
arc(265,118,305,0,170);
line(384,229,361,260);
line(425,120,429,105);
line(428,105,435,120);
line(428,105,429,100);
circle(430,115,1);
setfillstyle(1,6);
floodfill(430,117,8);
}

void numbering()
{
outtextxy(50,393,"1 2 4 5 6 7 8 10");
outtextxy(50,353,"20 19 18 16 15 1 13 11");
outtextxy(50,313,"21 22 25 26 29 30");
outtextxy(50,273,"40 39 38 37 36 35 34 33 32 31");
outtextxy(50,233,"41 43 44 45 47 48 50");
outtextxy(50,193,"60 58 57 56 55 53 52 ");
outtextxy(50,153,"61 62 63 64 66 67 68 69 ");
outtextxy(50,113,"80 79 76 75 74 73 71");
outtextxy(50,73, "81 83 84 85 86 87 89 90");
outtextxy(50,33, "100 99 98 97 96 95 94 93 92 91");
setcolor(15);

outtextxy(465,50,"The Classic Game of");


settextstyle(GOTHIC_FONT, HORIZ_DIR, 2);
setcolor(LIGHTRED);
outtextxy(470,60,"Snake & Ladder");
setcolor(WHITE);
settextstyle(DEFAULT_FONT, HORIZ_DIR, 1);
}

void status()
{
setcolor(YELLOW);
outtextxy(480, 95, "Dice output...");
setlinestyle(SOLID_LINE, 1, 3);
rectangle(480, 110, 600, 230);
outtextxy(480, 260, "Turn...");
rectangle(480, 275, 600, 300);
}

void welcome()
{
float octave[]={130.81, 146.83, 164.81, 174.61, 196, 220, 246.94}, n;
for (int i=0; i<50; i++)
{ n=random(6); sound(octave[n]*4); delay(150); nosound(); }
}

void dispdice()
{
switch (dice)
{
case 1: putimage(535, 165, dot, COPY_PUT); break;
case 2: putimage(515, 145, dot, COPY_PUT);
putimage(555, 185, dot, COPY_PUT); break;
case 3: putimage(515, 145, dot, COPY_PUT);
putimage(535, 165, dot, COPY_PUT);
putimage(555, 185, dot, COPY_PUT); break;
case 4: putimage(515, 145, dot, COPY_PUT);
putimage(555, 145, dot, COPY_PUT);
putimage(515, 185, dot, COPY_PUT);
putimage(555, 185, dot, COPY_PUT); break;
case 5: putimage(515, 145, dot, COPY_PUT);
putimage(555, 145, dot, COPY_PUT);
putimage(535, 165, dot, COPY_PUT);
putimage(515, 185, dot, COPY_PUT);
putimage(555, 185, dot, COPY_PUT); break;
case 6: putimage(515, 145, dot, COPY_PUT);
putimage(515, 165, dot, COPY_PUT);
putimage(515, 185, dot, COPY_PUT);
putimage(555, 145, dot, COPY_PUT);
putimage(555, 165, dot, COPY_PUT);
putimage(555, 185, dot, COPY_PUT); break;
}
}

void getdice()
{ dice=random(6); dice++; dispdice(); }

void play()
{
getimage(50, 410, 60, 420, o1);
getimage(70, 410, 80, 420, o2);
putimage(50, 410, obj1, COPY_PUT);
putimage(70, 410, obj2, COPY_PUT);
while (1)
{
if (user==0)
{
putimage(487, 282, turn, COPY_PUT);
setcolor(GREEN);
outtextxy(480, 285, " Player 1");
again:
ch=getch();
if (ch==13) getdice();
else if (ch==27)
exit(1);
else
goto again;
setcolor(YELLOW);
if (cnt1==96 && dice>=4)
{
delay(1000);
user=1;
goto invalid1;
}
else if (cnt1==97 && dice>=3)
{ delay(1000);
user=1;
goto invalid1;

}
else if (cnt1==98 && dice>=2)
{ delay(1000);
user=1;
goto invalid1;

}
else if (cnt1==99 && dice>=1)
{ delay(1000);
user=1;
goto invalid1;

}
for (i=1; i<=dice; i++, cnt1++)
{
putimage(x1, y1, o1, COPY_PUT);
if (dir1==0)
{
// size=imagesize(x1, y1, x1+10, y1+10);
// o1=malloc(size);
getimage(x1, y1, x1+10, y1+10, o1);
x1+=40;
if (x1>410) x1-=40, y1-=40, dir1=1;
// size=imagesize(x1, y1, x1+10, y1+10);
// o1=malloc(size);
getimage(x1, y1, x1+10, y1+10, o1);
putimage(x1, y1, obj1, COPY_PUT);
delay(1000); goto ch1;
}
else
{
size=imagesize(x1, y1, x1+10, y1+10);
// o1=malloc(size);
getimage(x1, y1, x1+10, y1+10, o1);
x1-=40;
if (x1<50) x1+=40, y1-=40, dir1=0;
// size=imagesize(x1, y1, x1+10, y1+10);
// o1=malloc(size);
getimage(x1, y1, x1+10, y1+10, o1);
putimage(x1, y1, obj1, COPY_PUT);
delay(1000); goto ch1;
}
ch1: if (cnt1==99) goto over;
}
if (cnt1==12 || cnt1==72 || cnt1==78)
{
putimage(x1, y1, o1, COPY_PUT);
x1-=80; y1-=80;
// size=imagesize(x1, y1, x1+10, y1+10);
// o1=malloc(size);
getimage(x1, y1, x1+10, y1+10, o1);
putimage(x1, y1, obj1, COPY_PUT);
if (cnt1==12) cnt1=34;
else if (cnt1==72) cnt1=94;
else if (cnt1==78) { cnt1=100; goto over; }
}
else if (cnt1==22 || cnt1==46)
{
putimage(x1, y1, o1, COPY_PUT);
x1+=40; y1-=40;
// size=imagesize(x1, y1, x1+10, y1+10);
// o1=malloc(size);
getimage(x1, y1, x1+10, y1+10, o1);
putimage(x1, y1, obj1, COPY_PUT);
if (cnt1==22) cnt1=38;
else if (cnt1==46) cnt1=54;
dir1=1;
}
else if (cnt1==36 || cnt1==99)
{
putimage(x1, y1, o1, COPY_PUT);
x1+=160; y1+=120;
// size=imagesize(x1, y1, x1+10, y1+10);
// o1=malloc(size);
getimage(x1, y1, x1+10, y1+10, o1);
putimage(x1, y1, obj1, COPY_PUT);
if (cnt1==36) cnt1=9;
else if (cnt1==99) cnt1=66;
dir1=0;
}
else if (cnt1==62)
{
putimage(x1, y1, o1, COPY_PUT);
y1+=240;
// size=imagesize(x1, y1, x1+10, y1+10);
// o1=malloc(size);
getimage(x1, y1, x1+10, y1+10, o1);
putimage(x1, y1, obj1, COPY_PUT);
cnt1=2;
}
else if (cnt1==90)
{
putimage(x1, y1, o1, COPY_PUT);
x1-=80; y1+=160;
// size=imagesize(x1, y1, x1+10, y1+10);
// o1=malloc(size);
getimage(x1, y1, x1+10, y1+10, o1);
putimage(x1, y1, obj1, COPY_PUT);
cnt1=48;
}
if (dice==5 || dice==6) user=0; else user=1;
invalid1: putimage(500, 130, back, COPY_PUT);
}
else
{
putimage(487, 282, turn, COPY_PUT);
setcolor(BROWN);
outtextxy(480, 285, " Player 2");
setcolor(YELLOW);
again2: ch=getch();
if (ch==13) getdice();
else if (ch==27) exit(1);
else goto again2;
if (cnt2==96 && dice!=4)
{ delay(1000);
user=0;
goto invalid2; }
else if (cnt2==97 && dice!=3)
{ delay(1000);
user=0;
goto invalid2;
}
else if (cnt2==98 && dice!=2)
{ delay(1000);
user=0;
goto invalid2;
}
else if (cnt2==99 && dice!=1)
{ delay(1000);
user=0;
goto invalid2;
}
for (i=1; i<=dice; i++, cnt2++)
{
putimage(x2, y2, o2, COPY_PUT);
if (dir2==0)
{
// size=imagesize(x2, y2, x2+10, y2+10);
// o2=malloc(size);
getimage(x2, y2, x2+10, y2+10, o2);
x2+=40;
if (x2>440) x2-=40, y2-=40, dir2=1;
// size=imagesize(x2, y2, x2+10, y2+10);
// o2=malloc(size);
getimage(x2, y2, x2+10, y2+10, o2);
putimage(x2, y2, obj2, COPY_PUT);
delay(1000); goto ch2;
}
else
{
// size=imagesize(x2, y2, x2+10, y2+10);
// o2=malloc(size);
getimage(x2, y2, x2+10, y2+10, o2);
x2-=40;
if (x2<50) x2+=40, y2-=40, dir2=0;
// size=imagesize(x2, y2, x2+10, y2+10);
// o2=malloc(size);
getimage(x2, y2, x2+10, y2+10, o2);
putimage(x2, y2, obj2, COPY_PUT);
delay(1000); goto ch2;
}
ch2: if (cnt2==99) goto over;
}
if (cnt2==12 || cnt2==72 || cnt2==78)
{
putimage(x2, y2, o2, COPY_PUT);
x2-=80; y2-=80;
// size=imagesize(x2, y2, x2+10, y2+10);
// o2=malloc(size);
getimage(x2, y2, x2+10, y2+10, o2);
putimage(x2, y2, obj2, COPY_PUT);
if (cnt2==12) cnt2=34;
else if (cnt2==72) cnt2=94;
else if (cnt2==78) { cnt2=100; goto over; }
}
else if (cnt2==22 || cnt2==46)
{
putimage(x2, y2, o2, COPY_PUT);
x2+=40; y2-=40;
// size=imagesize(x2, y2, x2+10, y2+10);
// o2=malloc(size);
getimage(x2, y2, x2+10, y2+10, o2);
putimage(x2, y2, obj2, COPY_PUT);
if (cnt2==22) cnt2=38;
else if (cnt2==46) cnt2=54;
dir2=1;
}
else if (cnt2==36 || cnt2==99)
{
putimage(x2, y2, o2, COPY_PUT);
x2+=160; y2+=120;
// size=imagesize(x2, y2, x2+10, y2+10);
// o2=malloc(size);
getimage(x2, y2, x2+10, y2+10, o2);
putimage(x2, y2, obj2, COPY_PUT);
if (cnt2==36) cnt2=9;
else if (cnt2==99) cnt2=66;
dir2=0;
}
else if (cnt2==62)
{
putimage(x2, y2, o2, COPY_PUT);
y2+=240;
// size=imagesize(x2, y2, x2+10, y2+10);
// o2=malloc(size);
getimage(x2, y2, x2+10, y2+10, o2);
putimage(x2, y2, obj2, COPY_PUT);
cnt2=2;
}
else if (cnt2==90)
{
putimage(x2, y2, o2, COPY_PUT);
x2-=80; y2+=160;
// size=imagesize(x2, y2, x2+10, y2+10);
// o2=malloc(size);
getimage(x2, y2, x2+10, y2+10, o2);
putimage(x2, y2, obj2, COPY_PUT);
cnt2=48;
}
if (dice==5 || dice==6) user=1; else user=0;
invalid2: putimage(500, 130, back, COPY_PUT);
delay(1000);
}
}
over: }

int snakeladder()
{
randomize();

size=imagesize(487, 282, 593, 293);


turn=malloc(size);
getimage(487, 282, 593, 293, turn);

rectangle(100, 100, 110, 110);


setfillstyle(1, 2);
floodfill(102, 102, 15);
size=imagesize(100, 100, 110, 110);
obj1=malloc(size);
getimage(100, 100, 110, 110, obj1);
cleardevice();

rectangle(100, 100, 110, 110);


setfillstyle(1, 6);
floodfill(102, 102, 15);
size=imagesize(100, 100, 110, 110);
obj2=malloc(size);
getimage(100, 100, 110, 110, obj2);
cleardevice();

o1=malloc(size); o2=malloc(size);

setcolor(WHITE);
setfillstyle(SOLID_FILL, 15);
rectangle(500, 130, 580, 210);
floodfill(510, 140, 15);

size=imagesize(500, 130, 580, 210);


back=malloc(size);
getimage(500, 130, 580, 210, back);
setcolor(0);
setfillstyle(1, 0);
rectangle(535, 165, 545, 175);
floodfill(540, 170, 0);
size=imagesize(535, 165, 545, 175);
dot=malloc(size);
getimage(535, 165, 545, 175, dot);
cleardevice();

setcolor(WHITE);
setfillstyle(SOLID_FILL, 15);
rectangle(500, 130, 580, 210);
floodfill(510, 140, 15);

for(int i=0;i<=360;i+=40)
for(int j=0;j<=360;j+=40)
{
setfillstyle(SOLID_FILL, k);
bar(50+j,30+i,90+j,70+i);
k+=2;
}
ladder1(); ladder2();
snake1(); snake2(); snake3();
numbering(); status();

welcome();
play();
if (cnt1>=99) outtextxy(480, 330, "PLAYER 1 WINS!");
else if (cnt2>=99) outtextxy(480, 330, "PLAYER 2 WINS!");
getch();
return 1;
}

You might also like