我试图编写一个Python类,创建一个由零组成的矩阵,然后使用随机数生成器在矩阵上选取点。它把那个点上的0变成1,直到矩阵都是1。有人能批评/纠正我的代码吗?(我还希望生成器检查其在矩阵上的接近度,并尝试3次以找到一个与任何一个点相距2点的点。)
import random
import numpy as np
#agents is amount of agents available to fill grid
class Gridmodel():
def __init__(self, gridsize, agents):
self.gridsize = gridsize
self.agents = agents
self.gridmodel = np.zeros([self.gridsize, self.gridsize],dtype=int)
def foundspot(self):
foundspot = False
tries = 0
while foundspot == False and tries <= 3:
x = random.randint(0, self.gridsize)
y = random.randint(0, self.gridsize)
if self.gridmodel[x][y] < 0:
foundspot = True
else:
tries += 1
def goodspot(self, x, y):
goodspot = self.gridmodel[x][y]
for i in range(-1,2):
for j in range(-1,2):
print i, j, self.gridmodel[i][j]
目前没有回答
相关问题 更多 >
编程相关推荐