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=State(0)不创建类的新实例吗?如果是这样,为什么结果是这样呢?
_state不是类的数据成员,对于类的每个实例都是不同的吗?
我做错了什么!
阅读一些文档,并找出它。
在类定义下定义任何变量都将使其成为"静态数据成员",一个类变量而不是实例变量。必须在构造函数中定义所有实例变量!
class Stae:
def __init__( self, value ):
'constructor call with value 0 to load intial pos, and with 1 to load blank board'
self._state = []