[Python] Print out chess board from list of objects

Discussão em 'Python' iniciado por Stack, Maio 28, 2025.

  1. Stack

    Stack Membro Participativo

    I have the following task to print on screen the pieces of the chees board, they have to be in this format ♔ but this is the least of the problem.

    if I loop through I get this <main.Rook object at 0x7fc8fa510790>, how can I code --> if main.Rook == ♖,

    The output when the code is completed should be like that:

    ♖ ♔  
     ♜  ♜
     ♚ ♜ 
    ♖   ♗
    ♗    



    at the moment I build the board like that



    class Piece:
    pos_x : int
    pos_y : int
    side_ : bool #True for White and False for Black
    def __init__(self, pos_X : int, pos_Y : int, side_ : bool):
    '''sets initial values'''
    self.pos_x=pos_X
    self.pos_y=pos_Y
    self.side_=side_ #maybe set it to True?

    def __str__(self):#
    return f"{self.pos_x} {self.pos_y} {self.side_}"

    def get_side(self):
    return side_

    class Rook(Piece):
    def __init__(self, pos_X : int, pos_Y : int, side_ : bool):
    '''sets initial values by calling the constructor of Piece'''
    super().__init__(pos_X, pos_Y, side_)
    def __repr__(self):
    if self.side_==False:
    return "♜"
    else:
    return "♖"


    wr1 = Rook(1,2,True)
    br1 = Rook(4,3,False)
    br2 = Rook(2,4,False)
    br3 = Rook(5,4, False)
    wr2 = Rook(1,5, True)
    B1 = (10, [wr1,br1, br2, br3, wr2])



    so now I am working on the following function, I did not go to far, I can get the piece position, side and so on but how would I know if they are ROOK or something else, if I had a variable I could use their name for example, but here I have an object, can someone illustrate a method how to approach this?

    def conf2unicode(B: Board) -> str:
    '''converts board cofiguration B to unicode format string (see section Unicode board configurations)'''
    for piece in B[1]:
    print (piece)

    Continue reading...

Compartilhe esta Página