Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


C Programming Code Examples

C > Games and Graphics Code Examples

Chess for two humans

/* Chess for two humans */ #include<stdio.h> #include<conio.h> #include<graphics.h> void blk(int,int); void display(); //enum bool{TRUE,FALSE}; /*void main() //MAIN FUNCTION { display(); getch(); } */ void display() //DISPLAY TO SHOW THE BOARD { int gd=DETECT,gm,i,j,l,m,b; char pattern[]={0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF}; int arrodd[32][2]={ {0,0},{100,0},{200,0},{300,0},{50,50},{150,50} ,{250,50},{350,50},{0,100},{100,100},{200,100},{300,100} ,{50,150},{150,150},{250,150},{350,150},{0,200},{100,200} ,{200,200},{300,200},{50,250},{150,250},{250,250},{350,250}, {0,300},{100,300},{200,300},{300,300},{50,350},{150,350}, {250,350},{350,350} }; int arrevn[33][2]={{50,0},{150,0},{250,0},{350,0},{0,50},{100,50},{200,50}, {300,50},{50,100},{150,100},{250,100},{350,100},{0,150}, {100,150},{200,150},{300,150},{50,200},{150,200},{250,200}, {350,200},{0,250},{100,250},{200,250},{300,250},{50,300}, {150,300},{250,300},{350,300},{0,350},{100,350},{200,350}, {300,350}}; clrscr(); initgraph(&gd,&gm,""); for(i=0;i<=400;i=i+50) //to draw vertical lines of the chess board. { line(i,0,i,400); } for(j=0;j<=400;j=j+50) //to draw the horizontal lines of the chess board. { line(0,j,400,j); } blk(420,0); blk(420,250); line(0,430,0,460); //to draw the display at the bottom line(0,430,400,430); line(0,460,400,460); line(400,430,400,460); settextstyle(7,HORIZ_DIR,3); setfillpattern(&pattern,GREEN); for(b=0;b<=31;b++) { bar(arrodd[b][0],arrodd[b][1],arrodd[b][0]+50,arrodd[b][1]+50); rectangle(arrodd[b][0],arrodd[b][1],arrodd[b][0]+50,arrodd[b][1]+50); } setfillpattern(&pattern,DARKGRAY); for(m=0;m<=31;m++) { bar(arrevn[m][0],arrevn[m][1],arrevn[m][0]+50,arrevn[m][1]+50); rectangle(arrevn[m][0],arrevn[m][1],arrevn[m][0]+50,arrevn[m][1]+50); } } void blk(int q,int r) { int y,z; for(y=q;y<=(q+200);y=y+50) { line(y,r,y,r+200);} for(z=r;z<=(r+200);z=z+50) { line(q,z,q+200,z);} } //Source file// #include<akhchs.c> //we used akhchs.c as header file #include<stdio.h> #include<conio.h> #include<dos.h> char pos[8][8],color[8][8]; void assign(char*,char*); struct block{ //structure for every block int curx,cury; char clr,blkcolor; }; union REGS i,o; //for hardware interaction char getblkcolor(int,int,int,int,char); //functions prototype char chkpawnmov(struct block* selected,int x3,int y3); char chkrookmov(struct block* selected,int x3,int y3); char chkknightmov(struct block* selected,int x3,int y3); char chkbishopmov(struct block* selected,int x3,int y3); char chkqueenmov(struct block* selected,int x3,int y3); char chkkingmov(struct block* selected,int x3,int y3); void checkpwn(int,int); void main() { int i1,j1,button,x,y,x1,y1,x2,y2,x3,y3,ch,i=0,j=0,z=1,turn=0; struct block *selected,pw[8],pb[8],rookw[2],rookb[2],bishopw[2],bishopb[2]; struct block kingw,kingb,queenw,queenb,knightw[2],knightb[2]; char id,nwblkclr,move='i'; assign(pos,color); clrscr(); display(); //for showing chess board design(); setspare(); //for drawing rook etc. setposw(pw); //sets position of black and setposb(pb); //white pawns with color setposrw(rookw,bishopw,knightw,&kingw,&queenw);//sets position of whites setposrb(rookb,bishopb,knightb,&kingb,&queenb);//sete position of blacks //restrictmouseptr(0,0,400,400); //restricts pointer of mouse in the area specified there. showmouseptr(); while(z) { i=i+1; setcolor(WHITE); if(turn%2==0) //means white's turn outtextxy(100,430,"white's turn"); else if(turn%2!=0) //means black's turn outtextxy(100,430,"black's turn"); while(i) { if(kbhit()) { ch=getch(); if(ch==' ') //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; y1=relposy(y)/50; gotoxy(6,28); if(x1==pw[0].curx && y1==pw[0].cury ) { selected=&pw[0]; id='p'; } else if(x1==pw[1].curx && y1==pw[1].cury ) { selected=&pw[1]; id='p'; } else if(x1==pw[2].curx && y1==pw[2].cury ) { selected=&pw[2]; id='p'; } else if(x1==pw[3].curx && y1==pw[3].cury ) { selected=&pw[3]; id='p'; } else if(x1==pw[4].curx && y1==pw[4].cury ) { selected=&pw[4]; id='p'; } else if(x1==pw[5].curx && y1==pw[5].cury ) { selected=&pw[5]; id='p'; } else if(x1==pw[6].curx && y1==pw[6].cury ) { selected=&pw[6]; id='p'; } else if(x1==pw[7].curx && y1==pw[7].cury ) { selected=&pw[7]; id='p'; } else if(x1==pb[0].curx && y1==pb[0].cury ) { selected=&pb[0]; id='p'; } else if(x1==pb[1].curx && y1==pb[1].cury ) { selected=&pb[1]; id='p'; } else if(x1==pb[2].curx && y1==pb[2].cury ) { selected=&pb[2]; id='p'; } else if(x1==pb[3].curx && y1==pb[3].cury ) { selected=&pb[3]; id='p'; } else if(x1==pb[4].curx && y1==pb[4].cury ) { selected=&pb[4]; id='p'; } else if(x1==pb[5].curx && y1==pb[5].cury ) { selected=&pb[5]; id='p'; } else if(x1==pb[6].curx && y1==pb[6].cury ) { selected=&pb[6]; id='p'; } else if(x1==pb[7].curx && y1==pb[7].cury ) { selected=&pb[7]; id='p'; } else if(x1==rookw[0].curx && y1==rookw[0].cury) { selected=&rookw[0]; id='r'; } else if(x1==rookw[1].curx && y1==rookw[1].cury) { selected=&rookw[1]; id='r'; } else if(x1==rookb[0].curx && y1==rookb[0].cury) { selected=&rookb[0]; id='r'; } else if(x1==rookb[1].curx && y1==rookb[1].cury) { selected=&rookb[1]; id='r'; } else if(x1==bishopw[0].curx && y1==bishopw[0].cury) { selected=&bishopw[0];id='b'; } else if(x1==bishopw[1].curx && y1==bishopw[1].cury) { selected=&bishopw[1];id='b'; } else if(x1==bishopb[0].curx && y1==bishopb[0].cury) { selected=&bishopb[0];id='b'; } else if(x1==bishopb[1].curx && y1==bishopb[1].cury) { selected=&bishopb[1];id='b'; } else if(x1==kingw.curx && y1==kingw.cury) { selected=&kingw; id='g'; } else if(x1==queenw.curx && y1==queenw.cury) { selected=&queenw; id='q'; } else if(x1==kingb.curx && y1==kingb.cury) { selected=&kingb; id='g'; } else if(x1==queenb.curx && y1==queenb.cury) { selected=&queenb; id='q'; } else if(x1==knightw[0].curx && y1==knightw[0].cury) { selected=&knightw[0];id='k'; } else if(x1==knightw[1].curx && y1==knightw[1].cury) { selected=&knightw[1];id='k'; } else if(x1==knightb[0].curx && y1==knightb[0].cury) { selected=&knightb[0];id='k'; } else if(x1==knightb[1].curx && y1==knightb[1].cury) { selected=&knightb[1];id='k'; } if((turn%2==0 && selected->clr=='b')||(turn%2!=0 && selected->clr=='w')) exit(); turn=turn+1; j=j+1; while(j) { if(kbhit()) { ch=getch(); if(ch==' ') //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=getblkcolor(x1,y1,x3,y3,selected->blkcolor);//get destination block's color; gotoxy(6,28); bottom(); //clears bottom display if(id=='p') //wheteher pawn? { move= chkpawnmov(selected,x3,y3); if(move=='v') { draw(x3,y3,selected,nwblkclr,'p'); checkpwn(x3,y3); } } else if(id=='r') //whether rook? { move=chkrookmov(selected,x3,y3); if(move=='v' ) { draw(x3,y3,selected,nwblkclr,'r'); } } else if(id=='k') //whether knight? { move= chkknightmov(selected,x3,y3); if(move=='v' ) { draw(x3,y3,selected,nwblkclr,'k'); } } else if(id=='b') //whether bishop? { move=chkbishopmov(selected,x3,y3); if(move=='v') { draw(x3,y3,selected,nwblkclr,'b'); } } else if(id=='q') //whether queen? { move=chkqueenmov(selected,x3,y3); if(move=='v') draw(x3,y3,selected,nwblkclr,'q'); } else if(id=='g') //whether king? { move=chkkingmov(selected,x3,y3); if(move=='v') draw(x3,y3,selected,nwblkclr,'g'); } if(move=='i') turn=turn-1; } 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=0; 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; } setposw(struct block* pawnw) //sete each white pawn. { int count,togl; for(count=0,togl=0;count<=7;count++,togl++) { pawnw->curx=count; pawnw->cury=6; pawnw->clr='b'; if(togl%2==0) pawnw->blkcolor='g'; else if(togl%2==1) pawnw->blkcolor='m'; pawnw++; } return; } setposb(struct block* pawnb) //sets each black pawn. { int count,togl; for(count=0,togl=0;count<=7;count++,togl++) { pawnb->curx=count; pawnb->cury=1; pawnb->clr='w'; if(togl%2==0) pawnb->blkcolor='m'; else if(togl%2==1) pawnb->blkcolor='g'; pawnb++; } return; } char chkpawnmov(struct block* selected,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(selected->clr=='w') //white pawn? { if(x3==selected->curx && y3==selected->cury+1 ) //move 1 { if(id=='n') //id is none when no body is there mov='v'; } else if( (x3==selected->curx+1 || x3==selected->curx-1) && y3==selected->cury+1) { if(id!='n' && c=='b') //black present? { mov='v'; send(id,c); } } } else if(selected->clr=='b') { if(x3==selected->curx && y3==selected->cury-1) //black and white both not there? { if(id=='n') mov='v'; } else if((x3==selected->curx-1 || x3==selected->curx+1) && y3==selected->cury-1) { if(id!='n' && c=='w' ) //white there { mov='v'; //valid move send(id,c); } } } else { //no action at invalid move. mov='i'; } return(mov); } setposrw(struct block* rookw,struct block* bishopw,struct block* knightw,struct block* kingw,struct block* queenw) { //it sets the white rook,knight,bishop etc. int i; rookw->curx=rookw->cury=7; rookw->clr='w'; rookw->blkcolor='m'; rookw++; rookw->curx=7;rookw->cury=7; rookw->clr='w'; rookw->blkcolor='g'; bishopw->curx=2; bishopw->cury=7; bishopw->clr='w'; bishopw->blkcolor='m'; bishopw++; bishopw->curx=5; bishopw->cury=7; bishopw->clr='w'; bishopw->blkcolor='g'; knightw->curx=1; knightw->cury=7; knightw->clr='w'; knightw->blkcolor='g'; knightw++; knightw->curx=6; knightw->cury=7; knightw->clr='w'; knightw->blkcolor='m'; kingw->curx=3;kingw->cury=7; kingw->clr='w';kingw->blkcolor='m'; queenw->curx=4;queenw->cury=7; queenw->clr='w';queenw->blkcolor='g'; return; } setposrb(struct block* rookb,struct block* bishopb,struct block* knightb,struct block* kingb,struct block* queenb) { //it sets the current coords of black rook,bishop,knight etc. int i; rookb->curx=0;rookb->cury=0; rookb->clr='b'; rookb->blkcolor='g'; rookb++; rookb->curx=7;rookb->cury=0; rookb->clr='b'; rookb->blkcolor='m'; bishopb->curx=2; bishopb->cury=0; bishopb->clr='b'; bishopb->blkcolor='g'; bishopb++; bishopb->curx=5; bishopb->cury=0; bishopb->clr='b'; bishopb->blkcolor='m'; knightb->curx=1; knightb->cury=0; knightb->clr='b'; knightb->blkcolor='m'; knightb++; knightb->curx=6; knightb->cury=0; knightb->clr='b'; knightb->blkcolor='g'; kingb->curx=3;kingb->cury=0; kingb->clr='b';kingb->blkcolor='m'; queenb->curx=4;queenb->cury=0; queenb->clr='b';queenb->blkcolor='g'; return; } draw(int x3,int y3,struct block* selected,char nwblkclr,char id1) { int nx3,ny3,ncx,ncy; nx3=x3*50; ny3=y3*50; ncx=(selected->curx)*50; ncy=(selected->cury)*50; 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(selected->blkcolor=='g') //CLEAR PREVIOUS POSITION setfillpattern(pattern,GREEN); else if(selected->blkcolor=='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(selected->clr=='w') pawndraw(nx3,ny3,0); // 0 for white. else if(selected->clr=='b') pawndraw(nx3,ny3,1); } else if(id1=='r') //if rook was selected { if(selected->clr=='w') rookdraw(nx3,ny3,0); // 0 for white. else if(selected->clr=='b') rookdraw(nx3,ny3,1); } else if(id1=='k') //if knight was selected { if(selected->clr=='w') drawknite(nx3,ny3,0); // 0 for white. else if(selected->clr=='b') drawknite(nx3,ny3,1); } else if(id1=='b') //if bishop was selected { if(selected->clr=='w') drawbishop(nx3,ny3,0); // 0 for white. else if(selected->clr=='b') drawbishop(nx3,ny3,1); } else if(id1=='g') { if(selected->clr=='w') drawking(nx3,ny3,0); // 0 for white. else if(selected->clr=='b') drawking(nx3,ny3,1); } else if(id1=='q') { if(selected->clr=='w') drawqueen(nx3,ny3,0); // 0 for white. else if(selected->clr=='b') drawqueen(nx3,ny3,1); } showmouseptr(); pos[selected->curx][selected->cury]='n'; color[selected->curx][selected->cury]='n'; //welcome back mr. pointer. selected->curx=x3; selected->cury=y3; selected->blkcolor=nwblkclr; pos[x3][y3]=id1; color[x3][y3]=selected->clr; return; } char chkrookmov(struct block* selected,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==selected->curx) || (y3==selected->cury) ) { if(x3>selected->curx && y3==selected->cury) { for(i=selected->curx+1;i<x3;i++) { id=pos[i][y3]; // c=color[i][y3]; if(id!='n') //if black or white present move is invalid { mov='i'; } } } else if(x3<selected->curx && y3==selected->cury) { for(i=selected->curx-1;i>x3;i--) { id=pos[i][y3]; // c=color[i][y3]; if(id!='n') //if black or white { mov='i'; } } } else if(x3==selected->curx && y3>selected->cury) { for(i=selected->cury+1;i<y3;i++) { id=pos[x3][i]; // c=color[x3][i]; if(id!='n') //if black or white { mov='i'; } } } else if(x3==selected->curx && y3<selected->cury) { for(i=selected->cury-1;i>y3;i--) { id=pos[x3][i]; // c=color[x3][i]; 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' && selected->clr=='w') //means black at x3,y3 { mov='v'; send(id,c); } else if(id!='n' && c=='w' && selected->clr=='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(struct block* selected,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]=selected->curx+1; posk[0][1]=selected->cury-2; posk[1][0]=selected->curx-1; posk[1][1]=selected->cury-2; posk[2][0]=selected->curx+1; posk[2][1]=selected->cury+2; posk[3][0]=selected->curx-1; posk[3][1]=selected->cury+2; posk[4][0]=selected->curx+2; posk[4][1]=selected->cury-1; posk[5][0]=selected->curx+2; posk[5][1]=selected->cury+1; posk[6][0]=selected->curx-2; posk[6][1]=selected->cury+1; posk[7][0]=selected->curx-2; posk[7][1]=selected->cury-1; for(i=0;i<=7;i++) { if(x3==posk[i][0] && y3==posk[i][1]) { id=pos[x3][y3]; c=color[x3][y3]; if(id!='n'&& c=='b' && selected->clr=='w') { mov='v'; //means black is there. send(id,c); } else if(id!='n'&&c=='w' && selected->clr=='b') { mov='v'; //means white is there. send(id,c); } else if(id=='n') mov='v'; //means it is blank. } } return(mov); } char chkbishopmov(struct block* selected,int x3,int y3) { int m,n,m1,n1,i,j; char mov,id,c; m=x3-selected->curx; n=y3-selected->cury; if(abs(m)==abs(n)) { if(m>0 && n<0)//move 1 { for(i=selected->curx+1,j=selected->cury-1;(i<x3 && j>y3);i=i++,j--) { id=pos[i][j]; // c=color[i][j]; if(id!='n') mov='i'; } } if(m>0 && n>0)//move 2 { for(i=selected->curx+1,j=selected->cury+1;(i<x3 && j<y3);i++,j++) { id=pos[i][j]; // c=color[i][j]; if(id!='n') mov='i'; } } if(m<0 && n<0)//move 3 { for(i=selected->curx-1,j=selected->cury-1;(i!=x3 && j!=y3);i--,j--) { id=pos[i][j]; // c=color[i][j]; if(id!='n') mov='i'; } } if(m<0 && n>0)//move 4 { for(i=selected->curx-1,j=selected->cury+1;(i>x3 && j<y3);i--,j++) { id=pos[i][j]; // c=color[i][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(selected->clr=='w') { mov='v'; send(id,c); } else mov='i'; } else if(id!='n'&& c=='w') //means white there { if(selected->clr=='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(struct block* selected,int x3,int y3) { char mov1,mov2,mov3='i',id,c; mov1=chkrookmov(selected,x3,y3); mov2=chkbishopmov(selected,x3,y3); if(mov1=='v' || mov2=='v') //move is valid and color is checked already. { mov3='v'; } return(mov3); } char chkkingmov(struct block* selected,int x3,int y3) { char id,c,mov='i'; id=pos[x3][y3]; c=pos[x3][y3]; if(x3==selected->curx+1 && y3==selected->cury-1) //move 1 { if(id=='n') mov='v'; else if(selected->clr=='w' && id!='n' && c=='b')//means black there. { mov='v'; send(id,c); } else if(selected->clr=='b' && id!='n' && c=='w')//means white there. { mov='v'; send(id,c); } } else if(x3==selected->curx-1 && y3==selected->cury-1) //move 2 { if(id=='n') mov='v'; else if(selected->clr=='w' && id!='n' && c=='b')//means black there. { mov='v'; send(id,c); } else if(selected->clr=='b' && id!='n' && c=='w')//means white there. { mov='v'; send(id,c); } } else if(x3==selected->curx-1 && y3==selected->cury+1) //move 3 { if(id=='n') mov='v'; else if(selected->clr=='w' && id!='n' && c=='b')//means black there. { mov='v'; send(id,c); } else if(selected->clr=='b' && id!='n' && c=='w') //means white there. { mov='v'; send(id,c); } } else if(x3==selected->curx+1 && y3==selected->cury+1) //move 4 { if(id=='n' ) mov='v'; else if(selected->clr=='w' && id!='n' && c=='b')//means black there. { mov='v'; send(id,c); } else if(selected->clr=='b' && id!='n' && c=='w')//means white there. { mov='v'; send(id,c); } } else if(x3==selected->curx+1 && y3==selected->cury) //move 5 { if(id=='n') mov='v'; else if(selected->clr=='w' && id!='n' && c=='b')//means black there. { mov='v'; send(id,c); } else if(selected->clr=='b' && id!='n' && c=='w') //means white there. { mov='v'; send(id,c); } } else if(x3==selected->curx && y3==selected->cury-1) //move 6 { if(id=='n') mov='v'; else if(selected->clr=='w' && id!='n' && c=='b')//means black there. { mov='v'; send(id,c); } else if(selected->clr=='b' && id!='n' && c=='w')//means white there. { mov='v'; send(id,c); } } else if(x3==selected->curx-1 && y3==selected->cury) //move 7 { if(id=='n') mov='v'; else if(selected->clr=='w' && id!='n' && c=='b')//means black there. { mov='v'; send(id,c); } else if(selected->clr=='b' && id!='n' && c=='w')//means white there. { mov='v'; send(id,c); } } else if(x3==selected->curx && y3==selected->cury+1) //move 8 { if(id=='n') mov='v'; else if(selected->clr=='w' && id!='n' && c=='b')//means black there. { mov='v'; send(id,c); } else if(selected->clr=='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) { int i; char val1[64]={"rpnnnnprkpnnnnpkbpnnnnpbgpnnnnpqqpnnnnpgbpnnnnpbkpnnnnpkrpnnnnpr"}; char val2[64]={"wwnnnnbbwwnnnnbbwwnnnnbbwwnnnnbbwwnnnnbbwwnnnnbbwwnnnnbbwwnnnnbb"}; for(i=0;i<=63;i++,post++,col++) { *post=val1[i]; *col=val2[i]; } } void checkpwn(int x3,int y3) { char a; a=color[x3][y3];//whether pawn is white or black? bottom(); setcolor(WHITE); if(a=='w' && (x3>=0 && x3<=7) && y3==7) { outtextxy(100,430,"well done white..press w"); getch(); bottom(); } else if(a=='b' && (x3>=0 && x3<=7) && y3==0) { outtextxy(100,430,"well done black..press b"); getch(); bottom(); } return; }

