I'm designing a card game (of shedding type) and I would like to use the command design pattern. Please let me explain my intention:
The deck has two stacks, one showing the backs and the other showing the faces of cards. A player can do three types of actions during a turn: throw selected card(s) onto the "faces" stack, draw a given number of cards from the "backs" stack or pass. Without the command pattern, the class Player could look like this:
class Player
{
var hand: [Card]
func throw() {
// select cards to throw
// remove them from the hand
deck.addCardsToFacesStack(cards: chosenCards)
}
...functions draw and pass here...
}
With the command pattern in place, I would add this concrete command:
class throwCommand: Command {
var player: Player
var deck: Deck
var arrayOfIndices: [Int]
init(player: Player, deck: Deck, indices: [Int]) {
...initialization here...
}
func execute() {
removedCards = player.removeCardsFromHand(indices: arrayOfIndices)
deck.addCardsToFacesStack(cards: removedCards)
}
}
The player would instantiate this concrete command, passing it itself, the deck and indices of cards to be removed from the hand and thrown onto the faces stack of the deck. The execute method would be called by the GameManager (the concrete command would be returned by the throw method of a player).
Is this solution OK? It uses multiple (two) receivers and Player is the Client and at the same time a Receiver and the deck is just a Receiver.
Thanks in advance for your responses,
Caroline
Aucun commentaire:
Enregistrer un commentaire