added full statistics

This commit is contained in:
JerryXiao 2018-12-30 17:07:00 +08:00
parent e15252bca4
commit 6c8ed2ebc8
Signed by: Jerry
GPG key ID: 9D9CE43650FF2BAA
2 changed files with 47 additions and 1 deletions

View file

@ -137,3 +137,42 @@ class Board():
self.state = 1 self.state = 1
(row, col) = row_col (row, col) = row_col
self.__open(row, col) self.__open(row, col)
def gen_statistics(self):
self.__visited = np.zeros((self.height, self.width), dtype=np.int8)
self.__op = 0
self.__is = 0
self.__3bv = 0
def scan_open(row, col):
self.__visited[row][col] = 1
for nbr_rc in self.__iter_neighbour(row, col):
(nrow, ncol) = nbr_rc
if self.__visited[nrow][ncol] == 0:
nbr = self.mmap[nrow][ncol]
if nbr == 0:
scan_open(nrow, ncol)
elif nbr <= 8:
self.__visited[nrow][ncol] = 1
def scan_island(row, col):
self.__3bv += 1
self.__visited[row, col] = 1
for nbr_rc in self.__iter_neighbour(row, col):
(nrow, ncol) = nbr_rc
if self.__visited[nrow][ncol] == 0:
nbr = self.mmap[nrow][ncol]
if nbr >= 1 and nbr <= 8:
scan_island(nrow, ncol)
for row in range(self.height):
for col in range(self.width):
if self.__visited[row][col] == 0 and self.mmap[row][col] == 0:
self.__op += 1
self.__3bv += 1
scan_open(row, col)
for row in range(self.height):
for col in range(self.width):
cell = self.mmap[row][col]
if self.__visited[row][col] == 0 and cell >= 1 and cell <= 8:
self.__is += 1
scan_island(row, col)
return (self.__op, self.__is, self.__3bv)

View file

@ -118,6 +118,10 @@ def send_keyboard(bot, update, args):
mines = int(args[2]) mines = int(args[2])
except: except:
pass pass
# telegram doesn't like keyboard width to exceed 8
if width > 8:
width = 8
msg.reply_text('宽度太大已经帮您设置成8了')
ck = check_params(height, width, mines) ck = check_params(height, width, mines)
if ck[0]: if ck[0]:
board = Board(height, width, mines) board = Board(height, width, mines)
@ -144,6 +148,8 @@ def send_keyboard(bot, update, args):
parse_mode="Markdown", reply_markup=InlineKeyboardMarkup(keyboard)) parse_mode="Markdown", reply_markup=InlineKeyboardMarkup(keyboard))
def send_help(bot, update): def send_help(bot, update):
msg = update.message
msg.reply_text("这是一个扫雷bot\n\n/mine@{} 开始新游戏".format(bot.username))
logger.debug("Start from {0}".format(update.message.from_user.id)) logger.debug("Start from {0}".format(update.message.from_user.id))
def send_source(bot, update): def send_source(bot, update):
@ -215,6 +221,7 @@ def handle_button_click(bot, update):
if mmap is not None: if mmap is not None:
game.save_action(user, (row, col)) game.save_action(user, (row, col))
update_keyboard(bot, bhash, game, chat_id, msg.message_id) update_keyboard(bot, bhash, game, chat_id, msg.message_id)
(s_op, s_is, s_3bv) = board.gen_statistics()
ops_count = game.actions_sum() ops_count = game.actions_sum()
ops_list = game.get_actions() ops_list = game.get_actions()
last_player = game.get_last_player() last_player = game.get_last_player()
@ -225,7 +232,7 @@ def handle_button_click(bot, update):
template = WIN_TEXT_TEMPLATE template = WIN_TEXT_TEMPLATE
else: else:
template = LOSE_TEXT_TEMPLATE template = LOSE_TEXT_TEMPLATE
myreply = template.format(s_op=0, s_is=0, s_3bv=0, ops_count=ops_count, myreply = template.format(s_op=s_op, s_is=s_is, s_3bv=s_3bv, ops_count=ops_count,
ops_list=ops_list, last_player=last_player, ops_list=ops_list, last_player=last_player,
time=round(time_used, 4), timeouts=timeouts, time=round(time_used, 4), timeouts=timeouts,
bot_username=bot_username) bot_username=bot_username)