setcolor() function is used to set the foreground color in graphics mode. After resetting the foreground color you will get the text or any other shape which you want to draw in that color. setcolor sets the current drawing color to color, which can range from 0 to getmaxcolor. The current drawing color is the value to which pixels are set when lines, and so on are drawn. The drawing colors shown below are available for the CGA and EGA, respectively.

Function clrscr() clears the screen and moves the cursor to the upper left-hand corner of the screen. If you are using the GCC compiler, use system function to execute the clear/cls command. clrscr() function is also a non-standard function defined in "conio.h" header. This function is used to clear the console screen. It is often used at the beginning of the program (mostly after variable declaration but not necessarily) so that the console is clear for our output.

Delay function is used to suspend execution of a program for a particular time. delay() function requires a parameter which should be a number, defining the milliseconds for the delay. To use delay function in your program you should include the "dos.h" header file which is not a part of standard C library. Here unsigned int is the number of milliseconds (remember 1 second = 1000 milliseconds).

A union is a special data type available in C that allows to store different data types in the same memory location. You can define a union with many members, but only one member can contain a value at any given time. Unions provide an efficient way of using the same memory location for multiple-purpose. To define a union, you must use the union statement in the same way as you did while defining a structure. The union statement defines a new data type with more than one member for your program. The format of the union statement is as follows:

