Python 中的井字游戏计算机,语法无效问题
Tic-tac-toe computer in Python, invalid syntax problem
我才刚刚开始学习 Python。有人告诉我这将是一个很好的初学者项目。但是我 运行 在第 32 行遇到了一个无效的语法问题。我没有得到任何细节,但直到我在第 7 行创建列表时问题才开始出现。我之前的尝试是从第 12 行到第 20 行以及第 25 行到第 28 行,打印得很好。但这只是为了在可见板上放置移动。我仍然需要一种方法来跟踪该方块是否被占用,以及它如何影响计算机对未占用方块的偏好。不过我好像哪里做错了。
就连第31行和第32行的语法高亮都不一样,所以不管是什么问题,都是在第32行触发的(下图)。有人可以帮我理解这个问题吗?
import copy
def main():
# Board squares list. String is either blank, O, or X. Int is adjusted bias.
# Bool is whether or not the square is occupied, affects bias int.
sq = [[" ", 0, False], [" ", 0, False], [" ", 0, False],
[" ", 0, False], [" ", 0, False], [" ", 0, False],
[" ", 0, False], [" ", 0, False], [" ", 0, False]]
# DEBUG: Attempting to condense square attributes into a list.
#sq_1 = " "
#sq_2 = " "
#sq_3 = " "
#sq_4 = " "
#sq_5 = " "
#sq_6 = " "
#sq_7 = " "
#sq_8 = " "
#sq_9 = " "
# Board print, blank spaces and lines.
board_blank = [" | | " + "\n", "___|___|___" + "\n"]
# DEBUG: Attempting to read squares from new list
#board1 = " " + sq_1 + " | " + sq_2 + " | " + sq_3 + " " + "\n"
#board2 = " " + sq_4 + " | " + sq_5 + " | " + sq_6 + " " + "\n"
#board3 = " " + sq_7 + " | " + sq_8 + " | " + sq_9 + " " + "\n"
# Board print, updates with occupied squares after each move
board1 = " " + sq[0][0] + " | " + sq[1][0] + " | " + sq[2[0] + " " + "\n"
board2 = " " + sq[3][0] + " | " + sq[4][0] + " | " + sq[5][0] + " " + "\n"
board3 = " " + sq[6][0] + " | " + sq[7][0] + " | " + sq[8][0] + " " + "\n"
# TEST: print board
print_board(board1, board2, board3, board_blank)
# Board printing function
def print_board(board1, board2, board3, board_blank):
print((" " + board_blank[0]), board1, board_blank[1], board_blank[0], board2,
board_blank[1], board_blank[0], board3, board_blank[0])
# Acquire player's next move
def player_move():
pass
# Calculate and execute computer's move
def comp_move():
pass
main()
你在这一行中有语法错误
board1 = " " + sq[0][0] + " | " + sq[1][0] + " | " + sq[2[0] + " " + "\n"
# should be
board1 = " " + sq[0][0] + " | " + sq[1][0] + " | " + sq[2][0] + " " + "\n"
^
您在第 31 行中缺少右方括号:
board1 = " " + sq[0][0] + " | " + sq[1][0] + " | " + sq[2[0] + " " + "\n"
^
用以下内容替换该行:
board1 = " " + sq[0][0] + " | " + sq[1][0] + " | " + sq[2][0] + " " + "\n"
^
我才刚刚开始学习 Python。有人告诉我这将是一个很好的初学者项目。但是我 运行 在第 32 行遇到了一个无效的语法问题。我没有得到任何细节,但直到我在第 7 行创建列表时问题才开始出现。我之前的尝试是从第 12 行到第 20 行以及第 25 行到第 28 行,打印得很好。但这只是为了在可见板上放置移动。我仍然需要一种方法来跟踪该方块是否被占用,以及它如何影响计算机对未占用方块的偏好。不过我好像哪里做错了。
就连第31行和第32行的语法高亮都不一样,所以不管是什么问题,都是在第32行触发的(下图)。有人可以帮我理解这个问题吗?
import copy
def main():
# Board squares list. String is either blank, O, or X. Int is adjusted bias.
# Bool is whether or not the square is occupied, affects bias int.
sq = [[" ", 0, False], [" ", 0, False], [" ", 0, False],
[" ", 0, False], [" ", 0, False], [" ", 0, False],
[" ", 0, False], [" ", 0, False], [" ", 0, False]]
# DEBUG: Attempting to condense square attributes into a list.
#sq_1 = " "
#sq_2 = " "
#sq_3 = " "
#sq_4 = " "
#sq_5 = " "
#sq_6 = " "
#sq_7 = " "
#sq_8 = " "
#sq_9 = " "
# Board print, blank spaces and lines.
board_blank = [" | | " + "\n", "___|___|___" + "\n"]
# DEBUG: Attempting to read squares from new list
#board1 = " " + sq_1 + " | " + sq_2 + " | " + sq_3 + " " + "\n"
#board2 = " " + sq_4 + " | " + sq_5 + " | " + sq_6 + " " + "\n"
#board3 = " " + sq_7 + " | " + sq_8 + " | " + sq_9 + " " + "\n"
# Board print, updates with occupied squares after each move
board1 = " " + sq[0][0] + " | " + sq[1][0] + " | " + sq[2[0] + " " + "\n"
board2 = " " + sq[3][0] + " | " + sq[4][0] + " | " + sq[5][0] + " " + "\n"
board3 = " " + sq[6][0] + " | " + sq[7][0] + " | " + sq[8][0] + " " + "\n"
# TEST: print board
print_board(board1, board2, board3, board_blank)
# Board printing function
def print_board(board1, board2, board3, board_blank):
print((" " + board_blank[0]), board1, board_blank[1], board_blank[0], board2,
board_blank[1], board_blank[0], board3, board_blank[0])
# Acquire player's next move
def player_move():
pass
# Calculate and execute computer's move
def comp_move():
pass
main()
你在这一行中有语法错误
board1 = " " + sq[0][0] + " | " + sq[1][0] + " | " + sq[2[0] + " " + "\n"
# should be
board1 = " " + sq[0][0] + " | " + sq[1][0] + " | " + sq[2][0] + " " + "\n"
^
您在第 31 行中缺少右方括号:
board1 = " " + sq[0][0] + " | " + sq[1][0] + " | " + sq[2[0] + " " + "\n"
^
用以下内容替换该行:
board1 = " " + sq[0][0] + " | " + sq[1][0] + " | " + sq[2][0] + " " + "\n"
^