如何根据掷骰子的得分对姓名进行排序

1 投票
3 回答
614 浏览
提问于 2025-04-17 17:05

我正在做一个程序,这个程序会生成三个不同的整数,并把它们分别分配给不同的值,以决定谁先、谁第二、谁最后。在这个例子中,我假设有三个不同的玩家,每个人都掷一个“十面骰子”。掷出点数最高的玩家先行动,第二高的玩家第二行动,第三高的玩家最后行动。现在一切看起来都还不错,但我已经得到了这些值,却不知道怎么把它们排列好,以便开始让玩家轮流进行游戏。我很感激任何建议。

这是我目前写的代码:

import sys
import os
import random
import time

os.system('clear')
print ('Welcome!  Please type Player 1\'s name!: ')
playerOne = input()
print ('Okay!  Please type Player 2\'s name!: ')
playerTwo = input()
print ('Fantastic!  Finally, please type Player 3\'s name!: ')
playerThree = input()
os.system('clear')
print()
time.sleep(2)

def startFightRoll():
    playerOneRoll = random.randint(1,10)
    time.sleep(.5)
    print('Okay, let\'s roll a ten-sided die to see who gets to go first!')
    print()
    time.sleep(2)
    print ('<Ten-sided dice roll> ')
    print ('---------------------------------')
    print (playerOne + ' rolls ' + str(playerOneRoll))
    print()
    print()
    playerTwoRoll = random.randint(1,10)
    time.sleep(2)
    print ('<Ten-sided dice roll> ')
    print ('---------------------------------')
    print (playerTwo + ' rolls ' + str(playerTwoRoll))
    print()
    print()
    playerThreeRoll = random.randint(1,10)
    time.sleep(2)
    print ('<Ten-sided dice roll> ')
    print ('---------------------------------')
    print (playerThree + ' rolls ' + str(playerThreeRoll))

startFightRoll()

关于下面的讨论;

好吧,我是新手,所以请原谅我的代码——它不是很高效,我还在适应中。我添加了一些部分来处理1)玩家在名字字段中什么都不输入,和2)掷骰子时出现平局的情况。我还创建了一个按降序排列的骰子点数列表,但现在我需要找到一种方法,把这些点数和产生它们的用户关联起来。任何关于如何正确做到这一点的建议都非常感谢;

import sys
import os
import random
import time

os.system('clear')


def playerOneName():
    global playerOne
    playerOne = input()
    if len(playerOne) < 1:
        print('Please enter your name, Player 1!')
        playerOneName()

def playerTwoName():
    global playerTwo
    playerTwo = input()
    if len(playerTwo) < 1:
        print('Please enter your name, Player 2!')
        playerTwoName()

def playerThreeName():
    global playerThree
    playerThree = input()
    if len(playerThree) < 1:
        print('Please enter your name, Player 3!')
        playerThreeName()

os.system('clear')
print()
time.sleep(2)
def startFightRoll():
    global playerOneRoll
    global playerTwoRoll
    global playerThreeRoll
    playerOneRoll = random.randint(1,10)
    time.sleep(.5)
    print('Okay, let\'s roll a ten-sided die to see who gets to go first!')
    print()
    time.sleep(2)
    print ('<Ten-sided dice roll> ')
    print ('---------------------------------')
    print (playerOne + ' rolls ' + str(playerOneRoll))
    print()
    print()
    playerTwoRoll = random.randint(1,10)
    time.sleep(2)
    print ('<Ten-sided dice roll> ')
    print ('---------------------------------')
    print (playerTwo + ' rolls ' + str(playerTwoRoll))
    print()
    print()
    playerThreeRoll = random.randint(1,10)
    time.sleep(2)
    print ('<Ten-sided dice roll> ')
    print ('---------------------------------')
    print (playerThree + ' rolls ' + str(playerThreeRoll))
    if playerOneRoll == playerTwoRoll:
        print ('There\'s a tie, rolling again!')
        time.sleep(3)
        os.system('clear')
        startFightRoll()
    if playerOneRoll == playerThreeRoll:
        print ('There\'s a tie, rolling again!')
        time.sleep(3)
        os.system('clear')
        startFightRoll()
    if playerTwoRoll == playerThreeRoll:
        print ('There\'s a tie, rolling again!')
        os.system('clear')
        time.sleep(3)
        startFightRoll()    

    O = [playerOneRoll, playerTwoRoll, playerThreeRoll]
    O = sorted(O, reverse = True)
    print (O)

