vendredi 20 septembre 2019

Design pattern for game implementation

Sorry for the generic title, if anyone after reading the below has a better one please change it.

I'm implementing a popular board game in python. One game feature includes five piles of tokens from which the player can take. I'm having trouble thinking of a good way to represent this in code. My current implementation looks somewhat like this (simplified for the post):

class Game:
    def __init__(self):
        ...        
        self.tokens = num_tokens

class Player:
    def take_token(self, game):
        game.tokens[token] -= 1
        self.tokens[token] += 1
        return game  # the updated game instance

To me, this clearly seems like a terrible idea, because I'm passing the game state into Player just to manipulate it and then sending it back... there should be a much cleaner way to represent this. The problem is I like having the data stored under self for each object, as that seems a good way to bind the data to the object. Can someone help me identify the proper design pattern here?

Aucun commentaire:

Enregistrer un commentaire