如何根据滚动骰子的分数对名称进行排序

时间:2013-02-25 19:57:04

标签: python

我正在开发一个程序,它生成三个不同的整数并将它们分配给各个值,以便可以决定是第一个,第二个和第三个。在这种情况下,我假设有三个不同的玩家,每个玩家都会掷出一个“十边模具”。最高的应该是第一个转弯,第二个应该是第二个,第三个应该是最后一个。一切似乎都滚动好了,但现在我有我的价值观,我无法弄清楚如何安排它们,以便我可以开始让玩家轮流玩。我很感激任何意见。

以下是我到目前为止编写的代码:

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 :(得分: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))

答案 1 :(得分: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]

答案 2 :(得分: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()