let first move to be an open

This commit is contained in:
JerryXiao 2019-10-15 20:24:33 +08:00
parent 53d3fb4bb5
commit 9719d52230
Signed by: Jerry
GPG key ID: 9D9CE43650FF2BAA

View file

@ -1,7 +1,7 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import numpy as np import numpy as np
from random import randint from random import shuffle, choice
from copy import deepcopy from copy import deepcopy
# 0 - 8: means 0-8 mines, not opened # 0 - 8: means 0-8 mines, not opened
@ -58,17 +58,25 @@ class Board():
return return
elif mines < 0: elif mines < 0:
return return
mines_1d = list(range(height * width)) # first_move should't be a mine, and if possible, it should be an open.
# first_move should't be a mine
del mines_1d[get_index(width, first_move)]
mines_index = list()
self.map = np.zeros((height, width), dtype=np.int8) self.map = np.zeros((height, width), dtype=np.int8)
for i in range(mines): map_1d = [IS_MINE] * mines
index = randint(1, len(mines_1d)) - 1 zero_blocks = list()
mines_index.append(mines_1d[index]) fm_index = get_index(width, first_move)
del mines_1d[index] zero_blocks.append(fm_index)
for mine_index in mines_index: fm_nbrs = [rc for rc in self.__iter_neighbour(*first_move)]
(row, col) = get_row_col(width, mine_index) if height * width - mines - 1 >= len(fm_nbrs):
fm_nbrs_index = [get_index(width, fm_nbr) for fm_nbr in fm_nbrs]
zero_blocks += fm_nbrs_index
map_1d += [0] * (height * width - mines - 1 - len(fm_nbrs))
else:
map_1d += [0] * (height * width - mines - 1)
shuffle(map_1d)
for mindex in sorted(zero_blocks):
map_1d.insert(mindex, 0)
for mindex in range(len(map_1d)):
if map_1d[mindex] == IS_MINE:
(row, col) = get_row_col(width, mindex)
self.map[row][col] = IS_MINE self.map[row][col] = IS_MINE
for row in range(height): for row in range(height):
for col in range(width): for col in range(width):