我的功能不是';我不工作,我也不';我不知道为什么。python p>所以我有一个棋盘,它有9个位置和6个棋子,3个 [X]和3个代码> []/Cord>(你也可以考虑[])一个棋子,但是它代表一个空的位置)。玩家可以选择将棋子移动到任何他想移动的位置。在下面的棋盘上,“X”(这就是我们如何表示棋子的方式,与棋盘上的棋子一样,但没有[])必须移动,但他没有地方去。我想要一个函数,看看一个球员是否没有合法的运动,如果是这样,它返回真的。以下是帮助功能: #pc stands for piece, brd for board and pos for position board = ["[X]","[X]","[X]", "[O]","[O]","[O]", "[ ]","[ ]","[ ]"] def near_pos(pos): #positions are represented 1-9 and this functions gives the near positions of a position if pos== 1: return (2,4,5) elif pos== 2: return (1,3,4,5,6) elif pos== 3: return (2,5,6) elif pos== 4: return (1,2,5,7,8) elif pos== 5: return (1,2,3,4,6,7,8,9) elif pos== 6: return (2,3,5,8,9) elif pos== 7: return (4,5,8) elif pos== 8: return (4,5,6,7,9) elif pos== 9: return (5,6,8) def free_pos(brd,pos): #you give it a board and a position and it returns True if it is free pos -= 1 if brd[pos] == "[ ]": return True else: return False def player_pos(brd,pc): #It works now l = [] for i in range(0,9): if pc == "X": if brd[i] == "[X]": l.append(i + 1) elif pc == "O": if brd[i] == "[O]": l.append(i + 1) return (* l,) #The last function (the one that I'll write next, sorry if I caused any misunderstanding) was supposed to see if from the positions I got from player_pos, if any had a close position available. The way I thought I could do that was by seeing for each position the positions near that one, using near_pos function, and determine if there was any clear one, using free_pos.

我的功能不是';我不工作,我也不';我不知道为什么。python p>所以我有一个棋盘,它有9个位置和6个棋子,3个 [X]和3个代码> []/Cord>(你也可以考虑[])一个棋子,但是它代表一个空的位置)。玩家可以选择将棋子移动到任何他想移动的位置。在下面的棋盘上,“X”(这就是我们如何表示棋子的方式,与棋盘上的棋子一样,但没有[])必须移动,但他没有地方去。我想要一个函数,看看一个球员是否没有合法的运动,如果是这样,它返回真的。以下是帮助功能: #pc stands for piece, brd for board and pos for position board = ["[X]","[X]","[X]", "[O]","[O]","[O]", "[ ]","[ ]","[ ]"] def near_pos(pos): #positions are represented 1-9 and this functions gives the near positions of a position if pos== 1: return (2,4,5) elif pos== 2: return (1,3,4,5,6) elif pos== 3: return (2,5,6) elif pos== 4: return (1,2,5,7,8) elif pos== 5: return (1,2,3,4,6,7,8,9) elif pos== 6: return (2,3,5,8,9) elif pos== 7: return (4,5,8) elif pos== 8: return (4,5,6,7,9) elif pos== 9: return (5,6,8) def free_pos(brd,pos): #you give it a board and a position and it returns True if it is free pos -= 1 if brd[pos] == "[ ]": return True else: return False def player_pos(brd,pc): #It works now l = [] for i in range(0,9): if pc == "X": if brd[i] == "[X]": l.append(i + 1) elif pc == "O": if brd[i] == "[O]": l.append(i + 1) return (* l,) #The last function (the one that I'll write next, sorry if I caused any misunderstanding) was supposed to see if from the positions I got from player_pos, if any had a close position available. The way I thought I could do that was by seeing for each position the positions near that one, using near_pos function, and determine if there was any clear one, using free_pos.,python,python-3.x,Python,Python 3.x,我写的是这个,但它是错误的,我不知道如何修复它 def No_Free_Positions(brd,pc): if pc == "X": a = "O" if pc =="O": a = "X" for player_pos(brd,pc) in near_pos(pc): if Free_Pos(brd,near_pos(pc)) is Fal

我写的是这个,但它是错误的,我不知道如何修复它

def No_Free_Positions(brd,pc):
    if pc == "X":
        a = "O"
    if pc =="O":
        a = "X"
    for player_pos(brd,pc) in near_pos(pc):
        if Free_Pos(brd,near_pos(pc)) is False:
            return "No"
        else:
            return "Yes"

有什么问题?

由于以下情况,
free\u pos
函数将始终返回false:
如果brd[pos]==”
。这将检查给定的平方是否为空,但是程序中的空平方是
“[]”
,因此它永远不会返回true。以下是更正后的代码:

if brd[pos] == "[ ]":

brd[pos]==”
永远不会为真。如果你想匹配一个空方块,它应该是
brd[pos]=“[]”
也许你不应该把
[
]
放在
板上
,只要把
X
O
或空格。然后在显示板时添加括号。哦,这是我在复制时犯的错误,对不起