我的目标是建立一个游戏成就系统,你可以在其中添加玩家(有名字,整数id,他们玩的游戏),游戏(有名字,整数id,成就列表)和成就(有名字) ,整数id,以及该成就所属的游戏ID。)
我不是要求任何代码,我问的是如何理想地构建这个程序。乍一看,我只会有一个玩家列表,每个玩家类包含他们玩的游戏,每个游戏类包含成就。但问题是玩家本身必须具备成就,并且他们玩的每个玩家的游戏必须参考同一个游戏,而不是每个玩家的同一游戏的个人副本。
构建这样的程序的最佳方法是什么?
答案 0 :(得分:1)
因此,您希望将句柄存储在玩家类别中的玩家游戏中,而不是实际游戏本身(它们将存储在其他地方)。句柄可以是指针(可以是智能指针),数组索引或vector
或类似指针,或引用游戏的其他一些抽象层。
答案 1 :(得分:1)
我知道课堂上会有几种设计。这可以是其中之一。我只是使用std::list
并希望展示一个基础和简单的设计,而不是实用的。因此,在适用于您的情况之前,您必须关注这一点。
using namespace std;
class Achievement
{
public:
Achievement(string name, int id) : name_(name), id_(id) {}
string name_;
int id_;
};
typedef list<Achievement> ACHIEVEMENT_LIST;
class Game
{
public:
Game(string name, int id) : name_(name), id_(id) {}
string name_;
int id_;
ACHIEVEMENT_LIST achievement_list_;
};
typedef list<Game> PLAYED_GAME_LIST;
class Player
{
public:
Player(string name, int id) : name_(name), id_(id) {}
string name_;
int id_;
PLAYED_GAME_LIST played_game_list;
};
int main()
{
//firstly, player A, B joins 'dungeon' Game
Player a("A", 01);
Player b("B", 02);
Game dungeon("dungeon", 1001);
//when game finishes, create achievement
Achievement hero("hero", 100001);
//and, add to achievement_list in Game
dungeon.achievement_list_.push_back(hero);
//add 'dungeon' to played_game_list.
a.played_game_list.push_back(dungeon);
b.played_game_list.push_back(dungeon);
//Player a,b have same game reference
Game a_played_game = a.played_game_list.front();
Game b_played_game = b.played_game_list.front();
cout << "GAME : a_played_game.name_ = " << a_played_game.name_ << ", b_played_game.name_ = " << b_played_game.name_ << endl;
//we can access achievement in player class
Achievement a_achive = a_played_game.achievement_list_.front();
Achievement b_achive = b_played_game.achievement_list_.front();
cout << "ACHIEVEMENT : a_achive.name_ = " << a_achive.name_ << ", b_achive.name_ = " << b_achive.name_ << endl;
getchar();
return 0;
}