我正在尝试用Python模拟蛇和梯子游戏。到目前为止,我已经创建了一个名为iRandom
的模块,其中包含一个要掷骰子的骰子(数字1至6),以及两个函数:其中一个叫做movement
,另一个叫做{{1} }。 [注意:我知道可以用许多不同的方式来编程该游戏,但是我正在编程课程中,需要遵循一些特定的说明。]
这是我到目前为止的代码:
snakes_and_ladders
我的问题是,当我从终端运行import iRandom
def movement(starting_position, distance):
starting_position = 0
distance = iRandom.snakes_and_ladders_dice()
current_space = starting_position + distance
if current_space > 100:
current_space = starting_position
elif current_space == 1:
current_space = 38
elif current_space == 4:
current_space = 14
elif current_space == 9:
current_space = 31
elif current_space == 21:
current_space = 42
elif current_space == 28:
current_space = 84
elif current_space == 36:
current_space = 44
elif current_space == 51:
current_space = 67
elif current_space == 71:
current_space = 91
elif current_space == 80:
current_space = 100
elif current_space == 16:
current_space = 6
elif current_space == 47:
current_space = 26
elif current_space == 49:
current_space = 11
elif current_space == 56:
current_space = 53
elif current_space == 62:
current_space = 19
elif current_space == 64:
current_space = 60
elif current_space == 87:
current_space = 24
elif current_space == 93:
current_space = 73
elif current_space == 95:
current_space = 75
elif current_space == 98:
current_space = 78
return current_space
def snakes_and_ladders(must_print):
current_position = 0
while current_position < 100:
number = iRandom.snakes_and_ladders_dice()
new_position = movement(current_position, number)
if must_print == TRUE:
print("{}\t{}\t{}").format(current_position, number, new_position)
current_position += new_position
if __name__ == "__main__":
snakes_and_ladders(must_print)
时(即,存储此脚本时的文件),我得到了语法错误消息或打印了一些内容,这些内容在纠正了错误之后是我所期望的错误。
我最苦苦挣扎的事情如下:
./snakes_and_ladders.py
函数集成到第二个函数中。movement
(我正在使用它,因为说明要求使用它,但是我并没有真正理解它...我什至不确定我是否正在使用它,正确的方法。)让我知道您是否可以帮助我弄清楚发生了什么或者是否需要其他信息!