1

我正在开发一个程序,该程序生成三个不同的整数并将它们分配给单独的值,以便可以决定它是第一个、第二个和第三个。在这种情况下,我假设三个不同的玩家,每个人掷一个“十面骰子”。最高的应该第一回合,第二高的应该第二,第三应该最后。一切似乎都很好,但现在我有了自己的价值观,我不知道如何安排它们,以便我可以开始让玩家轮流玩。我会很感激任何意见。

这是我到目前为止整理的代码:

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()
4

3 回答 3

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))
于 2013-02-25T20:53:13.660 回答
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]
于 2013-02-25T20:00:39.627 回答
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()
于 2013-02-25T20:09:40.083 回答