Помогите адаптировать код под cogs(discord.py)
import discord
from discord.ext import commands
import random
class game(commands.Cog):
def __init__(self, Bot):
self.Bot = Bot
player1 = ""
player2 = ""
turn = ""
gameOver = True
board = []
winningConditions = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6]
]
@commands.commands()
async def tictactoe(ctx, p1: discord.Member, p2: discord.Member):
global count
global player1
global player2
global turn
global gameOver
if gameOver:
global board
board = [":white_large_square:", ":white_large_square:", ":white_large_square:",
":white_large_square:", ":white_large_square:", ":white_large_square:",
":white_large_square:", ":white_large_square:", ":white_large_square:"]
turn = ""
gameOver = False
count = 0
player1 = p1
player2 = p2
# print the board
line = ""
for x in range(len(board)):
if x == 2 or x == 5 or x == 8:
line += " " + board[x]
await ctx.send(line)
line = ""
else:
line += " " + board[x]
# determine who goes first
num = random.randint(1, 2)
if num == 1:
turn = player1
await ctx.send("Это ход <@" + str(player1.id) + ">")
elif num == 2:
turn = player2
await ctx.send("Это ход <@" + str(player2.id) + ">")
else:
await ctx.send("Игра уже идет полным ходом! Закончите его, прежде чем начинать новый.")
@commands.command()
async def place(ctx, pos: int):
global turn
global player1
global player2
global board
global count
global gameOver
if not gameOver:
mark = ""
if turn == ctx.author:
if turn == player1:
mark = ":regional_indicator_x:"
elif turn == player2:
mark = ":o2:"
if 0 < pos < 10 and board[pos - 1] == ":white_large_square:" :
board[pos - 1] = mark
count += 1
# print the board
line = ""
for x in range(len(board)):
if x == 2 or x == 5 or x == 8:
line += " " + board[x]
await ctx.send(line)
line = ""
else:
line += " " + board[x]
checkWinner(winningConditions, mark)
print(count)
if gameOver == True:
await ctx.send(mark + " победили!")
elif count >= 9:
gameOver = True
await ctx.send("Это ничья!")
# switch turns
if turn == player1:
turn = player2
elif turn == player2:
turn = player1
else:
await ctx.send("Обязательно выберите целое число от 1 до 9 (включительно)")
else:
await ctx.send("Это не ваш ход.")
else:
await ctx.send("Пожалуйста, начните новую игру, используя команду !tictactoe @pl1, @pl2.")
def checkWinner(winningConditions, mark):
global gameOver
for condition in winningConditions:
if board[condition[0]] == mark and board[condition[1]] == mark and board[condition[2]] == mark:
gameOver = True
@tictactoe.error
async def tictactoe_error(ctx, error):
print(error)
if isinstance(error, commands.MissingRequiredArgument):
await ctx.send("Пожалуйста, отметье двух игроков для этой команды..")
elif isinstance(error, commands.BadArgument):
await ctx.send("Пожалуйста, не забудьте упомянуть игрока. (ie. <@688534433879556134>).")
@place.error
async def place_error(ctx, error):
if isinstance(error, commands.MissingRequiredArgument):
await ctx.send("Пожалуйста, введите позицию.")
elif isinstance(error, commands.BadArgument):
await ctx.send("Пожалуйста, введите целое число.")
def setup(Bot):
Bot.add_cog(game(Bot))
Ответы (1 шт):
Автор решения: zrx
→ Ссылка
import discord
from discord.ext import commands
import random
class game(commands.Cog):
def __init__(self, Bot):
self.Bot = Bot
self.player1 = ""
self.player2 = ""
self.turn = ""
self.gameOver = True
self.board = []
self.winningConditions = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6]
]
def checkWinner(self, winningConditions, mark):
for condition in winningConditions:
if self.board[condition[0]] == mark and self.board[condition[1]] == mark and self.board[condition[2]] == mark:
self.gameOver = True
@commands.commands()
async def tictactoe(self, ctx, p1:discord.Member, p2:discord.Member):
if self.gameOver:
self.board = [
":white_large_square:",
":white_large_square:",
":white_large_square:",
":white_large_square:",
":white_large_square:",
":white_large_square:",
":white_large_square:",
":white_large_square:",
":white_large_square:"
]
self.turn = ""
self.gameOver = False
self.count = 0
self.player1 = p1
self.player2 = p2
# print the board
line = ""
for x in range(len(self.board)):
if x == 2 or x == 5 or x == 8:
line += " " + self.board[x]
await ctx.send(line)
line = ""
else:
line += " " + self.board[x]
# determine who goes first
num = random.randint(1, 2)
if num == 1:
self.turn = player1
await ctx.send("Это ход <@" + str(player1.id) + ">")
elif num == 2:
self.turn = player2
await ctx.send("Это ход <@" + str(player2.id) + ">")
else:
await ctx.send("Игра уже идет полным ходом! Закончите его, прежде чем начинать новый.")
@commands.command()
async def place(self, ctx, pos:int):
if not self.gameOver:
mark = ""
if self.turn == ctx.author:
if self.turn == self.player1:
mark = ":regional_indicator_x:"
elif self.turn == self.player2:
mark = ":o2:"
if 0 < pos < 10 and self.board[pos - 1] == ":white_large_square:" :
self.board[pos - 1] = mark
self.count += 1
# print the board
line = ""
for x in range(len(self.board)):
if x == 2 or x == 5 or x == 8:
line += " " + self.board[x]
await ctx.send(line)
line = ""
else:
line += " " + self.board[x]
self.checkWinner(self.winningConditions, mark)
print(self.count)
if self.gameOver == True:
await ctx.send(mark + " победили!")
elif self.ount >= 9:
self.gameOver = True
await ctx.send("Это ничья!")
# switch turns
if self.turn == self.player1:
self.turn = self.player2
elif self.turn == self.player2:
turn = self.player1
else:
await ctx.send("Обязательно выберите целое число от 1 до 9 (включительно)")
else:
await ctx.send("Это не ваш ход.")
else:
await ctx.send("Пожалуйста, начните новую игру, используя команду !tictactoe @pl1, @pl2.")
@tictactoe.error
async def tictactoe_error(self, ctx, error):
print(error)
if isinstance(error, commands.MissingRequiredArgument):
await ctx.send("Пожалуйста, отметье двух игроков для этой команды..")
elif isinstance(error, commands.BadArgument):
await ctx.send("Пожалуйста, не забудьте упомянуть игрока. (ie. <@688534433879556134>).")
@place.error
async def place_error(self, ctx, error):
if isinstance(error, commands.MissingRequiredArgument):
await ctx.send("Пожалуйста, введите позицию.")
elif isinstance(error, commands.BadArgument):
await ctx.send("Пожалуйста, введите целое число.")
def setup(Bot):
Bot.add_cog(game(Bot))
Я просто адаптировал Ваш код под ког, поэтому не могу гарантировать, что он будет работать.