-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcar_system.h
61 lines (46 loc) · 2.18 KB
/
car_system.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#ifndef CAR_SYSTEM_H
#define CAR_SYSTEM_H
#include "configuration.h"
#include <vector>
#include <algorithm>
class Car_System
{
std::vector<std::size_t> system;
std::size_t length;
std::size_t lanes;
std::size_t car_density;
std::size_t max_speed;
std::size_t slow_down_chance;
bool multiple_lanes;
static constexpr const std::size_t SLOW_DOWN_BIT = (1ul << (sizeof(std::size_t) * 8 - 1));
static constexpr const std::size_t EMPTY = (std::numeric_limits<std::size_t>::max() & ~SLOW_DOWN_BIT);
static constexpr const std::size_t BARRIER = (EMPTY - 1);
inline std::size_t set_slow_down_bit(std::size_t num) const { return num | SLOW_DOWN_BIT; }
inline std::size_t clear_slow_down_bit(std::size_t num) const { return num & ~SLOW_DOWN_BIT; }
void calculate();
public:
Car_System(std::size_t length = 0, std::size_t lanes = 0, std::size_t car_density = 0, std::size_t max_speed = 0, std::size_t slow_down_chance = 0);
void generate();
void create(std::size_t length, std::size_t lanes, std::size_t car_density, std::size_t max_speed, std::size_t slow_down_chance);
void reset();
void add_car(std::size_t speed, std::size_t pos, std::size_t lane = 0);
void remove_car(std::size_t pos, std::size_t lane = 0);
void set_car_speed(std::size_t speed, std::size_t pos, std::size_t lane = 0);
void add_barrier(std::size_t pos, std::size_t lane = 0);
void remove_barrier(std::size_t pos, std::size_t lane = 0);
void add_slow_down(std::size_t pos, std::size_t lane = 0);
void remove_slow_down(std::size_t pos, std::size_t lane = 0);
// default argument selects all lanes
double get_avg_speed(std::size_t lane = std::numeric_limits<std::size_t>::max());
std::size_t get_car_amount();
std::size_t get_speed(std::size_t pos, std::size_t lane = 0);
bool is_car_at(std::size_t pos, std::size_t lane = 0);
bool is_slow_down_at(std::size_t pos, std::size_t lane = 0);
bool is_barrier_at(std::size_t pos, std::size_t lane = 0);
std::size_t get_length() { return length; }
std::size_t get_lanes() { return lanes; }
std::size_t get_car_density() { return car_density; }
std::size_t get_max_speed() { return max_speed; }
std::size_t get_slow_down_chance() { return slow_down_chance; }
};
#endif // CAR_SYSTEM_H