Command Pattern - Undo

This is directed toward nantuko84 but other people might be interested also.
The command pattern can be used to implement an undo/redo system. The "trick" is that everything in your program has to generate a Command object. Generally the Command object is just an abstract interface with a single method. To implement undo you just make each Command action reversible.
In my opinion generating a Command object for every action is a bit of a pain. So if a player draws 3 cards, undo() would put those cards back on top of the library in the correct order. With Magic and state effects like the "legendary rule", I presume each state effect would also generate a Command object. So if a player played a 2nd legendary creature, that would generate one Command and then state effects would be checked and it would generate a second Command. And as always the devil is in the details, especially when considering combat and EOT effects.
There are a ton of tiny, detailed stuff you could to in order to make undo better for the user. You could combine "minor" Commands together to make a "major" Command, like minor Commands would be tapping individual cards and the major Command would be paying for a card.
And if you implement the Command pattern you should be able to write each Command to the hard drive and be able to replay the game, which would really help debugging.
This is how I understand things and I hope this helps.
The command pattern can be used to implement an undo/redo system. The "trick" is that everything in your program has to generate a Command object. Generally the Command object is just an abstract interface with a single method. To implement undo you just make each Command action reversible.
In my opinion generating a Command object for every action is a bit of a pain. So if a player draws 3 cards, undo() would put those cards back on top of the library in the correct order. With Magic and state effects like the "legendary rule", I presume each state effect would also generate a Command object. So if a player played a 2nd legendary creature, that would generate one Command and then state effects would be checked and it would generate a second Command. And as always the devil is in the details, especially when considering combat and EOT effects.
There are a ton of tiny, detailed stuff you could to in order to make undo better for the user. You could combine "minor" Commands together to make a "major" Command, like minor Commands would be tapping individual cards and the major Command would be paying for a card.
And if you implement the Command pattern you should be able to write each Command to the hard drive and be able to replay the game, which would really help debugging.
This is how I understand things and I hope this helps.
- Code: Select all
public interface Command
{
public void execute();
}
- Code: Select all
public interface UndoCommand
{
public void execute();
public void undo();
}