冻结lis中的特定项目

2024-06-28 21:01:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我不知道怎样才能使一些(不是所有)项目冻结在一个正常的名单。我正在用python生成一个二维列表中的二进制拼图。但是我不知道怎样才能使生成的部分不变,所以当使用者玩游戏时,他不能改变拼图中生成的部分

抱歉用荷兰语编码。我是个学生

我不知道该怎么做。我在考虑做一个字典的位置,让程序检查每一次的suer改变瓷砖在游戏中。但我不认为这会有效

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]

我想使这个随机生成的董事会不变,但“.”可变。但是如果用户犯了错误,它们应该可以再次更改


Tags: andinforrangelocationrandomendlimit