If anyone can help me that would be great the information about the issue is below the code.
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>).")
This is in a cog and for line 34 "checkWinner" is not defined but I've tried a few things and I cant get it to work. Any ideas. This is a tictac toe game and Im putting it in a cog. The cog is in a class with the name help. This code is python.
CodePudding user response:
Python reads code top to bottom, so in order to use a function, you must define it vertically before it being used.
You need to define Check Winner before the usage.
Move def checkWinner()
before the function in which it is being used
CodePudding user response:
It's because the function call is above the function itself, Python reads down the code and when it calls checkWinner, It has yet to see that function.
You can either move the function to the top of the page.
or the more elegant solution is to function most things you can and use this as the bottom of the page which calls to a function at the top of the page.
if __name__ == "__main__":
# do something here, call a function etc