我在JavaScript中创建了一个程序来执行您提供的Robot指令。每个机器人对象。
机器人位于矩形区域内。它以(x,y)点移动。这样(0 <= x <= 50)和(0 <= y <= 50)
我不知道如何防止新机器人进入与先前机器人丢失相同的坐标(因为超出矩形表面)。
如何防止另一个新机器人(当我说新的机器人,我的意思是新的机器人对象)跳到同一个机器人前去的地方并迷路时。
我有一堆(x,y)机器人迷路了。但是,我如何使用这个阵列让新机器人走到这一步呢?
我尝试使用for循环来运行数组来查看坐标,但是没有做任何事情。
在处理我的问题时,您是否也可以提供一些提示,说明如何简化代码但执行相同功能,但效率更高。
=
var orientation = ["N", "E", "S", "W"];
var instruction = ["L", "R", "F"];
var lost_Robot_Scent = [];
// function created for assigning coordinates and an orientation
function Robot_Coordinatation(x, y, orientation) {
// coordinate (x,y) must be located at (0,0) at the initial state of the program
this.x = 0;
this.y = 0;
// orientation assigned
this.orientation = orientation;
// this is printed for the purpose of tidiness
document.write("============================" + "<br />");
// | ( x,y) | e.g(S)
document.write("| ( " + x + ", " + y + " ) | " + orientation + "<br />");
// We have a nested function here that will determine the movement/instruction of the robot
this.Robot_Instruction = function(instruct_The_Robot) {
// We are making sure here that the length of the instruction is less than 100
if (instruct_The_Robot.length <= 100) {
// if...statement - if x & y is bigger than or equal to 0 and smaller than or equal to 50 -> If its true then go inside the if statment.
// Essentiallly, what this statement is actually doing is that its creating the rectangular grid.
if ((x <= 50 && x >= 0) && (y <= 50 && y >= 0)) {
// itterate the array of the instruct_The_Robot
for (var i = 0; i < instruct_The_Robot.length; i++) {
// if any value of instruct_The_Robot is strictly equal to "L", then go inside this if statement. refer to line: 10
if (instruct_The_Robot[i] === instruction[0]) {
// variable Left declared and instantiated with -90°
var Left = -90 + "°";
// variable result instantiated with value (x,y) & orientation
var result = " | ( " + x + ", " + y + " ) " + " | " + orientation + " " + Left + "<br />";
// however, if the if...statment at line: 33 is not true, then follow this : if the value of instruct_The_Robot is equal to "R"...
} else if (instruct_The_Robot[i] === instruction[1]) {
// variable Right instantiated with 90°
var Right = 90 + "°";
// variable result instantiated
var result = " | ( " + x + ", " + y + " ) " + " | " + orientation + " " + Right + "<br />";
// however, if the if...statment at line: 33 & elseif at line: 39 is not true, then if instruct_The_Robot is equal to "F"...
} else if (instruct_The_Robot[i] === instruction[2]) {
// variable y_Plus_One is instantiated with the current value of y and moves y one point forward
var y_Plus_One = y += 1;
// if the negation of x & y_Plus_One is smaller than 50 and bigger the 0, then...
if (!((x <= 50 && x >= 0) && (y_Plus_One <= 50 && y_Plus_One >= 0))) {
// then print " lost! "
document.write("LOST!" + "<br />");
// & keep the record of the x and y_Plus_One value to the lost_Robot_Scent array
lost_Robot_Scent.push([x, y]);
// and return false - this stops printing "Lost!" more than one times
return false;
// Otherwise, if the above doesn't satisfy, then...
} else {
// variable result instantiated with the updated coordinates (y_Plus_One)
var result = " | ( " + x + ", " + y_Plus_One + " ) " + " | " + orientation + " " + "<br />";
}
}
}
//print the result
document.write(result);
// if none of the if...statement above satisfy, then...
} else {
// variale lost instantiated with "Lost!" message
var lost = "LOST!" + "<br />";
// push the robot to the lost_Robot_Scent
lost_Robot_Scent.push("| ( " + x + ", " + y + " ) " + "<br />");
}
} else {
alert("There is alot of of instructions given. Please make sure that the instruction is less than 100 instructions");
}
}
}
// new Robot object initialised
var one = new Robot_Coordinatation(50, 50, orientation[1]);
one.Robot_Instruction("LRLRLRLRLRLLRRLRLRLLRLLRRLL");
var two = new Robot_Coordinatation(20, 30, orientation[3]);
two.Robot_Instruction("FFFLLFLRLFLRFLRLLLFRL");
var two = new Robot_Coordinatation(30, 7, orientation[3]);
two.Robot_Instruction("FFFFLRLFLRFLRL");
&#13;
答案 0 :(得分:0)
如何防止新机器人与旧机器人在同一个地方迷路
假设您修复了错误,机器人正在移动。当您确定机器人正在向前移动时,您必须确定新坐标是否已在lost_Robot_Scent
数组中。您可以使用以下内容执行此操作:
var robotLostAtSameLocation = false;
for (var i = 0; i < lost_Robot_Scent.length; i++) {
var lostRobotLocation = lost_Robot_Scent[i];
if(lostRobotLocation[0] === x && lostRobotLocation[1] === y) {
robotLostAtSameLocation = true;
break;
}
}
if (robotLostAtSameLocation) {
// whatever you want to do in this case
}
else {
// whatever you want to do in this case
}
简单优化
请注意,如果您将lost_Robot_Scent
数组从包含[x,y]
更改为包含'x:y'
之类的内容,则可以摆脱此循环。因此,不是包含其他数组的数组:[[39,51], [51,15], [-1,11]]
,它将是一个包含字符串的数组:['39:51', '51:15', '-1:11']
。这给你带来的是你可以说var robotLostAtSameLocation = lost_Robot_Scent.indexOf(x + ':" + y) > -1;
<强>建议强>
var instruction = ["L", "R", "F"];
更改为更像var INSTRUCTIONS = {LEFT: 'L', RIGHT: 'R', FORWARD: 'F'};
的内容。这会将instruct_The_Robot[i] === instruction[0]
之类的行转换为instruct_The_Robot[i] === INSTRUCTIONS.LEFT
,从而提高可读性。<强>错误强>
[x,y]
将lost_Robot_Scent
数组推入一行,然后将"| ( " + x + ", " + y + " ) " + "<br />"
推入另一行的同一数组中。不要这样做。如果程序不一致,很难推理出一个程序。答案 1 :(得分:0)
Sometimes we don’t get response from http://rubygems.org/. So it will show the given error.
You can use following ways for fixing this issue
Try again for gem install using gem install rack command and run bundle update rake for updating your Gemfile.lock file.
Delete the Gemfile.lock and again bundle install it.
我可以帮助您保留机器人丢失的位置集合(您的lost_Robot_Scent
数组)。我建议在lost_Robot_Scent
上使用Set
。集提供O(1)插入和O(1)查找。在这种情况下,这可能不是什么大问题,但无论如何都要了解Array
。
您遇到的主要问题是数组相等是指针相等:例如,Set
返回[1, 1] === [1, 1]
。一种解决方法是在数组上使用false
并在toString()
中存储 。例如:
Set
我不知道这是否是最干净的解决方案,但它确实有效。如果您想使用var lost_Robot_Scent = new Set();
lost_Robot_Scent.add( [1, 1].toString() );
lost_Robot_Scent.has( [1, 1].toString() ); // -> true
lost_Robot_Scent.has( [2, 2].toString() ); // -> false
而不是Array
,请使用Set
代替push
和includes
(或add
)代替indexOf
,例如
has
如果您担心性能问题,可以在特定情况下相互测试这两种方法。
简化代码可以做的一件事是减少嵌套的var lost_Robot_Scent = [];
lost_Robot_Scent.push( [1, 1].toString() );
lost_Robot_Scent.includes( [1, 1].toString() ); // -> true
lost_Robot_Scent.includes( [2, 2].toString() ); // -> false
语句。例如,if
函数中的第一个if
可能是
Robot_Instruction
也许这有点偏离主题,但你应该只使用评论,因为事情并不明显。例如,if (instruct_The_Robot.length > 100) {
alert("Too many instructions! Please limit to 100."
return;
}
// continue...
是不必要的。
最后一件事:你可以简化&#34;指令&#34;使用variable Left declared and instantiated with -90°
语句而不是switch
块进行循环。或者,如果您想要使其具有超级可读性,则可以使用如下的JavaScript对象:
if/else
这是一个非常巧妙的技巧。