def initialize(self, coordinates): #not run until after first selection!
# set up mines
# randomly place mines anywhere *except* first selected location AND surrounding cells
# so that first selection is always a 0
# weird, yes, but that's how the original minesweeper worked
availableCells = range(self.totalCells)
selected = coordinates[0]*self.dim2 + coordinates[1]
offLimits = np.array([selected-self.dim2-1, selected-self.dim2, selected-self.dim2+1, selected-1, selected, selected+1, selected+self.dim2-1, selected+self.dim2, selected+self.dim2+1]) #out of bounds is ok
availableCells = np.setdiff1d(availableCells, offLimits)
self.nMines = np.minimum(self.nMines, len(availableCells)) #in case there are fewer remaining cells than mines to place
minesFlattened = np.zeros([self.totalCells])
minesFlattened[np.random.choice(availableCells, self.nMines, replace=False)] = 1
self.mines = minesFlattened.reshape([self.dim1, self.dim2])
# set up neighbors
for i in range(self.dim1):
for j in range(self.dim2):
nNeighbors = 0
for k in range(-1, 2):
if i + k >= 0 and i + k < self.dim1:
for l in range(-1, 2):
if j + l >= 0 and j + l < self.dim2 and (k != 0 or l != 0):
nNeighbors += self.mines[i + k, j + l]
self.neighbors[i, j] = nNeighbors
#done
self.initialized = True
评论列表
文章目录