2013-04-21 113 views
2

我正在編寫一個Tic-Tac-Toe遊戲。我完成了大部分程序。但我不斷收到以下錯誤,我不明白我做錯了什麼。我嘗試過不同的格式。Tic-Tac-Toe TypeError:'NoneType'對象沒有屬性'__getitem__'

Traceback (most recent call last): 
    File "C:/Users/Akshay Sastry/Documents/CS 303E/Tic-Tac-Toe.py", line 66, in <module> 
    main() 
    File "C:/Users/Akshay Sastry/Documents/CS 303E/Tic-Tac-Toe.py", line 3, in main 
    while isWinner(board) == 0 and movesLeft(board) == True: 
    File "C:/Users/Akshay Sastry/Documents/CS 303E/Tic-Tac-Toe.py", line 20, in isWinner 
    if (b[0][0]=='x') and (b[0][0]==b[0][1]==b[0][2]): 
TypeError: 'NoneType' object has no attribute '__getitem__' 

這是我的代碼:

def main(): 
    board = makeBoard() 
    while isWinner(board) == 0 and movesLeft(board) == True: 
     printBoard(board) 
     p1row, p1col = input("Enter a row and column for x: ") 
     board[p1row][p1col] = 'x' 
     if isWinner(board) == 0 and movesLeft(board) == True: 
      printBoard(board) 
      p2row, p2col = input("Enter a row and column for o: ") 
      board[p2row][p2col] = 'o' 
    if isWinner(board) != 0: 
     print isWinner(board), 'won!' 
    else: 
     print 'Tie game.' 

def makeBoard(): 
    board = [['*','*','*'],['*','*','*'],['*','*','*']] 

def isWinner(b): 
    if (b[0][0]=='x') and (b[0][0]==b[0][1]==b[0][2]): 
     return 'x' 
    elif (b[1][0]=='x') and (b[1][0]==b[1][1]==b[1][2]): 
     return 'x' 
    elif (b[2][0]=='x') and (b[2][0]==b[1][1]==b[2][2]): 
     return 'x' 
    elif (b[0][0]=='x') and (b[0][0]==b[1][0]==b[2][0]): 
     return 'x' 
    elif (b[0][1]=='x') and (b[0][1]==b[1][1]==b[2][1]): 
     return 'x' 
    elif (b[0][2]=='x') and (b[0][2]==b[1][2]==b[2][2]): 
     return 'x' 
    elif (b[0][0]=='x') and (b[0][0]==b[1][1]==b[2][2]): 
     return 'x' 
    elif (b[0][2]=='x') and (b[0][2]==b[1][1]==b[2][0]): 
     return 'x' 
    elif (b[0][0]=='o') and (b[0][0]==b[0][1]==b[0][2]): 
     return 'o' 
    elif (b[1][0]=='o') and (b[1][0]==b[1][1]==b[1][2]): 
     return 'o' 
    elif (b[2][0]=='o') and (b[2][0]==b[1][1]==b[2][2]): 
     return 'o' 
    elif (b[0][0]=='o') and (b[0][0]==b[1][0]==b[2][0]): 
     return 'o' 
    elif (b[0][1]=='o') and (b[0][1]==b[1][1]==b[2][1]): 
     return 'o' 
    elif (b[0][2]=='o') and (b[0][2]==b[1][2]==b[2][2]): 
     return 'o' 
    elif (b[0][0]=='o') and (b[0][0]==b[1][1]==b[2][2]): 
     return 'o' 
    elif (b[0][2]=='o') and (b[0][2]==b[1][1]==b[2][0]): 
     return 'o' 
    else: 
     return 0 

def printBoard(board): 
    for i in range(3): 
     for j in range(3): 
      print board[i][j], 
     print 

def movesLeft(board): 
    if board[0].count("*") != 0 or board[1].count("*") != 0 or board[2].count("*") != 0: 
     return True 
    else: 
     return False 
main() 

回答

3

makeBoard()函數返回None。你應該這樣說:

def makeBoard(): 
    return [['*','*','*'],['*','*','*'],['*','*','*']] 
+0

哇!不能相信我錯過了那個。謝謝! – user2303670 2013-04-21 05:07:47

1

isWinner功能,可以進行3倍小,如下

def isWinner(b): 
    for i in range(3): 
     if (b[i][0] != '*') and (b[i][0]==b[i][1]==b[i][2]): # all rows 
      return b[i][0] 
     if (b[0][i] != '*') and (b[0][i]==b[1][i]==b[2][i]): # all cols 
      return b[0][i] 
    if (b[0][0] != '*') and (b[0][0]==b[1][1]==b[2][2]): # tl-br diag 
     return b[0][0] 
    elif (b[0][2] != '*') and (b[0][2]==b[1][1]==b[2][0]): # bl-tr diag 
     return b[0][2] 
    else: 
     return 0 

對於較大的電路板,如連接4個,但是,你會遍歷所有點並寫出一個方法,在每個方向上檢查一個循環中的任意距離,而不是硬編碼每行可以放置的位置。

相關問題