Reeborg's World

What is Karel

卡雷尔语言(KAREL)是一种专门用于机器人系统架构的语言,对于初学 C++, JAVA, Python 等程序性语言有着相当大的帮助,提升初学者的逻辑思维,下面以 Reeborg's world 上的一些关卡做一些简单介绍,帮助初学者更好的理解 Python。

Karel is an educational programming language for beginners, created by Richard E. Pattis in his book Karel The Robot: A Gentle Introduction to the Art of Programming . Pattis used the language in his courses at Stanford University, California. The language is named after Karel Čapek, a Czech writer who introduced the word robot in his play R.U.R.

What Should You Preview in Advance

Python 基础语法 | 菜鸟教程 (runoob.com)

Python 条件语句 | 菜鸟教程 (runoob.com)

Python 循环语句 | 菜鸟教程 (runoob.com)

Python for 循环语句 | 菜鸟教程 (runoob.com)

Python 函数 | 菜鸟教程 (runoob.com)

Python range() 函数 | 菜鸟教程 (runoob.com)

Start Up

Reeborg's World

导航栏有 World info, Reeborg's keyboard & Additional options

World info 中主要查看当前关卡的一些帮助信息

Reeborg's keyboard 中可以查看有哪些内置命令以及其他指令,同时可以帮助我们快速填充函数

Additional options 自己看

关卡选择在 World info 旁,是一个下拉菜单

Hurdle Loop With Fixed Distance (Lv. 1)

第一关(Hurdel 1):障碍物位置及数目固定不变

# 基础函数
move()
turn_left()

# turn_left()可以让机器人向左转。

重复使用turn_left()使机器人向右转会使代码冗杂,所以,我们先用def关键字定义一个向右转的函数,然后给我们的函数取一个名字,就是turn_right()

此关中,一个简单的跨栏循环主要包括:移动,左转,移动,右转,移动,右转,移动,左转。可以发现一共有六面墙,为避免重复操作,我们将定义一个jump()函数,取代上述的重复操作

或许在最后你依然会重复调用六次jump()函数以此到达目标所在地(如下所示)

def loops():
    jump()
    jump()
    jump()
    jump()
    jump()
    jump()

loops()

实际上,我们可以写一个for循环,循环调用这个跳转。在我的例子中,我将使用range()函数。而我要说的是从0到6。但不包括6。 所以在这种情况下,它将是0,1,2,3,,4,5。所以,其实要执行六次。最终结果如下

# 定义向右转
def turn_right():
    turn_left()
    turn_left()
    turn_left()

# 一个循环
def jump():
    move()
    turn_left()
    move()
    turn_right()
    move()
    turn_right()
    move()
    turn_left()

# 采用for循环
for step in range(0,6):
    jump()

Hurdle Loop by Using While Loop (Lv. 2)

第二关 (Hurdle 2):障碍的位置会随机改变。此时我们需要进行判断,采用While循环进行判断,若值为真,机器人就会行动,若值为假,则机器人停止行动。环境函数中有一个函数正好在此适用,即at_goal()

# 基础函数
move()
turn_left()

# 环境函数
at_goal()

# 额外条件:熟练应用While循环
while loop

有个疑问就是这个at_goal()默认值是TRUE还是FALSE呢,参考如下判断

# 假设法
while at_goal != True:
    jump()

# 反证法
while not at_goal():
    jump()

最终结果如下,仅在第一关的基础下增加while循环进行判断即可

def turn_right():...

def jump():...

while not at_goal():
    jump()

Hurdle Loop by Using While Loop Advanced (Lv. 3)

第三关(Hurdle 3):障碍的位置和数量都会改变。我们不仅不知道墙的位置外,我们也不知道会有多少堵墙。如果我们不知道墙的位置,那么我们就要测试一下前面是否有墙。

# 本关所需函数
move()
turn_left()

# 环境函数
front_is_clear()
wall_in_front()
at_goal()

# 额外条件:熟练应用While循环,if条件语句
while loop
if statement

环境函数有两个函数正好适用,即front_is_clear() wall_in_front,任选其一在本关都行得通,如果前面有墙,则执行jump()函数进行一次循环,否则则执行move()函数进行移动

if wall_in_front():
    jump()
else:
    move()

你会发现在后面依旧会有冲突,原因在于最初我们定义的jump()函数第一步是执行move()函数,删除这一步,最终结果如下所示

def turn_right():...

def jump():
    turn_left()
    move()
    turn_right()
    move()
    turn_right()
    move()
    turn_left()
  
while not at_goal():
    if wall_in_front():
        jump()
    else:
        move()

Hurdel Loop With Variable Height (Lv. 4)

第四关(Hurdle 4):位置、高度和障碍的数量都会发生变化,基于前面第三关的基础,本关主要解决不同高度的问题

主要思路:在机器人执行jump()函数的第一步turn_left()后,应当检查一下右边是否有一堵墙。所以我可以用wall_on_right()这个条件来实现。如果这是真的。那我就想继续往前走,直到没有墙的地方为止。转向后采用front_is_clear()来判断前方是否可走,若可走,则一直走到有障碍物为止

def turn_right():...

def jump():
    turn_left()
    while wall_on_right():
        move()
    turn_right()
    move()
    turn_right()
    while front_is_clear():
        move()
    turn_left()
  
while not at_goal():...

Maze Escaping (Lv. 5)

第五关(Maze)参考代码如下,但在某些特殊情况下,程序会进入无限循环(Endless Loop)

def turn_right():...
  
while front_is_clear():
    move()
turn_left()
  
while not at_goal():
    if front_is_clear():
        move()
    elif right_is_clear():
        turn_right()
        move()
    else:
        turn_left()
最后修改:2022 年 08 月 28 日
如果觉得我的文章对你有用,请随意赞赏