Skip to content
This repository was archived by the owner on Dec 18, 2021. It is now read-only.

Commit fc43d30

Browse files
authored
Merge pull request #76 from PythonSerious/patch-5
Create tictactoe.py
2 parents 6a627bd + d22d2e4 commit fc43d30

File tree

1 file changed

+222
-0
lines changed

1 file changed

+222
-0
lines changed

examples/games/tictactoe.py

Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
"""
2+
@PythonSerious - 2021
3+
https://github.com/PythonSerious
4+
"""
5+
import discord
6+
from discord.ext.commands import command, Cog
7+
from discord_components import DiscordComponents, Button, ButtonStyle, InteractionType
8+
import asyncio
9+
10+
11+
class TicTacToe(Cog):
12+
def __init__(self, bot):
13+
self.bot = bot
14+
15+
16+
@command()
17+
async def tictactoe(self, ctx, member: discord.Member):
18+
if ctx.author == member:
19+
return await ctx.send("You can't play against yourself!")
20+
embed = discord.Embed(color=0xF5F5F5, title=f"Hey, {ctx.author.name} wants to play tic-tac-toe with you!")
21+
acceptdenycomps = [
22+
[
23+
Button(style=ButtonStyle.green, label="Accept"),
24+
Button(style=ButtonStyle.red, label="Decline")
25+
]
26+
]
27+
#
28+
board = [
29+
[
30+
Button(style=ButtonStyle.grey, label="⠀", id="0 0"),
31+
Button(style=ButtonStyle.grey, label="⠀", id="0 1"),
32+
Button(style=ButtonStyle.grey, label="⠀", id="0 2")
33+
34+
],
35+
[
36+
Button(style=ButtonStyle.grey, label="⠀", id="1 0"),
37+
Button(style=ButtonStyle.grey, label="⠀", id="1 1"),
38+
Button(style=ButtonStyle.grey, label="⠀", id="1 2")
39+
40+
],
41+
[
42+
Button(style=ButtonStyle.grey, label="⠀", id="2 0"),
43+
Button(style=ButtonStyle.grey, label="⠀", id="2 1"),
44+
Button(style=ButtonStyle.grey, label="⠀", id="2 2")
45+
]
46+
]
47+
selections = [
48+
[
49+
"unchosen",
50+
"unchosen",
51+
"unchosen"
52+
],
53+
[
54+
"unchosen",
55+
"unchosen",
56+
"unchosen"
57+
],
58+
[
59+
"unchosen",
60+
"unchosen",
61+
"unchosen"
62+
]
63+
]
64+
65+
m = await ctx.send(embed=embed, components=acceptdenycomps, content=member.mention)
66+
def haswon(team):
67+
if selections[0][0] == team and selections[0][1] == team and selections[0][2] == team:
68+
return True
69+
if selections[1][0] == team and selections[1][1] == team and selections[1][2] == team:
70+
return True
71+
if selections[2][0] == team and selections[2][1] == team and selections[2][2] == team:
72+
return True
73+
if selections[0][0] == team and selections[1][0] == team and selections[2][0] == team:
74+
return True
75+
if selections[0][1] == team and selections[1][1] == team and selections[2][1] == team:
76+
return True
77+
if selections[0][2] == team and selections[1][2] == team and selections[2][2] == team:
78+
return True
79+
if selections[0][0] == team and selections[1][1] == team and selections[2][2] == team:
80+
return True
81+
if selections[0][2] == team and selections[1][1] == team and selections[2][0] == team:
82+
return True
83+
else:
84+
return False
85+
def istie(team):
86+
if not "unchosen" in str(selections):
87+
if not haswon(team):
88+
89+
return True
90+
else:
91+
92+
return False
93+
else:
94+
95+
return False
96+
97+
98+
def confirmcheck(res):
99+
return res.user.id == member.id and res.channel.id == ctx.channel.id and str(res.message.id) == str(m.id)
100+
101+
try:
102+
res = await self.bot.wait_for("button_click", check=confirmcheck, timeout=50)
103+
except asyncio.TimeoutError:
104+
await msg.edit(
105+
embed=Embed(color=0xED564E, title="Timeout!", description="No-one reacted. ☹️"),
106+
components=[
107+
Button(style=ButtonStyle.red, label="Oh-no! Timeout reached!", disabled=True),
108+
Button(style=ButtonStyle.URL, label="View creator", url="https://github.com/PythonSerious")
109+
],
110+
)
111+
return
112+
await res.respond(type=6)
113+
if res.component.label == "Accept":
114+
accept = True
115+
embed = discord.Embed(color=discord.Colour.green(), title=f'{member.name} has accepted!', description="The game will now begin...")
116+
await m.edit(embed=embed)
117+
await asyncio.sleep(1)
118+
119+
else:
120+
accept = False
121+
embed = discord.Embed(color=discord.Colour.red(), title=f'{member.name} has declined.')
122+
await m.edit(embed=embed)
123+
return
124+
125+
async def winner(team):
126+
if team == "red":
127+
color = discord.Colour.red()
128+
user = member
129+
if team == "green":
130+
color = discord.Colour.green()
131+
user = ctx.author
132+
e = discord.Embed(color=color, title=f"{user.name} has won!")
133+
board.append(Button(style=ButtonStyle.URL, label="View creator", url="https://github.com/PythonSerious"))
134+
await m.edit(embed=e, components=board)
135+
return
136+
137+
138+
139+
greensturnembed = discord.Embed(color=0xF5F5F5, title=f"{ctx.author.name}'s turn")
140+
redsturnembed = discord.Embed(color=0xF5F5F5, title=f"{member.name}'s turn")
141+
greenstatus = True
142+
# True = green False = red
143+
def greensturncheck(res):
144+
return res.user.id == ctx.author.id and res.channel.id == ctx.channel.id and res.message.id == m.id
145+
def redsturncheck(res):
146+
return res.user.id == member.id and res.channel.id == ctx.channel.id and res.message.id == m.id
147+
while accept:
148+
if greenstatus:
149+
await m.edit(embed=greensturnembed, components=board)
150+
try:
151+
res = await self.bot.wait_for("button_click", check=greensturncheck, timeout=50)
152+
await res.respond(type=6)
153+
listid = res.component.id
154+
firstpart, secondpart = listid.split(' ')
155+
board[int(firstpart)][int(secondpart)] = Button(style=ButtonStyle.green, label="⠀", id="1 0", disabled=True)
156+
selections[int(firstpart)][int(secondpart)] = "green"
157+
if haswon('green'):
158+
await winner('green')
159+
accept = False
160+
return
161+
if istie('green'):
162+
e = discord.Embed(color=0xF5F5F5, title=f"Call it a tie!")
163+
board.append(Button(style=ButtonStyle.URL, label="View creator", url="https://github.com/PythonSerious"))
164+
await m.edit(embed=e, components=board)
165+
accept = False
166+
return
167+
greenstatus = False
168+
pass
169+
170+
171+
except asyncio.TimeoutError:
172+
await msg.edit(
173+
embed=Embed(color=0xED564E, title="Timeout!", description="No-one reacted. ☹️"),
174+
components=[
175+
Button(style=ButtonStyle.red, label="Oh-no! Timeout reached!", disabled=True),
176+
Button(style=ButtonStyle.URL, label="View creator", url="https://github.com/PythonSerious")
177+
],
178+
)
179+
return
180+
if not greenstatus:
181+
await m.edit(embed=redsturnembed, components=board)
182+
try:
183+
res = await self.bot.wait_for("button_click", check=redsturncheck, timeout=50)
184+
await res.respond(type=6)
185+
listid = res.component.id
186+
firstpart, secondpart = listid.split(' ')
187+
board[int(firstpart)][int(secondpart)] = Button(style=ButtonStyle.red, label="⠀", id="1 0",
188+
disabled=True)
189+
selections[int(firstpart)][int(secondpart)] = "red"
190+
if haswon('red'):
191+
await winner('red')
192+
accept = False
193+
return
194+
if istie('red'):
195+
e = discord.Embed(color=0xF5F5F5, title=f"Call it a tie!")
196+
board.append(Button(style=ButtonStyle.URL, label="View creator", url="https://github.com/PythonSerious"))
197+
await m.edit(embed=e, components=board)
198+
accept = False
199+
return
200+
201+
greenstatus = True
202+
pass
203+
204+
205+
except asyncio.TimeoutError:
206+
await msg.edit(
207+
embed=Embed(color=0xED564E, title="Timeout!", description="No-one reacted. ☹️"),
208+
components=[
209+
Button(style=ButtonStyle.red, label="Oh-no! Timeout reached!", disabled=True),
210+
Button(style=ButtonStyle.URL, label="View creator", url="https://github.com/PythonSerious")
211+
],
212+
)
213+
return
214+
215+
216+
217+
218+
219+
def setup(bot):
220+
print('Tictactoe - by python#0001 has loaded!')
221+
DiscordComponents(bot) #if you have this in an on_ready event you can remove this line.
222+
bot.add_cog(TicTacToe(bot))

0 commit comments

Comments
 (0)