An expression containing logical operator returns either 0 or 1 depending upon whether expression results true or false. Logical operators are commonly used in decision making in C programming. These operators are used to perform logical operations and used with conditional statements like C if-else statements.

The nosound() function in C language is used to stop the sound played by sound() function. nosound() function is simply silent the system. Sound function produces the sound of a specified frequency and nosound function turn off the PC speaker.

The exit() function is used to terminate a process or function calling immediately in the program. It means any open file or function belonging to the process is closed immediately as the exit() function occurred in the program. The exit() function is the standard library function of the C, which is defined in the stdlib.h header file. So, we can say it is the function that forcefully terminates the current program and transfers the control to the operating system to exit the program. The exit(0) function determines the program terminates without any error message, and then the exit(1) function determines the program forcefully terminates the execution process.

#include is a way of including a standard or user-defined file in the program and is mostly written at the beginning of any C/C++ program. This directive is read by the preprocessor and orders it to insert the content of a user-defined or system header file into the following program. These files are mainly imported from an outside source into the current program. The process of importing such files that might be system-defined or user-defined is known as File Inclusion. This type of preprocessor directive tells the compiler to include a file in the source code program. Here are the two types of file that can be included using #include:

line() is a library function of graphics.c in c programming language which is used to draw a line from two coordinates. line() function is used to draw a line from a point(x1,y1) to point(x2,y2) i.e. (x1,y1) and (x2,y2) are end points of the line.

