Python 多维数组的路径搜索
我正在尝试制作一个基于地下城的游戏,并且正在制作一个理论路径查找器,但是每当我运行这个程序时,它都会打印出我在第一名输入的相同坐标(理论坐标)。一般来说,我对编程有点“陌生”,所以我被卡住了Python 多维数组的路径搜索,python,Python,我正在尝试制作一个基于地下城的游戏,并且正在制作一个理论路径查找器,但是每当我运行这个程序时,它都会打印出我在第一名输入的相同坐标(理论坐标)。一般来说,我对编程有点“陌生”,所以我被卡住了 import winsound def main(): snd = winsound.Beep a = input("Enter the x value for the entrance: ") b = input("Enter the y value for the entranc
import winsound
def main():
snd = winsound.Beep
a = input("Enter the x value for the entrance: ")
b = input("Enter the y value for the entrance: ")
entrance = [a, b]
x = input("Enter the x value for the exit: ")
y = input("Enter the y value for the exit: ")
a = float(a)
b = float(b)
x = float(x)
y = float(y)
exut = [x, y] #deliberatly placed exit misspelling as exit is a command
done = False
while done == False:
if b > a:
b = b - 1
elif b < a:
b = b + 1
else:
if a > x:
a = a - 1
elif a < x:
a = a + 1
else:
done = True
done2 = False
while done2 == False:
if b > y:
b = b - 1
elif b < y:
b = b + 1
else:
snd(494, 250)
snd(659, 400)
print("done!")
print(entrance)
print(exut)
entrance = [a, b]
exut = [a, b]
done2 = True
我不知道它为什么会这样做,请您帮忙,我们将不胜感激,谢谢。首先,您不需要转换为float,因为您只需要一步一步地移动,所以请删除以下代码:
a = float(a)
b = float(b)
x = float(x)
y = float(y)
这是不一致的,因为您在转换为浮动之前将a
和b
分配给entry
,但在转换之后将x
和y
分配给exut
。但是,您应该添加代码,以确保只能为入口
和出口
输入整数值
在第一个循环中,您正在将b
与a
进行比较,这时它应该与y
进行比较(因为y
是b
的目标值):
如果您想查看寻路器的进度,可以将print(a,b)
添加到寻路循环中:
while done == False:
print(a, b)
if b > y: # changed a to y here
b = b - 1
elif b < y: # changed a to y here too
b = b + 1
else:
if a > x:
a = a - 1
elif a < x:
a = a + 1
else:
done = True
完成时==False:
印刷品(a、b)
如果b>y:#此处将a更改为y
b=b-1
elif bx:
a=a-1
如果a
不清楚问题出在哪里,该怎么办?
while done == False:
if b > y: # changed a to y here
b = b - 1
elif b < y: # changed a to y here too
b = b + 1
else:
if a > x:
a = a - 1
elif a < x:
a = a + 1
else:
done = True
print("done!")
print(entrance)
print(exut)
print(a, b) # print out a and b. they should be equal to exut
while done == False:
print(a, b)
if b > y: # changed a to y here
b = b - 1
elif b < y: # changed a to y here too
b = b + 1
else:
if a > x:
a = a - 1
elif a < x:
a = a + 1
else:
done = True