-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
175 lines (140 loc) · 4.36 KB
/
Copy pathmain.cpp
File metadata and controls
175 lines (140 loc) · 4.36 KB
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
#include "Vehicle.h"
#include <iostream>
#include "grid.cpp"
#include <random>
#include <queue>
using namespace std;
// global size of grid
const int rowSize = 10;
const int columnSize = 10;
bool visited[rowSize][columnSize]; // track visited cells
// store vehicles when making them
vector<Vehicle*> vehicles;
// generate Vehicles to put on the grid
void createVehicles(int amount, Grid &grid, int rows, int columns)
{
// generate random amount of vehicles bounded by the rows and columns
random_device rd;
mt19937 gen(rd());
uniform_int_distribution<> rowDistrib(0, rows - 1);
uniform_int_distribution<> colDistrib(0, columns - 1);
// place the vehicles on the grid
for (int i = 0; i < amount; i++)
{
int row = 0;
int column = 0;
do
{
row = rowDistrib(gen);
column = rowDistrib(gen);
}
while (grid.getCell(row, column).isOccupied());
Vehicle *v = new Vehicle();
vehicles.emplace_back(v);
grid.placeVehicle(v, row, column);
}
}
bool isValid(bool visited[][columnSize], int row, int col, Grid &grid)
{
if (row < 0 || col < 0)
{
return false;
}
if (visited[row][col] == true)
{
return false;
}
if (grid.getCell(row, col).isOccupied())
{
return false;
}
return true;
}
void pathFind(Grid &grid, Vehicle &vehicle, int targetRow, int targetColumn)
{
bool BFS = true; // future implementation allow for diff algs i guess this is just for rn
auto loc = vehicle.getLocation();
int currentRow = loc.first;
int currentColumn = loc.second;
if (currentRow == targetRow && currentColumn == targetColumn) // base case
{
cout << "Already at target area!" << endl;
grid.getCell(currentRow, currentColumn).setVehicle(nullptr);
grid.showGrid();
}
if (BFS)
{
// direction vectors
int dRow[] = { -1, 0, 1, 0 };
int dCol[] = { 0, 1, 0, -1 };
queue<pair<int, int> > q;
q.push({currentRow, currentColumn});
visited[currentRow][currentColumn] = true;
while (!q.empty())
{
pair<int, int> cell = q.front();
int x = cell.first;
int y = cell.second;
// update car position and output grid here!!
q.pop();
// go to other cells
for (int i = 0; i < 4; i++)
{
int adjX = x + dRow[i];
int adjY = y + dCol[i];
if(isValid(visited, adjX, adjY, grid))
{
q.push({adjX, adjY});
visited[adjX][adjY] = true;
// update grid
grid.getCell(x, y).setOccupied(false);
grid.getCell(adjX, adjY).setOccupied(true);
grid.showGrid();
cout << endl;
}
}
}
}
}
int main()
{
// define all the variables needed for the grid
Grid grid;
int availableSpots = 4; // how many empty spots should there be for movement
int maxCars = 20;
grid.generateGrid(rowSize, columnSize);
// create random amount robot cars to drive around
try
{
random_device rd;
mt19937 gen(rd());
uniform_int_distribution<> distrib(1, maxCars + 1);
int carAmount = distrib(gen);
if (grid.NumOfCells >= carAmount - availableSpots)
{
createVehicles(carAmount, grid, rowSize, columnSize);
}
else
{
throw(carAmount);
}
}
catch (int carAmount)
{
cout << "Too many cars (" << carAmount << ") for this grid!" << endl;
;
}
grid.showGrid();
// get user's target grid space (where should the cars go to?)
int targetRow = 0;
int targetColumn = 0;
cout << "Enter the row of where you want the cars to go to." << endl;
cin >> targetRow;
cout << "Enter the row of where you want the cars to go to." << endl;
cin >> targetColumn;
auto vehicleLocation = vehicles[0]->getLocation();
int vehicleRow = vehicleLocation.first;
int vehicleColumn = vehicleLocation.second;
cout << "You're using the vehicle at " << vehicleRow << " row, and column " << vehicleColumn << endl;
pathFind(grid, *vehicles[0], targetRow, targetColumn);
}