-
Notifications
You must be signed in to change notification settings - Fork 4
/
connect_4.py
289 lines (251 loc) · 15.2 KB
/
connect_4.py
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import os
import telebot
import pyrebase
import threading
import schedule
from PIL import Image
from time import time, sleep
from telebot.types import InlineKeyboardButton
from telebot.types import InlineKeyboardMarkup
from telebot.types import InputMediaPhoto
from telebot.types import InlineQueryResultPhoto
config = { # Your FIREBASE DATABASE configurations
"apiKey" : "",
"authDomain" : "",
"databaseURL" : "",
"projectId" : "",
"storageBucket" : "",
"messagingSenderId" : "",
"appId" : "",
"measurementId" : "",
}
firebase = pyrebase.initialize_app(config)
database = firebase.database()
API_KEY = "" # API TOKEN from @BotFather
STORAGE_CHANNEL = 1234567890 # Channel Id for a private storage place
bot = telebot.TeleBot(API_KEY, parse_mode="HTML") # Initializing the bot
BANNER = "" # FILE ID of banner for your bot
def connect_4_markup(play, positions): # Creates the move markup
game_board, buttons = InlineKeyboardMarkup(row_width = 7), []
for pos in positions:
buttons.append(InlineKeyboardButton(play, callback_data=pos))
game_board.add(*buttons)
return game_board
def board_img(red, yellow, game_id): # Creates the board image
board = Image.open(r"./assets/game_board.jpg")
p1 = Image.open(r"./assets/p1.png")
p2 = Image.open(r"./assets/p2.png")
tiles = {
"A1" : (0,0), "B1" : (0,0), "C1" : (0,0), "D1" : (0,0), "E1" : (0,0), "F1" : (0,0), "G1" : (0,0),
"A2" : (0,0), "B2" : (0,0), "C2" : (0,0), "D2" : (0,0), "E2" : (0,0), "F2" : (0,0), "G2" : (0,0),
"A3" : (0,0), "B3" : (0,0), "C3" : (0,0), "D3" : (0,0), "E3" : (0,0), "F3" : (0,0), "G3" : (0,0),
"A4" : (0,0), "B4" : (0,0), "C4" : (0,0), "D4" : (0,0), "E4" : (0,0), "F4" : (0,0), "G4" : (0,0),
"A5" : (0,0), "B5" : (0,0), "C5" : (0,0), "D5" : (0,0), "E5" : (0,0), "F5" : (0,0), "G5" : (0,0),
"A6" : (0,0), "B6" : (0,0), "C6" : (0,0), "D6" : (0,0), "E6" : (0,0), "F6" : (0,0), "G6" : (0,0),
}
game = board.copy()
for _ in red:
game.paste(p1, tiles[_])
for _ in yellow:
game.paste(p2, tiles[_])
game.save(f'Games/{game_id}.png')
return f'Games/{game_id}.png'
def remove_expired(): # Deletes all expired games
try:
for game in database.child("Connect-4").get().each():
expiry, id = int(game.val()["expiry"]), game.val()["id"]
if int(time()) - expiry >= 600:
database.child(id).remove()
bot.edit_message_text(inline_message_id=id, text="<b>Game expired! 🙃</b>")
bot.edit_message_reply_markup(inline_message_id=id, reply_markup=None)
except:
pass
def parse_board(red, yellow): # Converts board image to ascii format
board = [
['A1', 'B1', 'C1', 'D1', 'E1', 'F1', 'G1'],
['A2', 'B2', 'C2', 'D2', 'E2', 'F2', 'G2'],
['A3', 'B3', 'C3', 'D3', 'E3', 'F3', 'G3'],
['A4', 'B4', 'C4', 'D4', 'E4', 'F4', 'G4'],
['A5', 'B5', 'C5', 'D5', 'E5', 'F5', 'G5'],
['A6', 'B6', 'C6', 'D6', 'E6', 'F6', 'G6'],
]
for __ in board:
for _ in __:
if _ in red: __[__.index(_)] = 1
elif _ in yellow: __[__.index(_)] = 2
else: __[__.index(_)] = 0
return board
def chk_win(board, piece, cols=7, rows=6): # Checks if player has wons
for c in range(cols-3): # Horizonatal arrangement
for r in range(rows):
if board[r][c] == piece\
and board[r][c+1] == piece\
and board[r][c+2] == piece\
and board[r][c+3] == piece:
return True
for c in range(cols): # Vertical arrangement
for r in range(rows-3):
if board[r][c] == piece\
and board[r+1][c] == piece\
and board[r+2][c] == piece\
and board[r+3][c] == piece:
return True
for c in range(cols-3): # Positive Diagonals
for r in range(rows-3):
if board[r][c] == piece\
and board[r+1][c+1] == piece\
and board[r+2][c+2] == piece\
and board[r+3][c+3] == piece:
return True
for c in range(cols-3): # Negative Diagonals
for r in range(3, rows):
if board[r][c] == piece\
and board[r-1][c+1] == piece\
and board[r-2][c+2] == piece\
and board[r-3][c+3] == piece:
return True
@bot.message_handler(commands="start")
def start(message): # Starts the bot
bot.send_photo(message.chat.id, BANNER, caption="<b>Wanna a play a game of Connect-4?\n\
\nClick the buton and play with your friends!</b>",
reply_markup = InlineKeyboardMarkup().row(InlineKeyboardButton("Play Connect-4!",
switch_inline_query="connect_4")))
@bot.inline_handler(lambda query: len(query.query) == 0 or query.query == 'connect_4')
def send_game(query): # Creating the inline query handler
try:
c_4 = InlineQueryResultPhoto('_c4_',
photo_url='https://raw.githubusercontent.com/TECH-SAVVY-GUY/telegram-games/master/assets/game_board.jpg',
thumb_url='https://raw.githubusercontent.com/TECH-SAVVY-GUY/telegram-games/master/assets/connect-4.jpg',
title = "ᑕᗝᑎᑎᗴᑕ丅 - 4", description="Play a game of Connect-4 with your friends and family! ✌🏻",
reply_markup = InlineKeyboardMarkup().row(InlineKeyboardButton("Tap to play!",
callback_data=f"c-play{query.from_user.id}")), caption = "<b>Start the game! 🥳\n\
\nGame will be expire in 10 minutes!</b>", parse_mode = "HTML"
)
bot.answer_inline_query(query.id, [c_4])
except: pass
@bot.callback_query_handler(func=lambda call: True)
def callback_listener(call): # A single callback listener for all calls
data, game_id = call.data, call.inline_message_id
if data[:6] == "c-play": # Starting the game
red, yellow = int(data[6:]), int(call.from_user.id)
if red == yellow:
bot.answer_callback_query(call.id,
"⚠️ Must be a different player! ⚠️", show_alert=True)
else:
database.child("Connect-4").child(game_id).child("id").set(game_id)
database.child("Connect-4").child(game_id).child("red").set(int(data[6:]))
database.child("Connect-4").child(game_id).child("yellow").set(call.from_user.id)
database.child("Connect-4").child(game_id).child("count").set(1)
database.child("Connect-4").child(game_id).child(
"board").set("['A6', 'B6', 'C6', 'D6', 'E6', 'F6', 'G6']")
database.child("Connect-4").child(game_id).child("red_places").set("[]")
database.child("Connect-4").child(game_id).child("yellow_places").set("[]")
database.child("Connect-4").child(game_id).child("expiry").set(int(time()))
bot.edit_message_caption(inline_message_id=game_id, caption=" ")
bot.edit_message_reply_markup(inline_message_id=game_id,
reply_markup=connect_4_markup("🔴", ["A6", "B6", "C6", "D6", "E6", "F6", "G6"]))
else: # Player move algorithm
game = database.child("Connect-4").child(game_id).get()
players = [int(game.val()["red"]), int(game.val()["yellow"])]
if call.from_user.id not in players:
bot.answer_callback_query(call.id,
"❌ You are not a player! ❌", show_alert=True)
else:
count = int(game.val()["count"])
if count % 2 != 0:
if call.from_user.id != players[0]:
bot.answer_callback_query(call.id,
"⚠️ Wait for your Turn! ⚠️", show_alert=True)
else:
if data[1] != "0":
board = eval(game.val()["board"])
board[board.index(data)] = data[0] + str(int(data[1]) - 1)
database.child("Connect-4").child(game_id).update({"board":f"{board}"})
red_places, yellow_places = eval(game.val()["red_places"]), eval(game.val()["yellow_places"])
red_places.append(data)
parsed_board = parse_board(red_places, yellow_places)
if chk_win(parsed_board, 1):
database.child("Connect-4").child(game_id).remove()
path = board_img(red_places, yellow_places, game_id)
with open(path, "rb") as pic:
pic = bot.send_photo(STORAGE_CHANNEL, pic)
pic = bot.get_file(pic.photo[-1].file_id).file_id
bot.edit_message_media(inline_message_id=game_id, media=InputMediaPhoto(pic))
bot.edit_message_caption(inline_message_id=game_id,
caption="<b><i>Player 🔴 wins! 🥳</i></b>")
try: os.remove(path)
except: pass
elif board == ["A0", "B0", "C0", "D0", "E0", "F0", "G0"]:
database.child("Connect-4").child(game_id).remove()
path = board_img(red_places, yellow_places, game_id)
with open(path, "rb") as pic:
pic = bot.send_photo(STORAGE_CHANNEL, pic)
pic = bot.get_file(pic.photo[-1].file_id).file_id
bot.edit_message_media(inline_message_id=game_id, media=InputMediaPhoto(pic))
bot.edit_message_caption(inline_message_id=game_id,
caption="<b><i>It's a draw! 🥱</i></b>")
else:
database.child("Connect-4").child(game_id).update({"red_places":f"{red_places}"})
database.child("Connect-4").child(game_id).update({"count":count + 1})
path = board_img(red_places, yellow_places, game_id)
with open(path, "rb") as pic:
pic = bot.send_photo(STORAGE_CHANNEL, pic)
pic = bot.get_file(pic.photo[-1].file_id).file_id
bot.edit_message_media(inline_message_id=game_id, media=InputMediaPhoto(pic))
bot.edit_message_reply_markup(inline_message_id=game_id,
reply_markup=connect_4_markup("🟡", board))
try: os.remove(path)
except: pass
else:
if call.from_user.id != players[-1]:
bot.answer_callback_query(call.id,
"⚠️ Wait for your Turn! ⚠️", show_alert=True)
else:
if data[1] != "0":
board = eval(game.val()["board"])
board[board.index(data)] = data[0] + str(int(data[1]) - 1)
database.child("Connect-4").child(game_id).update({"board":f"{board}"})
red_places, yellow_places = eval(game.val()["red_places"]), eval(game.val()["yellow_places"])
yellow_places.append(data)
parsed_board = parse_board(red_places, yellow_places)
if chk_win(parsed_board, 2):
database.child("Connect-4").child(game_id).remove()
path = board_img(red_places, yellow_places, game_id)
with open(path, "rb") as pic:
pic = bot.send_photo(STORAGE_CHANNEL, pic)
pic = bot.get_file(pic.photo[-1].file_id).file_id
bot.edit_message_media(inline_message_id=game_id, media=InputMediaPhoto(pic))
bot.edit_message_caption(inline_message_id=game_id,
caption="<b><i>Player 🟡 wins! 🥳</i></b>")
try: os.remove(path)
except: pass
elif board == ["A0", "B0", "C0", "D0", "E0", "F0", "G0"]:
database.child("Connect-4").child(game_id).remove()
path = board_img(red_places, yellow_places, game_id)
with open(path, "rb") as pic:
pic = bot.send_photo(STORAGE_CHANNEL, pic)
pic = bot.get_file(pic.photo[-1].file_id).file_id
bot.edit_message_media(inline_message_id=game_id, media=InputMediaPhoto(pic))
bot.edit_message_caption(inline_message_id=game_id,
caption="<b><i>It's a draw! 🥱</i></b>")
else:
database.child("Connect-4").child(game_id).update({"yellow_places":f"{yellow_places}"})
database.child("Connect-4").child(game_id).update({"count":count + 1})
path = board_img(red_places, yellow_places, game_id)
with open(path, "rb") as pic:
pic = bot.send_photo(STORAGE_CHANNEL, pic)
pic = bot.get_file(pic.photo[-1].file_id).file_id
bot.edit_message_media(inline_message_id=game_id, media=InputMediaPhoto(pic))
bot.edit_message_reply_markup(inline_message_id=game_id,
reply_markup=connect_4_markup("🔴", board))
try: os.remove(path)
except: pass
def thrd():
while True:
schedule.run_pending()
sleep(1)
schedule.every(1).minutes.do(remove_expired)
t = threading.Thread(target=thrd)
def main():
t.start()
bot.infinity_polling()