Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: Road.cpp
- Revision:
- 1:54512aca944d
- Parent:
- 0:ca7cb51e9fd1
--- a/Road.cpp Sun Nov 10 23:02:16 2019 +0000
+++ b/Road.cpp Mon Nov 11 00:50:05 2019 +0000
@@ -1,15 +1,16 @@
#include <math.h>
#include "Road.h"
-Road::Road() {
+Road::Road(Intersection* intersection, int roadId) {
active_cars = 0x00;
last_car = NULL;
- intersection_car = -1;
+ this->intersection = intersection;
+ this->roadId = roadId;
next_release = 0;
num_cars = 0;
- pending_car = new AccCar(num_cars, this, pow(2, num_cars));
+ pending_car = new AccCar(num_cars + 5 * (roadId - 1), this, pow(2, num_cars));
pending_car->set_target_speed(rand() % 11 + 5);
}
@@ -26,7 +27,7 @@
num_cars++;
if(num_cars < MAX_CARS) {
- pending_car = new AccCar(num_cars, this, pow(2, num_cars));
+ pending_car = new AccCar(num_cars + 5 * (this->roadId - 1), this, pow(2, num_cars));
pending_car->set_target_speed(rand() % 11 + 5);
next_release = time + rand() % 3;
@@ -53,26 +54,23 @@
}
}
- int Road::check_exit_cars(int cars[]) {
- int arr_index = 0;
-
+ void Road::check_exit_cars() {
for( int i=0; i < num_cars; i++ ) {
- if( this->cars[i]->position >= 100 ) {
- cars[arr_index] = this->cars[i]->id;
- arr_index++;
-
+ if(this->cars[i]->position >= 100 ) {
active_cars = active_cars ^ this->cars[i]->flag;
this->cars[i]->position = -1;
this->cars[i]->speed = -1;
}
- }
-
- return arr_index;
+ }
}
void Road::print_status() {
for( int i=0; i < num_cars; i++ ) {
- printf("Car %d: %d -> %d\r\n", cars[i]->id, cars[i]->position, cars[i]->speed);
+ printf("Car %d on road %d: %d -> %d\r\n", cars[i]->id, this->roadId, cars[i]->position, cars[i]->speed);
}
- }
\ No newline at end of file
+ }
+
+void Road::intendToEnter(int carId) {
+ intersection->intendToEnter(carId, this->roadId);
+}
