Hi, so i've been given an assignment which asks me to simulate traffic on a road, the best output would be if there was a road with two lanes going in different directions, where the cars are of different speeds and the cars are able to overtake other vehicles, this is to be coded in c++. However, I've mostly worked with html and converting it into c++ is a proving to be more than a problem. I'll include the html code followed by the converted c++ code.
Road.php
<?php
class Road {
public $time = 0; // seconds
public $lanes = array();
public function addLane($lane) {
$this->lanes[] = $lane;
}
public function runSimulation($simulationTime) {
while ($this->time < $simulationTime) {
echo "Time: " . $this->time . " seconds\n";
foreach ($this->lanes as $laneNumber => $currentLane) {
echo " - Lane " . $laneNumber . ":\n";
$output = $currentLane->updateVehicles();
}
sleep(0.01); // seconds
$this->time++;
};
}
}
Lane.php
<?php
class Lane {
public $direction;
public $length;
public $speedLimit;
public $vehicleTypes;
public $vehicleQueue;
public function __construct($vehicleTypes) {
$this->vehicleTypes = $vehicleTypes;
$this->addVehicle($this->speedLimit);
}
class Road
{
private:
int time;
array lanes;
public:
void SetTime(int value)
{
time = 0; // seconds
}
void Setlanes(int value)
{
lanes = [];
}
int function (void *addLane(void *lane))
{
this->lanes[] = lane;
}
public function runSimulation(simulationTime) {
while (this->time < simulationTime) {
echo "Time: " . this->time . " seconds\n";
foreach (this->lanes as laneNumber => currentLane) {
echo " - Lane " . laneNumber . ":\n";
output = currentLane->updateVehicles();
}
sleep(0.01); // seconds
this->time++;
};
}
}
lane.h
#include"traffic.h"
class Lane
{
public: int direction;
int length;
int speedLimit;
int vehicleTypes;
int vehicleQueue;
int function (__construct(vehicleTypes))
{
this->vehicleTypes = vehicleTypes;
this->addVehicle(this->speedLimit);
}
function updateVehicles() {
foreach (this->vehicleQueue as index => vehicle) {
echo " - Vehicle " . index . ": " . vehicle->position . " metres (" . vehicle->speed . ")\n";
vehicle->updatePosition();
}
}