-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathconnect4.py
187 lines (167 loc) · 6.4 KB
/
connect4.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
import asyncio
import threading
import unicodedata
import discord
from django.db import close_old_connections
from .models import ConnectFourGame
# Some emoji aren't in unicodedata, so the literal emoji are here
BLUE_SQUARE = "🟦"
NUMBERS_PLAINTEXT = [f"[{i}]" for i in range(7)]
NUMBERS_EMOJI = [f"{i}\N{COMBINING ENCLOSING KEYCAP}" for i in range(7)]
RED_CIRCLE = unicodedata.lookup("LARGE RED CIRCLE")
YELLOW_CIRCLE = "🟡"
RED = "R"
YELLOW = "Y"
BLUE = "_"
BELL = unicodedata.lookup("BELL")
DOWN_ARROW = unicodedata.lookup("DOWNWARDS BLACK ARROW")
DOWN_ARROW_2 = "⬇️"
async def on_message(client, message):
args = message.content.split(" ")
if len(args) != 2 or len(message.mentions) != 1:
await message.channel.send("Usage: !connectfour @opponent")
return
opponent_id = message.mentions[0].id
new_board = Board.new(player1=message.author, player2=message.mentions[0])
m = await message.channel.send(new_board.get_message())
await add_reaccs(m)
close_old_connections()
game = ConnectFourGame.objects.create(
message_id=m.id,
player1=message.author.id,
player2=opponent_id,
state=new_board.get_state(),
)
async def add_reaccs(message):
for num in NUMBERS_EMOJI:
await message.add_reaction(num)
await message.add_reaction(DOWN_ARROW)
await message.add_reaction(BELL)
async def handle_event(client, game, event, message):
is_player1 = game.player1 == event.user_id
is_player2 = game.player2 == event.user_id
if game.winner or not (is_player1 or is_player2):
return
if event.emoji.name in [DOWN_ARROW, DOWN_ARROW_2]:
new_message = await message.channel.send(message.content)
await message.delete()
await add_reaccs(new_message)
game.message_id = new_message.id
game.save()
return
if event.emoji.name == BELL:
user = await client.fetch_user(
game.player1 if game.is_player1_turn else game.player2
)
dm_channel = user.dm_channel
if not dm_channel:
dm_channel = await user.create_dm()
await dm_channel.send(f"It's your turn in connect 4!: {message.jump_url}")
return
if event.emoji.name in NUMBERS_EMOJI:
column = NUMBERS_EMOJI.index(event.emoji.name)
board = Board.from_state(game.state)
color = RED if game.is_player1_turn else YELLOW
if board.try_move(column, color):
game.winner = board.get_winner()
game.is_player1_turn = not game.is_player1_turn
user = await client.fetch_user(
game.player1 if game.is_player1_turn else game.player2
)
if game.winner:
winner_user = await client.fetch_user(
game.player1 if game.winner == 1 else game.player2
)
color = RED if game.winner == 1 else YELLOW
board.set_win_footer(winner_user, color)
else:
color = RED if game.is_player1_turn else YELLOW
board.set_footer(user, color)
game.state = board.get_state()
game.save()
await message.edit(content=board.get_message())
async def on_raw_reaction_add(client, event):
try:
if event.user_id != client.user.id and (
event.emoji.name in NUMBERS_EMOJI
or event.emoji.name in [DOWN_ARROW, DOWN_ARROW_2, BELL]
):
channel = await client.fetch_channel(event.channel_id)
message = await channel.fetch_message(event.message_id)
close_old_connections()
game = ConnectFourGame.objects.get(message_id=event.message_id)
await handle_event(client, game, event, message)
try:
await message.remove_reaction(event.emoji.name, event.member)
except discord.errors.NotFound:
pass
except ConnectFourGame.DoesNotExist:
pass
class Board:
@staticmethod
def new(player1, player2):
b = Board()
b.header = (
f"Connect Four game between {player1.mention}({RED}) "
f"and {player2.mention}({YELLOW})"
)
b.rows = [BLUE * 7 for _ in range(6)]
b.number_line = "".join(NUMBERS_PLAINTEXT)
b.set_footer(player1, RED)
return b
@staticmethod
def from_state(state):
b = Board()
lines = state.split("\n")
b.header = lines[0]
b.rows = lines[1:7]
b.number_line = lines[7]
b.footer = lines[8]
return b
def set_footer(self, user: discord.User, color):
self.footer = f"{color} {user.mention} to move"
def set_win_footer(self, user: discord.User, color):
self.footer = f"{color} {user.mention} won!"
def get_state(self):
"""Serialize this Board object a string to store in the Game.state field"""
return "\n".join([self.header] + self.rows + [self.number_line, self.footer])
def get_message(self):
"""Get a string to display this Board to the user via Discord message"""
message = (
self.get_state()
.replace(BLUE, BLUE_SQUARE)
.replace(RED, RED_CIRCLE)
.replace(YELLOW, YELLOW_CIRCLE)
)
for plaintext, emoji in zip(NUMBERS_PLAINTEXT, NUMBERS_EMOJI):
message = message.replace(plaintext, emoji)
return message
def try_move(self, column, color):
valid_move = False
for i in range(5, -1, -1):
if self.rows[i][column] == BLUE:
row = list(self.rows[i])
row[column] = color
self.rows[i] = "".join(row)
return True
return False
def get_winner(self):
rows = self.rows
columns = ["".join(rows[r][c] for r in range(6)) for c in range(7)]
diagonals = []
for offset in range(-2, 4):
diagonals.append(
"".join(rows[i][offset + i] for i in range(6) if 0 <= offset + i < 7)
)
diagonals.append(
"".join(
rows[5 - i][offset + i] for i in range(6) if 0 <= offset + i < 7
)
)
red_win = RED * 4
yellow_win = YELLOW * 4
if any(red_win in check for check in rows + columns + diagonals):
return 1
if any(yellow_win in check for check in rows + columns + diagonals):
return 2
return None