The kbhit is basically the Keyboard Hit. Function kbhit in C is used to determine if a key has been pressed or not. This function is present at conio.h header file. So for using this, we have to include this header file into our code. The functionality of kbhit() is that, when a key is pressed it returns nonzero value, otherwise returns zero. kbhit() is used to determine if a key has been pressed or not. If a key has been pressed then it returns a non zero value otherwise returns zero.

Settextstyle function is used to change the way in which text appears, using it we can modify the size of text, change direction of text and change the font of text. settextstyle sets the text font, the direction in which text is displayed, and the size of the characters. A call to settextstyle affects all text output by outtext and outtextxy.

The gotoxy() function places the cursor at the desired location on the screen. This means it is possible to change the cursor location on the screen using the gotoxy() function. It is basically used to print text wherever the cursor is moved.

rectangle() is used to draw a rectangle. Coordinates of left top and right bottom corner are required to draw the rectangle. left specifies the X-coordinate of top left corner, top specifies the Y-coordinate of top left corner, right specifies the X-coordinate of right bottom corner, bottom specifies the Y-coordinate of right bottom corner.

outtextxy displays a text string in the viewport at the given position (x, y), using the current justification settings and the current font, direction, and size. To maintain code compatibility when using several fonts, use textwidth and textheight to determine the dimensions of the string. If a string is printed with the default font using outtext or outtextxy, any part of the string that extends outside the current viewport is truncated. outtextxy is for use in graphics mode; it will not work in text mode.

