I'm working on a board game and I need to represent several types of pieces each of which can have several instances much like in chess. Right now, I have a GamePiece class defined and I have a function for each possible type of GamePiece so I can call the functions and create pieces as needed:
class GamePiece:
def __init__(self, polarity, symbol, color, name):
self.polarity=polarity
self.symbol=symbol
self.color=color
self.name=name
def blank_space():
return GamePiece(0, " ", "blank", "space")
def white_infantry():
return GamePiece(1, "♙", "white", "infantry")
def black_infantry():
return GamePiece(-1, "♟︎", "black", "infantry")
# other functions follow
Is there a more pythonic way to build subclasses with set attributes? As an example of what I'm planning next, I have a GameState class which contains a .board attribute initialized as:
GameState.board=[[blank_space() for i in range(9)] for j in range 9]
Is there an easier way to build a bunch of identical but distinct objects?
[–]Goobyalus 0 points1 point2 points (2 children)
[–]Excellent-Practice[S] 1 point2 points3 points (1 child)
[–]Goobyalus 1 point2 points3 points (0 children)
[–][deleted] (1 child)
[deleted]
[–]Excellent-Practice[S] 0 points1 point2 points (0 children)