You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Karel机器人跨栏4次后故障:无法左转致停机求助

Karel跨栏机器人故障排查

问题现象

我在完成编程作业,任务是让Karel机器人遍历8行高度1-3块的栏架。前几次跨栏正常,但第4次调用jumpHurdle()后,Karel在glideDown()结束后保持错误朝向,无法转回正确方向执行jumpUp(),尝试移动时因前方有栏架停机,最多只能完成3-4次跨栏(取决于随机布局)。

我曾在jumpUp()末尾添加以下代码,但问题未解决:

if (!this.frontIsClear()) 
{
  this.turnLeft()
  if (this.frontIsClear()) 
  {
    this.move()
  }
  this.turnRight()
}

限制条件

老师禁止使用循环、if-else-if-else和递归,仅允许if/else语句及&&/||运算符。

完整代码

Steeplechaser类

class Steeplechaser(st: Int, ave: Int, dir: Direction, beepers: Int) : Racer(st, ave, dir, beepers) {

override fun jumpHurdle() 
{
  if (!this.frontIsClear()) 
  {
    this.jumpUp()
    this.move()         // karel moves east/right after climbing a hurdle between 1-3 blocks
    this.glideDown()
    } 
      else            // karel moves forward continuously if there are no hurdles
    {
      this.move()  
      }
 }

override fun jumpUp() 
{
  if (!this.frontIsClear()) // karel starts facing east, checks if the front is not clear
  {
    this.turnLeft()       // since it returns true that the front is not clear, karel turns left, facing north now
    this.move()          // karel moves 1 hurdle
    this.turnRight()     // karel turns right, and begins checking again if the front is not clear.         
                            // The code works for 
                            // hurdles of 1-3 blocks high
    }

      if (!this.frontIsClear()) 
      {
        this.turnLeft()
        this.move()
        this.turnRight()
        }
          if (!this.frontIsClear()) 
          {
            this.turnLeft()
            this.move()
            this.turnRight()
            }
              if (!this.frontIsClear()) 
              {
                this.turnLeft()
                if (this.frontIsClear()) 
                {
                  this.move()
                }
                this.turnRight()
              }
}

override fun glideDown() 
{
  this.turnRight()             // since karel moved after jumpUp(), karel turns right, to face south
  if (this.frontIsClear())     // karel checks if the front is clear
  {
    this.move()                 // since it is clear, karel moves, "gliding down" the hurdle. The  
                                   code should work for    
                                   hurdles of 1-3 blocks high
    }
      if (this.frontIsClear()) 
      {
        this.move() 
        }
          if (this.frontIsClear()) 
          {
            this.move() 
          }
            else                  // this is for when karel reaches the bottom.
                {
                  this.turnLeft() // since the front is not clear, karel should theoretically turn 
                                     // left in preparation to 
                                     // climb the next hurdle with jumpUp(). If there isn't then it  
                                     // moves until it encounters a 
                                     // hurdle as stated in jumpHurdle()
                }
}

          
override fun turnRight() 
{
  this.turnLeft()
  this.turnLeft()
  this.turnLeft()
  }
}

父类Racer

open class Racer:Robot
{
  constructor(st:Int, ave:Int, dir:Direction, beepers:Int):super(st, ave, dir, beepers) {}
  
  fun raceStride()
  {
    if (this.frontIsClear())
    {
      this.move()
    }
    else
    {
      this.jumpHurdle()
    }
  }

  open fun jumpHurdle()
  {
    this.jumpUp()
    this.move()
    this.glideDown()
  }

  open fun jumpUp()
  {
    this.turnLeft()
    this.move()
    this.turnRight()
  }

  open fun glideDown()
  {
    this.turnRight()
    this.move()
    this.turnLeft()
  }

  open fun turnRight()
  {
    this.turnLeft()
    this.turnLeft()
    this.turnLeft()
  }
}

main函数

fun main() {
  initializeWorld("world${(0..3).random()}.kwld")
  // Create your robot objects here and tell them what to do!
  var karel:Racer = Steeplechaser(1, 1, East, 0);
  karel.jumpHurdle()
  karel.jumpHurdle()
  karel.jumpHurdle()
  karel.jumpHurdle() // error, not turning left. moves and shuts off...
  // karel.jumpHurdle()
}

问题分析与修复

核心问题:多余的上升逻辑破坏了下滑转向

你在jumpUp()末尾添加的第四个if是错误根源——作业要求栏架高度仅为1-3,这个额外的逻辑会让Karel爬到超过栏架高度的位置(比如第4行)。此时执行glideDown()时,三次move后回到地面,但第三个if的条件为true(可以移动),不会触发配对的else分支执行turnLeft(),最终Karel面朝南方而非正确的东方,导致后续跨栏方向完全混乱。

修复步骤

  1. 移除jumpUp()末尾多余的第四个if:保留前三个if即可覆盖1-3高度的栏架,避免Karel过度爬升。
    修改后的jumpUp():
override fun jumpUp() 
{
  // 处理高度1的栏架
  if (!this.frontIsClear()) 
  {
    this.turnLeft()
    this.move()
    this.turnRight()
  }

  // 处理高度2的栏架(第一次上升后仍有障碍)
  if (!this.frontIsClear()) 
  {
    this.turnLeft()
    this.move()
    this.turnRight()
  }

  // 处理高度3的栏架(第二次上升后仍有障碍)
  if (!this.frontIsClear()) 
  {
    this.turnLeft()
    this.move()
    this.turnRight()
  }
}
  1. 验证glideDown()逻辑:现有逻辑在Karel从1-3高度下滑时,都会触发else分支执行turnLeft()转回东方,确保下一次跨栏时方向正确。

修改后,Karel每次跨栏结束后都会保持面朝东方,能正确执行后续的jumpHurdle()操作,不会再出现方向错误导致的停机问题。

内容的提问来源于stack exchange,提问作者R4NG

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 20:34:57