我不知道如何将一些(不是全部)物品冻结在正常列表中。我在python的二维列表中生成了一个binairy拼图。但是我不知道如何使生成的内容不可变,因此,在申服者玩游戏时,他无法更改拼图中生成的内容。
很抱歉荷兰语编码。我是学生
我不知道该怎么办。我当时想制作一个带有位置的字典,并在每次用户更换游戏中的瓷砖时让程序检查。但我认为那不会有效。
import random
import string
alfabet = list(string.ascii_uppercase)
def limit(number, mini, maxi):
return max(min(maxi, number), mini)
def game(grootte):
# maak speelveld
speelveld = []
for i in range(0, grootte):
rij = []
for x in range(0, grootte):
rij.append('.')
speelveld.append(rij)
# vul speelveld
for i in range(0, grootte+4):
errorcheck = False
while errorcheck == False:
r_binairy = random.randint(0, 1)
r_location_a = random.randint(0, grootte-1)
r_location_b = random.randint(0, grootte-1)
errorcheck = speelveld[limit(r_location_a+1, 0, grootte-1)][limit(r_location_b, 0, grootte-1)] and speelveld[limit(r_location_a+2, 0, grootte-1)][limit(r_location_b, 0, grootte-1)] != r_binairy and \
speelveld[limit(r_location_a-1, 0, grootte-1)][limit(r_location_b, 0, grootte-1)] and speelveld[limit(r_location_a-2, 0, grootte-1)][limit(r_location_b, 0, grootte-1)] != r_binairy and \
speelveld[limit(r_location_a+1, 0, grootte-1)][limit(r_location_b, 0, grootte-1)] and speelveld[limit(r_location_a-1, 0, grootte-1)][limit(r_location_b, 0, grootte-1)] != r_binairy and \
speelveld[limit(r_location_a, 0, grootte-1)][limit(r_location_b+1, 0, grootte-1)] and speelveld[limit(r_location_a, 0, grootte-1)][limit(r_location_b+2, 0, grootte-1)] != r_binairy and \
speelveld[limit(r_location_a, 0, grootte-1)][limit(r_location_b-1, 0, grootte-1)] and speelveld[limit(r_location_a, 0, grootte-1)][limit(r_location_b-2, 0, grootte-1)] != r_binairy and \
speelveld[limit(r_location_a, 0, grootte-1)][limit(r_location_b-1, 0, grootte-1)] and speelveld[limit(r_location_a, 0, grootte-1)][limit(r_location_b+1, 0, grootte-1)] != r_binairy
speelveld[r_location_a][r_location_b] = r_binairy
# print speelveld
def print_speelveld():
print("\n")
teller = 1
print(' A', end="")
for i in range(0, grootte-1):
print(' ', format(alfabet[teller], '>6'), end="")
teller += 1
print("\n")
teller = 1
for i in speelveld:
print(format(teller, ' <4'), "|", end="")
for x in i:
print('', format(x , '^5'), '|', end="")
print('\n')
teller += 1
# speel spel
bord_niet_vol = True
while bord_niet_vol == True:
print_speelveld()
for i in speelveld:
for x in i:
if x == '.':
bord_niet_vol = True
else:
bord_niet_vol = False
vak = input('\ngeef het volgende in: RIJ KOLOM GETAL. Als voorbeeld "A10" (Kolom A, Rij 1, Getal 0). Gebruik HOOFDLETTERS\n\ninvoer: ')
invoer = list(vak)
invoer[0] = alfabet.index(invoer[0])
invoer[1] = int(invoer[1])-1
invoer[2] = int(invoer[2])
speelveld[invoer[1]][invoer[0]] = invoer[2]
我想使此随机生成的板不可变,但“。”多变。但是如果用户犯了错误,它们应该可以第二次更改。
答案 0 :(得分:0)
我将建立一个'index:givenvalues'
字典,其中value是所有可能值的列表(假定不是无限!),但在len(givenvalues) == 1
处插入一个中断以防止更新。