![](/img/trans.png)
[英]What can I do to fix my Rock, Paper, Scissors code to keep score and to end the game
[英]How do I keep score in a rock paper scissors game?
我剛剛開始 Python 並編寫一個簡單的石頭剪刀布游戲。 這是我到目前為止所擁有的。 我可以完美地和另一個玩家一起玩游戲。 我想知道的是如何保持勝利的得分,所以如果玩家 1 贏得了第一場比賽,它會說:
玩家 1 = 1 玩家 2 = 0
等等。 此外,如果有任何提示可以使我的代碼更高效(有一個很好的解釋),那就太好了。
謝謝!
考慮這個用於建議的偽代碼(某些部分需要實現):
# a Player class. it should have a Name and a Score
class Player():
def __init__(name):
self.name = name
self.score = 0
# displays a prompt and reads in the players name returning a string
def getPlayerName():
# needs code here, see next function for idea of what
pass
# ask the player to make a choice, takes a Player object
# and returns a string
def getPlayerAttack(player):
print "%s, what do you choose?" % player.name
return raw_input("> ")
# determines who wins and updates score accordingly
# takes in the player objects and their attack choices
def attack(player1, choice1, player2, choice2):
if choice1 == choice2:
print "Its's a tie."
elif choice1 == "1" and choice2 == "2":
print "%s wins." % player2
player2.score = player2.score + 1
elif ... # other attacks
# display the scores of the two players
def displayScores(player1, player2):
print "%s vs %s" % (player1.score, player2.score)
player1 = Player(getPlayerName())
player2 = Player(getPlayerName())
while true:
choice1 = getPlayerAttack(player1)
choice2 = getPlayerAttack(player2)
attack(player1, choice1, player2, choice2)
displayScores(player1, player2)
這將需要一些工作,而且它不是超級優化的,但它應該是一個開始並且應該顯示更多的概念。 使用 Ctrl-C 停止或添加停止條件(例如,任一玩家輸入“0”)- 免費包含錯誤。 :)
快樂編碼。
好吧,有幾種方法可以提高效率,但是如果您必須一次輸入一個,我不得不問您將如何玩石頭剪刀布:-P
得分可以歸結為:
if choice1 == choice2 :
print "Its's a tie."
elif choice1 - choice2 == 1 or choice2 - choice1 == 2 :
print "%s wins." % player1
else:
print "%s wins." % player2
如果你想做多個游戲,只需將整個內容放在一個while(1):
循環中,並為每個分數添加一個變量。 然后它會變成:
score1 = 0
score2 = 0
while(1):
<lines 6-18>
if choice1 == choice2 :
print "Its's a tie."
elif choice1 - choice2 == 1 or choice2 - choice1 == 2 :
print "%s wins." % player1
score1 = score1 + 1
else:
print "%s wins." % player2
score2 = score2 + 1
print "%s: %d points. %s: %d points." % (player1, score1, player2, score2)
嘗試這個:
player1 = raw_input("Player 1 name: ")
player2 = raw_input("Player 2 name: ")
while(1)
player1score = 0
player2score = 0
print "%s, what do you choose?" % player1
print "1. Rock"
print "2. Paper"
print "3. Scissors"
choice1 = raw_input("> ")
print "%s, what do you choose?" % player2
print "1. Rock"
print "2. Paper"
print "3. Scissors"
choice2 = raw_input("> ")
if choice1 == "1" and choice2 == "1":
print "Its's a tie."
if choice1 == "1" and choice2 == "2":
print "%s wins." % player2
player2score=player2score+1
if choice1 == "2" and choice2 == "1":
print "Player 1 wins." % player1
player1score=player1score+1
if choice1 == "1" and choice2 == "3":
print "Player 1 wins." % player1
player1score=player1score+1
if choice1 == "3" and choice2 == "1":
print "%s2 wins." % player2
player2score=player2score+1
if choice1 == "2" and choice2 == "3":
print "%s wins." % player2
player2score=player2score+1
if choice1 == "3" and choice2 == "2":
print "Player 1 wins." % player1
player1score=player1score+1
print "Player1: %s" % player1score
print "Player2: %s" % player2score
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.