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類的數據成員,這將不同類的每個實例?
我在做什麼錯了!