I was trying to remind myself of OO programming by creating some kind of chess clone in python and found myself with the current issue.
I'd like to give each piece on the board an 'identifier' variable so that it can be displayed on screen e.g : bR would indicate a black rook. However I'm not sre how I can establish this due to colour being a variable being inherited to the rook class, rather than being a part of it.
This is probably a silly question but I've also looked into using the __str__ function for this display but I run into a similar issue that way too. The code I have is below.
I could use another __init__ variable but I'd rather not have to specify every single pawns identifier (P) when the pawn class could surely do the same for me?
class piece(object):
identifier = ' '
def __init__(self, colour):
self.colour = colour
class rook(piece):
identifier = 'R'
rookA = rook('b')
And I'm looking to achieve:
print(rookA.identifier)
"bR"
or
print(rookA)
"bR"
if I'm going about this task in the wrong way, please suggest an alternative!
Thanks
return "%s%s" % (colour, identifier)when colour was part of the 'piece' class and not the 'rook' class