Chessboard Traversal Project in C++

Welcome to Our new C++ project, Chessboard Traversal Project in C++ by a Cockroach with the help of the comprehensive guide by implementing using a queue in C++. We’ll show you step-by-step basic operations for Chessboard Traversal by using queues including creating nodes, enqueuing elements, and dequeuing them. Also, Enhance your understanding of data structures and boost your C++ programming skills today.

Front Interface, How to Enter the Input

First enter the number of rows and then enter the number of columns and then the traversal of the cockroach starts. Here is the output that is shown in the console.

Steps to Set Up the Project on Your Machine

Get the Project Files from GitHub.

Download the project files from the GitHub repository via the provided link. You can clone the repository and utilize it on your local machine.

Download zip file

  • Download the Zip file directly by clicking on the download button below.
  • Extract the downloaded files to your preferred location.
  • Open the Project folder and click on the solution file named Cockroach Traversal project in C++ shown in the figure below.

Definition of classes in queue (Project Overview)

For the Cockroach Chessboard Traversal, we need to define two classes

  • Node Class: Represents the elements in the queue. Each node contains the data and a pointer to the next node in the queue.
  • Queue Class:  The Queue class implements basic queue operations using a linked list. It keeps track of the head (front)if the queue is empty (IsEmpty), adds an element to the queue (Enqueue), and removes an element from the tail (end) of the queue which is also called Dequeue.
#ifndef QUEUE_H
#define QUEUE_H

class Node
{
public:
	int data;
	Node* next;
};
class Queue
{
public:
	Node* head;
	Node* tail;
	Queue();
	bool IsEmpty();
	void Enqueue(int);
	int Dequeue();
};


#endif // !QUEUE_H

Main Class of Cockroach Chessboard Traversal project in C++

The main class performs two main functions. Initially, it calls the AddChessBoardEdges() method to establish the layout which represents the connections between squares on the chessboard, and then it invokes the BreadthFirstSearch() method on the Traversal object to perform a breadth-first search (BFS), a common algorithm for traversing. It is used to explore all nodes at the current position depth before moving on to nodes at the next depth level, which, in the context of a chessboard, could be utilized to find the shortest path between squares, explore possible moves from a given position, or solve chess puzzles and problems.

#include <iostream>
#include "Traversal.h"
using namespace std;
int main()
{
	Traversal g;
	g.AddChessBoardEdges();
	g.BreadthFirstSearch();
}

Basic operations of queue

The basic operations of a queue follow the FIFO principle where the first element added to the queue is the first one to be removed. Queue is a linear data structure that follows a particular order in which the operations are performed.

  • Enqueue
  • Dequeue
  • Is Empty
  • Front (Peak)
  • Display

Enqueue

Enqueue is one of a queue’s basic operations, which means adding an element to the end of the queue. This operation inserts an item into the queue from the rear end.

void Queue::Enqueue(int value) {
	Node* t = (Node*)malloc(sizeof(Node));
	if (t != nullptr) {
		t->data = value;
		t->next = nullptr;
		if (this->head == nullptr) {
			this->head = t;
			this->tail = t;
		}
		else {
			this->tail->next = t;
			this->tail = t;
		}
	}
}

Dequeue

Removes an element from the front of the queue. This operation deletes an item from the front of the queue. In the dequeue operation first, we check if the queue is empty or not and then delete a node from the front of the queue.

int Queue::Dequeue() {
	if (!IsEmpty()) {
		Node* ptr = this->head;
		int store = this->head->data;
		if (this->head->next == nullptr) {
			this->head = nullptr;
			this->tail = nullptr;
			delete(ptr);
			return store;
		}
		else {
			this->head = ptr->next;
			delete (ptr);
			return store;
		}
	}
	return -1;
}

Is Empty

Check if the queue is empty. This operation verifies whether there are any elements in the queue or not, returning true if the queue is empty and false otherwise.

bool Queue::IsEmpty() {
	return(this->head == nullptr);
}

Front (Peek)

This operation allows you to see the first element that would be removed from the queue if a dequeue operation were performed. Still, it does not remove the element from the queue.

  int Front() {
        if (IsEmpty()) {
            cout << "Queue is empty" << endl;
            return -1; // Return -1 or handle underflow
        }
        return head->data;
    }

Display

The display operation returns the number of elements in the queue. This operation counts and returns the total number of elements in the queue at any given time.

void Traversal::Display(int current) {
	int count = -1;
	for (int i = 0; i < 8; i++) {
		for (int j = 0; j < 8; j++) {
			count++;
			if (count == current) {
				cout << "(" << i << "," << j << ")" << "	";

				break;
			}

		}
	}

}

Breadth First Search in Queue

Breadth First Search (BFS) is a traversal algorithm that explores vertices in the shortest path from the starting vertex. It uses a queue to keep the track of the next vertex to visit and ensures that each vertex is visited exactly once. BFS is commonly used in graphs, but it can also be adapted for use in trees, where it would visit all nodes level by level.

void Traversal::BreadthFirstSearch() {
	int row;
	int col;
	
	cout << "Enter row : ";
	do {
		cin >> row;
		if (row < 0 || row > 7) {
			cout << "Enter a number between 0 and 7:  ";
		}
	} while (row < 0 || row > 7);

	cout << "Enter col : ";
	do {
		cin >> col;
		if (col < 0 || col > 7) {
			cout << "Enter a number between 0 and 7:  ";
		}
	} while (col < 0 || col > 7);

	cout << endl;
	int startVertex = getStartVertex(row, col);
	visited[startVertex] = true;
	q.Enqueue(startVertex);

	while (!q.IsEmpty()) {
		int currentVertex = q.Dequeue();
		Display(currentVertex);

		for (int i = 0; i < this->vertices; ++i) {
			if (this->Matrix[currentVertex][i] == 1 && !this->visited[i]) {
				this->visited[i] = true;
				q.Enqueue(i);
			}
		}
	}
	cout << endl;
}

Conclusion

The concept of cockroach traversal remind us that sometimes, the most efficient path forward is one that embraces adaptability thorough exploration. The traversal of cockroach is a way of doing things shows the strength of getting inspired by nature when we’re thinking about computer problems. Looking ahead, the ability to adapt the efficiency of the cockroach method might lead the way in how we tackle computer challenges and problem-solving.

  • How do I enter input for the Chessboard Traversal project?

    First, enter the number of rows and then the number of columns.
    Lastly, input the starting position of the cockroach traversal

  • What are the steps to set up the project on my machine?

    Obtain the project files from GitHub via cloning or downloading the zip file.
    Extract the downloaded files to your preferred location.
    Open the project folder and click on the solution file named “Cockroach Traversal project in C++.”

  • What are the basics of queue?

    These basic queue operations adhere to the FIFO (First-In-First-Out) principle, where the element that is added first is the first to be removed. The article also provides insights into how these operations are used in the context of the Chessboard Traversal project to simulate the movement of a cockroach on a chessboard using a queue data structure.

  • What are Basic operations of queue?

    The basic operations of a queue follow the FIFO principle where the first element added to the queue is the first one to be removed. Queue is a linear data structure that follows a particular order in which the operations are performed.
    Enqueue
    Dequeue
    Is Empty
    Front (Peak)
    Display