2013-04-26 268 views
0
class State: 
'defines the state of the class' 

_state = [] 

"""pawn = 1  rook = 2 knight = 3 bishop = 4 queen = 5 king = 6 none = 7 whiteSq = 0 blackSq = 1 whiteCol = 0 blackCol = 1""" 

def __init__(self, value): 
    'constructor call with value 0 to load intial pos, and with 1 to load blank board' 

    if(value == 1): 
     for i in range(8): 
      self._state.append([]) 
      for j in range(8): 
       self._state[i].append([]) 
       self._state[i][j].append((i+j)%2 , 7 , 7) 

    elif(value == 0): 

     for i in range(8): 
      self._state.append([]) 

      if(i==0): 
       self._state[i].append([ 0 , 1 , 2]) 
       self._state[i].append([ 1 , 1 , 3]) 
       self._state[i].append([ 0 , 1 , 4]) 
       self._state[i].append([ 1 , 1 , 5]) 
       self._state[i].append([ 0 , 1 , 6]) 
       self._state[i].append([ 1 , 1 , 4]) 
       self._state[i].append([ 0 , 1 , 3]) 
       self._state[i].append([ 1 , 1 , 2]) 

      elif(i==1): 
       for j in range(8): 
        self._state[i].append([(i+j)%2 , 1 , 1]) 

      elif(i==6): 
       for j in range(8): 
        self._state[i].append([(i+j)%2 , 0 , 1]) 

      elif(i==7): 
       self._state[i].append([ 1 , 0 , 2]) 
       self._state[i].append([ 0 , 0 , 3]) 
       self._state[i].append([ 1 , 0 , 4]) 
       self._state[i].append([ 0 , 0 , 5]) 
       self._state[i].append([ 1 , 0 , 6]) 
       self._state[i].append([ 0 , 0 , 4]) 
       self._state[i].append([ 1 , 0 , 3]) 
       self._state[i].append([ 0 , 0 , 2]) 

      else: 
       for j in range(8): 
        self._state[i].append([(i+j)%2 , 7 , 7]) 

def printState(self): 
    for i in range(8): 
     print(self._state[i]) 

的價值觀我無法理解這個輸出實例變量,受另一個實例

c=State(0);c.printState() 
[[0, 1, 2], [1, 1, 3], [0, 1, 4], [1, 1, 5], [0, 1, 6], [1, 1, 4], [0, 1, 3], [1, 1, 2]] 
[[1, 1, 1], [0, 1, 1], [1, 1, 1], [0, 1, 1], [1, 1, 1], [0, 1, 1], [1, 1, 1], [0, 1, 1]] 
[[0, 7, 7], [1, 7, 7], [0, 7, 7], [1, 7, 7], [0, 7, 7], [1, 7, 7], [0, 7, 7], [1, 7, 7]] 
[[1, 7, 7], [0, 7, 7], [1, 7, 7], [0, 7, 7], [1, 7, 7], [0, 7, 7], [1, 7, 7], [0, 7, 7]] 
[[0, 7, 7], [1, 7, 7], [0, 7, 7], [1, 7, 7], [0, 7, 7], [1, 7, 7], [0, 7, 7], [1, 7, 7]] 
[[1, 7, 7], [0, 7, 7], [1, 7, 7], [0, 7, 7], [1, 7, 7], [0, 7, 7], [1, 7, 7], [0, 7, 7]] 
[[0, 0, 1], [1, 0, 1], [0, 0, 1], [1, 0, 1], [0, 0, 1], [1, 0, 1], [0, 0, 1], [1, 0, 1]] 
[[1, 0, 2], [0, 0, 3], [1, 0, 4], [0, 0, 5], [1, 0, 6], [0, 0, 4], [1, 0, 3], [0, 0, 2]] 

>>> a=State(0);a.printState() 

[[0, 1, 2], [1, 1, 3], [0, 1, 4], [1, 1, 5], [0, 1, 6], [1, 1, 4], [0, 1, 3], [1, 1, 2], [0, 1, 2], [1, 1, 3], [0, 1, 4], [1, 1, 5], [0, 1, 6], [1, 1, 4], [0, 1, 3], [1, 1, 2]] 
[[1, 1, 1], [0, 1, 1], [1, 1, 1], [0, 1, 1], [1, 1, 1], [0, 1, 1], [1, 1, 1], [0, 1, 1], [1, 1, 1], [0, 1, 1], [1, 1, 1], [0, 1, 1], [1, 1, 1], [0, 1, 1], [1, 1, 1], [0, 1, 1]] 
[[0, 7, 7], [1, 7, 7], [0, 7, 7], [1, 7, 7], [0, 7, 7], [1, 7, 7], [0, 7, 7], [1, 7, 7], [0, 7, 7], [1, 7, 7], [0, 7, 7], [1, 7, 7], [0, 7, 7], [1, 7, 7], [0, 7, 7], [1, 7, 7]] 
[[1, 7, 7], [0, 7, 7], [1, 7, 7], [0, 7, 7], [1, 7, 7], [0, 7, 7], [1, 7, 7], [0, 7, 7], [1, 7, 7], [0, 7, 7], [1, 7, 7], [0, 7, 7], [1, 7, 7], [0, 7, 7], [1, 7, 7], [0, 7, 7]] 
[[0, 7, 7], [1, 7, 7], [0, 7, 7], [1, 7, 7], [0, 7, 7], [1, 7, 7], [0, 7, 7], [1, 7, 7], [0, 7, 7], [1, 7, 7], [0, 7, 7], [1, 7, 7], [0, 7, 7], [1, 7, 7], [0, 7, 7], [1, 7, 7]] 
[[1, 7, 7], [0, 7, 7], [1, 7, 7], [0, 7, 7], [1, 7, 7], [0, 7, 7], [1, 7, 7], [0, 7, 7], [1, 7, 7], [0, 7, 7], [1, 7, 7], [0, 7, 7], [1, 7, 7], [0, 7, 7], [1, 7, 7], [0, 7, 7]] 
[[0, 0, 1], [1, 0, 1], [0, 0, 1], [1, 0, 1], [0, 0, 1], [1, 0, 1], [0, 0, 1], [1, 0, 1], [0, 0, 1], [1, 0, 1], [0, 0, 1], [1, 0, 1], [0, 0, 1], [1, 0, 1], [0, 0, 1], [1, 0, 1]] 
[[1, 0, 2], [0, 0, 3], [1, 0, 4], [0, 0, 5], [1, 0, 6], [0, 0, 4], [1, 0, 3], [0, 0, 2], [1, 0, 2], [0, 0, 3], [1, 0, 4], [0, 0, 5], [1, 0, 6], [0, 0, 4], [1, 0, 3], [0, 0, 2]] 

不A =狀態(0)創建類的新實例? 如果是這樣,結果爲何如此!

是不是_state類的數據成員,這將不同類的每個實例?

我在做什麼錯了!

回答

0

閱讀一些文檔,並找出它。

定義類定義下的任何變量將使其成爲'靜態數據成員',一個類變量而不是實例變量。必須在構造函數中定義所有實例變量!

class Stae: 

def __init__(self, value): 
     'constructor call with value 0 to load intial pos, and with 1 to load blank board' 
     self._state = []