+1 (315) 557-6473 

Writing an Agent to Control a Vacuum Cleaner in a 2D Array Using C++

In this guide, we'll embark on an exciting journey into the world of programming by crafting an agent capable of efficiently navigating a virtual space to clean dirty spots. Using the power of C++, this hands-on exercise will not only introduce you to programming logic and simulation but also equip you with practical problem-solving skills that are invaluable in software development. Whether you're a beginner or an experienced programmer looking to sharpen your skills, this project offers a fun and engaging way to explore the fascinating realm of autonomous agents.

Building a Vacuum Cleaner Agent in C++

Discover how to write your C++ assignment with our comprehensive guide on creating an agent to control a vacuum cleaner in a 2D array. This engaging project not only helps you master programming logic and simulation but also equips you with practical problem-solving skills, making your C++ assignments a breeze. Dive into the world of autonomous agents and enhance your programming expertise.


Before we dive into the code, let's make sure you have everything you need:

  • Basic Programming Knowledge: You should have a basic understanding of C++ programming concepts.
  • Development Environment: Ensure you have a C++ development environment set up on your computer.

The Challenge

Our mission is to tackle the following problem: You have a vacuum cleaner and a 2D grid that represents a room. Some spots in the room are dirty, and your task is to program the vacuum cleaner to navigate the room efficiently, cleaning each dirty spot it encounters.

The Solution

We've broken down the solution into several key parts:

  1. Room Representation: We'll begin by creating a 2D array to represent the room, with each cell indicating whether it's dirty or clean.
  2. Vacuum Cleaner Class: Our vacuum cleaner will be encapsulated in a class. This class will provide methods to move the cleaner in different directions, clean the current position, and check if the current position is clean.
  3. Randomized Start: To make things interesting, we'll randomly place the vacuum cleaner in the room at the start of each simulation.
  4. Simulation Loop: We'll run a simulation loop where the vacuum cleaner moves randomly, cleans dirty spots, and displays its position.
  5. Complete Code: We'll provide you with the full C++ code for this simulation.

The C++ Code

To access the complete C++ code for our vacuum cleaner simulation, scroll down to the code section below. Feel free to copy it and run it in your C++ development environment. Experiment with different room sizes and step counts to observe how the vacuum cleaner performs. Here's the C++ code for our vacuum cleaner simulation:

```cpp #include #include #include #include using namespace std; // Define the size of the 2D array const int numRows = 5; const int numCols = 5; // Define possible directions for the vacuum cleaner enum class Direction { UP, DOWN, LEFT, RIGHT }; class VacuumCleaner { public: VacuumCleaner(int startX, int startY) : x(startX), y(startY) {} // Move the vacuum cleaner in the specified direction void move(Direction dir) { switch (dir) { case Direction::UP: if (x > 0) x--; break; case Direction::DOWN: if (x < numRows - 1) x++; break; case Direction::LEFT: if (y > 0) y--; break; case Direction::RIGHT: if (y < numCols - 1) y++; break; } } // Clean the current position void clean() { cleaned[x][y] = true; } // Check if the current position is cleaned bool isClean() const { return cleaned[x][y]; } // Display the current position of the vacuum cleaner void displayPosition() const { cout << "Vacuum Cleaner Position: (" << x << ", " << y << ")" << endl; } private: int x; int y; }; // Create a 2D array to represent the room and initialize it as dirty vector> cleaned(numRows, vector(numCols, false)); int main() { // Seed the random number generator for random initial position srand(static_cast(time(0))); // Create a vacuum cleaner object with a random initial position int initialX = rand() % numRows; int initialY = rand() % numCols; VacuumCleaner cleaner(initialX, initialY); // Define the number of cleaning steps const int numSteps = 20; // Perform cleaning steps for (int step = 0; step < numSteps; step++) { cleaner.displayPosition(); if (!cleaner.isClean()) { cleaner.clean(); cout << "Cleaning..." << endl; } else { cout << "Already clean." << endl; } // Choose a random direction to move Direction randomDirection = static_cast(rand() % 4); cleaner.move(randomDirection); cout << endl; } return 0; } ```

Explanation for each block of the code:

  1. Import necessary libraries: We include the necessary C++ libraries, such as iostream, vector, cstdlib (for random number generation), and ctime (to seed the random number generator).
  2. Define the size of the 2D array: We specify the number of rows and columns for the room where the vacuum cleaner operates.
  3. Define possible directions: We define an enumeration `Direction` to represent the possible directions in which the vacuum cleaner can move.
  4. Create a class for the vacuum cleaner: We define a `VacuumCleaner` class with methods to move the cleaner, clean the current position, check if the position is clean, and display the cleaner's position.
  5. Create a 2D array to represent the room: We create a 2D vector called `cleaned` to represent the room and initialize it with all positions marked as dirty (false).
  6. The `main` function: We start the main part of the program.
  7. Seed the random number generator: We seed the random number generator to ensure different initial positions for the vacuum cleaner each time the program runs.
  8. Create a vacuum cleaner object: We create a `VacuumCleaner` object with a random initial position.
  9. Define the number of cleaning steps: We specify the number of cleaning steps the vacuum cleaner will take.
  10. Perform cleaning steps: In a loop, the cleaner displays its position, checks if the current position is clean, cleans it if necessary, chooses a random direction to move, and moves accordingly.

How It Works

Here's a brief overview of how our vacuum cleaner simulation operates:

  • The vacuum cleaner begins at a random position within the room.
  • It randomly selects a direction to move in (up, down, left, or right) during each step.
  • If it encounters a dirty spot, it cleans it.
  • The simulation continues for a predetermined number of steps.


Creating a vacuum cleaner agent for a 2D array in C++ is an enjoyable way to reinforce your programming abilities and learn about the fundamentals of simulation. You can take this project even further by adding advanced features such as obstacle detection, intelligent pathfinding, or interactive visualizations. Whether you choose to build upon this project or embark on new coding adventures, remember that programming offers endless possibilities for innovation and problem-solving. Happy coding!