Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tictactoe #5

Open
wants to merge 37 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
552f14b
Delete Discord Bot - Music Player directory
angwarrenn Jul 14, 2022
3045b31
Delete #Discord Bot Basic.py
angwarrenn Jul 14, 2022
3af94a9
Delete README.md
angwarrenn Jul 14, 2022
541e4a1
init
angwarrenn Jul 14, 2022
08ee4fd
v2 bot alive
angwarrenn Jul 14, 2022
66340a4
added README
Jul 14, 2022
8a23d46
slash commands
angwarrenn Jul 20, 2022
3b5eaf7
Delete __pycache__ directory
angwarrenn Jul 20, 2022
161dc9f
updated
angwarrenn Jul 21, 2022
7a4fc4d
clean up
angwarrenn Jul 21, 2022
5a9e924
Merge branch 'tictactoe' of https://github.com/HackMelbourne/Decoded-…
angwarrenn Jul 21, 2022
626c122
updated
angwarrenn Jul 21, 2022
4a238b2
clean up
angwarrenn Jul 21, 2022
7188dfe
update
Jul 27, 2022
d029d2a
init
angwarrenn Aug 2, 2022
325ebb6
workbook
angwarrenn Aug 6, 2022
083f87e
Merge branch 'main'
Ry-DS Aug 15, 2022
dbb4c10
Make cogs work
Ry-DS Aug 15, 2022
eddf1b3
Delete redundant notebook
Ry-DS Aug 15, 2022
0e1771e
make things work again
Ry-DS Aug 17, 2022
798672f
fixed refactoring error
angwarrenn Aug 17, 2022
29100ef
fixed refactoring error
angwarrenn Aug 17, 2022
aa8238a
Make thing work with main2.py
Ry-DS Aug 17, 2022
3c55de3
fix env
Ry-DS Aug 17, 2022
692c595
Merge branch 'main' into tictactoe
Ry-DS Aug 29, 2022
636ad1a
Move to w4
Ry-DS Sep 4, 2022
ba61606
Merge branch 'main' into tictactoe
Ry-DS Sep 4, 2022
de657b0
workbook changes
angwarrenn Sep 6, 2022
c833985
minor changes
Sep 6, 2022
bf2420f
Merge branch 'tictactoe' of https://github.com/HackMelbourne/Decoded-…
Sep 6, 2022
7274911
example tictactoe
Sep 6, 2022
794e1a5
cleaned up branch
angwarrenn Sep 6, 2022
dd4232c
cleaned up codebase
angwarrenn Sep 6, 2022
98780e0
updated workbook
angwarrenn Sep 6, 2022
7038a42
updated workbook
angwarrenn Sep 6, 2022
3e24690
updated table of contents
angwarrenn Sep 7, 2022
1e070e2
added requirements.txt
angwarrenn Sep 14, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@
.idea/

*.pyc

__pycache__/
/logs/
45 changes: 45 additions & 0 deletions bot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import json
import os

import discord
from discord.ext import commands
from dotenv import load_dotenv

load_dotenv()
# load environment variables
TOKEN = os.getenv("TOKEN")
GUILDS = os.getenv("GUILDS")
# convert guilds from .env file to discord.py guild objects
GUILDS = json.loads(GUILDS)
GUILDS = [discord.Object(id=guild) for guild in GUILDS]


class Bot(commands.Bot):
# constructor to initialize object
def __init__(self, token, guild_list):
# call parent constructor
super().__init__(command_prefix="",
intents=discord.Intents.default())
# store bot token
self.token = token
# store guild list
self.guild_list = guild_list

# load tictactoe game engine COG extension
async def setup_hook(self):
await self.load_extension(f"cogs.tictactoe")

# manually sync slash commands to each guild specified in .env file
for guild in self.guilds:
await self.tree.sync(guild=guild)

# print message when the bot is finished initializing and is
# ready to be used
async def on_ready(self):
print("ready...")


# initialize bot with TOKEN and GUILDS from .env file
bot = Bot(TOKEN, GUILDS)
# run bot with its token
bot.run(bot.token)
22 changes: 0 additions & 22 deletions cogless.py

This file was deleted.

18 changes: 0 additions & 18 deletions cogs/hello.py

This file was deleted.

138 changes: 138 additions & 0 deletions cogs/tictactoe.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
import discord
import numpy as np
from discord import app_commands
from discord.ext import commands

SIZE = 3


class TicTacToeButton(discord.ui.Button):
def __init__(self, row, col):
super().__init__(style=discord.ButtonStyle.secondary, label=" ",
row=row)
self.row = row
self.col = col

async def callback(self, interaction):
if self.view.current_player == self.view.player_1:
if interaction.user != self.view.player_1:
await interaction.response.send_message("Its not your Turn!",
ephemeral=True)
return

self.style = discord.ButtonStyle.danger
self.label = 'X'
self.disabled = True
self.view.board[self.row][self.col] = 1
self.view.current_player = self.view.player_2
content = f":game_die: `{self.view.player_1.display_name}` **VS** `{self.view.player_2.display_name}`\n\n{self.view.player_2.mention}, select your move:"

else:
if interaction.user != self.view.player_2:
await interaction.response.send_message("Its not your Turn!",
ephemeral=True)
return

self.style = discord.ButtonStyle.success
self.label = 'O'
self.disabled = True
self.view.board[self.row][self.col] = -1
self.view.current_player = self.view.player_1
content = f":game_die: `{self.view.player_1.display_name}` **VS** `{self.view.player_2.display_name}`\n\n{self.view.player_1.mention}, select your move:"

winner = self.view.check_winner()
if winner == self.view.player_1:
content = f":tada: {self.view.player_1.mention} has won the game!"
elif winner == self.view.player_2:
content = f":tada: {self.view.player_2.mention} has won the game!"
elif winner == "tie":
content = "No one won the game, it's a tie! Try again?"

await interaction.response.edit_message(content=content, view=self.view)


class TicTacToeView(discord.ui.View):
def __init__(self, player_1, player_2):
super().__init__()
self.player_1 = player_1
self.player_2 = player_2
self.current_player = self.player_1
self.board = np.zeros((SIZE, SIZE))

for row in range(SIZE):
for col in range(SIZE):
self.add_item(TicTacToeButton(row, col))

def check_winner(self):
col_sums = np.sum(self.board, axis=1)
for col_sum in col_sums:
if col_sum == SIZE:
return self.player_1
elif col_sum == -SIZE:
return self.player_2

row_sums = np.sum(self.board, axis=0)
for row_sum in row_sums:
if row_sum == SIZE:
return self.player_1
elif row_sum == -SIZE:
return self.player_2

diag_sum = self.board[0, 0] + self.board[1, 1] + self.board[2, 2]
if diag_sum == SIZE:
return self.player_1
elif diag_sum == -SIZE:
return self.player_2

diag_sum = self.board[0, 2] + self.board[1, 1] + self.board[2, 0]
if diag_sum == SIZE:
return self.player_1
elif diag_sum == -SIZE:
return self.player_2

if np.all(self.board):
return "tie"


class TicTacToe(commands.Cog):
# constructor to store bot object
def __init__(self, bot):
self.bot = bot

# specify slash command name and description
@app_commands.command(
name="tictactoe",
description="Play a game of TicTacToe"
)
# specify slash command parameter description
@app_commands.describe(
opponent="User you want to play with"
)
# function is run every time a slash command is issued
# specify that the slash command parameter will be a discord member
async def tictactoe(self, interaction, opponent: discord.Member):
# player_1 is the user that issued the slash command
player_1 = interaction.user
# player_2 is the opponent specified in the slash command
player_2 = opponent

# make sure user does not challenger itself or the bot
if player_1 == player_2 or player_2.bot:
await interaction.response.send_message(
"you cannot challenge that user")
else:
# acknowledge command by responding with a message and sending a
# tictactoe board view
await interaction.response.send_message(
f":game_die: `{player_1.display_name}` **VS** `{player_2.display_name}`\n\n"
f"{player_1.mention}, select your move:",
view=TicTacToeView(player_1, player_2))


async def setup(bot):
# add COG extension to calling bot
await bot.add_cog(
TicTacToe(bot),
# calling bot's guild list
guilds=bot.guild_list
)
17 changes: 0 additions & 17 deletions main.py

This file was deleted.

12 changes: 12 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
aiohttp==3.8.1
aiosignal==1.2.0
async-timeout==4.0.2
attrs==21.4.0
charset-normalizer==2.1.0
discord.py==2.0.1
frozenlist==1.3.0
idna==3.3
multidict==6.0.2
numpy==1.23.1
python-dotenv==0.20.0
yarl==1.7.2
Loading