-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemo.cpp
158 lines (138 loc) · 2.39 KB
/
memo.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include <iostream>
#include <list>
#include <boost/shared_ptr.hpp>
using namespace std;
class GameMemo
{
public:
explicit GameMemo(int index, int scene )
:m_index( index ), m_scene( scene )
{
}
int getIndex( )
{
return m_index;
}
void setIndex( int index )
{
m_index = index;
}
int getScene( )
{
return m_scene;
}
void setScene( int scene )
{
m_scene = scene;
}
~GameMemo( )
{
cout<<"----------game memo destroy"<<endl;
}
private:
int m_index;
int m_scene;
};
class MemoMgr
{
public:
typedef boost::shared_ptr<GameMemo> GAME_MEMO_PTR;
typedef list<GAME_MEMO_PTR >::iterator GAME_MEMO_ITER;
explicit MemoMgr( int size = 0 )
:m_size( size )
{
}
void addMemo( GameMemo* memo )
{
GAME_MEMO_PTR tmp( memo );
if( m_memos.size( ) >= m_size )
{
m_memos.pop_front( );
}
m_memos.push_back( tmp );
}
GameMemo* getMemo( int index )
{
GameMemo* memo = NULL;
GAME_MEMO_ITER iter = m_memos.begin( );
for( ; iter != m_memos.end( ); iter++ )
{
GameMemo* tmp = ( *iter ).get( );
if( tmp->getIndex( ) == index )
{
memo = tmp;
break;
}
}
return memo;
}
private:
int m_size;
list<GAME_MEMO_PTR> m_memos;
};
class Game
{
public:
Game( int index, int scene )
:m_index( index ), m_scene( scene )
{
}
GameMemo* createMemo( )
{
return new GameMemo( m_index, m_scene );
}
void resetGame( GameMemo* memo )
{
if( memo == NULL )
{
cout<<"can't reset, invalid memo"<<endl;
return;
}
m_index = memo->getIndex( );
m_scene = memo->getScene( );
}
void setIndex( int index )
{
m_index = index;
}
void setScene( int scene )
{
m_scene = scene;
}
void toString( )
{
cout<<"index:"<<m_index<<" scene:"<<m_scene<<endl;
}
private:
int m_index;
int m_scene;
};
int main( void )
{
MemoMgr memoMgr( 1 );
GameMemo* memo = NULL;
Game game( 0, 0 );
cout<<"pass to gate 1"<<endl;
game.setIndex( 1 );
game.setScene( 1 );
memo = game.createMemo( );
memoMgr.addMemo( memo );
game.toString( );
cout<<"pass to gate 2"<<endl;
game.setIndex( 2 );
game.setScene( 2 );
memo = game.createMemo( );
memoMgr.addMemo( memo );
game.toString( );
cout<<"pass to gate 3"<<endl;
game.setIndex( 3 );
game.setScene( 3 );
memo = game.createMemo( );
memoMgr.addMemo( memo );
game.toString( );
cout<<"rollback to 1"<<endl;
memo = memoMgr.getMemo( 1 );
game.resetGame( memo );
game.toString( );
return 0;
}