|
// Memo class
Class Memento
{
// Crystal Ore
Public $ ore;
// Gas mine
Public $ gas;
// All troops of the player
Public $ troop;
// All construction objects of Players
Public $ building;
// Constructor. The parameter is the Player object to be saved. The force parameter type here is the Player class.
Public function _ construct (Player $ player)
{
// Save the player's crystal mine
$ This-> ore = $ player-> ore;
// Save the player's gas mines
$ This-> gas = $ player-> gas;
// Save all the troops of the player.
$ This-> troop = $ player-> troop;
// Save all the building objects of the player
$ This-> building = $ player-> building;
}
}
// Player class
Class Player
{
// Crystal Ore
Public $ ore;
// Gas mine
Public $ gas;
// All troops of the player
Public $ troop;
// All construction objects of Players
Public $ building;
// Get the player's memo object
Public function getMemento ()
{
Return new Memento ($ this );
}
// Use the player's memo object to restore the player. The type of mandatory parameter here is the Memento class.
Public function restore (Memento $ m)
{
// Crystal Ore
$ This-> ore = $ m-> ore;
// Gas mine
$ This-> gas = $ m-> gas;
// All troops of the player
$ This-> troop = $ m-> troop;
// All construction objects of Players
$ This-> building = $ m-> building;
}
}
// Create a player
$ P1 = new Player ();
// Assume that he has collected 100 crystal mines.
$ P1-> ore = 100;
// We save the game and then continue playing the game.
$ M = $ p1-> getMemento ();
// Assume that he has collected 200 crystal mines.
$ P1-> ore = 200;
// Now we load the stored game
$ P1-> restore ($ m );
// Output the crystal mine. You can see that it has changed to the original saved state.
Echo $ p1-> ore; |