StarCraft in php memo mode-PHP source code

Source: Internet
Author: User
The following is an example of Starcraft in php memo mode. I hope the example can help you. The following is an example of Starcraft in php memo mode. I hope the example can help you.

Script ec (2); script

When we play the star task version or stand-alone vs. computer, sometimes we suddenly leave the game, or in front of the troops, we need to store the game.

So how can we save the current information? And can the stored game be recovered at any time?

The problem to be solved: stores all information about the game, if it is restored completely.

Idea: create a class dedicated to saving information and let him handle these things, just like a memorandum.
For simplicity, we will use the player information to demonstrate how to recover a player.

The Code is as follows:

// 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;

Purpose Summary: The memo mode allows us to save information at a certain time point and restore the required information as needed, just like saving and loading a game to archive.

Summary: a memo class is required to save information. The saved class needs to generate a memo object and call a method to restore its status.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.