1

I have got this error when I try to run my program:

C:\Users\Goldsmitd\chess\Scripts\python.exe C:/Users/Goldsmitd/PycharmProjects/CHESS/chess_ver0.07.py Traceback (most recent call last): File "C:/Users/Goldsmitd/PycharmProjects/CHESS/chess_ver0.07.py", line 138, in a.display() File "C:/Users/Goldsmitd/PycharmProjects/CHESS/chess_ver0.07.py", line 80, in display if self.board[i][j].sl=='R': AttributeError: 'str' object has no attribute 'sl'

Someone know what I do wrong?

__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)

1 Answer 1

2

Since

self.board = [['.']*8 for _ in range(8)]

for some values of i and j, self.board[i][j] is a string. Therefore,

self.board[i][j].sl=='R'

is raising AttributeError: 'str' object has no attribute 'sl' because strings have no sl attribute.


Perhaps the easiest way to fix your code with minimal changes would be to add an Empty class similar to other chess piece classes to represent unoccupied squares on the chess board. Make sure instances of the Empty class have an sl attribute.

Note however that it is not clear (to me) that you really need separate classes for each kind of chess piece. They are all basically the same kind of object, they merely differ in terms of how they move. So you may be better off with one chess piece class and give each instance a kind attribute which could equal 'rook', 'knight', etc.

Also note that in your code there is redundant information: The self.board records the location of the pieces, and the chess piece also records the location:

    self.board[7][0] = Rook(x=7,y=0,sl='R',team='white')

Recording the information in two places poses a problem:

  • The information may become corrupted -- if the logic in your code is not correct, the board may record the location of a piece in a different place than where the piece itself thinks it is on the board. Thus you have a coordination problem.

  • Having the information in two places makes your code more complicated because you have to update the location in both the board and the chess piece to maintain consistency every time a piece is moved.

Sign up to request clarification or add additional context in comments.

1 Comment

Ok, I know what do I do. I have to create board which all elements are instances of classes. So empty squares without figures must also be a class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.