-
Notifications
You must be signed in to change notification settings - Fork 0
/
utily.js
174 lines (165 loc) · 5.22 KB
/
utily.js
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
var lodash = require('lodash');
module.exports.findInMatrix = function(mat, obj) {
var x = -1;
var y = -1;
y = lodash.findIndex(mat, function(array) {
x = lodash.findIndex(array, function(row) {
return row === obj;
});
return x != -1;
});
return {
"x": x,
"y": y
}
};
module.exports.arraysExist = function (mat,array,idname,selector) {
var self = this;
return lodash.every(array, function (obj) {
var objCoord = self.findInMatrix(mat, selector.toString() + obj[idname.toString()].toString());
console.log('Path : ' + obj[idname.toString()] + ' Coord : ' + JSON.stringify(objCoord));
return (objCoord.x != -1 && objCoord.y != -1);
});
};
module.exports.pushToArray = function (array,obj) {
if(array.constructor == Array){
array.push(obj);
}else{
if (array === '0') {
array = [obj];
}
}
return array;
};
// TODO: Check for edge cases
module.exports.moveToExit = function (matrix,PathObj) {
var self = this;
var PathZero = PathObj;
var PathZeroName = 'L' + PathZero.pathNum.toString();
var PathZeroExitName = 'E' + PathZero.exitNum.toString();
var PathZero_coord = self.findInMatrix(matrix, PathZeroName);
var PathZeroExit_coord = self.findInMatrix(matrix, PathZeroExitName);
//console.log('Path Coord : ' + toJson(PathZero_coord) + ' Exit Coord : ' + toJson(PathZeroExit_coord));
var movX = PathZero_coord.x;
var movY = PathZero_coord.y;
var tempMovX;
var tempMovY;
if (PathZero_coord.x != PathZeroExit_coord.x && PathZero_coord.y != PathZeroExit_coord.y) {
//Move on the X axis
if (PathZero_coord.x < PathZeroExit_coord.x) {
tempMovX = movX;
tempMovY = movY;
while (movX != PathZeroExit_coord.x) {
movX++;
if (matrix[movY][movX] === '-1') {
movX = tempMovX;
movY = tempMovY;
break;
}
matrix[movY][movX] = self.pushToArray(matrix[movY][movX], PathZeroName);
}
}
if (PathZero_coord.x > PathZeroExit_coord.x) {
tempMovX = movX;
tempMovY = movY;
while (movX != PathZeroExit_coord.x) {
movX--;
if (matrix[movY][movX] === '-1') {
movX = tempMovX;
movY = tempMovY;
break;
}
matrix[movY][movX] = self.pushToArray(matrix[movY][movX], PathZeroName);
}
}
if (PathZero_coord.y < PathZeroExit_coord.y) {
tempMovX = movX;
tempMovY = movY;
while (movY != PathZeroExit_coord.y) {
movY++;
if (matrix[movY][movX] === '-1') {
movX = tempMovX;
movY = tempMovY;
break;
}
matrix[movY][movX] = self.pushToArray(matrix[movY][movX], PathZeroName);
}
}
//Move on the Y axis
if (PathZero_coord.y > PathZeroExit_coord.y) {
tempMovX = movX;
tempMovY = movY;
while (movY != PathZeroExit_coord.y) {
movY--;
if (matrix[movY][movX] === '-1') {
movX = tempMovX;
movY = tempMovY;
break;
}
matrix[movY][movX] = self.pushToArray(matrix[movY][movX], PathZeroName);
}
}
}
//Move straight
if (PathZero_coord.x == PathZeroExit_coord.x) {
if (PathZero_coord.y < PathZeroExit_coord.y) {
while (movY != PathZeroExit_coord.y) {
movY++;
matrix[movY][movX] = self.pushToArray(matrix[movY][movX], PathZeroName);
}
}
if (PathZero_coord.y > PathZeroExit_coord.y) {
while (movY != PathZeroExit_coord.y) {
movY--;
matrix[movY][movX] = self.pushToArray(matrix[movY][movX], PathZeroName);
}
}
}
if (PathZero_coord.y == PathZeroExit_coord.y) {
if (PathZero_coord.x < PathZeroExit_coord.x) {
while (movX != PathZeroExit_coord.x) {
movX++;
matrix[movY][movX] = self.pushToArray(matrix[movY][movX], PathZeroName);
}
}
if (PathZero_coord.x > PathZeroExit_coord.x) {
while (movX != PathZeroExit_coord.x) {
movX--;
matrix[movY][movX] = self.pushToArray(matrix[movY][movX], PathZeroName);
}
}
}
if(movY == PathZeroExit_coord.y && movX != PathZeroExit_coord.x - 1){
if (movX < PathZeroExit_coord.x) {
while (movX != PathZeroExit_coord.x) {
movX++;
matrix[movY][movX] = self.pushToArray(matrix[movY][movX], PathZeroName);
}
}
if (movX > PathZeroExit_coord.x) {
while (movX != PathZeroExit_coord.x) {
movX--;
matrix[movY][movX] = self.pushToArray(matrix[movY][movX], PathZeroName);
}
}
}
if(movX == PathZeroExit_coord.x && movY != PathZeroExit_coord.y - 1){
if (movY < PathZeroExit_coord.y) {
while (movY != PathZeroExit_coord.y) {
movY++;
matrix[movY][movX] = self.pushToArray(matrix[movY][movX], PathZeroName);
}
}
if (movY > PathZeroExit_coord.y) {
while (movY != PathZeroExit_coord.y) {
movY--;
matrix[movY][movX] = self.pushToArray(matrix[movY][movX], PathZeroName);
}
}
}
return matrix;
};
module.exports.extractConflicts = function (matrix, lane) {
lodash.each(matrix,function (oneRow) {
})
}