Skip to content

Commit 4b598f2

Browse files
committed
Initial commit
0 parents  commit 4b598f2

File tree

8 files changed

+279
-0
lines changed

8 files changed

+279
-0
lines changed

.gitignore

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# C++ Build files
2+
build/
3+
Debug/
4+
Release/
5+
*.o
6+
*.obj
7+
*.out
8+
*.exe
9+
*.dll
10+
*.so
11+
*.dylib
12+
*.a
13+
*.lib
14+
*.lo
15+
*.la
16+
17+
# CMake generated files
18+
CMakeFiles/
19+
CMakeCache.txt
20+
CMakeScripts/
21+
Testing/
22+
Makefile
23+
cmake_install.cmake
24+
install_manifest.txt
25+
compile_commands.json
26+
CTestTestfile.cmake
27+
_deps/
28+
*.cmake
29+
!CMakeLists.txt
30+
31+
# Editor files
32+
.vscode/
33+
.idea/
34+
*.swp
35+
*.swo
36+
*~
37+
.DS_Store
38+
39+
# Generated documentation
40+
docs/generated/
41+
html/
42+
latex/
43+
44+
# Dependency directories
45+
vcpkg/
46+
_vcpkg/
47+
deps/
48+
external/
49+
50+
# Package files
51+
*.tar.gz
52+
*.zip
53+
54+
# Log files
55+
*.log
56+
57+
# Core dumps
58+
core
59+
60+
# Cache files
61+
.cache/

CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
cmake_minimum_required(VERSION 3.10)
2+
project(CarSimulation)
3+
4+
set(CMAKE_CXX_STANDARD 17)
5+
6+
add_executable(CarSimulation src/main.cpp src/Road.cpp src/Vehicle.cpp)

README.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Car Simulation Exercise
2+
3+
This exercise involves simulating cars moving on a simple road network using C++ and CMake as the build tool. The goal is to model vehicles on a circular road and implement basic rules for their movement.
4+
5+
## Features
6+
- **Road Network**: A circular road represented as a 1D array.
7+
- **Vehicle Class**: Vehicles with position, speed, and simple movement rules:
8+
- Accelerate if the space ahead is clear.
9+
- Decelerate if the vehicle ahead is too close.
10+
- Maintain current speed otherwise.
11+
- Maximum speed limit.
12+
- **Simulation Loop**: Updates vehicle states and prints the road state to the console.
13+
14+
## Requirements
15+
- **C++ Compiler**: A C++17-compatible compiler (e.g., GCC, Clang).
16+
- **CMake**: Version 3.10 or higher.
17+
18+
## Setup Instructions
19+
1. **Clone the repository**:
20+
Open a terminal and run the following commands to clone the repository and navigate to the project folder:
21+
```bash
22+
git clone https://github.com/EficodeDemoOrg/copilot-cpp-exercise
23+
cd copilot-cpp-exercise
24+
```
25+
26+
2. **Create a build directory**:
27+
Create a separate directory for the build files to keep the project organized:
28+
```bash
29+
mkdir build
30+
cd build
31+
```
32+
33+
3. **Run CMake to configure the project**:
34+
Use CMake to generate the necessary build files:
35+
```bash
36+
cmake ..
37+
```
38+
39+
4. **Build the project**:
40+
Compile the project using the generated build files:
41+
```bash
42+
cmake --build .
43+
```
44+
45+
5. **Run the simulation**:
46+
Execute the compiled program to start the simulation:
47+
```bash
48+
./CarSimulation
49+
```
50+
51+
## Output
52+
The simulation prints the state of the road at each step. For example:
53+
```
54+
..V.....V...........
55+
.....V......V.......
56+
.........V.......V..
57+
```
58+
Here, `.` represents an empty road cell, and `V` represents a vehicle.
59+
60+
## Extensions
61+
You can extend the exercise with the following features:
62+
- **Intersections**: Implement a 2D grid with traffic lights or priority rules.
63+
- **Multiple Lanes**: Extend the road model to support multiple lanes.
64+
- **Different Vehicle Types**: Add cars and trucks with varying speed and acceleration profiles.
65+
- **Driver Behavior**: Introduce randomness in vehicle behavior.
66+
- **Pathfinding**: Assign destinations to vehicles in a grid network.
67+
- **Metrics**: Calculate average speed, throughput, and queue lengths.
68+
- **Visualization**: Add basic graphics or enhanced console output.
69+
- **OOP Refinement**: Use polymorphism for different vehicle types or intersection logic.
70+
71+
## Notes
72+
This exercise is designed to be completed in approximately 2 hours and is compatible with GitHub Copilot for assistance.

