-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathsokoban.py
More file actions
73 lines (57 loc) · 2.13 KB
/
sokoban.py
File metadata and controls
73 lines (57 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# -*- coding: utf-8 -*-
import telebot
from telebot import types
from sokoban_config import token
bot = telebot.TeleBot(token)
def show_map(gmap):
gmap_width = str(gmap.find(u'█\n') + 1)
btn = types.InlineKeyboardButton
markup = types.InlineKeyboardMarkup(row_width=2)
markup.add(
btn('', callback_data='0'),
btn(u'⬆', callback_data='-' + gmap_width),
btn(u'⬅', callback_data='-1'),
btn(u'➡', callback_data='1'),
btn('', callback_data='0'),
btn(u'⬇', callback_data=gmap_width),
)
return {
'text': '<code>' + gmap + '</code>',
'parse_mode': 'html',
'reply_markup': markup
}
@bot.message_handler(content_types=['text'])
def any_msg(message):
gmap = u"""
██████████
██████ . █
█ ◯☿◯ ◯ █
█ ..██
██████████
""".replace('\n ', '\n')
bot.send_message(message.chat.id, **show_map(gmap))
def replace_on_map(game_map, pos, char):
return game_map[:pos] + char + game_map[pos + 1:]
@bot.callback_query_handler(func=lambda call: True)
def callback_inline(call):
if call.message:
gmap = call.message.text
movement = int(call.data)
pos = gmap.find(u'☿')
if pos < 0:
pos = gmap.find(u'♆')
new_pos = pos + movement
new_place = gmap[new_pos]
next_place = gmap[new_pos + movement]
if new_place in (' ', '.') or (new_place in (u'◯', u'◉') and next_place in (' ', '.')):
if new_place in (u'◯', u'◉'):
gmap = replace_on_map(gmap, new_pos + movement, u'◉' if next_place == '.' else u'◯')
gmap = replace_on_map(gmap, pos, ' ' if gmap[pos] == u'☿' else '.')
gmap = replace_on_map(gmap, new_pos, u'☿' if new_place in (' ', u'◯') else u'♆')
if gmap != call.message.text:
bot.edit_message_text(
chat_id=call.message.chat.id,
message_id=call.message.message_id,
**show_map(gmap)
)
bot.polling(none_stop=True)