2012-08-05 85 views
3

我試圖讓戰艦的做法,和單人遊戲是成功的......當只有一個球員和一個集船舶和董事會:Pint對象是unscriptable Python的

知道爲什麼這是給我一個'int object is unscriptable'錯誤?

這是董事會類。那麼,一些也無妨:

class Board: 
    'Game Board' 
    topMarkers = list('ABCDEFGHIJKLMNOPQRSTUVWXYZ') 
    sideMarkers = list(range(0, 26)) 


    def __init__(self,h,w): #Makes the map 
     self.height = h 
     self.width = w 

     self.xMarkers = [] 
     self.yMarkers = [] 
     self.hitList = [] 
     self.hitListCopy = [] 

     self.boardSpace = '   ' 

     wave = '~' 
     self.row = [] 
     self.column = [] 
     #self.column = wave * self.width # If width is 4, column is now '~~~~' 

     for c in range(self.width): 
      self.column.append(wave) 
      self.xMarkers.append(Board.topMarkers[c]) 

     for r in range(self.height): # 
      self.row.append(self.column[:]) #Use the slice to make copies 
      self.yMarkers.append(Board.sideMarkers[r]) 



    def showGrid(self): 
     print self.boardSpace + ' ' + ' '.join(self.xMarkers) 
     for i in range(self.height): 
      print self.boardSpace + str(self.yMarkers[i]) + ' ' + '-'.join(self.row[i]) 

下面是實際運行它的代碼,該代碼是導致它在底部的一部分,同時和循環,它在註釋中顯示的說您的地圖的網格。

p1 = [[Board(7,7), Board(7,7)], Move(), Connection(1, netEnable)] 
p2 = [[Board(7,7), Board(7,7)], Move(), Connection(2, netEnable)] 
#p3 = [[Board(7,7), Board(7,7)], Move(), Connection(3, netEnable)] 

#Like this p1 = [[theirBoard, attackBoard], Moves, Connection] 



#p = [p1,p2,p3] 
p = [p1,p2] 


ships = [Ship(0),Ship(0),Ship(0)] 
ships2 = [Ship(0),Ship(0),Ship(0)] 

numOfPlayers = len(p) 


youPlayer = int(raw_input('Which player are you? 1,2,3: ')) 

youPlayer -= 1 



boardr = p[youPlayer][0][1].getRowData() 
boardM = p[youPlayer][0][0].getMarkerData() 
#raw_input(boardr) 
raw_input(boardM) 



#Set Ships Dialog. 
for i in range(len(ships)): 
    p[youPlayer][0][1].showGrid() 
    ships[i].setShip(boardr, boardM) 


shipPosAll = [[],[],[]]  

for i in range(len(ships)): 
    shipPosAll[youPlayer].append(ships[i].getShip()) #USE THIS INFO TO FEED TO THE BOARD TO TELL IT WHERE SHIPS ARE SO YOU KNOW WHERE HITS ARE. 

print 'shipPos at line 382 : %s' % (shipPosAll[youPlayer]) 




#INIT, DO ONLY ONCE 

for i in range(numOfPlayers): 
    print p[i][2].GetTime() 

#MAIN LOOP 
while gameNotOver: 

    for i in range(numOfPlayers): 
     playersCheck = [0,1] 
     del playersCheck[youPlayer] 

     print 'Player %s turn' % (i) 
     print 'Here are your ships' 
     #raw_input (p[i][0][1]) 
     p[i][0][1].showGrid() #Show the grid of the player (TheirShips) HERES WHERE THE PROBLEM IS!!!! 

     print 'Where do you want to attack?' 
     p[i][0][0].showGrid() #Show the grid of the player (AttackShips) 

     hits = p[i][0][0].getHitList() 
     alreadyMade = False 
     while alreadyMade == False: 
      coords = p[i][0][0].chooseMove() 
      alreadyMade = p[i][1].addMove(coords) #Returns true if move accepted, otherwise false 

     lastMove = p[i][1].getMove() 
     print 'The move you made was %s' % (lastMove) 
     p[i][2].Send(lastMove) 
     changeCoords = p[i][2].Check() 
     p[i][0][0].changeRow('B') 

     for p in playersCheck: #Check every player who is not you. You were deleted at teh beginning. 
      checkForSunk(shipPosAll[p], hits) 
      print 'Current Attacked Players shipPos: %s' % (shipPosAll[p]) 
     #p1Site.Send(coords) 
     #print 'Thing %s' % (shipPos[i]) 
      if isGameOver(shipPosAll[p]): #PROBLEM. 
       gameNotOver = False 
       break 

    for i in range(numOfPlayers): 
     print p[i][1].getAllMoves() 


print 'All Ships Sunk. The End. Wat more you want from mme its 3:48am' 

感謝

這裏有確切的消息,我得到。

Traceback (most recent call last): File "name/battleship_wClasses.py", line 431, in p[i][0][1].showGrid() #Show the grid of the player (TheirShips) TypeError: 'int' object is unsubscriptable –

+1

請發佈確切的錯誤消息,包括行號。 – Dogbert 2012-08-05 10:42:22

+0

哇,這是很多代碼,你能否提供完整的回溯以及在代碼中標註問題行的註釋。 – Levon 2012-08-05 10:43:48

+0

回溯(最近通話最後一個): 文件 「名/ battleship_wClasses.py」,線路431,在 P [I] [0] [1] .showGrid()#Show播放器(TheirShips)組成的柵格 TypeError:'int'object is unsubscriptable – user1159454 2012-08-05 10:44:15

回答

8

你有兩個(重疊)矛盾的定義爲p

p = [p1,p2] 

playersCheck = [0,1] 
    for p in playersCheck: 

p[i][0][1].showGrid()作品與第一個定義,但是失敗時p分配從一個整數值第二個定義。

+0

中有一條評論哦,這是第一次for循環實際上干擾了一個變量......我總是假設循環迭代變量只存在於循環中。謝謝 – user1159454 2012-08-05 10:55:51

+1

是的,這是一個Python的陷阱 - 'for-loops'將他們的迭代變量「泄漏」到周圍的範圍中。 – unutbu 2012-08-05 10:57:41

相關問題