In the memo mode, the most direct thing is to record the game progress. If an 18-level game cannot record the game progress, it must start from the first level each time, if you want to give up the game, I will, hey.
Serialization in C # makes it easier for us to complete the memorandum mode. The UML diagram gives you a concept:
Details Code See the complete code.
Test code:
Dotapatternlibrary. Memento. Game = new dotapatternlibrary. Memento. Game ();
Game. gamelevel = 1;
Landpyform. Form. outputresult ("gamelevel:" + game. gamelevel );
Landpyform. Form. outputresult ("ten minute left ");
Game. gamelevel = 5;
Landpyform. Form. outputresult ("gamelevel:" + game. gamelevel );
Landpyform. Form. outputresult ("savegame ");
Dotapatternlibrary. Memento. gameprogress = game. getgameprogress ();
System. runtime. serialization. iformatter formatter = new system. runtime. serialization. formatters. Binary. binaryformatter ();
System. Io. filestream fsobj = new system. Io. filestream ("gameprogress. dat", system. Io. filemode. Create );
Formatter. serialize (fsobj, gameprogress );
Fsobj. Close ();
Landpyform. Form. outputresult ("Save OK ");
Thread. Sleep (3000 );
Landpyform. Form. outputresult ("3 second left ");
System. Io. filestream fsobj2 = new system. Io. filestream ("gameprogress. dat", system. Io. filemode. Open );
Gameprogress = formatter. deserialize (fsobj2) as dotapatternlibrary. Memento. gameprogress;
Dotapatternlibrary. Memento. Game game2 = new dotapatternlibrary. Memento. Game ();
Landpyform. Form. outputresult ("gamelevel:" + game2.gamelevel );
Landpyform. Form. outputresult ("loadgame ");
Game2.setprogress (gameprogress );
Landpyform. Form. outputresult ("gamelevel:" + game2.gamelevel );
Complete code: Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. text;
Using System. runtime. serialization. formatters. Binary;
Using System. runtime. serialization;
Using System. IO;
NamespaceDotapatternlibrary. Memento
{
[Serializable]
Public ClassGame
{
Private Int_ Gamelevel;
Public IntGamelevel
{
Get{Return_ Gamelevel ;}
Set{_ Gamelevel=Value ;}
}
Public Gameprogress getgameprogress ()
{
Return New Gameprogress ( This );
}
Public Void Setprogress (gameprogress)
{
This . _ Gamelevel = Gameprogress. Game. gamelevel;
}
}
[serializable]
Public class gameprogress
{< br> Public game;
Public gameprogress (Game)
{< br> This . game = game;
}< BR >}< br>