-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLane.cpp
121 lines (108 loc) · 2.58 KB
/
Lane.cpp
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
* Lane.cpp
*
* Created on: 14 nov 2016
* Author: PontusArfwedson
*/
#include "Lane.h"
#include "Vehicle.h"
#include "InvalidLength.h"
#include <vector>
#include <algorithm>
#include <cstddef>
#include <stdlib.h> /* srand, rand */
#include <time.h>
#include <iostream>
using namespace std;
Lane::Lane() {
}
Lane::Lane(int length) {
// TODO Auto-generated constructor stub
//std::fill(theLane.begin(),theLane.begin()+length, NullVehicle());;
try {
if(length < 0) {
throw InvalidLength();
}
theLane.resize(length);
std::fill(theLane.begin(),theLane.begin()+length, Vehicle(' '));
}
catch (InvalidLength e){
cout << "InvalidLength exception is caught" << endl;
cout << e.what() << endl;
}
}
std::string Lane::stringify() {
std::string res = "";
for (int i= 0; i<theLane.size(); i++) {
res += theLane[i].stringify();
}
return "<" + res + ">";
}
// void Lane::step() {
// for (int i = 1; i<theLane.size(); i++) {
// if(theLane[i-1].isNull() ) {
// theLane[i-1] = theLane[i];
// NullVehicle temp;
// theLane[i] = temp;
// }
// }
// }
//without using NullVehicle
void Lane::step() {
for (int i = 1; i<theLane.size(); i++) {
if(theLane[i-1].getDestination() == ' ') {
theLane[i-1] = theLane[i];
Vehicle temp = Vehicle(' ');
theLane[i] = temp;
}
}
}
// Vehicle Lane::removeFirst() {
// Vehicle result = theLane[0];
// NullVehicle temp;
// theLane[0] = temp;
// return result;
// }
//using without NullVehicle
Vehicle Lane::removeFirst() {
Vehicle result = theLane[0];
Vehicle temp = Vehicle(' ');
theLane[0] = temp;
return result;
}
Vehicle Lane::getFirst() {
return theLane[0];
}
bool Lane::lastFree() {
//return theLane[theLane.size()-1].isNull();
// I do not want to use NullVehicle class
return theLane[theLane.size()-1].getDestination() == ' ';
}
void Lane::putLast(Vehicle v) {
theLane[theLane.size()-1] = v;
}
Lane::~Lane() {
// TODO Auto-generated destructor stub
}
// int main() {
// srand(time(0));
// Lane lane = Lane(10);
//
// for (int i = 0; i<20; i++) {
// if (lane.getFirst().getDestination() != ' ') {
// cout << "Out from lane: " << lane.removeFirst().stringify() << endl;
// }
// lane.step();
// if ((rand()%11)/10.0<0.5) {
// lane.putLast(Vehicle('x'));
// }
// cout << lane.stringify() << endl;
// }
// cout << endl;
// if (lane.lastFree()) {
// lane.putLast(Vehicle('y'));
// cout << lane.stringify() << endl;
// }
// cout << "Time to crash!" << endl;
// lane.putLast(Vehicle('z'));
// }