An important place to use is the storage of game records. When playing a boss, you can start from the first stop without restarting.
Why: in the future, Project expansion and maintenance, such as the vitality, attack strength, and defensive strength of game roles cannot be directly exposed on the client.
Figure:
Code:
Class Program {static void Main (string [] args) {// GameRole lixiaoyao = new GameRole (); lixiaoyao. getIninState (); lixiaoyao. stateDisplay (); // Save the progress RoleStateCaretaker stateAdmin = new RoleStateCaretaker (); stateAdmin. memento = lixiaoyao. saveState (); // The loss of lixiaoyao. fight (); lixiaoyao. stateDisplay (); // restore the previous status lixiaoyao. recoveryState (stateAdmin. memento); lixiaoyao. stateDisplay (); Console. read () ;}// game role class GameRole {private int vit; // life public int vitality {get {return vit ;}set {vit = value ;}} private int ack; // attack force public int attack {get {return ack;} set {ack = value;} private int def; // defensive force public int defense {get {return def;} set {def = value ;}/// the status shows public void stateDisplay () {Console. writeLine ("current role status:"); Console. writeLine ("physical strength: {0}", this. vit); Console. writeLine ("Attack Strength: {0}", this. ack); Console. writeLine ("defensive force: {0}", this. def); Console. writeLine ("");} // obtain the initial state public void GetIninState () {this. vit = 100; this. ack= 100; this. def = 100;} // combat public void Fight () {this. vit = 0; this. ack = 0; this. def = 0;} // Save the role state public RoleStateMementto saveState () {return (new RoleStateMementto (vit, ack, def ));} // restore the role state public void RecoveryState (RoleStateMementto memento) {this. vit = memento. vitality; this. ack = memento. vitality; this. def = memento. defense ;}// role status storage box class RoleStateMementto {private int vit; private int atk; private int def; public RoleStateMementto (int vit, int atk, int def) {this. vit = vit; this. atk = atk; this. def = def ;}// vitality public int vitality {get {return vit ;}set {vit = value ;}// attack force public int attack {get {return atk ;} set {atk = value ;}/// defense Force public int defense {get {return def ;}set {def = value ;}}} // role status manager class RoleStateCaretaker {private RoleStateMementto memento; public RoleStateMementto Memento {get {return memento;} set {memento = value ;}}}
Running result: