我正在做的这项作业有问题。我的任务是对 karel 进行编程,使其跨越 8 排 1-3 块不等的障碍。首先,一切都很顺利,karel 轻松地跨过了最初的几个障碍。
我的代码:
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()
}
}
第四次执行
jumpHurdle()
后,Karel 拒绝左转面向北执行 jumpUp()
和 glideDown()
命令,而是在 glideDown()
结束后保持面向左,并尝试移动,自此它前面有一个障碍,它就会关闭。在第四次执行 jumpHurdle
之前,karel 最多能够跨越 3-4 个障碍,具体取决于随机布局。
我尝试在
jumpUp()
末尾添加if语句:
if (!this.frontIsClear())
{
this.turnLeft()
if (this.frontIsClear())
{
this.move()
}
this.turnRight()
}
}
基本上,我希望卡雷尔在滑翔下来并遇到障碍物后能够检测到前面有一堵墙。如果它检测到情况不清楚,卡雷尔就会左转面向北,然后可能会移动并开始攀爬障碍物?相反,问题仍然存在。卡雷尔仍然没有向左转面向北开始执行
jumpUp()
,并且像往常一样保持向左转,试图移动,然后......关闭。
在此处输入图像描述(卡雷尔失败) 在此处输入图像描述(karel 的任务)
需要注意的是,循环、if-else-if-else、递归是我禁止使用的。老师禁止这样做。所以我仅限于纯粹的
if
和 else
陈述。 and (&&)
/ or (||)
是允许的,但我认为它们在这里没有任何用处。这只是想指出,我很清楚使用简单循环之类的东西可以让我更轻松,并且代码更短/可读。
如果需要更多上下文,这是 Steeplechaser 继承的类:
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()
}
}
主要:
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 和 glideDown 都有问题。
jumpUp 可以通过使用 this.rightIsClear() 来优化,而不是来回转动。 如果由于某种原因该功能不可用,也可以很容易地定义它。