2017-06-27 1 views
0

Kann mir jemand helfen, wie man dieses Programm ändert, damit 2 Spieler es spielen können? eine Prozedur definieren die Würfel werfenWie würde ich dieses Programm zwei Spieler in Python machen?

def dice(sides): 
# Get a random number between 1 and 6 
    throw=random.randint(1,6) 
    print("score",throw) 
    return throw 
#Welcome messages 
print("Snakes and ladders") 
#Main program for the game 
Player1=input("What is the name of player 1? ") 
import random 
position=0 
#eeping it all in a loop will allow the players to keep rolling the dice until the game has finished 
while position<49: 
    throw=input("Roll the dice") 
    dice1=dice(6) 
    dice2=dice(6) 
    total=dice1+dice2 
#If the total of the dice thrown is not a double the player will move the total numbers of squares 
    if dice1!=dice2: 
     print("You are now on space", position+total) 
     position=total+position 
    if position>=49: 
     print("You have won") 
     if position<=0: 
      print("You are on square 0") 
#Now I will check to see if the player has rolled a double 
    if dice1==dice2: 
     print("This is a double you will now move back",total,"spaces") 
     if position<=0: 
       print("You are on space 0") 
     else: 
       print("You are now on space", position-total) 
#This means that if the vaule of dice1 is the same as dice2 then it will move back and if not it will move forward 
+0

Willkommen bei Stack Overflow! Bitte nimm die [Tour] (https://stackoverflow.com/tour), schau dich um und lies die [Hilfe] (https://stackoverflow.com/help), insbesondere [Wie frage ich? eine gute Frage?] (https://stackoverflow.com/help/how-to-ask) und [Welche Themen kann ich hier fragen?] (https://stackoverflow.com/help/on-topic). Von diesem zweiten Link aus: "Fragen, die nach Hausaufgabenhilfe fragen, müssen eine Zusammenfassung der bisherigen Arbeit enthalten, um das Problem zu lösen, und eine Beschreibung der Schwierigkeit, die Sie bei der Lösung des Problems haben." –

Antwort

0

Sie könnten eine Menge lernen, indem sie die Umsetzung dieser Code mithilfe von Klassen zu simulieren.

Soweit es den Code so ähnlich wie möglich zu dem, den Sie verwenden, gibt es viele Möglichkeiten, wie Sie einen zweiten Player hinzufügen können.

Sie könnten zum Beispiel fügen Sie eine weitere Variable position_p2 Spieler 2 die Position zu speichern, und wiederholen Sie die gleiche überprüfen Sie für den Spieler ein auf player2 tun, wie folgt:

def dice(sides): # NOTE function names should start with verbs, e.g. roll_dice 
# Get a random number between 1 and 6 
    throw=random.randint(1,6) 
    print("score",throw) 
    return throw 
#Welcome messages 
print("Snakes and ladders") 
#Main program for the game 
Player1=input("What is the name of player 1? ") #NOTE variables should be lower case 

# Ask player2 for it's name 
player2 = input("What is the name of player 2? ") 
import random #NOTE imports should go at the beggining of the script 
position_p1=0 # rename so that is easy to remember that it refers to p1 
# store p2 position 
positoin_p2 = 0 
#eeping it all in a loop will allow the players to keep rolling the dice until the game has finished 
while position<49: 
    throw_p1=input("Player 1: roll the dice") #same as above 
    dice1=dice(6) 
    dice2=dice(6) 
    total=dice1+dice2 
#If the total of the dice thrown is not a double the player will move the total numbers of squares 
    if dice1!=dice2: 
     position_p1=total+position_p1 # you can use position_p1 += total 
     print("You are now on space", position_p1) 

    if position_p1>=49: 
     print("Player 1 won") # Change so that you know which player has won 
     if position_p1<=0: 
      print("You are on square 0") 
#Now I will check to see if the player has rolled a double 
    if dice1==dice2: 
     print("This is a double you will now move back",total,"spaces") 
    position_p1 -= total 
     if position_p1<=0: 
       print("You are on space 0") 
     else: 
       print("You are now on space", position_p1) 
#This means that if the vaule of dice1 is the same as dice2 then it will move back and if not it will move forward 

    throw_p2=input("Player 2: roll the dice") 
    dice1=dice(6) 
    dice2=dice(6) 
    total=dice1+dice2 
#If the total of the dice thrown is not a double the player will move the total numbers of squares 
    if dice1!=dice2: 
     position_p2=total+position_p2 # you can use position_p1 += total 
     print("You are now on space", position_p2) 

    if position_p2>=49: 
     print("Player 2 won") # Change so that you nkow which player has won 
     if position_p2<=0: 
      print("You are on square 0") 
#Now I will check to see if the player has rolled a double 
    if dice1==dice2: 
     print("This is a double you will now move back",total,"spaces") 
    position_p2 -= total 
     if position_p2<=0: 
       print("You are on space 0") 
     else: 
       print("You are now on space", position_p2) 
#This means that if the vaule of dice1 is the same as dice2 then it will move back and if not it will move forward 

Dies ist nicht der beste Weg, dies zu tun , aber es sollte funktionieren und es ist ähnlich dem Code, den Sie gepostet haben. Wenn Sie den Code verbessern möchten, können Sie versuchen, die Teile, die für die 2 Spieler identisch sind, in eine Funktion zu legen. Sie können auch Spielerpositionen in einem Wörterbuch speichern, sodass Sie das Spiel auf n Spieler erweitern können. Wenn Sie üben möchten, können Sie für jede Sache in Ihrem Spiel eine Klasse erstellen (das Spiel selbst, den Spieler usw.). Es gibt eine Menge anderer Dinge, die Sie tun können, aber beginnen Sie mit einigen einfachen Verallgemeinerungen, wie zum Beispiel für n Spieler.

Verwandte Themen