0

我用机器人创建了一个小迷宫,并使用 Blockly 生成代码来尝试解决它。我可以使用 Blockly 块的 Javascript 命令移动机器人。到目前为止,一切都很好。我目前对 if 语句和 while 循环的争论感到头疼。主要是,我尝试了两件事:

块状迷宫

  1. 创建一个变量“not_goal_reached”,它表示机器人是否已到达目标位置(交叉)。代码:

function not_done() {
  var goal_location = get_goal_position()
  var goal_x = goal_location[0];
  var goal_y = goal_location[1];
  console.log('in not done');
  //console.log(player.x!= goal_x || player.y != goal_y)
  return (player.x!= goal_x || player.y != goal_y);
};

Blockly.Blocks['not_goal_reached'] = {
  init: function() {
    this.appendDummyInput()
        .appendField("not at goal")
    this.setOutput(true, "Boolean");
    this.setColour(230);
    this.setTooltip('');
    this.setHelpUrl('');
  }
};

Blockly.JavaScript['not_goal_reached'] = function(block) {
  
  var code = 'not_done()';
  // TODO: Change ORDER_NONE to the correct strength.
  //console.log(code)
  return [code, Blockly.JavaScript.ORDER_ATOMIC];
};

但是,在 If 或 While 语句中使用此块时。我总是收到一个 Javascript 错误,无法帮助我找到解决方案:

TypeError: Cannot read property 'toBoolean' of undefined
    at Interpreter.stepConditionalExpression (acorn_interpreter.js:148)
    at Interpreter.step (acorn_interpreter.js:45)
    at nextStep (index.html:79)

我使用 Acorn js 解释器:

window.LoopTrap = 2000;
  //Blockly.JavaScript.INFINITE_LOOP_TRAP = 'if(--window.LoopTrap == 0) throw "Infinite loop.";\n';
	var code = Blockly.JavaScript.workspaceToCode(workspace);
	console.log(code);
	var myInterpreter = new Interpreter(code, initInterpreter);
	//Blockly.JavaScript.INFINITE_LOOP_TRAP = null
  

  var counter = 0;
  function nextStep() {
        try {
        if (myInterpreter.step()) {
          counter+=1;
          console.log(counter);
          if (counter < window.LoopTrap) {
              window.setTimeout(nextStep, 30);
              
            }
          else {
            throw "Infinite Loop!"
          }
        }
      }
  catch (e) {
        //alert(e);
        console.log(e)
    }
  } 
  
	nextStep();

问题:我无法解决的javascript错误:(

  1. 我创建了自己的不需要输入的 While 块。这个 While 块在内部检查机器人是否已经达到目标,然后处理 DO 语句:

Blockly.Blocks['repeat_forever'] = {
  init: function() {
    this.appendDummyInput()
        .appendField("While not at goal");
    this.appendStatementInput("DO")
        .appendField("Do");

    this.setPreviousStatement(true);
    this.setColour(230);
    this.setTooltip('');
    this.setHelpUrl('');
  }
};



Blockly.JavaScript['repeat_forever'] = function(block) {
  var branch = Blockly.JavaScript.statementToCode(block, 'DO');
  // TODO: Assemble JavaScript into code variable.
  //if (Blockly.JavaScript.INFINITE_LOOP_TRAP) {
  //  branch = Blockly.JavaScript.INFINITE_LOOP_TRAP.replace(/%1/g,
  //     '\'block_id_' + block.id + '\'') + branch;
  //  console.log(branch);
  //}

  var code = 'while (' + not_done() + ') {' + branch + '}';
  console.log(code)
  return [code, Blockly.JavaScript.ORDER_ATOMIC];

};

这可行,但是,在这里我有一个问题,即我的内部函数“not_done”仅被评估一次(在代码生成时)到 while(true)(因为第一次机器人当然不在目标位置)。此块正确应用 DO 代码,但不会停止(因为 while (true))。如果我在“not_done()”周围添加引号,则该函数显然会被评估一次,但随后我会收到与上面相同的 Javascript 错误(无法读取未定义的属性“toBoolean”)

我在这里错过了什么吗?非常感谢您的时间!

问候

ķ

4

1 回答 1

0

It seems that you setTimeout which cannot be reached while the while loop runs.

于 2018-11-07T20:40:56.853 回答