繁体   English   中英

如何在石头剪刀布游戏中记分?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM