-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcar.c
271 lines (263 loc) · 9.99 KB
/
car.c
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#include<stdio.h>
#include<stdlib.h>
#include "car.h"
nodec* addnode(nodec* list, int lin, int col, int dir){
//creates a new node and adds it to the list
//returns the list
nodec* newnode = malloc(sizeof(nodec));
newnode->next = NULL;
newnode->lin = lin;
newnode->col = col;
newnode->dir = dir;
newnode->t = 0;
newnode->TIME = (rand()%20)+(rand()%20);
newnode->status = 0;
nodec* temp = list;
if(list == NULL){
return newnode;
}
while(temp->next != NULL){
temp = temp->next;
}
temp->next = newnode;
return list;
}
nodec* removenode(nodec* list, nodec* mynode){
//removes a node
//returns the list
nodec* temp = list;
if(list == mynode){
temp = mynode->next;
free(mynode);
return temp;
}
while(temp->next != NULL && temp->next != mynode){
temp = temp->next;
}
temp->next = mynode->next;
free(mynode);
return list;
}
void cardir(nodec* mynode, int** matrix){
//chooses a direction for a car (parking mode)
//0 up, 1 right, 2 down, 3 left
int flag = 0;
int tempdir = rand()%4; //chooses a random direction
int lin = mynode->lin;
int col = mynode->col;
int try = 0;
while(flag == 0 && try < 4){ //while the direction is not approved
tempdir = (tempdir+1)%4;
try++;
if(tempdir == 0 && tempdir != (mynode->dir+2)%4 && matrix[lin-1][col] == 0 && matrix[lin-1][col] != 2){
//if tempdir=up and the car won't do a 180° and there in a path in this direction and there is no car in the next case
flag = 1; //say we approve this direction
mynode->dir = tempdir; //change the actual direction of the car
matrix[lin-1][col] = 2; //place the car in the matrix for the mext position
if(lin == 5 && col == 2){ //actualise the old position with 0, 4 (entrance only) or 5 (exit only)
matrix[lin][col] = 4;
}else if(lin == 5 && col == 74){
matrix[lin][col] = 5;
}else{
matrix[lin][col] = 0;
}
}else if(tempdir == 1 && tempdir != (mynode->dir+2)%4 && matrix[lin][col+1] == 0 && matrix[lin][col+1] != 2){
//if tempdir=right and the car won't do a 180° and there in a path in this direction and there is no car in the next case
flag = 1;
mynode->dir = tempdir;
matrix[lin][col+1] = 2;
if(lin == 5 && col == 2){
matrix[lin][col] = 4;
}else if(lin == 5 && col == 74){
matrix[lin][col] = 5;
}else{
matrix[lin][col] = 0;
}
}else if(tempdir == 2 && tempdir != (mynode->dir+2)%4 && (matrix[lin+1][col] == 0 || matrix[lin+1][col] == 4) && matrix[lin+1][col] != 2){
//if tempdir=down and the car won't do a 180° and there in a path in this direction and there is no car in the next case
flag = 1;
mynode->dir = tempdir;
matrix[lin+1][col] = 2;
if(lin == 5 && col == 2){
matrix[lin][col] = 4;
}else if(lin == 5 && col == 74){
matrix[lin][col] = 5;
}else{
matrix[lin][col] = 0;
}
}else if(tempdir == 3 && tempdir != (mynode->dir+2)%4 && matrix[lin][col-1] == 0 && matrix[lin][col-1] != 2){
//if tempdir=left and the car won't do a 180° and there in a path in this direction and there is no car in the next case
flag = 1;
mynode->dir = tempdir;
matrix[lin][col-1] = 2;
if(lin == 5 && col == 2){
matrix[lin][col] = 4;
}else if(lin == 5 && col == 74){
matrix[lin][col] = 5;
}else{
matrix[lin][col] = 0;
}
}
}
if(try == 4 && flag == 0){
mynode->dir = (mynode->dir+2)%4;
if(mynode->dir == 0){
matrix[lin-1][col] = 2;
}else if(mynode->dir == 1){
matrix[lin][col+1] = 2;
}else if(mynode->dir == 2){
matrix[lin+1][col] = 2;
}else if(mynode->dir == 3){
matrix[lin][col-1] = 2;
}
if(lin == 5 && col == 2){
matrix[lin][col] = 4;
}else if(lin == 5 && col == 74){
matrix[lin][col] = 5;
}else{
matrix[lin][col] = 0;
}
}
}
void carexit(nodec* mynode, int** matrix){
//chooses a direction for a car (exit mode)
//0 up, 1 right, 2 down, 3 left
int flag = 0;
int tempdir = rand()%4; //chooses a random direction
int lin = mynode->lin;
int col = mynode->col;
int try = 0;
while(flag == 0 && try < 4){ //while the direction is not approved
tempdir = (tempdir+1)%4;
try++;
if(tempdir == 0 && tempdir != (mynode->dir+2)%4 && (matrix[lin-1][col] == 0 || matrix[lin-1][col] == 5) && matrix[lin-1][col] != 2){
//if tempdir=up and the car won't do a 180° and there in a path in this direction and there is no car in the 3 next cases and there is no wall 3 cases later
if(lin<4){
flag = 1; //say we approve the direction
mynode->dir = tempdir; //change the actual direction of the car
matrix[lin-1][col] = 2; //place the car in the matrix for the mext position
if(lin == 5 && col == 2){ //actualise the old position with 0, 4 (entrance only) or 5 (exit only)
matrix[lin][col] = 4;
}else if(lin == 5 && col == 74){
matrix[lin][col] = 5;
}else{
matrix[lin][col] = 0;
}
}else if(matrix[lin-4][col] != 6){
flag = 1; //say we approve the direction
mynode->dir = tempdir; //change the actual direction of the car
matrix[lin-1][col] = 2; //place the car in the matrix for the mext position
if(lin == 5 && col == 2){ //actualise the old position with 0, 4 (entrance only) or 5 (exit only)
matrix[lin][col] = 4;
}else if(lin == 5 && col == 74){
matrix[lin][col] = 5;
}else{
matrix[lin][col] = 0;
}
}
}else if(tempdir == 1 && tempdir != (mynode->dir+2)%4 && matrix[lin][col+1] == 0 && matrix[lin][col+1] != 2){
//if tempdir=right and the car won't do a 180° and there in a path in this direction and there is no car in the 3 next cases and there is no wall 3 cases later
flag = 1;
mynode->dir = tempdir;
matrix[lin][col+1] = 2;
if(lin == 5 && col == 2){
matrix[lin][col] = 4;
}else if(lin == 5 && col == 74){
matrix[lin][col] = 5;
}else{
matrix[lin][col] = 0;
}
}else if(tempdir == 2 && tempdir != (mynode->dir+2)%4 && matrix[lin+1][col] == 0 && matrix[lin+1][col] != 2){
//if tempdir=down and the car won't do a 180° and there in a path in this direction and there is no car in the 3 next cases and there is no wall 3 cases later
if(lin>13){
flag = 1;
mynode->dir = tempdir;
matrix[lin+1][col] = 2;
if(lin == 5 && col == 2){
matrix[lin][col] = 4;
}else if(lin == 5 && col == 74){
matrix[lin][col] = 5;
}else{
matrix[lin][col] = 0;
}
}else if(matrix[lin+4][col] != 6){
flag = 1;
mynode->dir = tempdir;
matrix[lin+1][col] = 2;
if(lin == 5 && col == 2){
matrix[lin][col] = 4;
}else if(lin == 5 && col == 74){
matrix[lin][col] = 5;
}else{
matrix[lin][col] = 0;
}
}
}else if(tempdir == 3 && tempdir != (mynode->dir+2)%4 && matrix[lin][col-1] == 0 && matrix[lin][col-1] != 2){
//if tempdir=left and the car won't do a 180° and there in a path in this direction and there is no car in the 3 next cases and there is no wall 3 cases later
flag = 1;
mynode->dir = tempdir;
matrix[lin][col-1] = 2;
if(lin == 5 && col == 2){
matrix[lin][col] = 4;
}else if(lin == 5 && col == 74){
matrix[lin][col] = 5;
}else{
matrix[lin][col] = 0;
}
}
}
if(try == 4 && flag == 0){
mynode->dir = (mynode->dir+2)%4;
if(mynode->dir == 0){
matrix[lin-1][col] = 2;
}else if(mynode->dir == 1){
matrix[lin][col+1] = 2;
}else if(mynode->dir == 2){
matrix[lin+1][col] = 2;
}else if(mynode->dir == 3){
matrix[lin][col-1] = 2;
}
if(lin == 5 && col == 2){
matrix[lin][col] = 4;
}else if(lin == 5 && col == 74){
matrix[lin][col] = 5;
}else{
matrix[lin][col] = 0;
}
}
}
void carmove(nodec* mynode){
if(mynode->dir == 0){
mynode->lin--;
}else if(mynode->dir == 1){
mynode->col++;
}else if(mynode->dir == 2){
mynode->lin++;
}else{
mynode->col--;
}
}
void isparked(nodec* mynode, int** matrix){
//changes the status to 1 if the car is parked
//doesnt change it otherwise
if(mynode->dir == 0){
if(matrix[mynode->lin-1][mynode->col] == 6){
mynode->status = 1;
}
}else if(mynode->dir == 1){
if(matrix[mynode->lin][mynode->col+1] == 6){
mynode->status = 1;
}
}else if(mynode->dir == 2){
if(matrix[mynode->lin+1][mynode->col] == 6){
mynode->status = 1;
}
}else{
if(matrix[mynode->lin][mynode->col-1] == 6){
mynode->status = 1;
}
}
}
double pay(double price, nodec* mynode){
return price*mynode->t;
}