#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<stdlib.h>
#include<math.h>
#include<dos.h>
void hadese(int );
void partab(int ,int );
void nobat(int );
void board();
void pelle();
void mar();
void start();
void rasm(int,int[]);
void redraw();
void welcome();
int p1[2],p[2];
int pos[2];
int passport[2]={0,0};
//************************************************************************
main(){
clrscr();
int driver , mode ;
driver = DETECT ;
mode = 0 ;
initgraph( &driver , &mode , "c:\\tc\\bgi");
randomize();
//************************************************************************
board();
pelle();
mar();
welcome();
redraw();
start();
//**********************************************************************
return 0;
}//main()
void board(){
int size = 10;
for(int k=0 ; k<= size ;k++) {
line(120 , 40+k*(400/(size)) , 120+size*(400/size) , 40+k*(400/size));
line(120 , 41+k*(400/(size)) , 120+size*(400/size) , 41+k*(400/size));
}//for
for( k=0 ; k<=size ; k++) {
line(120+k*(400/size),40,120+k*(400/size),40+size*(400/size));
line(121+k*(400/size),40,121+k*(400/size),40+size*(400/size));
}//for
setcolor(3);
char string[4];
settextstyle(0,0,1);
outtextxy(122,405,"start");
for(int i=0 ; i<= 9 ;i++){
for(int j=0 ; j<= 9 ;j++){
if (j%2 == 1)itoa(abs(10-i)+j*10,string, 10);
else itoa(i+1+j*10,string, 10);
//outtextxy(125 + i*40 , 405-j*40 , string);
}//for
}//for
}//board()
void pelle(){
int x[2],y[2] ;
int temp;
int count = 1 ;
while ( count <= 7){
if(count == 1)x[0] = 340 , y[0] = 420 , x[1] = 380 , y[1] = 300;
if(count == 2)x[0] = 180 , y[0] = 340 , x[1] = 180 , y[1] = 220;
if(count == 3)x[0] = 340 , y[0] = 300 , x[1] = 260 , y[1] = 180;
if(count == 4)x[0] = 460 , y[0] = 420 , x[1] = 500 , y[1] = 380;
if(count == 5)x[0] = 180 , y[0] = 100 , x[1] = 220 , y[1] = 60;
if(count == 6)x[0] = 420 , y[0] = 180 , x[1] = 420 , y[1] = 100;
if(count == 7)x[0] = 260 , y[0] = 140 , x[1] = 260 , y[1] = 100;
setcolor(6);
line(x[0],y[0],x[1],y[1]);
line(x[0]+10,y[0],x[1]+10,y[1]);
line(x[0]+1,y[0],x[1]+1,y[1]); //for bold
line(x[0]+11,y[0],x[1]+11,y[1]); //for bold
float temp1 ;
if(x[1]-x[0] == 0 ) temp1 = 1000000 ;// baraye binahayat
else temp1=fabs(float(y[1]-y[0])/float(x[1]-x[0]));
int i;
if (y[0]<=y[1] && x[0]<=x[1]){for(i=1 ; i<=abs(y[1]-y[0])/10-1 ; i++){line( x[1]-int(i*10./temp1) , y[1]-10*i , x[1]+10-int(i*10./temp1) , y[1]-10*i);line( x[1]-int(i*10./temp1) , y[1]-10*i+1 , x[1]+10-int(i*10./temp1) , y[1]-10*i+1);}}//if
else if (y[0]<=y[1] && x[0]> x[1]){for(i=1 ; i<=abs(y[1]-y[0])/10-1 ; i++){line( x[1]+int(i*10./temp1) , y[1]-10*i , x[1]+10+int(i*10./temp1) , y[1]-10*i);line( x[1]+int(i*10./temp1) , y[1]-10*i+1 , x[1]+10+int(i*10./temp1) , y[1]-10*i+1);}}//if
else if (y[0]> y[1] && x[0]<=x[1]){for(i=1 ; i<=abs(y[1]-y[0])/10-1 ; i++){line( x[0]+int(i*10./temp1) , y[0]-10*i , x[0]+10+int(i*10./temp1) , y[0]-10*i);line( x[0]+int(i*10./temp1) , y[0]-10*i+1 , x[0]+10+int(i*10./temp1) , y[0]-10*i+1);}}//if
else if (y[0]> y[1] && x[0]> x[1]){for(i=1 ; i<=abs(y[1]-y[0])/10-1 ; i++){line( x[0]-int(i*10./temp1) , y[0]-10*i , x[0]+10-int(i*10./temp1) , y[0]-10*i);line( x[0]-int(i*10./temp1) , y[0]-10*i+1 , x[0]+10-int(i*10./temp1) , y[0]-10*i+1);}}//if
count++;
}//while()
}//pelle()
void mar(){
int zaribx0[8]={0,1,9,6,4,0,8,0},zaribx1[8]={0,2,8,5,2,1,7,0},zariby0[8]={0,1,6,8,1,7,9,6},zariby1[8]={0,0,4,5,7,9,0,1};
int x[2],y[2];
int count = 1;
while(count <= 7){
for(int i=1;i<=7;i++)
if(count == i)x[0]=140+40*zaribx0[i],y[0]=420-40*zariby0[i],x[1]=140+40*zaribx1[i],y[1]=420-40*zariby1[i];
count++;
setcolor(3);
line ( x[0] ,y[0] , x[1] ,y[1]);
setcolor(8);
line ( x[0]+1 ,y[0] , x[1]+1 ,y[1]);
line ( x[0]+2 ,y[0] , x[1]+2 ,y[1]);
line ( x[0]+3 ,y[0] , x[1]+3 ,y[1]);
setcolor(3);
line ( x[0]+4 ,y[0] , x[1]+4 ,y[1]);
}//while()
}//mar()
void start(){
//sleep(3);
int n=0;
pos[0]=pos[1] = -1 ;
p[0] = 125 , p1[0] =428;
p[1] = 125 , p1[1]=405;
rasm(n,pos);
int random1;
char ch=' ';
//randomize();
while(1){
if(passport[n] == 0){
nobat(n);
if(n==0) ch=getch();
if(n==1) ch=50; //for computer
while(ch>57 || ch<48){
ch=getch();
}
if(n==0) random1 = (ch+random(100))%6+1;
else random1 = random(100)%6+1;
//sleep(1);
partab(n,random1);
if(random1 == 6){
//printf("congrulation");
pos[n]=1;
if(n==0){p[n]=125,p1[n]=428;}
if(n==1){p[n]=125,p1[n]=405;}
redraw();
rasm(n,pos);
passport[n] = 1;
}//if
n=(n+1)%2;
}//if
else {
nobat(n);
if(n==0) ch=getch();
if(n==1) ch=50; //for computer
while(ch>57 || ch<48){
ch = getch();
}
if(n==0){ random1 = (ch+random(100))%6+1;p[n]=125,p1[n]=428;}
else { random1 = random(100)%6+1;p[n]=125,p1[n]=405;}
partab(n,random1);
if(pos[n]+random1 >100) goto l1;
// printf("ran = %d" ,random1);
pos[n] = pos[n]+random1;
redraw();
rasm(n,pos);
if(pos[n]==100) { if(n==0){printf("You Win");getch();exit(0);}
else {printf("Computer Wins"); getch();exit(0);}//else
}//if
if(n==6 ||n==9 ||n==22||n==35||n==68||n==77||n==82||n==19||n==61||n==99||n==78||n==87||n==92||n==70){
hadese(n);
if(pos[n] == 6)pos[n]=34;
if(pos[n] == 9)pos[n]=11;
if(pos[n] == 22)pos[n]=59;
if(pos[n] == 35)pos[n]=64;
if(pos[n] == 68)pos[n]=88;
if(pos[n] == 77)pos[n]=84;
if(pos[n] == 82)pos[n]=98;
//************************
if(pos[n] == 19)pos[n]=3;
if(pos[n] == 61)pos[n]=20;
if(pos[n] == 99)pos[n]=80;
if(pos[n] == 78)pos[n]=16;
if(pos[n] == 87)pos[n]=55;
if(pos[n] == 92)pos[n]=8;
if(pos[n] == 70)pos[n]=49;
redraw();
rasm(n,pos);
}//if
l1: if(random1 == 6 ) continue;
n = (n+1)%2;
}//else
}//while()
}//start
void rasm(int n,int pos[]){
for(int i=1;i<=2;i++){
n = (n+1)%2;
if(n==0)setcolor(1);
else setcolor(14);
int h,y;
h = (pos[n]-1)/10;
if(h%2 == 0) y = (pos[n]-1)%10;
else y = abs(10 - pos[n]%10)%10;
int x0,y0;
x0 = p[n]+y*40 ; y0 = p1[n]-40*h;
//setcolor(2);
for(int i=1;i<=8;i++)
line(x0,y0+i,x0+8,y0+i);
// printf("pos[%d]= %d",n,pos[n]);
}//for
getch();
}//rasm()
void redraw(){
//clrscr();
setcolor(0);
for(int i=0;i<=480;i++)
line(0,0+i,640,0+i);
//rectangle(0,0,640,480);
setfillstyle(0,0);
floodfill(50,50,0);
setcolor(15);
board();
pelle();
mar();
}//redraw()
void nobat(int n){
setcolor(0);
for(int i=1;i<=120;i++)
line(200,180+i,440,180+i);
setcolor(9);
if(n==0){outtextxy(220,210,"It's time for you");
outtextxy(220,220,"press key(0-9)");}
//if(n==1);//outtextxy(220,210,"nobat computer");
if(passport[n]== 0){
if(n==0){outtextxy(220,230,"you must take 6 ");
outtextxy(220,240,"for enter the game");}
//if(n==1)outtextxy(220,230,"nobat computer");
}//if
// if(passport[n]== 1){
//if(n==0)outtextxy(220,250,"throw tas ");
//if(n==1)outtextxy(220,250,"");
// }//if
setcolor(15);
rectangle(205,185,435,295);
rectangle(202,182,438,298);
// getch();
}//nobat()
void welcome(){
setcolor(0);
for(int i=1;i<=120;i++)
line(200,180+i,440,180+i);
setcolor(9);
outtextxy(220,240," welcome to the game");
setcolor(15);
rectangle(205,185,435,295);
rectangle(202,182,438,298);
getch();
}//welcome()
void partab(int n , int random1 ){
setcolor(0);
for(int i=1;i<=120;i++)
line(200,180+i,440,180+i);
setcolor(4);
char string[2];
itoa(random1,string,10);
if(n==0){outtextxy(238,225,"you take");outtextxy(250,240,string);}
if(n==1){outtextxy(238,225,"computer takes");outtextxy(250,240,string);}
setcolor(15);
rectangle(205,185,435,295);
rectangle(202,182,438,298);
getch();
}//partab()
void hadese(int n){
setcolor(12);
setcolor(0);
for(int i=1;i<=120;i++)
line(200,180+i,440,180+i);
setcolor(15);
rectangle(205,185,435,295);
rectangle(202,182,438,298);
if(pos[n]==6 || pos[n]==9 ||pos[n]== 22 || pos[n]==68 || pos[n]==77 ||pos[n]== 82 ||pos[n]==35)
{outtextxy(248,225,"ladder");}
else if(pos[n]==87 || pos[n]==78 ||pos[n]== 99 || pos[n]==61 || pos[n]==19 ||pos[n]== 70 || pos[n]==92)
{outtextxy(248,225,"snake");}
else return;
getch();
}//hadese()