TEXT BASED CHESS GAME IN
C
The project ‘DESIGNING OF TEXT BASED CHESS GAME USING COMPUTER
GRAPHICS’ implements a classic version of Chess with the help of
GRAPHICAL USER INTERFACE(GUI).
The Chess game follows the basic rules of chess, and all the chess
pieces only move according to valid moves for that piece. This project is a “
2D GAME”, it is is programmed using c.
Standard Definitions:
Chess Board: A board you need to play Chess. Have 64 black and white
square.
Chess: A game played by 2 people on a chessboard with 16 pieces each.
Pawn(P): One of eight men of one color and of the lowest value usually
moved one square at a time vertically and capturing diagonally.
King(K): The main piece of the game, checkmating this piece is the object of
the game. It can move 1 space in any direction.
Knight(H): This piece can move 1 space vertically and 2 spaces horizontally
or 2 spaces vertically and 1 space horizontally. This piece looks like a horse.
This piece can also jump over other pieces.
Queen(Q): This piece can move in any number of spaces in any
direction as long as no other piece is in its way.
Rook(R): one of two pieces of the same color that may be moved any
number squares horizontally or vertically, as long as no other piece blocks
its way.
Bishop(C): one of two pieces of the same color that may be moved any
number squares diagonally, as long as no other piece blocks its way.
Text-Based Chess Game
Text-based games are an excellent way to introduce students to
programming concepts and encourage them to think algorithmically. Chess,
with its well-defined rules and complexity, is an ideal candidate for such a
project.
The text-based game uses upper and lowercase letters for the chess pieces
for players 1 and 2 respectively. The blank pieces are indicated with a '-' and
there is a number grid provided for easier input of the coordinates of
squares. The player is prompted to input the coordinates of the piece to
be moved, and where to move it. We decided that because chess is a
complicated game with many rules, we would not be able to implement all
its functionality. Starting with testing the simpler functions and gradually
working up to playing chess matches, we tested the text-based version for
functionality. Designing and debugging the text-basedversion was
challenging.
#include <stdio.h>
#include <stdlib.h>
#define SIZE 8
// Representation of pieces
char board[SIZE][SIZE] = {
{'R', 'H', 'C', 'Q', 'K', 'C', 'H', 'R'},
{'P', 'P', 'P', 'P', 'P', 'P', 'P', 'P'},
{'-', '-', '-', '-', '-', '-', '-', '-'},
{'-', '-', '-', '-', '-', '-', '-', '-'},
{'-', '-', '-', '-', '-', '-', '-', '-'},
{'-', '-', '-', '-', '-', '-', '-', '-'},
{'p', 'p', 'p', 'p', 'p', 'p', 'p', 'p'},
{'r', 'h', 'c', 'q', 'k', 'c', 'h', 'r'}
};
// Function to display the board
void displayBoard() {
printf("\n 1 2 3 4 5 6 7 8\n");
for (int i = 0; i < SIZE; i++) {
printf("%d ", 8 - i); // Row labels
for (int j = 0; j < SIZE; j++) {
printf(" %c", board[i][j]);
printf(" %d\n", 8 - i);
}
printf(" 1 2 3 4 5 6 7 8\n");
// Function to check if a space is empty
int isEmpty(int row, int col) {
return board[row][col] == '-';
// Function to move a piece
int movePiece(int fromRow, int fromCol, int toRow, int toCol) {
if (isEmpty(fromRow, fromCol)) {
printf("No piece at the starting position!\n");
return 0;
char piece = board[fromRow][fromCol];
board[toRow][toCol] = piece;
board[fromRow][fromCol] = '-'; // Make the original spot empty
return 1;
// Function to check if a king exists on the board
int kingExists(char king) {
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
if (board[i][j] == king) {
return 1;
return 0;
// Check for checkmate
int checkForCheckmate() {
int whiteKingExists = kingExists('K');
int blackKingExists = kingExists('k');
if (!whiteKingExists) {
printf("Checkmate! Black wins!\n");
return 1;
if (!blackKingExists) {
printf("Checkmate! White wins!\n");
return 1;
return 0;
}
// Main function to play the game
int main() {
int fromRow, fromCol, toRow, toCol;
char fromColChar, toColChar;
int turn = 1; // 1 for white, -1 for black
while (1) {
displayBoard();
if (checkForCheckmate()) {
break;
if (turn == 1) {
printf("Player 1's move(UPPER CASE): ");
} else {
printf("Player 2's move(lower case): ");
scanf(" %c%d %c%d", &fromColChar, &fromRow, &toColChar,
&toRow);
fromCol = fromColChar - '1';
fromRow = 8 - fromRow;
toCol = toColChar - '1';
toRow = 8 - toRow;
if (movePiece(fromRow, fromCol, toRow, toCol)) {
turn *= -1; // Switch turns
return 0;
OUTPUT: