Open In App

Draw a Chess Board using Graphics Programming in C

Last Updated : 21 Jun, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

Prerequisite: graphics.h, How to include graphics.h in CodeBlocks?

In Computer Graphics, we use graphics.h which provide direct functions to draw different coordinate shapes(like circle, rectangle etc). By using these functions we can draw different objects like car, hut, trees etc. In this program, the task is to draw a Chess Board using the functions in graphics.

To run the program we have the include the below header file:

#include <graphic.h>

Approach: We will create a Chess Board with the help below functions:

  1. rectangle(left, top, right, bottom): A function from graphics.h header file which is used to draw a rectangle. Coordinates of the left top and right bottom corners are required to draw the rectangle. left specifies the X-coordinate of the top left corner, top specifies the Y-coordinate of the top left corner, right specifies the X-coordinate of the right bottom corner, bottom specifies the Y-coordinate of the right bottom corner.
  2. delay(): This function is present in library “dos.h” is used for holding the program output for a small period of time since processing is very fast so use it to see the result.
  3. setcolor(): A function from graphics.h header file which sets the color of the pointer (cursor). There are some predefined colors in computer graphics. Here n is the color number.
  4. setfillstyle(): A function from graphics.h header file which sets the current fill pattern and fill color.
  5. floodfill(): A function from graphics.h header file which is used to fill an enclosed area.

Below is the implementation of to draw Chess Board using graphics in C:




// C program to create a chess board
#include <conio.h>
#include <dos.h>
#include <graphics.h>
#include <stdio.h>
  
// Driver Code
void main()
{
    // Auto detection
    int gr = DETECT, gm;
    int r, c, x = 30, y = 30, black = 0;
  
    // Initialize graphics mode by passing
    // three arguments to initgraph function
  
    // &gdriver is the address of gdriver
    // variable, &gmode is the address of
    // gmode and  "C:\\Turboc3\\BGI" is the
    // directory path where BGI files are stored
    initgraph(&gr, &gm, "C:\\TURBOC3\\BGI");
  
    // Iterate over 8 rows
    for (r = 0; r < 8; r++) {
  
        // iterate over 8 cols
        for (c = 1; c <= 8; c++) {
  
            // If current block is to
            // color as black
            if (black == 0) {
  
                // set next color as white
                setcolor(WHITE);
  
                // sets the current fill
                // pattern and fill color
                // for black boxes
                setfillstyle(SOLID_FILL, BLACK);
  
                // creating rectangle
                // with length and breadth
                // with size 30
                rectangle(x, y, x + 30, y + 30);
  
                // Fill an enclosed area
                floodfill(x + 1, y + 1, WHITE);
  
                // Set black to true
                black = 1;
            }
  
            // If current block is to
            // color as white
            else {
                setcolor(WHITE);
  
                // sets the current fill
                // pattern and fill color
                // for whitw boxes
                setfillstyle(SOLID_FILL, WHITE);
  
                // creating rectangle
                // with length and breadth
                // with size 30
                rectangle(x, y, x + 30, y + 30);
  
                // Fill an enclosed area
                floodfill(x + 1, y + 1, WHITE);
  
                // Set black to false
                black = 0;
            }
  
            // Increment for next row
            x = x + 30;
  
            // delay function under library
            // "dos.h" for holding the
            // function for some time
            delay(30);
        }
        if (black == 0)
            black = 1;
        else
            black = 0;
  
        delay(30);
        x = 30;
        y = 30 + y;
    }
  
    // getch is used to hold the output screen
    // and wait until user gives any type of
    // input in turbo c
    getch();
  
    // close graph function is used to exit
    // from the graphics screen
    closegraph();
}


Output:
Below is the output of the above program:



Similar Reads

Introduction to Chess | Learn Chess Online
Chess is a timeless and strategic two-player board game that has been played for centuries. It's a game of skill, tactics, and strategy that requires players to out-think their opponent while considering multiple possibilities at every turn. Chess is often referred to as the "game of kings" due to its historical association with royalty and the dee
7 min read
Building a Web-based Chess Game with React and Chess.js
In this article, we’ll walk through the step-by-step process of creating a Chess game using Chess.js, ReactJS, ExpressJS, and NodeJS. This application will provide users with a platform for users to play chess. It will be a single player game, where the opponent will be played by the application itself. Output Preview: Let us have a look at how the
4 min read
Minimum number of moves required to reach the destination by the king in a chess board
Given four integers sourceX, sourceY, destinationX and destinationY which represent the source and destination coordinates on a chessboard. The task is to find the minimum number of moves required by the king to reach from source to destination. A king can move to the square that has a common side or a common vertex with the square the king is curr
7 min read
Number of ways to place two queens on a N*N chess-board
Given an integer N denoting a N * N chess-board, the task is to count the number of ways to place two queens on the board such that, they do not attack each other. Examples: Input: N = 9 Output: 2184 Explanation: There are 2184 ways to place two queens on 9 * 9 chess-board. Input: N = 3 Output: 8 Explanation: There are 8 ways to place two queens on
5 min read
Draw a moving car using computer graphics programming in C
In computer graphics, use graphics.h which provide direct functions to draw different coordinate shapes (like circle, rectangle etc). By using these functions we can draw different objects like car, hut, trees, etc. In this program, we will draw a moving car using line and circles. Functions used in program: delay(n): This function is used for hold
4 min read
Draw a moving cycle using computer graphics programming in C/C++
In C graphics, the graphics.h functions are used to draw different shapes like circles, rectangles, etc, display text(any message) in a different format (different fonts and colors). By using the functions in the header graphics.h, programs, animations, and different games can also be made. In this article, let's discuss how to draw a moving cycle
3 min read
C program to design a carrom board using graphics
In this article, we will discuss how to draw a Carrom Board using Graphics. Approach: Create the outline of the board using the rectangle() function.Create another smaller rectangle inside the bigger one using the rectangle() function.Create a total of four circles at each corner which will be the pockets of the carrom board by using the circle() f
3 min read
Design Ludo Board Using Computer Graphics
In Turbo C graphics the graphics.h functions are used to draw different shapes like circles, rectangles, etc, display text(any message) in a different format (different fonts and colors). By using graphics.h we can make programs, animations and also games. These can be useful for beginners. Function Used: rectangle(l, t, r, b): A function from grap
8 min read
Program to draw India Gate using computer graphics in C
In C graphics, the graphics.h functions are used to draw different shapes like circles, rectangles, etc, display text(any message) in a different format (different fonts and colors). By using graphics.h one can make programs, animations, and also games. Function Used: rectangle(l, t, r, b): A function from graphics.h header file which is used to dr
6 min read
C program to draw a cricket ground using computer graphics
In this article, we will discuss how to draw a 2D cricket ground is being designed using computer graphics. Approach: Draw a circle using the circle() function. This will act as the Ground Outline.Color the above circle with green using the setfillstyle() and floodfill() functions.Then to implement 30 yards Outline using the ellipse() function.Then
2 min read