如何根据用户输入在空板底部随机放置单个“0”?

问题描述 投票:0回答:1

这是 Connect4 游戏的一部分,其中程序在游戏开始时询问玩家被阻挡单元的大小。被阻挡的单元将不允许在整个游戏过程中放置 Connect4 圆盘。然而,我正在努力随机放置 1x1 阻塞单元(它只能放置在板的底部)。我还希望这个受阻的单元更新

empty_board
(因此,我可以选择打印这个新板,其中受阻的单元永久存在)

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

# prints an empty board
rows, cols = (6, 7)
arr = [['_' for i in range(cols)] for j in range(rows)]

for row in arr:
    print(row)

# asks player for size of desired obstructed cells
for i in range(2):
    while True:
        try:
            width = int(input("state the width of the obstructed cells you wish to have: "))
            height = int(input("state the height of the obstructed cells you wish to have: "))
            if width == 1 and height == 1:
                for row in arr[0]:
                    print(random.seed('0'))
                for row in arr:
                    print(row)
        except Exception:
            print("the inputs must be an integer; less than width 7 and length 8")

我尝试使用随机函数来让它工作。但我不知道为什么它不起作用。

python-3.x random
1个回答
0
投票

要从 random 包中获取随机 int,请使用

random.randint(min, max)
。例如。
random.randint(0,6)
会给你一个 0 到 6 之间的整数 - docs。使用从此 (n) 获得的值作为列 ID 并设置
arr[0][n] = 0

此外,正如其他人提到的,如果您不希望结果可重复,请勿将 random.seed 设置为固定值。强烈建议阅读文档以获取更多信息。

© www.soinside.com 2019 - 2024. All rights reserved.