from random import random # importing a randomizer for different shapes each time#
import tkinter as tk
inpt = open("desktop/input.txt", "r")
glider = open("desktop/glider.txt", "r")
import sys
if sys.version_info.major == 3 and sys.version_info.minor >= 9:
listtype = list
else:
from typing import List
listtype = List
from dataclasses import dataclass
@dataclass # using dataclass decorator to make this class possible
class pixel: # class pixel has value of 1 or 0 and status is its life status#
value : int
status : bool = False
tabletype = listtype[listtype[pixel]]
city = list() # city is where our pixels will be living#
for i in range(9): # our city will be 81cm^2#
city.append(list())
for j in range(9):
# using modulus operator gives us a gui filled with 1s and 0s#
city[-1].append(pixel((i+j) % 2))
#using the rpop(random population) f we get %30 of the pixels will be dead#
def rpop(table: tabletype):
for i in range(len(table)):
for j in range(len(table[0])):
x = random()
city[i][j].status = False
if x < 0.3:
city[i][j].value = 0
else:
city[i][j].value = 1
alive = 0
def neighbours(city: list[list[pixel]], row: int, col: int): #func for alive neighbors#
alive = 0
for i in range(row-1, row+2):
for j in range(col-1, col+2):
if i >= 0 and j>=0 and i < len(city) and j < len(city):
alive += city[i][j].value
alive -= city[row][col].value
print(alive)
def pixel_c(city: list[list[city]]): #func to check every pixel is alive or not
for i in range(len(city)):
for j in range(len(city)):
n = neighbours(city,i,j)
if city[i][j].value == 1:
if n > 3:
city[i][j].status = False
if n < 2 :
city[i][j].status == True
if n == 3 or n == 2 :
city[i][j].status == True
elif city[i][j].value == 0:
if n == 3 :
city[i][j].status == True
else :
city[i][j].status = False
def update(city: list[list[pixel]]) : #func for updating our canvas
for i in range(len(city)):
for j in range(len(city)) :
if city[i][j].status == True:
city[i][j].value == 1
else:
city[i][j].value == 0
def run(city: list[list[pixel]]) :
update(city)
pixel_c(city)
由于错误,无法进一步移动,无法运行代码
我在neighbours()
函数的第一行得到了错误
pixel_c()
函数代表像素检查,它设置规则
update()
更新值
rpop()
用于随机人口,它设置城市人口
neighbours()
设置相邻单元格的规则
run()
用于运行游戏
def邻居(城市:list[list[pixel]],行:int,列:int):#func表示活动邻居#
TypeError:“type”对象不可下标
目前没有回答
相关问题 更多 >
编程相关推荐