2017-03-20 3 views
0

Ich erstelle ein Python 2-Player Schlachtschiff-Spiel und alles ist fast fertig, abgesehen von ein paar kleinen Problemen. In der Phase, in der der Spieler alle Schiffe auf dem Board platziert - habe ich Probleme mit der Validierung, um nach doppelten Schiffen zu suchen. Hier ist mein Code für Schleife Schiff Platzierung:Python Validierungsüberprüfung Mein Loop

while True: 
     for ship_name, ship_size in Game.SHIP_INFO: 
      # create ship instance 
      ship1 = Ship(player1, ship_name, ship_size) 
      # ask user for starting coordinate for ship in form "A1" and split into x,y variables 
      x, y = ship1.split_coordinates(ship_name,player1.player) 
      # ask user for ship's position --horizontal or vertical 
      direction = ship1.ask_ship_location() 
      # create all coordinates for ship based on size of ship and location 
      created_coords = ship1.create_ship_coordinates(x, y, ship_size,direction) 
      # check to see if ship already on board 
      for coord in created_coords: 
       if any(coord in ship for ship in grid1.play_one_board): 
        print("Sorry you already have a ship in that location") 
        continue 
       else: 
        break 
      # add coordinates to player's grid 
      grid1.play_one_board.append(created_coords) 
      # loop through coords for ship to print out on displayed grid 
      grid1.print_ship_coordinates(created_coords,direction) 

es ist diese Validierung Teil hier, die ich habe gerade versucht zu implementieren, die Probleme verursacht.

for coord in created_coords: 
       if any(coord in ship for ship in grid1.play_one_board): 
        print("Sorry you already have a ship in that location") 
        continue 
       else: 
        break 

es richtig, wenn eine bestehende Identifizierung wird Koordinate allready platziert --Aber es weiter in der Schleife auf die nächsten beiden Schritte, auf der die Platine gedruckt und dann auf die nächste Schiff Platzierung auf bewegten, ohne erneut zu fragen für eine korrigierte Version der überlappenden Schiffsplatzierung. Nur müssen Sie herausfinden, wie die Schleife am Anfang zurückgehen kann, wenn ein Fehler in der Überlappung des Schiffs auftritt. Irgendwelche Ideen? Vielen Dank.

EDIT - Code wurde per Vorschlag geändert, aber keine Validierungsfehler.

while True: 
     for ship_name, ship_size in Game.SHIP_INFO: 
      # create ship instance 
      ship1 = Ship(player1, ship_name, ship_size) 
      ship_exists = True 
      while ship_exists: 
       # ask user for starting coordinate for ship in form "A1" and split into x,y variables 
       x, y = ship1.split_coordinates(ship_name,player1.player) 
       # ask user for ship's position --horizontal or vertical 
       direction = ship1.ask_ship_location() 
       # create all coordinates for ship based on size of ship and location 
       created_coords = ship1.create_ship_coordinates(x, y, ship_size,direction) 
       # check to see if ship already on board 
       for coord in created_coords: 
        ship_exists = any(coord in ship for ship in grid1.play_board) 
        if ship_exists: 
         print("sorry") 
        else: 
         break 
       # function to check for overlapped ships 
       # ship1.check_overlap(created_coords, grid1.play_one_board) 
       # add coordinates to player's grid 
      grid1.play_one_board.append(created_coords) 
      # loop through coords for ship to print out on displayed grid 
      grid1.print_ship_coordinates(created_coords, direction) 

Antwort

1

Ich glaube, Ihr Problem hier ist:

for coord in created_coords: 
    if any(coord in ship for ship in grid1.play_one_board): 
     print("Sorry you already have a ship in that location") 
     continue 
    else: 
     break 

Wenn ein Schiff in den bestehenden Standort gefunden wird, Sie wollen nach neuen Koordinaten zu stellen, um fortzufahren. In diesem Fall setzt Ihr continue eigentlich die innere Schleife fort, nicht die äußere Schleife.

Das bedeutet, dass Ihre Schleife alle Coords überprüft und bricht, wenn eine ohne vorhandenes Schiff gefunden wird, wodurch die nächsten zwei Schritte nach der for-Schleife ausgeführt werden. Ich würde einen Scheck Variable hinzufügen, anstatt nur weitermachen:

ship_exists = False 
for coord in created_coords: 
    if any(coord in ship for ship in grid1.play_one_board): 
     print("Sorry you already have a ship in that location") 
     ship_exists = True 
     break 
if ship_exists: 
    continue 

Diese gewährleisten, dass werden, wenn ein Schiff bereits vorhanden ist, ist der erste Schritt in der äußeren Schleife erneut ausgeführt.

=============

endgültige Antwort,

auf Kommentare basierend
def _are_valid_coordinates(created_coords, play_one_board): 
    for ship in play_one_board: 
     for coord in created_coords: 
      if created_coords in ship: 
       return False 
    return True 


while True: 
    for ship_name, ship_size in Game.SHIP_INFO: 
     # create ship instance 
     ship1 = Ship(player1, ship_name, ship_size) 

     valid_coords = False 
     # ask user for starting coordinate for ship in form "A1" and split into x,y variables 
     while not valid_coords: 
      x, y = ship1.split_coordinates(ship_name,player1.player) 
      # ask user for ship's position --horizontal or vertical 
      direction = ship1.ask_ship_location() 
      # create all coordinates for ship based on size of ship and location 
      created_coords = ship1.create_ship_coordinates(x, y, ship_size,direction) 
      # check to see if ship already on board 
      valid_coords = _are_valid_coordinates(created_coords, ship1.play_one_board) 
      if not valid_coords: 
       print("Sorry you already have a ship in that location") 
      else: 
       break 
    # add coordinates to player's grid 
    grid1.play_one_board.append(created_coords) 
    # loop through coords for ship to print out on displayed grid 
    grid1.print_ship_coordinates(created_coords,direction) 
+0

danke - das funktioniert perfekt wie auf das Problem der es zu lösen Drucken das fehlerhafte Gitter - aber es geht weiter zum neuen Schiff, anstatt den Spieler erneut zu bitten, die Koordinaten des Schiffs einzugeben - irgendeine Idee, wie das zu beheben ist? –

+0

@ JohnRogerson hinzugefügt, um zu antworten. Sehen Sie, ob das funktioniert - es geht darum, weiter zu fragen, bis eine gute Menge an Koordinaten zur Verfügung steht. Um das zu tun, anstatt zu überprüfen, ob die bereitgestellten Koordinaten gut sind, fragen Sie, als ob sie es nicht wären, und brechen Sie ab, sobald sie gut sind. AKA - gehe davon aus, dass sie schlecht sein werden und benutze 'while', bis sie gut sind. – Sagar

+0

hmm, das macht viel Sinn - ich habe den Code ausprobiert - ich habe OP bearbeitet, um den aktualisierten Code anzuzeigen - (und ich musste eine Schleife hinzufügen, um diese 'Koord'-Variable zu erstellen ... aber leider identifiziert jetzt nicht die überlappenden Schiffe. –

0

Wenn Sie die weiterhin tun, es ist einfach/weiter aus, dass "für coord in created_coords" innere Schleife.

Um die äußere Schleife fortzusetzen, könnten Sie dies anhand eines Flags tun. Etwas entlang der Linien von:

already_had_ship = False 
for coord in created_coords: 
    if any(coord in ship for ship in grid1.play_one_board): 
     already_had_ship = True 
     print("Sorry you already have a ship in that location") 
     break 

if already_had_ship: 
    continue