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

0 投票
3 回答
13351 浏览
提问于 2025-04-16 20:37

我刚开始学习Python,正在写一个简单的石头剪子布游戏。这是我目前的代码。我可以和另一个玩家完美地进行游戏。我想知道的是如何记录胜利的分数,比如如果玩家1赢了第一局,应该显示:

玩家1 = 1
玩家2 = 0

接下来继续记录分数。此外,如果有任何建议可以让我代码更高效(并且解释得通俗易懂),那就太好了。

谢谢!

3 个回答

0

试试这个:

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
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)
3

考虑一下这个建议的伪代码(有些部分需要实现):

# 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”)——这里面可能会有一些小问题,欢迎体验。 :)

祝你编码愉快。

撰写回答