C Program to Draw Chess Board

Share

C supports a special header file named graphics.h that provides various functions through which one can draw different shapes such as line, circle, triangle, and more. And with the combination of these functions, we can create objects like a hut, joker, cap, and more.

The program draws a Chess board with the help of graphic libraries. It uses rectangle(), setcolor(), floodfill and setfillstyle() functions to draw this object.

C program to Draw Chess Board

 Program:

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main(){
int gr=DETECT,gm;
int row,col,x=50,y=50,flag=0;
initgraph(&gr,&gm,"C:\\TURBOC3\\BGI");
printf("\t*********** CHESS BOARD **************\n");
for(row=0;row<8;row++)
{

for(col=1;col<=8;col++){
if(flag==0){
setcolor(YELLOW);
setfillstyle(SOLID_FILL,BLACK);
rectangle(x,y,x+50,y+50);
floodfill(x+1,y+1,YELLOW);
flag=1;
}
else
{
setcolor(YELLOW);
setfillstyle(SOLID_FILL,WHITE);
rectangle(x,y,x+50,y+50);
floodfill(x+1,y+1,YELLOW);
flag=0;
}
x=x+50;
}
if(flag==0)
flag=1;
else
flag=0;
delay(100);
x=50;
y=50+y;
}
getch();
closegraph();
}

 

Output:

This post was last modified on August 21, 2022

Sandeep Verma

Published by
Sandeep Verma
Tags: C c program C program to Draw a Chess Board chess chess board coding computer graphics