Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何向tictactoe discord BOT(discord.py)添加end函数_Discord.py_Tic Tac Toe - Fatal编程技术网

如何向tictactoe discord BOT(discord.py)添加end函数

如何向tictactoe discord BOT(discord.py)添加end函数,discord.py,tic-tac-toe,Discord.py,Tic Tac Toe,我的Tictaoe机器人只有在玩家获胜后才能结束游戏。我需要向机器人添加一个函数,以便在运行命令时结束游戏。 如何将end函数添加到此BOT? 例如:游戏正在进行中,我想在两者之间结束游戏。当我运行命令时,在本例中为-end,我需要机器人停止游戏 import discord from discord.ext import commands import random import urllib.request import re import youtube_dl import os impo

我的Tictaoe机器人只有在玩家获胜后才能结束游戏。我需要向机器人添加一个函数,以便在运行命令时结束游戏。 如何将end函数添加到此BOT? 例如:游戏正在进行中,我想在两者之间结束游戏。当我运行命令时,在本例中为-end,我需要机器人停止游戏

import discord
from discord.ext import commands
import random
import urllib.request
import re
import youtube_dl
import os
import requests

client = commands.Bot(command_prefix= "-")
@client.event
async def on_ready():
    print("READY TO PLAY TICTACTOE!!")
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]
]

@client.command()
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
            myEmbed = discord.Embed(title= "GAME IN PROGRESS",description="IT IS <@" + str(player1.id) + ">'s TURN.",color=0xe74c3c)
            await ctx.send(embed=myEmbed)
        elif num == 2:
            turn = player2
            myEmbed = discord.Embed(title= "GAME IN PROGRESS",description="IT IS <@" + str(player2.id) + ">'s TURN.",color=0xe74c3c)
            await ctx.send(embed=myEmbed)
    else:
        myEmbed = discord.Embed(title= "GAME IN PROGRESS",description="A GAME IS STILL IN PROGRESS. FINISH IT BEFORE STARTING A NEW ONE",color=0xe74c3c)
        await ctx.send(embed=myEmbed)

@client.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:
                    myEmbed = discord.Embed(title= "WINNER!",description=mark + " :crown: ",color=0xf1c40f)
                    await ctx.send(embed=myEmbed)
                elif count >= 9:
                    gameOver = True
                    myEmbed = discord.Embed(title= "TIE",description="IT'S A TIE :handshake:",color=0xf1c40f)
                    await ctx.send(embed=myEmbed)

                # switch turns
                if turn == player1:
                    turn = player2
                elif turn == player2:
                    turn = player1
            else:
                myEmbed = discord.Embed(title= "PLACE ERROR!",description="BE SURE TO CHOOSE AN INTEGER BETWEEN 1 AND 9 (INCLUSIVE) AND AN UNMARKED TILE. ",color=0xe74c3c)
                await ctx.send(embed=myEmbed)
        else:
            myEmbed = discord.Embed(title= "TURN ERROR!",description="IT'S NOT YOUR TURN",color=0xe74c3c)
            await ctx.send(embed=myEmbed)
    else:
        myEmbed = discord.Embed(title= "START GAME",description="TO START A NEW GAME, USE -tictactoe COMMAND",color=0x2ecc71)
        await ctx.send(embed=myEmbed)


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):
        myEmbed = discord.Embed(title= "MENTION ERROR!",description="PLEASE MENTION 2 USERS",color=0xe74c3c)
        await ctx.send(embed=myEmbed)
    elif isinstance(error, commands.BadArgument):
        myEmbed = discord.Embed(title= "ERROR!",description="PLEASE MAKE SURE TO MENTION/PING PLAYERS (ie. <@688534433879556134>)",color=0xe74c3c)
        await ctx.send(embed=myEmbed)

@place.error
async def place_error(ctx, error):
    if isinstance(error, commands.MissingRequiredArgument):
        myEmbed = discord.Embed(title= "NO POSITION",description="PLEASE ENTER A POSITION TO MARK",color=0xe74c3c)
        await ctx.send(embed=myEmbed)
    elif isinstance(error, commands.BadArgument):
        myEmbed = discord.Embed(title= "INTEGER ERROR!",description="PLEASE MAKE SURE IT'S AN INTEGER",color=0xe74c3c)
        await ctx.send(embed=myEmbed)