print ('Welcome!  Please type Player 1\'s name!: ')
playerOneName()
print ('Okay!  Please type Player 2\'s name!: ')
playerTwoName()
print ('Fantastic!  Finally, please type Player 3\'s name!: ')
playerThreeName()
os.system('clear')
startFightRoll()

3 个回答

0

把你的值放到一个列表里(假设这个列表叫做 x):

x = sorted(x)

你想使用一个字典。

把每个玩家的名字和他们的随机点数放到字典里:

players = {'player1': randint(1, 10), 'player2': randint(1, 10), 'player3': randint(1, 10)}

不过,出于你的需求,你想要这个字典的反向(先是点数再是玩家名字),因为你想要遍历这些点数。如果有相同的点数,这样会出问题,所以你可以设置一个更大的范围(比如 randint(1, 1000)):

players = {randint(1, 1000): 'player1', randint(1, 1000): 'player2', randint(1, 1000): 'player3'}
rolls = sorted(players.keys())
player_sequence = [players[n] for n in rolls]
0

这里有一个可能的替代方法,就是创建一个字典并对它的键进行排序(我承认这有点奇怪)。注意到有三个“重复”的代码块,所以请进一步改进,按照Lattyware的建议,遍历每个玩家。

import sys
import os
import random
import time

os.system('clear')
print ('Welcome!  Please type Player 1\'s name!: ')
playerOne = raw_input()
print ('Okay!  Please type Player 2\'s name!: ')
playerTwo = raw_input()
print ('Fantastic!  Finally, please type Player 3\'s name!: ')
playerThree = raw_input()
os.system('clear')
print()
time.sleep(2)
def startFightRoll():
    time.sleep(.5)
    print('Okay, let\'s roll a ten-sided die to see who gets to go first!')
    print

    rolls = {}

    playerOneRoll = random.randint(1,10)
    rolls[playerOneRoll] = 'Player One'
    time.sleep(1)
    print ('<Ten-sided dice roll> ')
    print ('---------------------------------')
    print (playerOne + ' rolls ' + str(playerOneRoll))
    print
    print

    playerTwoRoll = random.randint(1,10)
    rolls[playerTwoRoll] = 'Player Two'
    time.sleep(1)
    print ('<Ten-sided dice roll> ')
    print ('---------------------------------')
    print (playerTwo + ' rolls ' + str(playerTwoRoll))
    print
    print

    playerThreeRoll = random.randint(1,10)
    rolls[playerThreeRoll] = "Player Three"
    time.sleep(1)
    print ('<Ten-sided dice roll> ')
    print ('---------------------------------')
    print (playerThree + ' rolls ' + str(playerThreeRoll))
    print
    print

    print "The winner is %s" % rolls[max(rolls.keys())]


startFightRoll()
2

就像Lattyware提到的,你的代码中有一些部分是重复的。这不仅让代码看起来很乱,还可能导致逻辑出错(比如你复制粘贴后忘记修改某个变量)。所以,把重复的代码放到一个函数里是个好习惯。

关于你的代码,我考虑到了如果两个玩家掷出了相同的数字。在这种情况下,程序会继续掷骰子,直到掷出一个新的数字。

import os
import random
import time

os.system('clear')
print ('Welcome!  Please type Player 1\'s name!: ')
playerOne = input()
print ('Okay!  Please type Player 2\'s name!: ')
playerTwo = input()
print ('Fantastic!  Finally, please type Player 3\'s name!: ')
playerThree = input()
os.system('clear')
print()
time.sleep(2)


def initialRoll(player):
    """Roll the dice for the given player"""

    playerRoll = random.randint(1, 10)
    print ('<Ten-sided dice roll> ')
    print ('---------------------------------')
    print (player + ' rolls ' + str(playerRoll))
    print()
    return playerRoll


def startFightRoll():
    """Determine the order of the players."""

    time.sleep(.5)
    print('Okay, let\'s roll a ten-sided die to see who gets to go first!')
    print()

    // Temporarily store the rolls. The key is the roll number and the value is the
    // player who rolled it
    order = {}
    for player in [playerOne, playerTwo, playerThree]:
        playerRoll = initialRoll(player)

        # Let's make sure that two players didn't roll the same number, if they did
        # then let's roll a new number
        while playerRoll in order:
            print ('OH No! That number has already been rolled. Let\'s roll again')
            playerRoll = initialRoll(player)

        order[playerRoll] = player
        time.sleep(2)

    # Sort the keys (which are the numbers rolled), then create a new list with order
    # of who should go first
    return [order[roll] for roll in sorted(order.keys(), reverse=True)]

rollOrder = startFightRoll()
print ('The order of the players are: ' + ', '.join(rollOrder))

撰写回答