2016-11-15 21 views
0

我正在製作一艘戰艦遊戲。我不知道爲什麼,當你在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

+0

什麼樣的縮進需要修復? –

+0

我修好了。您未能縮進該功能。 – Prune

+0

「戰列艦」實際上是從「a1」開始的,這與「航母」相同。你可以檢查一下嗎?或者我可能會誤解某些東西。 – sal

回答

2

問題

你的錯誤是使用遞歸調用make_ships,而不是一個簡單的迭代(while循環)。當第一次輸入失敗並在空間a4上匹配時,您將得到新船的信息並很好地添加。但是,您然後從遞歸調用返回到第一個調用,您仍然處於檢查循環中。這將迭代到a列檢查中的下一個空間,發現你的原始電話也匹配(如果你固執的話,你會得到這四次,每次來自原始戰列艦的碰撞一次) 。

REPAIR

的清潔方法是使用「讀,直到好」,而循環替換此過程。造成這種情況的僞代碼:

獲取第一輸入 而輸入是不可接受的 GET更換輸入

便宜的方法是簡單地把遞歸調用後回報。我的代碼包括我的調試工具,如下所示。當我測試這個時,我把替換的戰列艦放在方形q7 :-),這明顯表明這個問題與船隻接觸無關。

def make_ships(name, length, position, orientation): 
    print ("===> ENTER 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()))) 

    # Validating for ship overlap 
    names = [] 
    for item in ships: 
     names.append(item) 
     # a.append(list(self.ships[item]["Coordinates"].keys())) 
    for var in names: 
     # print ("coords.keys=", coords.keys()) 
     for item in coords.keys(): 
      print ("\ncoords.keys=", coords.keys()) 
      print ("var=", var, "\tname=", name, "\titem=", item) 
      print (ships[var]["Coordinates"].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) 
       return 

    ships[name]["Coordinates"] = coords 

    print ("===> EXIT make_ships", coords) 


ships = {} 
ships["Aircraft Carrier"] = {} 
ships["Aircraft Carrier"] = {"length": 5, "Coordinates": {'a1':'|', 'a2':'|', 'a3':'|', 'a4':'|', 'a5':'|'}} 

make_ships("Battleship", 4, 'a1', 'n') 
+0

好的,在我遇到這個問題之前,我研究了一下,我的程序的其餘部分已經從遞歸切換到while循環,並且離開這個循環似乎更容易。但顯然這是壞習慣。 –

+1

是的,這是一個壞習慣 - 更多的是因爲它讓你的思維不太敏銳以區分遞歸和迭代過程。以這種方式進行編程對於學習遞歸是可以的,但從長遠來看,繼續它已經讓我的幾個學生絆倒了。 – Prune

相關問題