如何让langton的蚂蚁在python中迈出一步

时间:2013-09-05 18:44:18

标签: python python-2.7 ant

我刚刚开始学习计算机科学,并且一直停留在编写langton的蚂蚁上。 我的蚂蚁需要像这样大致迈出一步

白色饰面上的蚂蚁:东方>>> row + 1,south>>> column-1,west>>> row-1,north>>>柱+ 1

黑色饰面上的蚂蚁:东方>>> row-1,south>>> column + 1,west>>> row + 1,north>>>柱1

def ant_coordinates(ant_row, ant_col, orientation):
    color = orig_grid[ant_row][ant_col]
    if color == 'white':
        orientation == 'East'
        ant_row += 1
        orientation == 'South'
        ant_col -= 1
    return ant_row, ant_col, orientation

当我将南方作为方向时,我的东西会在行部分添加一个

1 个答案:

答案 0 :(得分:0)

你正在测试那些==的平等,但没有对它做任何事情。你需要的是:

def ant_coordinates(ant_row, ant_col, orientation):
    color = orig_grid[ant_row][ant_col]
    if color == 'white':
        if orientation == 'East':
            ant_row += 1
        elif orientation == 'South':
            ant_col -= 1
    return ant_row, ant_col, orientation

我不确定这个函数的实际行为应该是什么,但它会做的是:

如果颜色不是白色,则只返回所有参数。

如果颜色为白色且方向为东,则返回所有参数,行+ = 1.

如果颜色为白色且方向为南,则使用col - = 1返回所有参数。

如果颜色为白色且方向为其他,则返回所有参数不变。显然,您可以扩展此功能以添加其他方向的功能。

您的代码现在所做的是:

def ant_coordinates(ant_row, ant_col, orientation):
    color = orig_grid[ant_row][ant_col]
    if color == 'white': # goes in here if color is white
        orientation == 'East' # returns True or False, value not used anywhere
        ant_row += 1 # always increments row if color is white
        orientation == 'South' # returns True or False, value not used anywhere
        ant_col -= 1 # always decrements col if color is white
    return ant_row, ant_col, orientation

希望这有帮助! ==只是一个比较,它并不意味着if