2016-04-10 4 views
1

Ich möchte dem Benutzer die Wahl geben, ob sie die Koordinaten zufällig haben wollen oder ob sie sie für ein Schlachtschiffspiel auf Python eingeben wollen. Es sollte beachtet werden, dass ich eine portable Anwendung von PyScripter verwende, um dies zu bauen.Wie kann ich dem Benutzer die Möglichkeit geben, auszuwählen, in welchem ​​Modus gespielt werden soll?

Randomized Version

from random import randint 

Battleship_Board =[] 


for x in range (0,5): 
Battleship_Board.append(["O"] * 5) 

def print_Battleship_Board(Battleship_Board): 
for row in Battleship_Board: 
    print (" ".join(row)) 

print ("Let's play Battleships!") 
print_Battleship_Board(Battleship_Board) 

def random_row(Battleship_Board): 
return randint(0, len(Battleship_Board) - 1) 

def random_col(Battleship_Board): 
return randint(0, len(Battleship_Board[0]) - 1) 

Battleship_Row = random_row(Battleship_Board) 
Battleship_Column = random_col(Battleship_Board) 

for turn in range(5): 
    Guess_Board_Row = int(input("Guesss the X value")) 
    Guess_Board_Column = int(input("Guess the Y value")) 

if Guess_Board_Row == Battleship_Row and Guess_Board_Column == Battleship_Column: 
    print("You sunk my battleship!") 
    break 
else: 

    if (Guess_Board_Row < 1 or Guess_Board_Row > 5) or (Guess_Board_Column < 1 or Guess_Board_Column > 5): 
      print("Apologies, that's not on the grid") 

    elif(Battleship_Board[Guess_Board_Row][Guess_Board_Column] == "X"): 
      print("You already guessed that value") 

    else: 
     print("You missed my battleship") 
     Battleship_Board[Guess_Board_Row][Guess_Board_Column] = "X" 

     print("Turn" + str(turn+1) + " out of 4.") 
     print_Battleship_Board(Battleship_Board) 

    if turn >=4: 
     print("Game Over") 

Wie Sie sehen, ich lasse der Benutzer das Spiel basiert auf einer randomisierten Wert für die Spalte und Zeile.

Eingegebene Version

Battleship_Board =[] 


for x in range (0,5): 
Battleship_Board.append(["O"] * 5) 



def print_Battleship_Board(Battleship_Board): 
for row in Battleship_Board: 
    print (" ".join(row)) 

print ("Let's play Battleships!") 
print_Battleship_Board(Battleship_Board) 

Battleship_Row = int(input("Please enter a X value")) 
Battleship_Column = int(input("Please enter a Y value")) 

if (Battleship_Row < 1 or Battleship_Row > 5) or (Battleship_Column < 1 or Battleship_Row > 5): 
print("Apologies, that's not on the grid") 

for turn in range(5): 
    Guess_Board_Row = int(input("Guess the X value")) 
    Guess_Board_Column = int(input("Guess the Y value")) 

if Guess_Board_Row == Battleship_Row and Guess_Board_Column == Battleship_Column: 
    print("You sunk my battleship!") 
    print("My Ship was here: [" + str(Battleship_Row) + "][" + str(Battleship_Column) + "]") 
    break 

else: 
    if turn == 5: 
     Battleship_Board[Battleship_Row][Battleship_Column] = "X" 
     print_Battleship_Board(Battleship_Board) 
     print("Game Over") 
     print("My Ship was here: [" + str(Battleship_Row) + "][" + str(Battleship_Column) + "]") 

    else: 
     if (Guess_Board_Row < 1 or Guess_Board_Row > 5) or (Guess_Board_Column < 1 or Guess_Board_Column > 5): 
      print("Apologies, that's not on the grid") 

     elif(Battleship_Board[Guess_Board_Row][Guess_Board_Column] == "X"): 
      print("You already guessed that value") 

     else: 
      print("You missed my battleship!") 
      Battleship_Board[Guess_Board_Row][Guess_Board_Column] = "X" 

     print("Turns taken out of 5:", turn + 1) 
     print_Battleship_Board(Battleship_Board) 

Hier ist derjenige, in dem der Benutzer die Werte zur Eingabe erhält.

Ich möchte eine Option zwischen diesen beiden Versionen für den Benutzer beim Start der Anwendung auswählen auswählen. Wie kann ich das machen?

+2

Eine 'if' Aussage? –

+0

Ich ging davon aus, aber ich war mir nicht sicher, wie ich die Gesamtheit des Codes gestalten sollte, wenn man bedenkt, dass ich diese beiden Versionen unterscheiden musste, damit Python es verstehen und ausführen konnte. –

+0

Was hast du bisher versucht? Irgendwelche Versuche an einem Menü Skript/Funktion/Klasse oder ähnliches? – jDo

Antwort

2

Sie können Befehlszeilenargumente verwenden, die dem Benutzer erlauben, den Modus zu wählen, während er das Spiel Brände:

import sys 

if len(sys.argv) > 1 and sys.argv[1] == '<some-value>': 
    # Randomize 
else: 
    # Prompt user for input 

Mehr über Kommandozeilenargumente here. Für nutzerfreundlichere Befehlszeilenoptionen (lesen Sie besser) lesen Sie argparse.

Verwandte Themen