src/Road.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include "Road.h"
2+
#include <iostream>
3+
4+
Road::Road(int length) : length(length) {}
5+
6+
void Road::addVehicle(int position, int speed)
7+
{
8+
vehicles.push_back(std::make_shared<Vehicle>(position, speed));
9+
}
10+
11+
void Road::update()
12+
{
13+
for (size_t i = 0; i < vehicles.size(); ++i) {
14+
int nextPosition = vehicles[(i + 1) % vehicles.size()]->getPosition();
15+
int distanceToNext = (nextPosition - vehicles[i]->getPosition() + length) % length;
16+
vehicles[i]->updateSpeed(5, distanceToNext);
17+
}
18+
19+
for (auto &vehicle : vehicles)
20+
vehicle->updatePosition(length);
21+
}
22+
23+
void Road::print() const
24+
{
25+
std::vector<char> roadState(length, '.');
26+
for (const auto &vehicle : vehicles)
27+
roadState[vehicle->getPosition()] = 'V';
28+
29+
for (char cell : roadState)
30+
std::cout << cell;
31+
std::cout << std::endl;
32+
}

src/Road.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#ifndef ROAD_H
2+
#define ROAD_H
3+
4+
#include "Vehicle.h"
5+
#include <vector>
6+
#include <memory>
7+
8+
class Road {
9+
public:
10+
Road(int length);
11+
12+
void addVehicle(int position, int speed);
13+
void update();
14+
void print() const;
15+
const std::vector<std::shared_ptr<Vehicle>> &getVehicles() const { return vehicles; }
16+
17+
private:
18+
int length;
19+
std::vector<std::shared_ptr<Vehicle>> vehicles;
20+
};
21+
22+
#endif

src/Vehicle.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include "Vehicle.h"
2+
#include <algorithm>
3+
4+
Vehicle::Vehicle(int position, int speed) : position(position), speed(speed) {}
5+
6+
int Vehicle::getPosition() const
7+
{
8+
return position;
9+
}
10+
11+
int Vehicle::getSpeed() const
12+
{
13+
return speed;
14+
}
15+
16+
void Vehicle::updateSpeed(int maxSpeed, int distanceToNext)
17+
{
18+
if (distanceToNext > speed + 1)
19+
speed = std::min(speed + 1, maxSpeed);
20+
else if (distanceToNext <= speed)
21+
speed = std::max(speed - 1, 0);
22+
}
23+
24+
void Vehicle::updatePosition(int roadLength)
25+
{
26+
position = (position + speed) % roadLength;
27+
}

src/Vehicle.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#ifndef VEHICLE_H
2+
#define VEHICLE_H
3+
4+
class Vehicle {
5+
public:
6+
Vehicle(int position, int speed);
7+
8+
int getPosition() const;
9+
int getSpeed() const;
10+
11+
void updateSpeed(int maxSpeed, int distanceToNext);
12+
void updatePosition(int roadLength);
13+
14+
private:
15+
int position;
16+
int speed;
17+
};
18+
19+
#endif

src/main.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include <thread>
2+
#include <chrono>
3+
#include "Vehicle.h"
4+
#include "Road.h"
5+
#include <iostream>
6+
#include <vector>
7+
#include <iomanip>
8+
#include <map>
9+
10+
int main()
11+
{
12+
Road road(20);
13+
road.addVehicle(0, 1);
14+
road.addVehicle(5, 2);
15+
16+
int step_delay_ms = 1000;
17+
int total_steps = 10;
18+
19+
std::cout << "Number of steps: " << total_steps << std::endl;
20+
21+
int padding = std::to_string(total_steps).length();
22+
std::map<int, int> previous_positions;
23+
24+
for (int step = 0; step < total_steps; ++step) {
25+
std::cout << "Step " << std::setw(padding) << (step + 1) << " of " << total_steps << ": ";
26+
road.update();
27+
road.print();
28+
29+
for (size_t i = 0; i < road.getVehicles().size(); ++i) {
30+
int current_position = road.getVehicles()[i]->getPosition();
31+
int previous_position = previous_positions.count(i) ? previous_positions[i] : current_position;
32+
std::cout << "vehicle" << i << "(" << current_position << ") from vehicle" << i << "(" << previous_position << ")" << std::endl;
33+
previous_positions[i] = current_position;
34+
}
35+
36+
std::this_thread::sleep_for(std::chrono::milliseconds(step_delay_ms));
37+
}
38+
39+
return 0;
40+
}

0 commit comments

Comments
 (0)