作为一个初学者,我开始编写2048游戏。我做了一个矩阵,用0填充,然后我想写一个函数,在整个矩阵中循环,找到所有的0值。然后保存0值的坐标,然后用2或4值替换它们。我做了一个随机变量来选择甜菜糖2号或4号。问题是,我真的不知道如何将0值的x和y坐标推送到和数组中,然后读取它们。你知道吗
table = [[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]]
options = []
def saveOptions(i, j):
options.append([{i , j}])
return options
def optionsReaderandAdder(options):
if len(options) > 0:
spot = random.random(options)
r = random.randint(0, 1)
if r > 0.5:
table <-------- THIS IS THE LINE WHERE I WOULD LIKE TO CHANGE THE VALUE OF THE 0 TO 2 OR 4.
def optionsFinder():
for i in range(4):
for j in range(4):
if table[i][j] == 0:
saveOptions(i, j)
optionsReaderandAdder(options)
addNumber()
print('\n'.join([''.join(['{:4}'.format(item) for item in row])
for row in table]))
您可以遍历表的行和列:
我们不知道坐标是什么。你知道吗
Python有一个名为^{} 的有用函数。我们可以用与前面相同的方法进行迭代,但是我们也可以得到索引,或者数组中的位置。你知道吗
然后我们可以设置值,例如all为
2
:你的随机代码看起来不错。你知道吗
相关问题 更多 >
编程相关推荐