我正在製作一艘戰艦遊戲。我不知道爲什麼,當你在n不同位置重疊一艘船時,我的程序會要求你輸入不同的起始位置n次。爲什麼我的程序匹配錯誤的值?
def make_ships(name, length, position, orientation):
ships[name] = {"length": length, "Coordinates": {}}
x = ord(position[:1])
y = int(position[1:])
coords = {}
if orientation.lower() == "y":
for i in range(0, length):
place = ''.join([chr(x), str(y)])
coords[place] = "-"
x = x + 1
elif orientation.lower() == "n":
for i in range(0, length):
place = ''.join([chr(x), str(y)])
coords[place] = "|"
y = y + 1
print("Coordinates of incoming ship: {}".format(list(coords.keys())))
names = []
for item in ships:
names.append(item)
# a.append(list(self.ships[item]["Coordinates"].keys()))
for var in names:
for item in coords.keys():
if item in list(ships[var]["Coordinates"].keys()) and ships[name] != ships[var]:
print("Coordinates of {}: {}".format(var, list(ships[var]["Coordinates"].keys())))
new_position = input("There is an overlap at {}. Please enter a different starting position: ".format(item)).replace(" ","")
new_orientation = input("Is it horizontal? (Y/N): ").replace(" ","")
make_ships(name, length, new_position, new_orientation)
ships[name]["Coordinates"] = coords
ships = {}
ships["Aircraft Carrier"] = {}
ships["Aircraft Carrier"] = {"length": 5, "Coordinates": {'a1':'|', 'a2':'|', 'a3':'|', 'a4':'|', 'a5':'|'}}
make_ships("Battleship", 4, 'a1', 'n')
原來電話中的戰列艦與4個地點的現有航母重疊。 程序會要求您輸入新的位置和方向。 如果你選擇,比如說,B1,該程序要求的重疊,並顯示載體,A1-A5的座標,這顯然做不重疊的船舶在B1-B4。
什麼樣的縮進需要修復? –
我修好了。您未能縮進該功能。 – Prune
「戰列艦」實際上是從「a1」開始的,這與「航母」相同。你可以檢查一下嗎?或者我可能會誤解某些東西。 – sal