所以我正在为我的骰子游戏项目编写一个 dice_roll 函数,用于滚动 2 个骰子,目前我想以骰子格式包含相应的骰子结果。
虽然代码确实有效,但我想知道是否有更好的方法打印出骰子格式,而不是像这样将其放入数组中,这样输出就没有额外的括号,只显示骰子结果,如下所示:
[0 0]
[ 0 ]
[0 0]
我是编码的初学者,所以我不确定数组以外的任何其他方法。
这是我的代码:
def dice_roll(): # rolls 2 six-sided dice
dice1 = random.randint(1,6)
print("Dice 1: You rolled a", dice1, "\n"
if dice1 == 1:
dice_layer1 = np.array(["[ ]"])
dice_layer2 = np.array(["[ 0 ]"])
dice_layer3 = np.array(["[ ]"])
roll_animation = np.array([dice_layer1, dice_layer2, dice_layer3])
print(roll_animation, "\n")
elif dice1 == 2:
dice_layer1 = np.array(["[0 ]"])
dice_layer2 = np.array(["[ ]"])
dice_layer3 = np.array(["[ 0]"])
roll_animation = np.array([dice_layer1, dice_layer2, dice_layer3])
print(roll_animation, "\n")
elif dice1 == 3:
dice_layer1 = np.array(["[0 ]"])
dice_layer2 = np.array(["[ 0 ]"])
dice_layer3 = np.array(["[ 0]"])
roll_animation = np.array([dice_layer1, dice_layer2, dice_layer3])
print(roll_animation, "\n")
elif dice1 == 4:
dice_layer1 = np.array(["[0 0]"])
dice_layer2 = np.array(["[ ]"])
dice_layer3 = np.array(["[0 0]"])
roll_animation = np.array([dice_layer1, dice_layer2, dice_layer3])
print(roll_animation, "\n")
elif dice1 == 5:
dice_layer1 = np.array(["[0 0]"])
dice_layer2 = np.array(["[ 0 ]"])
dice_layer3 = np.array(["[0 0]"])
roll_animation = np.array([dice_layer1, dice_layer2, dice_layer3])
print(roll_animation, "\n")
elif dice1 == 6:
dice_layer1 = np.array(["[0 0]"])
dice_layer2 = np.array(["[0 0]"])
dice_layer3 = np.array(["[0 0]"])
roll_animation = np.array([dice_layer1, dice_layer2, dice_layer3])
print(roll_animation, "\n")
然后我重复这段代码,但不是 dice2
用
dice1
代替 dice2
重复你的代码违反了软件开发的原则——不要重复你自己!
相反,创建一个函数来打印给定值的骰子。您可以定义一个包含 strings 的列表来描述骰子的所有面:
die_faces = ["", # index = 0 doesn't have a face
"[ ]\n[ 0 ]\n[ ]", # index 1
"[0 ]\n[ ]\n[ 0]", # index 2
"[0 ]\n[ 0 ]\n[ 0]", # index 3
"[0 0]\n[ ]\n[0 0]", # index 4
"[0 0]\n[ 0 ]\n[0 0]", # index 5
"[0 0]\n[0 0]\n[0 0]", # index 6
]
def print_die(value: int):
print(die_faces[value])
然后,在你的函数中你可以这样做:
def dice_roll():
die1 = random.randint(1,6)
print(f"Die 1: You rolled a {die1}")
print_die(die1)
die2 = random.randint(1,6)
print(f"Die 2: You rolled a {die2}")
print_die(die2)
打印,例如:
Die 1: You rolled a 2
[0 ]
[ ]
[ 0]
Die 2: You rolled a 3
[0 ]
[ 0 ]
[ 0]
既然您提到要并排打印多个骰子,请认识到您需要先打印每个骰子的第一行,然后是第二行,然后是第三行。您可以通过拆分每张脸的线条,然后迭代结果列表的
zip
来轻松做到这一点。
def print_dice(values: list[int]):
faces = [die_faces[v].splitlines() for v in values]
for line in zip(*faces):
print(*line)
现在,你可以这样调用这个函数:
print_dice([1, 2, 3])
将打印:
[ ] [0 ] [0 ]
[ 0 ] [ ] [ 0 ]
[ ] [ 0] [ 0]
在你的函数中实现这个,
def dice_roll(num_dice=2): # rolls `num_dice` six-sided dice
all_dice = [random.randint(1,6) for _ in range(num_dice)]
print(f"You rolled ", all_dice)
print_dice(all_dice)
并调用它,例如
dice_roll(3)
You rolled [2, 5, 2]
[0 ] [0 0] [0 ]
[ ] [ 0 ] [ ]
[ 0] [0 0] [ 0]
我会使用字典来存储所有点的位置,这样您就不必一直重复代码:
dice_dict = {
1: [4],
2: [0, 8],
3: [0, 4, 8],
4: [0, 2, 6, 8],
5: [0, 2, 4, 6, 8],
6: [0, 2, 3, 5, 6, 8]
}
for roll, dots in dice_dict.items():
for i in range(0, 9, 3):
print(f'[{" ".join(["0" if j in dots else " " for j in range(i, i+3)])}]')
print()
本示例将按顺序打印所有骰子。
正如其他人所提到的,视觉表示可以记录在一个字符串中。
您的代码的简化版本是将每个骰子字符串放入列表中,然后使用您的随机 int 获取相应骰子的索引:
def roll_dice():
sides = ["[ ]\n[ 0 ]\n[ ]", "[0 ]\n[ ]\n[ 0]", "[0 ]\n[ 0 ]\n[ 0]", "[0 0]\n[ ]\n[0 0]", "[0 0]\n[ 0 ]\n[0 0]", "[0 0]\n[0 0]\n[0 0]"]
rolled_number = random.randint(1,6)
return rolled_number, sides[rolled_number - 1]
def play_game():
roll1, dice1 = roll_dice()
roll2, dice2 = roll_dice()
print(f"Dice 1: You rolled a {roll1}!\n{dice1}\n\nDice 2: You rolled a {roll2}!\n{dice2}")
play_game()
输出:
Dice 1: You rolled a 3!
[0 ]
[ 0 ]
[ 0]
Dice 2: You rolled a 2!
[0 ]
[ ]
[ 0]
有趣的问题。就像其他人所说的那样,只需格式化一个字符串即可。有硬编码骰子口味的方法,但我采用了不同的方法,因为它很容易扩展:
empty_dice = "|{} {}|\n\
|{} {} {}|\n\
|{} {}|\n"
def animate_roll(rolled_value: int) -> str:
is_filled = []
if rolled_value % 2: # Check if even
is_filled.append(3)
rolled_value -= 1
for do_fill in [(0, 6), (1, 5), (2, 4)]:
if rolled_value:
is_filled.extend(do_fill)
rolled_value -=2
return empty_dice.format(*['•' if i in is_filled else ' ' for i in range(empty_dice.count('{}')])
# Test
print(animate_roll(random.randint(1,6)))