PsychicFreak
Member
- Joined
- Jun 6, 2008
- Sythe Gold
- 0
- Messages
- 53
#include<akhil.h> //we used akhchs.c as header file
#include<stdio.h>
#include<conio.h>
#include<dos.h>
char pos[8][8],color[8][8],blkc[8][8],player,check,turn=0;
char gwmoved='f',gbmoved='f',rbmoved='f',rwmoved='f',castlew='f',castleb='f';
union REGS i,o; //for hardware interaction
//functions prototype
void assign(char*,char*,char*);
void checkpwn(int x3,int y3);
void castling(char clr);
char chkpawnmov(int x1,int y1,char c1,int x3,int y3);
char chkrookmov(int x1,int y1,char c1,int x3,int y3);
char chkknightmov(int x1,int y1,char c1,int x3,int y3);
char chkbishopmov(int x1,int y1,char c1,int x3,int y3);
char chkqueenmov(int x1,int y1,char c1,int x3,int y3);
char chkkingmov(int x1,int y1,char c1,int x3,int y3);
void promote(int,int,char,char);
//------------main function------------------------------------------------//
void main()
{
int i1,j1,button,x,y,x1,y1,x2,y2,x3,y3,ch,i=0,j=0,c;
char id,nwblkclr,move='i';
assign((char *)pos,(char *)color,(char *)blkc);
clrscr();
display(); //for showing chess board
design();
setspare(); //for drawing rook etc.
restrictmouseptr(0,0,400,400); //restricts pointer of mouse in the area specified there.
showmouseptr();
while(1)
{
i=i+1;
setcolor(WHITE);
if(turn%2==0) //means white's turn
{
outtextxy(100,430,"White's turn");
player='w';
}
else if(turn%2!=0) //means black's turn
{
outtextxy(100,430,"black's turn");
player='b';
}
check=IsInCheck(player); //finds whether king in check.
if( check )
{
bottom();
outtextxy(100,430,"check your king");
}
while(i) //select piece
{
if(kbhit())
{
ch=getch();
if(ch=='\r') //escape at pressing enter.
exit(0);
}
getmousepos(&button,&x,&y); //gets click position of mouse
if((button== 001))
{
sound(1000);
i=i-1;
delay(50);
nosound();
delay(500);
}
}
x1=relposx(x)/50; //get its position and reference point
y1=relposy(y)/50;
id=pos[x1][y1];
c=color[x1][y1];
if( (turn%2==0 && c=='b') || (turn%2==1 && c=='w') || (id=='n'))
continue;
setcolor(BLUE);
rectangle(x1*50,y1*50,x1*50+50,y1*50+50);
j=j+1;
while(j)
{
if(kbhit())
{
ch=getch();
if(ch=='\r') //escape at pressing enter.
exit(0);
}
getmousepos(&button,&x2,&y2); //gets click position of mouse
if((button== 001))
{
sound(500);
j=j-1;
delay(100);
nosound();
delay(500);
}
}
x3=relposx(x2)/50; //gives standard x coordinate
y3=relposy(y2)/50; //gives standard y coordinate
nwblkclr=blkc[x3][y3];//get destination block's color;
setcolor(WHITE);
rectangle(x1*50,y1*50,x1*50+50,y1*50+50);
bottom();
//------------------------------castling-------------------------------------
if( x1==4 && y1==7 && pos[4][7]=='g' && x3==6 && y3==7 && pos[5][7]=='n'
&& pos[6][7]=='n' && c=='w' && rwmoved=='f' && gwmoved=='f'
&& check==0 && castlew=='f')
{
//we need not to check whether king will be in check at new position
// bcoz if it will be in check the changes will be undone automatically
//for the king not for the rook and turn will move only when move saves king.
castling(c);
}
if( x1==4 && y1==0 && pos[1][0]=='n' && pos[2][0]=='n' && pos[3][0]=='n'
&& pos[4][0]=='g' && c=='b' && rbmoved=='f' && gbmoved=='f'
&& check==0 && castleb=='f')
{
castling(c);
}
//-------------------------------------------------------------------------------
if(id=='p') //wheteher pawn?
{
move= chkpawnmov(x1,y1,c,x3,y3);
if(move=='v')
{
draw(x3,y3,x1,y1,blkc[x1][y1],c,nwblkclr,'p');
checkpwn(x3,y3);
}
}
else if(id=='r') //whether rook?
{
move=chkrookmov(x1,y1,c,x3,y3);
if(move=='v' )
{
draw(x3,y3,x1,y1,blkc[x1][y1],c,nwblkclr,'r');
}
if(c=='w')
rwmoved='t';
else if(c=='b')
rbmoved='t';
}
else if(id=='k') //whether knight?
{
move= chkknightmov(x1,y1,c,x3,y3);
if(move=='v' )
{
draw(x3,y3,x1,y1,blkc[x1][y1],c,nwblkclr,'k');
}
}
else if(id=='b') //whether bishop?
{
move=chkbishopmov(x1,y1,c,x3,y3);
if(move=='v')
{
draw(x3,y3,x1,y1,blkc[x1][y1],c,nwblkclr,'b');
}
}
else if(id=='q') //whether queen?
{
move=chkqueenmov(x1,y1,c,x3,y3);
if(move=='v')
draw(x3,y3,x1,y1,blkc[x1][y1],c,nwblkclr,'q');
}
else if(id=='g') //whether king?
{
move=chkkingmov(x1,y1,c,x3,y3);
if(move=='v')
draw(x3,y3,x1,y1,blkc[x1][y1],c,nwblkclr,'g');
if(c=='w')
gwmoved='t';
else if(c=='b')
gbmoved='t';
}
if(move=='v') turn=turn+1;
// if(check=IsInCheck(player));
}
// getch();
}
getmousepos(int *button,int* x,int* y) //gets mouse position.
{
i.x.ax=3;
int86(0x33,&i,&o);
*button=o.x.bx;
*x=o.x.cx;
*y=o.x.dx;
return;
}
showmouseptr() //shows pointer in graphics mode.
{
i.x.ax=1;
int86(0x33,&i,&o);
return;
}
void hidemouseptr() //hides pointer
{
i.x.ax=2;
int86(0x33,&i,&o);
return;
}
restrictmouseptr(int x1,int y1,int x2,int y2) //restrict pointer
{
i.x.ax=7;
i.x.cx=x1;
i.x.dx=x2;
int86(0x33,&i,&o);
i.x.ax=8;
i.x.cx=y1;
i.x.dx=y2;
int86(0x33,&i,&o);
return;
}
char chkpawnmov(int x1,int y1,char c1,int x3,int y3)
{ char mov,id,c; //checks each pawn (black and white) move.
id=pos[x3][y3]; //anybody?
c=color[x3][y3]; //color?
if(c1=='w') //white pawn?
{
if(x3==x1 && y3==y1-1 ) //move 1
{ if(id=='n') //id is none when no body is there
mov='v';
}
else if( (x3==x1+1 || x3==x1-1) && y3==y1-1)
{
if(id!='n' && c=='b') //black present?
{
mov='v';
send(id,c);
}
}
else if( (x3==x1 && y3==y1-2) && y1==6)
{
mov='v';
}
}
else if(c1=='b')
{
if(x3==x1 && y3==y1+1) //black and white both not there?
{ if(id=='n')
mov='v';
}
else if((x3==x1-1 || x3==x1+1) && y3==y1+1)
{ if(id!='n' && c=='w' ) //white there
{
mov='v'; //valid move
send(id,c);
}
}
else if( (x3==x1 && y3==y1+2) && y1==1)
{
mov='v';
}
}
else
{ //no action at invalid move.
mov='i';
}
return(mov);
}
draw(int x3,int y3,int x1,int y1,char blkc,char c,char nwblkclr,char id1)
{
int nx3,ny3,ncx,ncy,oldi,oldc;
oldi=pos[x3][y3]; oldc=color[x3][y3];
nx3=x3*50; ny3=y3*50; ncx=x1*50; ncy=y1*50;
pos[x1][y1]='n';
color[x1][y1]='n'; //welcome back mr. pointer.
pos[x3][y3]=id1;
color[x3][y3]=c;
check=IsInCheck(player);
if(check)//that is king not safe even now
{
pos[x1][y1]=id1; //undo the changes
color[x1][y1]=c;
pos[x3][y3]=oldi;
color[x3][y3]=oldc;
turn=turn-1;
return;
}
bottom();
if(nwblkclr=='g') //CLEAR NEW POSITION
setfillpattern(pattern,GREEN);
else if(nwblkclr=='m')
setfillpattern(pattern,MAGENTA);
hidemouseptr(); //necessary to avoid the effect of mouse pointer.
bar(nx3,ny3,nx3+50,ny3+50);
setcolor(WHITE);
rectangle(nx3,ny3,nx3+50,ny3+50);
if(blkc=='g') //CLEAR PREVIOUS POSITION
setfillpattern(pattern,GREEN);
else if(blkc=='m')
setfillpattern(pattern,MAGENTA); //clear the previous pos.
bar(ncx,ncy,ncx+50,ncy+50);
setcolor(WHITE);
rectangle(ncx,ncy,ncx+50,ncy+50);
if(id1=='p') //if pawn was selected then draw the pawn at new position.
{ if(c=='w')
pawndraw(nx3,ny3,0); // 0 for white.
else if(c=='b')
pawndraw(nx3,ny3,1);
}
else if(id1=='r') //if rook was selected
{ if(c=='w')
rookdraw(nx3,ny3,0); // 0 for white.
else if(c=='b')
rookdraw(nx3,ny3,1);
}
else if(id1=='k') //if knight was selected
{ if(c=='w')
drawknite(nx3,ny3,0); // 0 for white.
else if(c=='b')
drawknite(nx3,ny3,1);
}
else if(id1=='b') //if bishop was selected
{ if(c=='w')
drawbishop(nx3,ny3,0); // 0 for white.
else if(c=='b')
drawbishop(nx3,ny3,1);
}
else if(id1=='g')
{ if(c=='w')
drawking(nx3,ny3,0); // 0 for white.
else if(c=='b')
drawking(nx3,ny3,1);
}
else if(id1=='q')
{ if(c=='w')
drawqueen(nx3,ny3,0); // 0 for white.
else if(c=='b')
drawqueen(nx3,ny3,1);
}
showmouseptr();
return;
}
char chkrookmov(int x1,int y1,char c1,int x3,int y3)
{ //checks rook move for black and white as both have same moves.
int i;
char id,c,mov='v';
if( (x3==x1) || (y3==y1) )
{
if(x3>x1 && y3==y1)
{
for(i=x1+1;i<x3;i++)
{
id=pos[y3];
if(id!='n') //if black or white present move is invalid
{
mov='i';
}
}
}
else if(x3<x1 && y3==y1)
{
for(i=x1-1;i>x3;i--)
{
id=pos[y3];
if(id!='n') //if black or white
{
mov='i';
}
}
}
else if(x3==x1 && y3>y1)
{
for(i=y1+1;i<y3;i++)
{
id=pos[x3];
if(id!='n') //if black or white
{
mov='i';
}
}
}
else if(x3==x1 && y3<y1)
{
for(i=y1-1;i>y3;i--)
{
id=pos[x3];
if(id!='n') //if black or white
{
mov='i';
}
}
}
if(mov=='v')
{
id=pos[x3][y3];
c=color[x3][y3];
if(id!='n' && c=='b' && c1=='w') //means black at x3,y3
{
mov='v';
send(id,c);
}
else if(id!='n' && c=='w' && c1=='b') //means white there
{
mov='v';
send(id,c);
}
else if(id=='n')//none of the white or black even at x3 & y3?
mov='v';
else
mov='i';
}
}
else mov='i';
return(mov);
}
char chkknightmov(int x1,int y1,char c1,int x3,int y3)
{
//knight's moves are not affected if some entity lies in the way.
//it just jumps over them.Atmost only eight moves are possible.
int i,posk[8][2];
char id,c,mov='i';
posk[0][0]=x1+1;
posk[0][1]=y1-2;
posk[1][0]=x1-1;
posk[1][1]=y1-2;
posk[2][0]=x1+1;
posk[2][1]=y1+2;
posk[3][0]=x1-1;
posk[3][1]=y1+2;
posk[4][0]=x1+2;
posk[4][1]=y1-1;
posk[5][0]=x1+2;
posk[5][1]=y1+1;
posk[6][0]=x1-2;
posk[6][1]=y1+1;
posk[7][0]=x1-2;
posk[7][1]=y1-1;
for(i=0;i<=7;i++)
{
if(x3==posk[0] && y3==posk[1])
{
id=pos[x3][y3];
c=color[x3][y3];
if(id!='n'&& c=='b' && c1=='w')
{
mov='v'; //means black is there.
send(id,c);
}
else if(id!='n' && c=='w' && c1=='b')
{
mov='v'; //means white is there.
send(id,c);
}
else if(id=='n')
mov='v'; //means it is blank.
}
}
return(mov);
}
char chkbishopmov(int x1,int y1,char c1,int x3,int y3)
{
int m,n,m1,n1,i,j;
char mov,id,c;
m=x3-x1;
n=y3-y1;
if(abs(m)==abs(n))
{ if(m>0 && n<0)//move 1
{ for(i=x1+1,j=y1-1;(i<x3 && j>y3);i++,j--)
{
id=pos[j];
if(id!='n')
mov='i';
}
}
if(m>0 && n>0)//move 2
{ for(i=x1+1,j=y1+1;(i<x3 && j<y3);i++,j++)
{
id=pos[j];
if(id!='n')
mov='i';
}
}
if(m<0 && n<0)//move 3
{ for(i=x1-1,j=y1-1;(i!=x3 && j!=y3);i--,j--)
{
id=pos[j];
if(id!='n')
mov='i';
}
}
if(m<0 && n>0)//move 4
{ for(i=x1-1,j=y1+1;(i>x3 && j<y3);i--,j++)
{
id=pos[j];
if(id!='n')
mov='i';
}
}
if(mov!='i') //..that no body is in the way,now at x3 y3?
{
id=pos[x3][y3];
c=color[x3][y3];
if(id!='n'&& c=='b') //means black at x3,y3
{
if(c1=='w')
{
mov='v';
send(id,c);
}
else
mov='i';
}
else if(id!='n'&& c=='w') //means white there
{
if(c1=='b')
{
mov='v';
send(id,c);
}
else
{ mov='i';}
}
else if(id=='n')//none of the white or black even at x3 & y3?
mov='v';
}
} //means not at the x cross.
else
{
mov='i';
}
return(mov);
}
char chkqueenmov(int x1,int y1,char c1,int x3,int y3)
{
char mov1,mov2,mov3='i',id,c;
mov1=chkrookmov(x1,y1,c1,x3,y3);
mov2=chkbishopmov(x1,y1,c1,x3,y3);
if(mov1=='v' || mov2=='v') //move is valid and color is checked already.
{
mov3='v';
}
return(mov3);
}
char chkkingmov(int x1,int y1,char c1,int x3,int y3)
{
char id,c,mov='i';
id=pos[x3][y3];
c=color[x3][y3];
if(x3==x1+1 && y3==y1-1) //move 1
{
if(id=='n')
mov='v';
else if(c1=='w' && id!='n' && c=='b')//means black there.
{
mov='v';
send(id,c);
}
else if(c1=='b' && id!='n' && c=='w')//means white there.
{
mov='v';
send(id,c);
}
}
else if(x3==x1-1 && y3==y1-1) //move 2
{
if(id=='n')
mov='v';
else if(c1=='w' && id!='n' && c=='b')//means black there.
{
mov='v';
send(id,c);
}
else if(c1=='b' && id!='n' && c=='w')//means white there.
{
mov='v';
send(id,c);
}
}
else if(x3==x1-1 && y3==y1+1) //move 3
{
if(id=='n')
mov='v';
else if(c1=='w' && id!='n' && c=='b')//means black there.
{
mov='v';
send(id,c);
}
else if(c1=='b' && id!='n' && c=='w') //means white there.
{
mov='v';
send(id,c);
}
}
else if(x3==x1+1 && y3==y1+1) //move 4
{
if(id=='n' )
mov='v';
else if(c1=='w' && id!='n' && c=='b')//means black there.
{
mov='v';
send(id,c);
}
else if(c1=='b' && id!='n' && c=='w')//means white there.
{
mov='v';
send(id,c);
}
}
else if(x3==x1+1 && y3==y1) //move 5
{
if(id=='n')
mov='v';
else if(c1=='w' && id!='n' && c=='b')//means black there.
{
mov='v';
send(id,c);
}
else if(c1=='b' && id!='n' && c=='w') //means white there.
{
mov='v';
send(id,c);
}
}
else if(x3==x1 && y3==y1-1) //move 6
{
if(id=='n')
mov='v';
else if(c1=='w' && id!='n' && c=='b')//means black there.
{
mov='v';
send(id,c);
}
else if(c1=='b' && id!='n' && c=='w')//means white there.
{
mov='v';
send(id,c);
}
}
else if(x3==x1-1 && y3==y1) //move 7
{
if(id=='n')
mov='v';
else if(c1=='w' && id!='n' && c=='b')//means black there.
{
mov='v';
send(id,c);
}
else if(c1=='b' && id!='n' && c=='w')//means white there.
{
mov='v';
send(id,c);
}
}
else if(x3==x1 && y3==y1+1) //move 8
{
if(id=='n')
mov='v';
else if(c1=='w' && id!='n' && c=='b')//means black there.
{
mov='v';
send(id,c);
}
else if(c1=='b' && id!='n' && c=='w')//means white there.
{
mov='v';
send(id,c);
}
}
else //means move is not valid
{ mov='i';}
return(mov);
}
void assign(char* post,char* col,char* blk)
{ int i;
char val1[64]={"rpnnnnprkpnnnnpkbpnnnnpbqpnnnnpqgpnnnnpgbpnnnnpbkpnnnnpkrpnnnnpr"};
char val2[64]={"bbnnnnwwbbnnnnwwbbnnnnwwbbnnnnwwbbnnnnwwbbnnnnwwbbnnnnwwbbnnnnww"};
char val3[64]={"gmgmgmgmmgmgmgmggmgmgmgmmgmgmgmggmgmgmgmmgmgmgmggmgmgmgmmgmgmgmg"};
for(i=0;i<=63;i++,post++,col++,blk++)
{ *post=val1; *col=val2; *blk=val3; }
}
void checkpwn(int x3,int y3) //n for normal and p for promote.
{
char a,choice;
a=color[x3][y3];//whether pawn is white or black?
bottom();
setcolor(WHITE);
if(a=='b' && (x3>=0 && x3<=7) && y3==7)
{
outtextxy(100,430,"well done black..press b");
getch(); bottom();
outtextxy(10,430,"promote enter initial r,k,b,q");
choice=getch();
pos[x3][y3]=choice;
color[x3][y3]='b';
bottom();
promote(x3,y3,choice,'b');
}
else if(a=='w' && (x3>=0 && x3<=7) && y3==0)
{
outtextxy(100,430,"well done white..press w");
getch(); bottom();
outtextxy(10,430,"promote enter initial r,k,b,q");
choice=getch();
pos[x3][y3]=choice;
color[x3][y3]='w';
bottom();
promote(x3,y3,choice,'w');
}
return;
}
void promote(int x3,int y3,char choice,char col)
{
int color,nx3,ny3;
char bkc;
nx3=x3*50;ny3=y3*50;
if(col=='w')
color=0;
else if(col=='b')
color=1;
bkc= blkc[x3][y3];
if(bkc=='g') //CLEAR NEW POSITION
setfillpattern(pattern,GREEN);
else if(bkc=='m')
setfillpattern(pattern,MAGENTA);
hidemouseptr(); //necessary to avoid the effect of mouse pointer.
bar(nx3,ny3,nx3+50,ny3+50);
setcolor(WHITE);
rectangle(nx3,ny3,nx3+50,ny3+50);
if(choice=='r')
rookdraw(nx3,ny3,color);
else if(choice=='k')
drawknite(nx3,ny3,color);
else if(choice=='b')
drawbishop(nx3,ny3,color);
else if(choice=='q')
drawqueen(nx3,ny3,color);
else exit();
showmouseptr();
}
int IsInCheck(int current_side) //finds whether king in check.
{
int kx,ky;//position of king.
int i1 ,j1,x1,x2,x3,x4,y1,y2,y3,y4;
char xside;
if(current_side=='w')
xside='b';
else
xside='w'; // opposite current_side, who may be checking your patience
for(i1=1;i1<=8;i1++)
{
for(j1=1;j1<=8;j1++)
{
if( (pos[i1-1][j1-1]=='g') && (color[i1-1][j1-1]==current_side) )
{ kx=i1-1; ky=j1-1; }
}
}
//now we know position of king.So check others.
//knight first
for(i1=1;i1<=8;i1++)
{
x1=kx-2; y1=ky-2;
x2=kx+2; y2=ky+2;
x3=kx-1; y3=ky-1;
x4=kx+1; y4=ky+1;
if( pos[x3][y1]=='g' && color[x3][y1]==xside ) //move1
return (1);
if( pos[x1][y3]=='g' && color[x1][y3]==xside ) //move2
return(1);
if( pos[x1][y4]=='g' && color[x1][y4]==xside ) //move3
return(1);
if( pos[x3][y2]=='g' && color[x3][y2]==xside ) //move4
return(1);
if( pos[x4][y2]=='g' && color[x4][y2]==xside ) //move5
return(1);
if( pos[x2][y4]=='g' && color[x2][y4]==xside) //move6
return(1);
if( pos[x2][y3]=='g' && color[x2][y3]==xside ) //move7
return(1);
if( pos[x4][y1]=='g' && color[x4][y1]==xside) //move8
return(1);
}
//thank god u r safe from knight.
//check just down and down.
if(pos[kx][y4]=='g' && color[kx][y4]==xside)
return(1);
for(i1=kx,j1=y4;j1<=7;j1++)
{ if( ( pos[i1][j1]=='r' || pos[i1][j1]=='q' ) && color[i1][j1]==xside )
return(1);
if(pos[i1][j1]!='n')//that is something in between
break;
}
//check just up and up.
if(pos[kx][y3]=='g' && color[kx][y3]==xside)
return(1);
for(i1=kx,j1=y3;j1>=0;j1=j1-1)
{ if( (pos[i1][j1]=='r' || pos[i1][j1]=='q' ) && color[i1][j1]==xside )
return(1);
if(pos[i1][j1]!='n')//that is something in between
break;
}
//check just left and left
if(pos[x3][ky]=='g' && color[x3][ky]==xside )
return(1);
for(i1=x3,j1=ky;i1>=0;i1--)
{ if( (pos[i1][j1]=='r' || pos[i1][j1]=='q' ) && color[i1][j1]==xside )
return(1);
if(pos[i1][j1]!='n')//that is something in between
break;
}
//just right and right.
if(pos[x4][ky]=='g' && color[x4][ky]==xside )
return(1);
for(i1=x4,j1=ky;i1<=7;i1++)
{ if( (pos[i1][j1]=='r' || pos[i1][j1]=='q' ) && color[i1][j1]==xside )
return(1);
if(pos[i1][j1]!='n')//that is something in between
break;
}
// go right down ~ \_
if(x4<8 && y4<8 && color[x4][y4]==xside)//just right down
{ if(pos[x4][y4]=='g' || pos[x4][y4]=='q' || pos[x4][y4]=='b' )
return(1);
if(current_side=='b' && pos[x4][y4]=='p' && color[x4][y4]=='w')
return(1);
}
if(x4<8 && y4<8 && pos[x4][y4]=='n')//along the diagonal
{
for(i1=x4,j1=y4; i1<=7 && j1<=7;i1++,j1++)
{
if( color[i1][j1]==xside && (pos[i1][j1]=='q'||pos[i1][j1]=='b') )
return(1);
if(pos[i1][j1]!='n')
break;
}
}
// go left down _/-
if(x3>0 && y4<8 && color[x3][y4]==xside)
{ if(pos[x3][y4]=='g' || pos[x3][y4]=='q' || pos[x3][y4]=='b')
return(1);
if(current_side=='b' && pos[x3][y4]=='p' && color[x3][y4]=='w')
return(1);
}
if(x3>0 && y4<8 && pos[x3][y4]=='n')
{
for(i1=x3,j1=y4; i1>=0 && j1<=7;i1--,j1++)
{
if(color[i1][j1]==xside && (pos[i1][j1]=='q' || pos[i1][j1]=='b') )
return(1);
if(pos[i1][j1]!='n')
break;
}
}
// go left up
if(x3>0 && y3>0 && color[x3][y3]==xside)//means sth of opposite party,
{ if(pos[x3][y3]=='g' || pos[x3][y3]=='q' || pos[x3][y3]=='b')
return(1);
if(current_side=='w' && pos[x3][y3]=='p' && color[x3][y3]=='b')
return(1);
}
if(x3>0 && y3>0 && pos[x3][y3]=='n') //means no body there.
{
for(i1=x3,j1=y3; i1>=0 && j1>=0;i1--,j1--)
{
if(color[i1][j1]==xside && (pos[i1][j1]=='q' || pos[i1][j1]=='b') )
return(1);
if(pos[i1][j1]!='n')//means sth of our party,so forgive him.
break;
}
}
// gojust right up and up.
if(x4<8 && y3>0 && color[x4][y3]==xside)
{ if(pos[x4][y3]=='g' || pos[x4][y3]=='q' || pos[x4][y3]=='b')
return(1);
if(current_side=='w' && pos[x4][y3]=='p' && color[x4][y3]=='b')
return(1);
}
if(x4<8 && y3>0 && pos[x4][y3]=='n')
{
for(i1=x4,j1=y3; i1<=7 && j1>=0;i1++,j1--)
{
if(color[i1][j1]==xside && (pos[i1][j1]=='q' || pos[i1][j1]=='b') )
return(1);
if(pos[i1][j1]!='n')
break;
}
}
return 0;//and finally none of the case means not in check.
}
void castling(char clr)
{
outtextxy(100,430,"CASTLING");
if(clr=='w')
{
castlew='t';
pos[4][7]='n';
color[4][7]='n'; //welcome back mr. pointer.
pos[6][7]='g';
color[6][7]='w';
pos[7][7]='n';
color[7][7]='n';
pos[5][7]='r';
color[5][7]='w';
check=IsInCheck(player);
if(check)//that is king not safe even now
{
pos[4][7]='g'; //undo the changes
color[4][7]='w';
pos[6][7]='n';
color[6][7]='n';
pos[7][7]='r'; //undo the changes
color[7][7]='w';
pos[5][7]='n';
color[5][7]='n';
turn=turn-1;
return;
}
bottom();
hidemouseptr();
setfillpattern(pattern,GREEN);
bar(350,350,400,400);
bar(250,350,300,400);
setcolor(WHITE);
rectangle(350,350,400,400);
rectangle(250,350,300,400);
setfillpattern(pattern,MAGENTA);
bar(300,350,350,400);
bar(200,350,250,400);
setcolor(WHITE);
rectangle(300,350,350,400);
rectangle(200,350,250,400);
drawking(300,350,0); // 0 for white.
rookdraw(250,350,0); // 0 for white.
turn=turn-1;
showmouseptr();
}
else if(clr=='b')
{
castleb='t';
pos[0][0]='n';
color[0][0]='n'; //welcome back mr. pointer.
pos[2][0]='g';
color[2][0]='b';
pos[4][0]='n';
color[4][0]='n';
pos[3][0]='r';
color[3][0]='b';
check=IsInCheck(player);
if(check)//that is king not safe even now
{
pos[4][0]='g'; //undo the changes
color[4][0]='b';
pos[2][0]='n';
color[2][0]='n';
pos[0][0]='r'; //undo the changes
color[0][0]='b';
pos[3][0]='n';
color[3][0]='n';
turn=turn-1;
return;
}
bottom();
setfillpattern(pattern,GREEN);
bar(0,0,50,50);
bar(200,0,250,50);
setcolor(WHITE);
rectangle(0,0,50,50);
rectangle(200,0,250,50);
setfillpattern(pattern,MAGENTA);
bar(50,0,100,50);
bar(150,0,200,0);
setcolor(WHITE);
rectangle(50,0,100,50);
rectangle(150,0,200,0);
drawking(100,0,1); // 1 for black.
rookdraw(150,0,1); // 1 for black.
turn=turn-1;
}
bottom();
}
//------------happy end---------------------------------------------------//
#include<stdio.h>
#include<conio.h>
#include<dos.h>
char pos[8][8],color[8][8],blkc[8][8],player,check,turn=0;
char gwmoved='f',gbmoved='f',rbmoved='f',rwmoved='f',castlew='f',castleb='f';
union REGS i,o; //for hardware interaction
//functions prototype
void assign(char*,char*,char*);
void checkpwn(int x3,int y3);
void castling(char clr);
char chkpawnmov(int x1,int y1,char c1,int x3,int y3);
char chkrookmov(int x1,int y1,char c1,int x3,int y3);
char chkknightmov(int x1,int y1,char c1,int x3,int y3);
char chkbishopmov(int x1,int y1,char c1,int x3,int y3);
char chkqueenmov(int x1,int y1,char c1,int x3,int y3);
char chkkingmov(int x1,int y1,char c1,int x3,int y3);
void promote(int,int,char,char);
//------------main function------------------------------------------------//
void main()
{
int i1,j1,button,x,y,x1,y1,x2,y2,x3,y3,ch,i=0,j=0,c;
char id,nwblkclr,move='i';
assign((char *)pos,(char *)color,(char *)blkc);
clrscr();
display(); //for showing chess board
design();
setspare(); //for drawing rook etc.
restrictmouseptr(0,0,400,400); //restricts pointer of mouse in the area specified there.
showmouseptr();
while(1)
{
i=i+1;
setcolor(WHITE);
if(turn%2==0) //means white's turn
{
outtextxy(100,430,"White's turn");
player='w';
}
else if(turn%2!=0) //means black's turn
{
outtextxy(100,430,"black's turn");
player='b';
}
check=IsInCheck(player); //finds whether king in check.
if( check )
{
bottom();
outtextxy(100,430,"check your king");
}
while(i) //select piece
{
if(kbhit())
{
ch=getch();
if(ch=='\r') //escape at pressing enter.
exit(0);
}
getmousepos(&button,&x,&y); //gets click position of mouse
if((button== 001))
{
sound(1000);
i=i-1;
delay(50);
nosound();
delay(500);
}
}
x1=relposx(x)/50; //get its position and reference point
y1=relposy(y)/50;
id=pos[x1][y1];
c=color[x1][y1];
if( (turn%2==0 && c=='b') || (turn%2==1 && c=='w') || (id=='n'))
continue;
setcolor(BLUE);
rectangle(x1*50,y1*50,x1*50+50,y1*50+50);
j=j+1;
while(j)
{
if(kbhit())
{
ch=getch();
if(ch=='\r') //escape at pressing enter.
exit(0);
}
getmousepos(&button,&x2,&y2); //gets click position of mouse
if((button== 001))
{
sound(500);
j=j-1;
delay(100);
nosound();
delay(500);
}
}
x3=relposx(x2)/50; //gives standard x coordinate
y3=relposy(y2)/50; //gives standard y coordinate
nwblkclr=blkc[x3][y3];//get destination block's color;
setcolor(WHITE);
rectangle(x1*50,y1*50,x1*50+50,y1*50+50);
bottom();
//------------------------------castling-------------------------------------
if( x1==4 && y1==7 && pos[4][7]=='g' && x3==6 && y3==7 && pos[5][7]=='n'
&& pos[6][7]=='n' && c=='w' && rwmoved=='f' && gwmoved=='f'
&& check==0 && castlew=='f')
{
//we need not to check whether king will be in check at new position
// bcoz if it will be in check the changes will be undone automatically
//for the king not for the rook and turn will move only when move saves king.
castling(c);
}
if( x1==4 && y1==0 && pos[1][0]=='n' && pos[2][0]=='n' && pos[3][0]=='n'
&& pos[4][0]=='g' && c=='b' && rbmoved=='f' && gbmoved=='f'
&& check==0 && castleb=='f')
{
castling(c);
}
//-------------------------------------------------------------------------------
if(id=='p') //wheteher pawn?
{
move= chkpawnmov(x1,y1,c,x3,y3);
if(move=='v')
{
draw(x3,y3,x1,y1,blkc[x1][y1],c,nwblkclr,'p');
checkpwn(x3,y3);
}
}
else if(id=='r') //whether rook?
{
move=chkrookmov(x1,y1,c,x3,y3);
if(move=='v' )
{
draw(x3,y3,x1,y1,blkc[x1][y1],c,nwblkclr,'r');
}
if(c=='w')
rwmoved='t';
else if(c=='b')
rbmoved='t';
}
else if(id=='k') //whether knight?
{
move= chkknightmov(x1,y1,c,x3,y3);
if(move=='v' )
{
draw(x3,y3,x1,y1,blkc[x1][y1],c,nwblkclr,'k');
}
}
else if(id=='b') //whether bishop?
{
move=chkbishopmov(x1,y1,c,x3,y3);
if(move=='v')
{
draw(x3,y3,x1,y1,blkc[x1][y1],c,nwblkclr,'b');
}
}
else if(id=='q') //whether queen?
{
move=chkqueenmov(x1,y1,c,x3,y3);
if(move=='v')
draw(x3,y3,x1,y1,blkc[x1][y1],c,nwblkclr,'q');
}
else if(id=='g') //whether king?
{
move=chkkingmov(x1,y1,c,x3,y3);
if(move=='v')
draw(x3,y3,x1,y1,blkc[x1][y1],c,nwblkclr,'g');
if(c=='w')
gwmoved='t';
else if(c=='b')
gbmoved='t';
}
if(move=='v') turn=turn+1;
// if(check=IsInCheck(player));
}
// getch();
}
getmousepos(int *button,int* x,int* y) //gets mouse position.
{
i.x.ax=3;
int86(0x33,&i,&o);
*button=o.x.bx;
*x=o.x.cx;
*y=o.x.dx;
return;
}
showmouseptr() //shows pointer in graphics mode.
{
i.x.ax=1;
int86(0x33,&i,&o);
return;
}
void hidemouseptr() //hides pointer
{
i.x.ax=2;
int86(0x33,&i,&o);
return;
}
restrictmouseptr(int x1,int y1,int x2,int y2) //restrict pointer
{
i.x.ax=7;
i.x.cx=x1;
i.x.dx=x2;
int86(0x33,&i,&o);
i.x.ax=8;
i.x.cx=y1;
i.x.dx=y2;
int86(0x33,&i,&o);
return;
}
char chkpawnmov(int x1,int y1,char c1,int x3,int y3)
{ char mov,id,c; //checks each pawn (black and white) move.
id=pos[x3][y3]; //anybody?
c=color[x3][y3]; //color?
if(c1=='w') //white pawn?
{
if(x3==x1 && y3==y1-1 ) //move 1
{ if(id=='n') //id is none when no body is there
mov='v';
}
else if( (x3==x1+1 || x3==x1-1) && y3==y1-1)
{
if(id!='n' && c=='b') //black present?
{
mov='v';
send(id,c);
}
}
else if( (x3==x1 && y3==y1-2) && y1==6)
{
mov='v';
}
}
else if(c1=='b')
{
if(x3==x1 && y3==y1+1) //black and white both not there?
{ if(id=='n')
mov='v';
}
else if((x3==x1-1 || x3==x1+1) && y3==y1+1)
{ if(id!='n' && c=='w' ) //white there
{
mov='v'; //valid move
send(id,c);
}
}
else if( (x3==x1 && y3==y1+2) && y1==1)
{
mov='v';
}
}
else
{ //no action at invalid move.
mov='i';
}
return(mov);
}
draw(int x3,int y3,int x1,int y1,char blkc,char c,char nwblkclr,char id1)
{
int nx3,ny3,ncx,ncy,oldi,oldc;
oldi=pos[x3][y3]; oldc=color[x3][y3];
nx3=x3*50; ny3=y3*50; ncx=x1*50; ncy=y1*50;
pos[x1][y1]='n';
color[x1][y1]='n'; //welcome back mr. pointer.
pos[x3][y3]=id1;
color[x3][y3]=c;
check=IsInCheck(player);
if(check)//that is king not safe even now
{
pos[x1][y1]=id1; //undo the changes
color[x1][y1]=c;
pos[x3][y3]=oldi;
color[x3][y3]=oldc;
turn=turn-1;
return;
}
bottom();
if(nwblkclr=='g') //CLEAR NEW POSITION
setfillpattern(pattern,GREEN);
else if(nwblkclr=='m')
setfillpattern(pattern,MAGENTA);
hidemouseptr(); //necessary to avoid the effect of mouse pointer.
bar(nx3,ny3,nx3+50,ny3+50);
setcolor(WHITE);
rectangle(nx3,ny3,nx3+50,ny3+50);
if(blkc=='g') //CLEAR PREVIOUS POSITION
setfillpattern(pattern,GREEN);
else if(blkc=='m')
setfillpattern(pattern,MAGENTA); //clear the previous pos.
bar(ncx,ncy,ncx+50,ncy+50);
setcolor(WHITE);
rectangle(ncx,ncy,ncx+50,ncy+50);
if(id1=='p') //if pawn was selected then draw the pawn at new position.
{ if(c=='w')
pawndraw(nx3,ny3,0); // 0 for white.
else if(c=='b')
pawndraw(nx3,ny3,1);
}
else if(id1=='r') //if rook was selected
{ if(c=='w')
rookdraw(nx3,ny3,0); // 0 for white.
else if(c=='b')
rookdraw(nx3,ny3,1);
}
else if(id1=='k') //if knight was selected
{ if(c=='w')
drawknite(nx3,ny3,0); // 0 for white.
else if(c=='b')
drawknite(nx3,ny3,1);
}
else if(id1=='b') //if bishop was selected
{ if(c=='w')
drawbishop(nx3,ny3,0); // 0 for white.
else if(c=='b')
drawbishop(nx3,ny3,1);
}
else if(id1=='g')
{ if(c=='w')
drawking(nx3,ny3,0); // 0 for white.
else if(c=='b')
drawking(nx3,ny3,1);
}
else if(id1=='q')
{ if(c=='w')
drawqueen(nx3,ny3,0); // 0 for white.
else if(c=='b')
drawqueen(nx3,ny3,1);
}
showmouseptr();
return;
}
char chkrookmov(int x1,int y1,char c1,int x3,int y3)
{ //checks rook move for black and white as both have same moves.
int i;
char id,c,mov='v';
if( (x3==x1) || (y3==y1) )
{
if(x3>x1 && y3==y1)
{
for(i=x1+1;i<x3;i++)
{
id=pos[y3];
if(id!='n') //if black or white present move is invalid
{
mov='i';
}
}
}
else if(x3<x1 && y3==y1)
{
for(i=x1-1;i>x3;i--)
{
id=pos[y3];
if(id!='n') //if black or white
{
mov='i';
}
}
}
else if(x3==x1 && y3>y1)
{
for(i=y1+1;i<y3;i++)
{
id=pos[x3];
if(id!='n') //if black or white
{
mov='i';
}
}
}
else if(x3==x1 && y3<y1)
{
for(i=y1-1;i>y3;i--)
{
id=pos[x3];
if(id!='n') //if black or white
{
mov='i';
}
}
}
if(mov=='v')
{
id=pos[x3][y3];
c=color[x3][y3];
if(id!='n' && c=='b' && c1=='w') //means black at x3,y3
{
mov='v';
send(id,c);
}
else if(id!='n' && c=='w' && c1=='b') //means white there
{
mov='v';
send(id,c);
}
else if(id=='n')//none of the white or black even at x3 & y3?
mov='v';
else
mov='i';
}
}
else mov='i';
return(mov);
}
char chkknightmov(int x1,int y1,char c1,int x3,int y3)
{
//knight's moves are not affected if some entity lies in the way.
//it just jumps over them.Atmost only eight moves are possible.
int i,posk[8][2];
char id,c,mov='i';
posk[0][0]=x1+1;
posk[0][1]=y1-2;
posk[1][0]=x1-1;
posk[1][1]=y1-2;
posk[2][0]=x1+1;
posk[2][1]=y1+2;
posk[3][0]=x1-1;
posk[3][1]=y1+2;
posk[4][0]=x1+2;
posk[4][1]=y1-1;
posk[5][0]=x1+2;
posk[5][1]=y1+1;
posk[6][0]=x1-2;
posk[6][1]=y1+1;
posk[7][0]=x1-2;
posk[7][1]=y1-1;
for(i=0;i<=7;i++)
{
if(x3==posk[0] && y3==posk[1])
{
id=pos[x3][y3];
c=color[x3][y3];
if(id!='n'&& c=='b' && c1=='w')
{
mov='v'; //means black is there.
send(id,c);
}
else if(id!='n' && c=='w' && c1=='b')
{
mov='v'; //means white is there.
send(id,c);
}
else if(id=='n')
mov='v'; //means it is blank.
}
}
return(mov);
}
char chkbishopmov(int x1,int y1,char c1,int x3,int y3)
{
int m,n,m1,n1,i,j;
char mov,id,c;
m=x3-x1;
n=y3-y1;
if(abs(m)==abs(n))
{ if(m>0 && n<0)//move 1
{ for(i=x1+1,j=y1-1;(i<x3 && j>y3);i++,j--)
{
id=pos[j];
if(id!='n')
mov='i';
}
}
if(m>0 && n>0)//move 2
{ for(i=x1+1,j=y1+1;(i<x3 && j<y3);i++,j++)
{
id=pos[j];
if(id!='n')
mov='i';
}
}
if(m<0 && n<0)//move 3
{ for(i=x1-1,j=y1-1;(i!=x3 && j!=y3);i--,j--)
{
id=pos[j];
if(id!='n')
mov='i';
}
}
if(m<0 && n>0)//move 4
{ for(i=x1-1,j=y1+1;(i>x3 && j<y3);i--,j++)
{
id=pos[j];
if(id!='n')
mov='i';
}
}
if(mov!='i') //..that no body is in the way,now at x3 y3?
{
id=pos[x3][y3];
c=color[x3][y3];
if(id!='n'&& c=='b') //means black at x3,y3
{
if(c1=='w')
{
mov='v';
send(id,c);
}
else
mov='i';
}
else if(id!='n'&& c=='w') //means white there
{
if(c1=='b')
{
mov='v';
send(id,c);
}
else
{ mov='i';}
}
else if(id=='n')//none of the white or black even at x3 & y3?
mov='v';
}
} //means not at the x cross.
else
{
mov='i';
}
return(mov);
}
char chkqueenmov(int x1,int y1,char c1,int x3,int y3)
{
char mov1,mov2,mov3='i',id,c;
mov1=chkrookmov(x1,y1,c1,x3,y3);
mov2=chkbishopmov(x1,y1,c1,x3,y3);
if(mov1=='v' || mov2=='v') //move is valid and color is checked already.
{
mov3='v';
}
return(mov3);
}
char chkkingmov(int x1,int y1,char c1,int x3,int y3)
{
char id,c,mov='i';
id=pos[x3][y3];
c=color[x3][y3];
if(x3==x1+1 && y3==y1-1) //move 1
{
if(id=='n')
mov='v';
else if(c1=='w' && id!='n' && c=='b')//means black there.
{
mov='v';
send(id,c);
}
else if(c1=='b' && id!='n' && c=='w')//means white there.
{
mov='v';
send(id,c);
}
}
else if(x3==x1-1 && y3==y1-1) //move 2
{
if(id=='n')
mov='v';
else if(c1=='w' && id!='n' && c=='b')//means black there.
{
mov='v';
send(id,c);
}
else if(c1=='b' && id!='n' && c=='w')//means white there.
{
mov='v';
send(id,c);
}
}
else if(x3==x1-1 && y3==y1+1) //move 3
{
if(id=='n')
mov='v';
else if(c1=='w' && id!='n' && c=='b')//means black there.
{
mov='v';
send(id,c);
}
else if(c1=='b' && id!='n' && c=='w') //means white there.
{
mov='v';
send(id,c);
}
}
else if(x3==x1+1 && y3==y1+1) //move 4
{
if(id=='n' )
mov='v';
else if(c1=='w' && id!='n' && c=='b')//means black there.
{
mov='v';
send(id,c);
}
else if(c1=='b' && id!='n' && c=='w')//means white there.
{
mov='v';
send(id,c);
}
}
else if(x3==x1+1 && y3==y1) //move 5
{
if(id=='n')
mov='v';
else if(c1=='w' && id!='n' && c=='b')//means black there.
{
mov='v';
send(id,c);
}
else if(c1=='b' && id!='n' && c=='w') //means white there.
{
mov='v';
send(id,c);
}
}
else if(x3==x1 && y3==y1-1) //move 6
{
if(id=='n')
mov='v';
else if(c1=='w' && id!='n' && c=='b')//means black there.
{
mov='v';
send(id,c);
}
else if(c1=='b' && id!='n' && c=='w')//means white there.
{
mov='v';
send(id,c);
}
}
else if(x3==x1-1 && y3==y1) //move 7
{
if(id=='n')
mov='v';
else if(c1=='w' && id!='n' && c=='b')//means black there.
{
mov='v';
send(id,c);
}
else if(c1=='b' && id!='n' && c=='w')//means white there.
{
mov='v';
send(id,c);
}
}
else if(x3==x1 && y3==y1+1) //move 8
{
if(id=='n')
mov='v';
else if(c1=='w' && id!='n' && c=='b')//means black there.
{
mov='v';
send(id,c);
}
else if(c1=='b' && id!='n' && c=='w')//means white there.
{
mov='v';
send(id,c);
}
}
else //means move is not valid
{ mov='i';}
return(mov);
}
void assign(char* post,char* col,char* blk)
{ int i;
char val1[64]={"rpnnnnprkpnnnnpkbpnnnnpbqpnnnnpqgpnnnnpgbpnnnnpbkpnnnnpkrpnnnnpr"};
char val2[64]={"bbnnnnwwbbnnnnwwbbnnnnwwbbnnnnwwbbnnnnwwbbnnnnwwbbnnnnwwbbnnnnww"};
char val3[64]={"gmgmgmgmmgmgmgmggmgmgmgmmgmgmgmggmgmgmgmmgmgmgmggmgmgmgmmgmgmgmg"};
for(i=0;i<=63;i++,post++,col++,blk++)
{ *post=val1; *col=val2; *blk=val3; }
}
void checkpwn(int x3,int y3) //n for normal and p for promote.
{
char a,choice;
a=color[x3][y3];//whether pawn is white or black?
bottom();
setcolor(WHITE);
if(a=='b' && (x3>=0 && x3<=7) && y3==7)
{
outtextxy(100,430,"well done black..press b");
getch(); bottom();
outtextxy(10,430,"promote enter initial r,k,b,q");
choice=getch();
pos[x3][y3]=choice;
color[x3][y3]='b';
bottom();
promote(x3,y3,choice,'b');
}
else if(a=='w' && (x3>=0 && x3<=7) && y3==0)
{
outtextxy(100,430,"well done white..press w");
getch(); bottom();
outtextxy(10,430,"promote enter initial r,k,b,q");
choice=getch();
pos[x3][y3]=choice;
color[x3][y3]='w';
bottom();
promote(x3,y3,choice,'w');
}
return;
}
void promote(int x3,int y3,char choice,char col)
{
int color,nx3,ny3;
char bkc;
nx3=x3*50;ny3=y3*50;
if(col=='w')
color=0;
else if(col=='b')
color=1;
bkc= blkc[x3][y3];
if(bkc=='g') //CLEAR NEW POSITION
setfillpattern(pattern,GREEN);
else if(bkc=='m')
setfillpattern(pattern,MAGENTA);
hidemouseptr(); //necessary to avoid the effect of mouse pointer.
bar(nx3,ny3,nx3+50,ny3+50);
setcolor(WHITE);
rectangle(nx3,ny3,nx3+50,ny3+50);
if(choice=='r')
rookdraw(nx3,ny3,color);
else if(choice=='k')
drawknite(nx3,ny3,color);
else if(choice=='b')
drawbishop(nx3,ny3,color);
else if(choice=='q')
drawqueen(nx3,ny3,color);
else exit();
showmouseptr();
}
int IsInCheck(int current_side) //finds whether king in check.
{
int kx,ky;//position of king.
int i1 ,j1,x1,x2,x3,x4,y1,y2,y3,y4;
char xside;
if(current_side=='w')
xside='b';
else
xside='w'; // opposite current_side, who may be checking your patience
for(i1=1;i1<=8;i1++)
{
for(j1=1;j1<=8;j1++)
{
if( (pos[i1-1][j1-1]=='g') && (color[i1-1][j1-1]==current_side) )
{ kx=i1-1; ky=j1-1; }
}
}
//now we know position of king.So check others.
//knight first
for(i1=1;i1<=8;i1++)
{
x1=kx-2; y1=ky-2;
x2=kx+2; y2=ky+2;
x3=kx-1; y3=ky-1;
x4=kx+1; y4=ky+1;
if( pos[x3][y1]=='g' && color[x3][y1]==xside ) //move1
return (1);
if( pos[x1][y3]=='g' && color[x1][y3]==xside ) //move2
return(1);
if( pos[x1][y4]=='g' && color[x1][y4]==xside ) //move3
return(1);
if( pos[x3][y2]=='g' && color[x3][y2]==xside ) //move4
return(1);
if( pos[x4][y2]=='g' && color[x4][y2]==xside ) //move5
return(1);
if( pos[x2][y4]=='g' && color[x2][y4]==xside) //move6
return(1);
if( pos[x2][y3]=='g' && color[x2][y3]==xside ) //move7
return(1);
if( pos[x4][y1]=='g' && color[x4][y1]==xside) //move8
return(1);
}
//thank god u r safe from knight.
//check just down and down.
if(pos[kx][y4]=='g' && color[kx][y4]==xside)
return(1);
for(i1=kx,j1=y4;j1<=7;j1++)
{ if( ( pos[i1][j1]=='r' || pos[i1][j1]=='q' ) && color[i1][j1]==xside )
return(1);
if(pos[i1][j1]!='n')//that is something in between
break;
}
//check just up and up.
if(pos[kx][y3]=='g' && color[kx][y3]==xside)
return(1);
for(i1=kx,j1=y3;j1>=0;j1=j1-1)
{ if( (pos[i1][j1]=='r' || pos[i1][j1]=='q' ) && color[i1][j1]==xside )
return(1);
if(pos[i1][j1]!='n')//that is something in between
break;
}
//check just left and left
if(pos[x3][ky]=='g' && color[x3][ky]==xside )
return(1);
for(i1=x3,j1=ky;i1>=0;i1--)
{ if( (pos[i1][j1]=='r' || pos[i1][j1]=='q' ) && color[i1][j1]==xside )
return(1);
if(pos[i1][j1]!='n')//that is something in between
break;
}
//just right and right.
if(pos[x4][ky]=='g' && color[x4][ky]==xside )
return(1);
for(i1=x4,j1=ky;i1<=7;i1++)
{ if( (pos[i1][j1]=='r' || pos[i1][j1]=='q' ) && color[i1][j1]==xside )
return(1);
if(pos[i1][j1]!='n')//that is something in between
break;
}
// go right down ~ \_
if(x4<8 && y4<8 && color[x4][y4]==xside)//just right down
{ if(pos[x4][y4]=='g' || pos[x4][y4]=='q' || pos[x4][y4]=='b' )
return(1);
if(current_side=='b' && pos[x4][y4]=='p' && color[x4][y4]=='w')
return(1);
}
if(x4<8 && y4<8 && pos[x4][y4]=='n')//along the diagonal
{
for(i1=x4,j1=y4; i1<=7 && j1<=7;i1++,j1++)
{
if( color[i1][j1]==xside && (pos[i1][j1]=='q'||pos[i1][j1]=='b') )
return(1);
if(pos[i1][j1]!='n')
break;
}
}
// go left down _/-
if(x3>0 && y4<8 && color[x3][y4]==xside)
{ if(pos[x3][y4]=='g' || pos[x3][y4]=='q' || pos[x3][y4]=='b')
return(1);
if(current_side=='b' && pos[x3][y4]=='p' && color[x3][y4]=='w')
return(1);
}
if(x3>0 && y4<8 && pos[x3][y4]=='n')
{
for(i1=x3,j1=y4; i1>=0 && j1<=7;i1--,j1++)
{
if(color[i1][j1]==xside && (pos[i1][j1]=='q' || pos[i1][j1]=='b') )
return(1);
if(pos[i1][j1]!='n')
break;
}
}
// go left up
if(x3>0 && y3>0 && color[x3][y3]==xside)//means sth of opposite party,
{ if(pos[x3][y3]=='g' || pos[x3][y3]=='q' || pos[x3][y3]=='b')
return(1);
if(current_side=='w' && pos[x3][y3]=='p' && color[x3][y3]=='b')
return(1);
}
if(x3>0 && y3>0 && pos[x3][y3]=='n') //means no body there.
{
for(i1=x3,j1=y3; i1>=0 && j1>=0;i1--,j1--)
{
if(color[i1][j1]==xside && (pos[i1][j1]=='q' || pos[i1][j1]=='b') )
return(1);
if(pos[i1][j1]!='n')//means sth of our party,so forgive him.
break;
}
}
// gojust right up and up.
if(x4<8 && y3>0 && color[x4][y3]==xside)
{ if(pos[x4][y3]=='g' || pos[x4][y3]=='q' || pos[x4][y3]=='b')
return(1);
if(current_side=='w' && pos[x4][y3]=='p' && color[x4][y3]=='b')
return(1);
}
if(x4<8 && y3>0 && pos[x4][y3]=='n')
{
for(i1=x4,j1=y3; i1<=7 && j1>=0;i1++,j1--)
{
if(color[i1][j1]==xside && (pos[i1][j1]=='q' || pos[i1][j1]=='b') )
return(1);
if(pos[i1][j1]!='n')
break;
}
}
return 0;//and finally none of the case means not in check.
}
void castling(char clr)
{
outtextxy(100,430,"CASTLING");
if(clr=='w')
{
castlew='t';
pos[4][7]='n';
color[4][7]='n'; //welcome back mr. pointer.
pos[6][7]='g';
color[6][7]='w';
pos[7][7]='n';
color[7][7]='n';
pos[5][7]='r';
color[5][7]='w';
check=IsInCheck(player);
if(check)//that is king not safe even now
{
pos[4][7]='g'; //undo the changes
color[4][7]='w';
pos[6][7]='n';
color[6][7]='n';
pos[7][7]='r'; //undo the changes
color[7][7]='w';
pos[5][7]='n';
color[5][7]='n';
turn=turn-1;
return;
}
bottom();
hidemouseptr();
setfillpattern(pattern,GREEN);
bar(350,350,400,400);
bar(250,350,300,400);
setcolor(WHITE);
rectangle(350,350,400,400);
rectangle(250,350,300,400);
setfillpattern(pattern,MAGENTA);
bar(300,350,350,400);
bar(200,350,250,400);
setcolor(WHITE);
rectangle(300,350,350,400);
rectangle(200,350,250,400);
drawking(300,350,0); // 0 for white.
rookdraw(250,350,0); // 0 for white.
turn=turn-1;
showmouseptr();
}
else if(clr=='b')
{
castleb='t';
pos[0][0]='n';
color[0][0]='n'; //welcome back mr. pointer.
pos[2][0]='g';
color[2][0]='b';
pos[4][0]='n';
color[4][0]='n';
pos[3][0]='r';
color[3][0]='b';
check=IsInCheck(player);
if(check)//that is king not safe even now
{
pos[4][0]='g'; //undo the changes
color[4][0]='b';
pos[2][0]='n';
color[2][0]='n';
pos[0][0]='r'; //undo the changes
color[0][0]='b';
pos[3][0]='n';
color[3][0]='n';
turn=turn-1;
return;
}
bottom();
setfillpattern(pattern,GREEN);
bar(0,0,50,50);
bar(200,0,250,50);
setcolor(WHITE);
rectangle(0,0,50,50);
rectangle(200,0,250,50);
setfillpattern(pattern,MAGENTA);
bar(50,0,100,50);
bar(150,0,200,0);
setcolor(WHITE);
rectangle(50,0,100,50);
rectangle(150,0,200,0);
drawking(100,0,1); // 1 for black.
rookdraw(150,0,1); // 1 for black.
turn=turn-1;
}
bottom();
}
//------------happy end---------------------------------------------------//












