forked from ICSoftware/rover
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchallenge.js
More file actions
249 lines (230 loc) · 7.42 KB
/
Copy pathchallenge.js
File metadata and controls
249 lines (230 loc) · 7.42 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
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
'use strict';
window.onload = function () {
console.log('test');
var command =
'5 3 \n 1 1 e\n rfrfrfrf\n 3 2 N \n frrffllffrrfll\n 0 3 w\n LLFFFLFLFL';
// this function parses the input string so that we have useful names/parameters
// to define the playfield and the robots for subsequent steps
var parseInput = function (input) {
// task #1
// replace the 'parsed' var below to be the string 'command' parsed into an object we can pass to genworld();
// genworld expects an input object in the form { 'bounds': [3, 8], 'robos': [{x: 2, y: 1, o: 'W', command: 'rlrlff'}]}
// where bounds represents the top right corner of the plane and each robos object represents the
// x,y coordinates of a robot and o is a string representing their orientation. a sample object is provided below
var parsed = {};
var splitInput = input.split('\n');
//bounds
parsed.bounds = splitInput
.shift()
.trim()
.split(' ')
.map(function(value) {
return Number.parseInt(value, 10);
});
//robos
parsed.robos = [];
splitInput.forEach(function(value,index) {
if(index % 2 === 0) {
var splitValue = value.trim().split(' ');
parsed.robos.push({
x: Number.parseInt(splitValue[0], 10),
y: Number.parseInt(splitValue[1], 10),
o: splitValue[2].toUpperCase(),
});
} else {
parsed.robos[parsed.robos.length-1].command = value.trim().toLowerCase();
}
});
return parsed;
};
var lostRobos = [],
summarized = false;
// this function replaces teh robos after they complete one instruction
// from their commandset
var tickRobos = function (robos) {
// task #2
// in this function, write business logic to move robots around the playfield
// the 'robos' input is an array of objects; each object has 4 parameters.
// This function needs to edit each robot in the array so that its x/y coordinates
// and orientation parameters match the robot state after 1 command has been completed.
// Also, you need to remove the command the robot just completed from the command list.
// example input:
// robos[0] = {x: 2, y: 2, o: 'N', command: 'frlrlrl'}
// |- becomes -|
// robos[0] = {x: 2, y: 1, o: 'N', command: 'rlrlrl'}
// if a robot leaves the bounds of the playfield, it should be removed from the robos
// array. It should leave a 'scent' in it's place. If another robot–for the duration
// of its commandset–encounters this 'scent', it should refuse any commands that would
// cause it to leave the playfield.
// !== write robot logic here ==!
var actionMap = getActionMap();
robos.forEach(function(bot,index,array) {
if(bot.command.length === 0) return;
var currentCommand = bot.command.substr(0,1);
var actionItem = actionMap.filter(function(item) { // find not always supported
return item.o === bot.o;
})[0];
bot.command = bot.command.substr(1);
if(currentCommand !== 'f') {
bot.o = actionItem[currentCommand];
} else if(!isCommandInScents(bot)) {
var positionValues = actionItem.moveAndReturnCheckObj(bot);
if(positionValues.coord < 0 || positionValues.coord > positionValues.bound) {
lostRobos.push(Object.create(bot)); // assign not always available
array.splice(index,1);
}
}
});
//leave the below line in place
placeRobos(robos);
if(!summarized && (robos.length === 0 || robos.filter(function(i) { return i.command.length > 0; }).length === 0)) {
summarized = missionSummary(robos);
}
///////////////
function getActionMap() {
return [{
o: 'N',
l: 'W',
r: 'E',
moveAndReturnCheckObj: function(state) {
state.y++;
return {
coord:state.y,
bound: bounds[1]
};
}
}, {
o: 'S',
l: 'E',
r: 'W',
moveAndReturnCheckObj: function(state) {
state.y--;
return {
coord:state.y,
bound: bounds[1]
};
}
}, {
o: 'E',
l: 'N',
r: 'S',
moveAndReturnCheckObj: function(state) {
state.x++;
return {
coord:state.x,
bound: bounds[0]
};
}
}, {
o: 'W',
l: 'S',
r: 'N',
moveAndReturnCheckObj: function(state) {
state.x--;
return {
coord:state.x,
bound: bounds[0]
};
}
}];
}
function isCommandInScents(state) {
return lostRobos.filter(function(i) { // find not always supported
return state.o === i.o && state.x === i.x && state.y === i.y;
}).length > 0;
}
};
// mission summary function
var missionSummary = function (robos) {
// task #3
// summarize the mission and inject the results into the DOM elements referenced in readme.md
var frag = document.createDocumentFragment();
robos.forEach(function(i) {
var li = document.createElement('li');
var content = 'Position: ' + i.x + ', ' + i.y + ' | Orientation: ' + i.o;
li.textContent = content;
frag.appendChild(li);
});
document.getElementById('robots').appendChild(frag);
frag = document.createDocumentFragment();
lostRobos.forEach(function(i) {
var li = document.createElement('li');
var content = 'Position: ' + i.x + ', ' + i.y + ' | Orientation: ' + i.o;
li.textContent = content + ' | ' + createKillerInstruction(i);
frag.appendChild(li);
});
function createKillerInstruction(state) {
var liveState = Object.create(state);
switch(state.o){
case 'N':
liveState.y--;
break;
case 'S':
liveState.y++;
break;
case 'E':
liveState.x--;
break;
case 'W':
liveState.x++;
}
return 'Killer Instruction - ' +
'Position: ' + liveState.x + ', ' + liveState.y + ' | Orientation: ' + liveState.o + ' | Command: f ';
}
document.getElementById('lostRobots').appendChild(frag);
return true;
};
// ~~~~~~!!!! please do not edit any code below this comment !!!!!!~~~~~~~;
var canvas = document.getElementById('playfield')
.getContext('2d'),
width = document.getElementById('playfield')
.width * 2,
height = document.getElementById('playfield')
.height * 2,
fontSize = 18,
gridText = [],
gameWorld,
bounds;
canvas.font = 'bold ' + fontSize + 'px monospace';
canvas.fillStyle = 'black';
canvas.textAlign = 'center';
var genworld = function (parsedCommand) {
//build init world array
gameWorld = [];
bounds = parsedCommand.bounds;
var robos = parsedCommand.robos;
var row = [];
for (var i = 0; i < bounds[0]; i++) {
row.push('.');
}
for (var i = 0; i < bounds[1]; i++) {
var test = [].concat(row);
gameWorld.push(test);
}
placeRobos(parsedCommand.robos);
render(gameWorld, parsedCommand.robos);
tickRobos(robos);
window.setTimeout(function () {
genworld(parsedCommand);
}, 1000);
};
var placeRobos = function (robos) {
for (var i in robos) {
var robo = robos[i];
var activeRow = gameWorld[robo.y];
if (activeRow) {
activeRow[robo.x] = robo.o;
}
}
};
//render block
var render = function (gameWorld, robos) {
canvas.clearRect(0, 0, width, height);
for (var i = 0; i < gameWorld.length; i++) {
var blob = gameWorld[i].join('');
canvas.fillText(blob, 250, i * fontSize + fontSize);
}
};
// wireup init functions for display
genworld(parseInput(command));
};