A goto statement in C programming language provides an unconditional jump from the 'goto' to a labeled statement in the same function. The goto statement is known as jump statement in C. As the name suggests, goto is used to transfer the program control to a predefined label. The goto statment can be used to repeat some part of the code for a particular condition. It can also be used to break the multiple loops which can't be done by using a single break statement.

The for loop is used in the case where we need to execute some part of the code until the given condition is satisfied. The for loop is also called as a per-tested loop. It is better to use for loop if the number of iteration is known in advance. The for-loop statement is a very specialized while loop, which increases the readability of a program. It is frequently used to traverse the data structures like the array and linked list.

In C, the "main" function is treated the same as every function, it has a return type (and in some cases accepts inputs via parameters). The only difference is that the main function is "called" by the operating system when the user runs the program. Thus the main function is always the first code executed when a program starts. main() function is a user defined, body of the function is defined by the programmer or we can say main() is programmer/user implemented function, whose prototype is predefined in the compiler. Hence we can say that main() in c programming is user defined as well as predefined because it's prototype is predefined. main() is a system (compiler) declared function whose defined by the user, which is invoked automatically by the operating system when program is being executed.

Our system can create various sounds on different frequencies. The sound() is very useful as it can create very nice music with the help of programming and our user can enjoy music during working in out the program. Sound function produces the sound of a specified frequency. Used for adding music to a C program, try to use some random values in loop, vary delay and enjoy.

