Я пытаюсь добавить свой бот ti c ta c toe в мой диск-бот, но я получаю эту ошибку:
File "main.py", line 110
await ctx.send("\n" + board['top-l'] + '|' + board['top-m'] + '|' + board['top-r'])
^
SyntaxError: 'await' outside async function
Я преобразовал все операторы print
в await ctx.send
и input
утверждений типа:
goAgain = input("Do you want to play again?\n\n").lower().startswith('y')
в
await ctx.send('Do you want to play again?\n\n')
goAgain = await client.wait_for('message', check=check).lower().startswith('y')
Это способ сделать это? Мой код на данный момент для пальца ноги ti c ta c:
@bot.command()
async def playgame(ctx):
await ctx.send("Lets play a game!")
def checkWin(state, letter):
return ((state['top-l'] == letter and state['top-m'] == letter and state['top-r'] == letter) or (state['mid-l'] == letter and state['mid-m'] == letter) and (state['mid-r'] == letter) or (state['low-l'] == letter and state['low-m'] == letter and state['low-r'] == letter) or (state['top-l'] == letter and state['mid-l'] == letter and state['low-l'] == letter) or (state['top-m'] == letter and state['mid-m'] == letter and state['low-m'] == letter) or (state['top-r'] == letter and state['mid-r'] == letter and state['low-r'] == letter) or (state['top-l'] == letter and state['mid-m'] == letter and state['low-r'] == letter) or (state['low-l'] == letter and state['mid-m'] == letter and state['top-r'] == letter))
def drawBoard(board):
await ctx.send("\n" + board['top-l'] + '|' + board['top-m'] + '|' + board['top-r'])
await ctx.send("-----")
await ctx.send(board['mid-l'] + '|' + board['mid-m'] + '|' + board['mid-r'])
await ctx.send("-----")
await ctx.send(board['low-l'] + '|' + board['low-m'] + '|' + board['low-r'] + "\n\n")
def isSpaceFree(move):
if board[move] == ' ':
return True
else:
return False
def inputPlayerLetter():
global computerLetter; global playerLetter
letter = ''
while not (letter == 'X' or letter == 'O'):
await ctx.send('Which letter do you want to be? (X or O)\n\n')
letter = await client.wait_for('message', check=check).upper()
#letter = input('Which letter do you want to be? (X or O)\n\n').upper()
if letter == 'X':
return ['X', 'O']
elif letter == 'O':
return ['O', 'X']
def whoGoesFirst():
if random.randint(0,1) == 0:
return 'player'
else:
return 'computer'
def isBoardFull():
freeSpaces = []
for i in 'top-L,top-M,top-R,mid-L,mid-M,mid-R,low-L,low-M,low-R'.lower().split(','):
if isSpaceFree(i):
return False
else:
freeSpaces.append(False)
if freeSpaces == [False,False,False,False,False,False,False,False,False]:
return True
def playAgain():
#goAgain = input("Do you want to play again?\n\n").lower().startswith('y')
await ctx.send('Do you want to play again?\n\n')
goAgain = await client.wait_for('message', check=check).lower().startswith('y')
await ctx.send("\n--------------------------------------------\n")
return goAgain
def playerMove():
while True == True:
#move = input("Where do you want to go?\n(top-L, top-M, top-R, mid-L, mid-M, mid-R, low-L, low-M, low-R) \n\n").lower()
await ctx.send('Where do you want to go?\n(top-L, top-M, top-R, mid-L, mid-M, mid-R, low-L, low-M, low-R) \n\n')
move = await client.wait_for('message', check=check).lower()
try:
if isSpaceFree(move):
board[move] = playerLetter
break
else:
await ctx.send("\nThat space is not free.\n")
except KeyError:
await ctx.send("\nThat is not an option.\n")
def computerMove():
for i in 'top-L,top-M,top-R,mid-L,mid-M,mid-R,low-L,low-M,low-R'.lower().split(','):
dupe = copy.copy(board)
if isSpaceFree(i):
dupe[i] = computerLetter
if checkWin(dupe, computerLetter):
await ctx.send("Computers move: " + i)
return i
else:
pass
for i in 'top-L,top-M,top-R,mid-L,mid-M,mid-R,low-L,low-M,low-R'.lower().split(','):
dupe = copy.copy(board)
if isSpaceFree(i):
dupe[i] = playerLetter
if checkWin(dupe, playerLetter):
await ctx.send("Computer's move: " + i)
return i
else:
pass
for i in 'top-L,top-R,low-L,low-R'.lower().split(','):
if isSpaceFree(i):
await ctx.send("Computer's move: " + i)
return i
for i in 'top-M,mid-L,mid-M,mid-R,low-M,low-R'.lower().split(','):
if isSpaceFree(i):
await ctx.send("Computer's move: " + i)
return i
print ("Welcome to Noughts and Crosses!")
while True:
board = {'top-l' : ' ', 'top-m' : ' ', 'top-r' : ' ', 'mid-l' : ' ', 'mid-m' : ' ', 'mid-r' : ' ', 'low-l' : ' ', 'low-m' : ' ', 'low-r' : ' '}
playerLetter, computerLetter = inputPlayerLetter()
await ctx.send("\n--------------------------------------------\n")
turn = whoGoesFirst()
drawBoard(board)
await ctx.send("The " + turn + " will go first.\n")
gameIsPlaying = True
while gameIsPlaying:
if turn == 'player':
playerMove()
if checkWin(board, playerLetter):
drawBoard(board)
await ctx.send("The " + turn + " wins!")
await ctx.send("\n--------------------------------------------\n")
if playAgain():
gameIsPlaying = False
break
else:
await ctx.send("Thanks for playing!")
break
if turn == 'computer':
board[computerMove()] = computerLetter
time.sleep(0.5)
if checkWin(board, computerLetter):
drawBoard(board)
await ctx.send("The " + turn + " wins!")
await ctx.send("\n--------------------------------------------\n")
if playAgain():
gameIsPlaying = False
break
else:
await ctx.send("Thanks for playing!")
break
drawBoard(board)
if isBoardFull():
await ctx.send("It's a tie!")
await ctx.send("\n--------------------------------------------\n")
if playAgain():
gameIsPlaying = False
break
else:
await ctx.send("Thanks for playing!")
break
if turn == 'player':
turn = 'computer'
elif turn == 'computer':
turn = 'player'
time.sleep(0.1)
await ctx.send(turn.capitalize() + "'s turn next!\n")
time.sleep(0.5)
Это потому, что я использовал функции в своем коде? Как мне сделать это лучше, чтобы я не получил ошибку?