From 6c8ed2ebc8e26c1fdeeaacc81e63f938b8aefd48 Mon Sep 17 00:00:00 2001 From: Jerry Date: Sun, 30 Dec 2018 17:07:00 +0800 Subject: [PATCH] added full statistics --- mscore.py | 39 +++++++++++++++++++++++++++++++++++++++ tgmsbot.py | 9 ++++++++- 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/mscore.py b/mscore.py index 6dccbec..3124764 100644 --- a/mscore.py +++ b/mscore.py @@ -137,3 +137,42 @@ class Board(): self.state = 1 (row, col) = 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) diff --git a/tgmsbot.py b/tgmsbot.py index b8e6003..0b20120 100644 --- a/tgmsbot.py +++ b/tgmsbot.py @@ -118,6 +118,10 @@ def send_keyboard(bot, update, args): mines = int(args[2]) except: 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) if ck[0]: board = Board(height, width, mines) @@ -144,6 +148,8 @@ def send_keyboard(bot, update, args): parse_mode="Markdown", reply_markup=InlineKeyboardMarkup(keyboard)) 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)) def send_source(bot, update): @@ -215,6 +221,7 @@ def handle_button_click(bot, update): if mmap is not None: game.save_action(user, (row, col)) 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_list = game.get_actions() last_player = game.get_last_player() @@ -225,7 +232,7 @@ def handle_button_click(bot, update): template = WIN_TEXT_TEMPLATE else: 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, time=round(time_used, 4), timeouts=timeouts, bot_username=bot_username)