def bot_step(user_id, field): db = Db(db_dir) user = str(db.get_user_by_id(user_id)[1]) type_ = other_type(user) right_step = check_can_win_lose(field, type_) if right_step: return db.set_step(user_id, right_step, True) # if '0' == field[5]: # return db.set_step(user_id, 5, True) while True: pos = random.randint(1, 9) if '0' == field[pos]: return db.set_step(user_id, pos, True)
def repeat_all_messages(message): user_id = message.from_user.id db = Db(db_dir) user = db.get_user_by_id(user_id) if user: if user[1]: if message.text in ['1', '2', '3', '4', '5', '6', '7', '8', '9']: pos = int(message.text) user_game = db.get_game(user_id) if '0' == user_game[2][pos]: field = db.set_step(user_id, pos) winner = who_win(field) if winner: markup = types.ReplyKeyboardHide() field = draw_field(field) bot.send_message(user_id, field + "\n" + winner, reply_markup=markup, parse_mode='HTML') db.set_type_to_user(user_id, None) if 'Ничья' == winner: db.user_draw(user_id) else: db.user_win(user_id) db.end_game(user_id) return True field = bot_step(user_id, field) winner = who_win(field) if winner: markup = types.ReplyKeyboardHide() field = draw_field(field) bot.send_message(user_id, field + "\n" + winner, reply_markup=markup, parse_mode='HTML') db.set_type_to_user(user_id, None) if 'Ничья' == winner: db.user_draw(user_id) else: db.user_lose(user_id) db.end_game(user_id) return True field = draw_field(field) bot.send_message(user_id, field, parse_mode='HTML') else: bot.send_message(user_id, 'Так сходить нельзя') else: markup = types.ReplyKeyboardHide() if message.text in ['X', 'O']: db.set_type_to_user(user_id, message.text) bot.send_message(user_id, 'Вы выбрали ' + str(message.text) + '!', reply_markup=markup) db.create_game(user_id) cmd_game(message)