The if...else statement executes two different codes depending upon whether the test expression is true or false. Sometimes, a choice has to be made from more than 2 possibilities. The if...else ladder allows you to check between multiple test expressions and execute different statements. In C/C++ if-else-if ladder helps user decide from among multiple options. The C/C++ if statements are executed from the top down. As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the C else-if ladder is bypassed. If none of the conditions is true, then the final else statement will be executed.

While loop is also known as a pre-tested loop. In general, a while loop allows a part of the code to be executed multiple times depending upon a given boolean condition. It can be viewed as a repeating if statement. The while loop is mostly used in the case where the number of iterations is not known in advance. The while loop evaluates the test expression inside the parentheses (). If test expression is true, statements inside the body of while loop are executed. Then, test expression is evaluated again. The process goes on until test expression is evaluated to false. If test expression is false, the loop terminates.

bar() function is a C graphics function that is used to draw graphics in the C programming language. The graphics.h header contains functions that work for drawing graphics. The bar() function is also defined in the header file. The bar() function is used to draw a bar ( of bar graph) which is a 2-dimensional figure. It is filled rectangular figure. The function takes four arguments that are the coordinates of (X, Y) coordinates of the top-left corner of the bar {left and top } and (X, Y) coordinates of the bottom-right corner of the bar {right and bottom}.