client.run("")
导入不一致
从discord.ext导入命令
随机输入
导入urllib.request
进口稀土
导入youtube\u dl
导入操作系统
导入请求
client=commands.Bot(command_prefix=“-”)
@客户端事件
_ready()上的异步定义:
打印(“准备好玩Tictatcoe!!”)
player1=“”
player2=“”
turn=“”
gameOver=True
董事会=[]
获胜条件=[
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6]
]
@client.command()
异步定义tictactoe(ctx,p1:discord.Member,p2:discord.Member):
全局计数
全球玩家1
全球玩家2
全球转向
全球游戏结束
如果游戏结束:
全球董事会
board=[“:白色大方形:”,“:白色大方形:”,“:白色大方形:”,
“:白色大方形:”,“:白色大方形:”,“:白色大方形:”,
“:白色大方形:”,“:白色大方形:”,“:白色大方形:”]
turn=“”
gameOver=False
计数=0
player1=p1
player2=p2
#打印电路板
line=“”
对于范围内的x(透镜(板)):
如果x==2或x==5或x==8:
线路+=“”+线路板[x]
等待ctx发送(线路)
line=“”
其他:
线路+=“”+线路板[x]
#决定谁先走
num=random.randint(1,2)
如果num==1:
旋转=播放者1
myEmbed=discord.Embed(title=“游戏进行中”,description=“该轮到你了”,color=0xe74c3c)
等待ctx.send(嵌入=myEmbed)
elif num==2:
回合=玩家2
myEmbed=discord.Embed(title=“游戏进行中”,description=“该轮到你了”,color=0xe74c3c)
等待ctx.send(嵌入=myEmbed)
其他:
myEmbed=discord.Embed(title=“游戏进行中”,description=“游戏仍在进行中。在开始新游戏之前完成它”,color=0xe74c3c)
等待ctx.send(嵌入=myEmbed)
@client.command()
异步def位置(ctx,位置:int):
全球转向
全球玩家1
全球玩家2
全球董事会
全局计数
全球游戏结束
如果没有结束:
mark=“”
如果turn==ctx.author:
如果回合==玩家1:
mark=“:区域_指示器_x:”
elif turn==播放机2:
mark=“:o2:”
如果0=9:
gameOver=True
myEmbed=discord.Embed(title=“TIE”,description=“这是领带:握手:”,color=0xf1c40f)
等待ctx.send(嵌入=myEmbed)
#开关转动
如果回合==玩家1:
回合=玩家2
elif turn==播放机2:
旋转=播放者1
其他:
myEmbed=discord.Embed(title=“PLACE ERROR!”,description=“确保选择1到9之间的整数(包括1到9)和未标记的磁贴。”,color=0xe74c3c)
等待ctx.send(嵌入=myEmbed)
其他:
myEmbed=discord.Embed(title=“TURN ERROR!”,description=“该轮到你了”,color=0xe74c3c)
等待ctx.send(嵌入=myEmbed)
其他:
myEmbed=discord.Embed(title=“开始游戏”,description=“要开始新游戏,请使用-tictactoe命令”,color=0x2EC71)
等待ctx.send(嵌入=myEmbed)
def checkWinner(赢取条件、标记):
全球游戏结束
对于Winning Conditions中的条件:
如果线路板[条件[0]==标记和线路板[条件[1]]==标记和线路板[条件[2]]==标记:
gameOver=True
@蒂克塔克托错误
异步def tictactoe_错误(ctx,错误):
打印(错误)
如果isinstance(错误,commands.MissingRequiredArgument):
myEmbed=discord.Embed(title=“提及错误!”,description=“请提及2个用户”,color=0xe74c3c)
等待ctx.send(嵌入=myEmbed)
elif isinstance(错误,commands.BadArgument):
myEmbed=discord.Embed(title=“ERROR!”,description=“请确保提及/PING播放器(即)”,color=0xe74c3c)
等待ctx.send(嵌入=myEmbed)
@地点错误
异步定义放置错误(ctx,错误):
如果isinstance(错误,commands.MissingRequiredArgument):
myEmbed=discord.Embed(title=“无位置”,description=“请输入要标记的位置”,color=0xe74c3c)
等待ctx.send(嵌入=myEmbed)
elif isinstance(错误,commands.BadArgument):
myEmbed=discord.Embed(title=“INTEGER ERROR!”,description=“请确保它是一个整数”,color=0xe74c3c)
等待ctx.send(嵌入=myEmbed)
client.run(“”)

我强烈建议您不要使用全局变量,因为它们非常危险,只能用作只读变量,您只需将这些变量作为参数传递到函数中即可

无论哪种方式,如果你想重新设定你的游戏,评判你