我尝试运行程序时遇到此错误:
C:\用户\ Goldsmitd \棋\脚本\ python.exe C:/Users/Goldsmitd/PycharmProjects/CHESS/chess_ver0.07.py Traceback (最近一次调用最后一次):文件 “C:/Users/Goldsmitd/PycharmProjects/CHESS/chess_ver0.07.py”,第138行, 在 a.display()文件“C:/Users/Goldsmitd/PycharmProjects/CHESS/chess_ver0.07.py”,第80行, 在显示 if self.board [i] [j] .sl =='R':AttributeError:'str'对象没有属性'sl'
有人知道我做错了吗?
__author__ = 'Goldsmitd'
class Rook:
def __init__(self,x,y,sl,team):
self.name = 'Rook'
self.x = x
self.y = y
self.sl = sl
self.team = team
class Knight:
def __init__(self,x,y,sl,team):
self.name = 'Knight'
self.x = x
self.y = y
self.sl = sl
self.team = team
class Bishop:
def __init__(self,x,y,sl,team):
self.name = 'Bishop'
self.x = x
self.y = y
self.sl = sl
self.team = team
class Queen:
def __init__(self,x,y,sl,team):
self.name = 'Queen'
self.x = x
self.y = y
self.sl = sl
self.team = team
class King:
def __init__(self,x,y,sl,team):
self.name = 'King'
self.x = x
self.y = y
self.sl = sl
self.team = team
class Pawn:
def __init__(self,x,y,sl,team):
self.name = 'Pawn'
self.x = x
self.y = y
self.sl = sl
self.team = team
class Chess_Board:
def __init__(self):
self.board = [['.']*8 for _ in range(8)]
self.board[7][0] = Rook(x=7,y=0,sl='R',team='white')
self.board[7][1] = Knight(x=7,y=1,sl='N',team='white')
self.board[7][2] = Bishop(x=7,y=2,sl='B',team='white')
self.board[7][3] = Queen(x=7,y=3,sl='Q',team='white')
self.board[7][4] = King(x=7,y=4,sl='K',team='white')
self.board[7][5] = Bishop(x=7,y=5,sl='B',team='white')
self.board[7][6] = Knight(x=7,y=6,sl='N',team='white')
self.board[7][7] = Rook(x=7,y=7,sl='R',team='white')
self.board[6][0] = Pawn(x=6,y=0,sl='P',team='white')
self.board[6][0] = Pawn(x=6,y=1,sl='P',team='white')
self.board[6][0] = Pawn(x=6,y=2,sl='P',team='white')
self.board[6][0] = Pawn(x=6,y=3,sl='P',team='white')
self.board[6][0] = Pawn(x=6,y=4,sl='P',team='white')
self.board[6][0] = Pawn(x=6,y=5,sl='P',team='white')
self.board[6][0] = Pawn(x=6,y=6,sl='P',team='white')
self.board[6][0] = Pawn(x=6,y=7,sl='P',team='white')
def display(self):
for i in range(8):
for j in range(8):
if self.board[i][j].sl=='R':
print('R',end=' ')
elif self.board[i][j].sl=='N':
print('N',end=' ')
elif self.board[i][j].sl=='B':
print('B',end=' ')
elif self.board[i][j].sl=='Q':
print('Q',end=' ')
elif self.board[i][j].sl=='K':
print('K',end=' ')
elif self.board[i][j].sl=='P':
print('P',end=' ')
else:
print(self.board[i][j],end=' ')
print()
def figure_choice(self):
while True:
try:
print('please give a position of figure which you chose')
sx=int(input())
sy=int(input())
return sx,sy
except:
print('ERROR. Your choice is valid. Please choose only integers')
def move_king(self):
while True:
try:
print('please give a position of figure which you chose')
sx=int(input())
sy=int(input())
return sx,sy
except:
print('ERROR. Your choice is valid. Please choose only integers')
try:
print('please give a position of king')
sx=int(input())
sy=int(input())
except:
print('ERROR. Your choice is valid. Please choose only integers')
try:
print('please choose a destination for king')
dx=int(input())
dy=int(input())
except:
print('ERROR. Your choice is valid. Please choose only integers')
if self.board[dx][dy] == '.' :
if ( abs(sx-dx) <2 and abs(sx-dy) < 2 ):
self.board[dx][dy]=King(x=dx,y=dy,sl='K',team='white')
self.board[sx][sy] = '.'
return self.board
break
a=Chess_Board()
a.display()
print(a.board[7][0].sl)
答案 0 :(得分:2)
由于
self.board = [['.']*8 for _ in range(8)]
对于i
和j
的某些值,self.board[i][j]
是一个字符串。因此,
self.board[i][j].sl=='R'
正在提升AttributeError: 'str' object has no attribute 'sl'
,因为字符串没有sl
属性。
使用最少的更改修复代码的最简单方法可能是添加类似于其他棋子类的Empty
类来表示棋盘上的未占用方块。确保Empty
类的实例具有sl
属性。
但请注意,对我来说,确实需要单独的课程并不清楚
对于每种棋子。它们基本上都是对象的种,
他们只是在行动方式上有所不同。所以你可能会用一个更好
棋子类并为每个实例提供一个kind
属性,该属性可以相等
'rook'
,'knight'
等
另请注意,在您的代码中有冗余信息:self.board
记录碎片的位置,棋子也记录位置:
self.board[7][0] = Rook(x=7,y=0,sl='R',team='white')
在两个地方记录信息会产生问题:
信息可能会损坏 - 如果您的代码中的逻辑不正确,则电路板可能会将某个部件的位置记录在与该部件本身认为位于电路板上的位置不同的位置。因此,你有一个协调问题。
将信息放在两个地方会使您的代码更加复杂,因为您必须更新棋盘和棋子中的位置,以便在每次移动棋子时保持一致性。