Pointers in C are easy and fun to learn. Some C programming tasks are performed more easily with pointers, and other tasks, such as dynamic memory allocation, cannot be performed without using pointers. So it becomes necessary to learn pointers to become a perfect C programmer. Let's start learning them in simple and easy steps. As you know, every variable is a memory location and every memory location has its address defined which can be accessed using ampersand (&) operator, which denotes an address in memory. Consider the following example, which prints the address of the variables defined. A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location. Like any variable or constant, you must declare a pointer before using it to store any variable address. The general form of a pointer variable declaration is:

setfillpattern is like setfillstyle, except that you use it to set a user-defined 8x8 pattern rather than a predefined pattern. upattern is a pointer to a sequence of 8 bytes, with each byte corresponding to 8 pixels in the pattern. Whenever a bit in a pattern byte is set to 1, the corresponding pixel is plotted. • EMPTY_FILL – 0. • SOLID_FILL – 1. • LINE_FILL – 2. • LTSLASH_FILL – 3. • SLASH_FILL – 4. • BKSLASH_FILL – 5. • LTBKSLASH_FILL – 6. • HATCH_FILL – 7.

An array is a collection of data items, all of the same type, accessed using a common name. A one-dimensional array is like a list; A two dimensional array is like a table; The C language places no limits on the number of dimensions in an array, though specific implementations may. Some texts refer to one-dimensional arrays as vectors, two-dimensional arrays as matrices, and use the general term arrays when the number of dimensions is unspecified or unimportant.

initgraph initializes the graphics system by loading a graphics driver from disk (or validating a registered driver), and putting the system into graphics mode. To start the graphics system, first call the initgraph function. initgraph loads the graphics driver and puts the system into graphics mode. You can tell initgraph to use a particular graphics driver and mode, or to autodetect the attached video adapter at run time and pick the corresponding driver.

Absolute value. The abs () function is a predefined function in the stdlib.h header file to return the absolute value of the given integers. So, if we want to return the absolute value of a given number, we need to implement the stdlib.h header file in the C program. The abs() function only returns the positive numbers.

The getch() is a predefined non-standard function that is defined in conio.h header file. It is mostly used by the Dev C/C++, MS- DOS's compilers like Turbo C to hold the screen until the user passes a single value to exit from the console screen. It can also be used to read a single byte character or string from the keyboard and then print. It does not hold any parameters. It has no buffer area to store the input character in a program.

Program code enter Month number between 1-12 and print number of days in month using If Else. Input month number from user. Store it in a variable say month and for each month




Dynamic array of structures. Add ONE element to the array. Allocate memory for one 'struct node'. Copy the data into the new element (structure). Print the array. Free all...