對於一個學校項目,我正在使用AI在Python中進行TicTacToe遊戲。現在我有點卡住了。所以我用這個代碼來檢查,如果有一個贏家:檢查三分之二是否等於
playField = [['.','.','.'],['.','.','.'],['.','.','.']]
def checkWinner(L):
return ((playField[0][0] == L and playField[0][1] == L and playField[0][2] == L) or #Line 1 horizontal
(playField[1][0] == L and playField[1][1] == L and playField[1][2] == L) or #Line 2 horizontal
(playField[2][0] == L and playField[2][1] == L and playField[2][2] == L) or #Line 3 horizontal
(playField[0][0] == L and playField[1][0] == L and playField[2][0] == L) or #Colomn a vertical
(playField[0][1] == L and playField[1][1] == L and playField[2][2] == L) or #Colomn b vertical
(playField[0][2] == L and playField[1][2] == L and playField[2][2] == L) or #Colomn c vertical
(playField[0][0] == L and playField[1][1] == L and playField[2][2] == L) or #Diagonal left-top to right-bot
(playField[0][2] == L and playField[1][1] == L and playField[2][0] == L)) #Diagonal right-top to left-bot
這是偉大的工作,但我的AI我要檢查,如果要不是他還有兩個行或「敵人」有兩個在一排。所以基本上我想檢查三個中的兩個是否等於'L',並且我不知道如何重寫該函數,或者使用另一種方法檢查三個中的兩個是否等於'L'。
幫助將是巨大的!
問候,吉榮
請發佈一個最小化,完整,可驗證的示例[MCVE](http://stackoverflow.com/help/mcve) – MarkyPython
編寫一個'check_row'函數,接收三個值並檢查三個中的兩個。然後爲8行中的每一行調用它。 – interjay