×
Samples Blogs Make Payment About Us Reviews 4.9/5 Order Now

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

July 16, 2024
Mr. Harrison Yu
Mr. Harrison
🇦🇺 Australia
C++
Mr. Harrison Yu holds an MSc in Computer Science from the University of Sydney, Australia. He has completed over 300 assignments and has practical experience working with start-ups in Sydney.
Tip of the day
Use well-structured shaders to optimize rendering and ensure efficient resource management. Start with simple shapes, gradually adding complexity, and debug in small steps to identify errors easily.
News
An open-source framework that allows developers to create rich Python applications in the browser using HTML's interface, bridging the gap between web development and Python scripting.
Key Topics
  • Building a Vacuum Cleaner Agent in C++
  • Prerequisites
  • The Challenge
  • The Solution
  • The C++ Code
  • How It Works
  • Conclusion

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.

Prerequisites

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 <iostream> #include <vector> #include <cstdlib> #include <ctime> 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 &gt; 0) x--; break; case Direction::DOWN: if (x &lt; numRows - 1) x++; break; case Direction::LEFT: if (y &gt; 0) y--; break; case Direction::RIGHT: if (y &lt; 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 &lt;&lt; "Vacuum Cleaner Position: (" &lt;&lt; x &lt;&lt; ", " &lt;&lt; y &lt;&lt; ")" &lt;&lt; endl; } private: int x; int y; }; // Create a 2D array to represent the room and initialize it as dirty vector<vector<bool>&gt; cleaned(numRows, vector<bool>(numCols, false)); int main() { // Seed the random number generator for random initial position srand(static_cast<unsigned>(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 &lt; numSteps; step++) { cleaner.displayPosition(); if (!cleaner.isClean()) { cleaner.clean(); cout &lt;&lt; "Cleaning..." &lt;&lt; endl; } else { cout &lt;&lt; "Already clean." &lt;&lt; endl; } // Choose a random direction to move Direction randomDirection = static_cast<direction>(rand() % 4); cleaner.move(randomDirection); cout &lt;&lt; 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.

Conclusion

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!

Related Samples

Browse our free C++ assignment samples for clarity on various programming concepts. These samples provide detailed solutions and practical examples, demonstrating the application of C++ in different scenarios. Whether you're tackling basic syntax or advanced algorithms, our examples serve as a valuable resource to help you navigate and solve C++ assignments with confidence. Access these resources to see how complex problems are approached and resolved, aiding your learning process effectively.