def _populate_mines(self):
"""
Method _populate_mines populates the cells of this minefield with
mines. Applies a random selection of simple constraints to where mines
may be placed, though about half of the mines are purely randomly
placed.
"""
if self.mine_count is None:
self.mine_count = int(0.15 * (self.height * self.width))
count = self.mine_count
selectionfuncs = [
lambda y: not (y % 2),
lambda y: bool(y % 2),
lambda y: not (y % 3),
]
yconstraint, xconstraint = random.sample(selectionfuncs * 2, 2)
for x in range(count):
while True:
rx, ry = random.randint(0, self.width - 1), random.randint(
0, self.height - 1)
if random.randint(0, 1):
if yconstraint(ry):
continue
if xconstraint(rx):
continue
c = self.board[rx][ry]
if c.contents == Contents.empty:
c.contents = Contents.mine
break
评论列表
文章目录