checkWinner没有定义



如果有人能帮助我,那就太好了,关于这个问题的信息在代码下面。

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.command(aliases=['tictactoe', 'ttt'])
async def tic_tac_toe(self, ctx: commands.Context, p1 : discord.Member, p2 :discord.Member):
global player1
global player2
global turn
global gameOver
global count
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

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]
num = random.randint(1, 2)
if num == 1:
turn = player1
await ctx.send("It is <@" + str(player1.id) + ">'s turn")
elif num == 2:
turn = player2
await ctx.send("It is <@" + str(player2.id) + ">'s turn")
else:
await ctx.send("A game is already in progress! Finish it before starting a new one.")

@commands.command()
async def place(self, ctx: commands.Context, 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 + " wins!")
elif count >= 9:
gameOver = True
await ctx.send("It's a tie!")


if turn == player1:
turn = player2
elif turn == player2:
turn = player1
else:
await ctx.send("Be sure to choose an integer between 1 and 9 (inclusive) and an unmarked tile.")
else:
await ctx.send("It is not your turn.")
else:
await ctx.send("Please start a new game using the !tictactoe command.")
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
@place.error
async def place_error(ctx, error):
if isinstance(error, commands.MissingRequiredArgument):
await ctx.send("Please enter a position you would like to mark.")
elif isinstance(error, commands.BadArgument):
await ctx.send("Please make sure to enter an integer.")
@tic_tac_toe.error
async def tictactoe_error(ctx, error):
print(error)
if isinstance(error, commands.MissingRequiredArgument):
await ctx.send("Please mention 2 players for this command.")
elif isinstance(error, commands.BadArgument):
await ctx.send("Please make sure to mention/ping players (ie. <@688534433879556134>).")

在第34行"checkWinner"没有定义但我试了一些方法,还是不行。任何想法。这是一个掷骰子游戏,我把它放在一个齿轮里。齿轮在名为help的类中。这段代码是python。

这是因为函数调用在函数本身之上,Python向下读取代码,当它调用checkWinner时,它还没有看到那个函数。

你可以将函数移到页面顶部

或者更优雅的解决方案是使用大多数你可以使用的功能,并使用它作为页面的底部,它调用页面顶部的函数。

if __name__ == "__main__":
# do something here, call a function etc

Python从上到下读取代码,因此为了使用函数,您必须在使用它之前垂直定义它。
您需要在使用Check Winner之前定义它。
def checkWinner()移到使用它的函数前

最新更新