C ++互相类的依赖 [英] C++ mutual class dependency

查看:169
本文介绍了C ++互相类的依赖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用C ++写一个纸牌游戏。我有一个播放器类,处理操作(即选择要放置的卡)。如果播放器是人类,则它将使用 GUI类,如果它是计算机,则它将使用 AI类

I am writing a card game in C++. I have a player class, which handles the actions (i.e. selecting a card to lay). If the player is a human, it will use a GUI class, if it is a computer, it will use an AI class.

为了AI类做出决定,它需要知道关于在游戏期间改变的每个玩家的一些事情,例如手牌的数量。现在我在AI类中的向量中为每个玩家存储一个玩家指针。

In order for the AI class to make decisions, it needs to know some things about every player that change during the game, for example the number of cards on hand. Right now I store a player pointer for every player in a vector in the AI class.

但是,这导致我的问题,AI类需要#include类,而玩家类需要#include AI类。

However, this leads to my problem, the AI class needs to #include the Player class, and the player class needs to #include the AI class.

也许解决方案不是如何处理#includes,而是更好的结构类?

Maybe the solution isn't how to handle the #includes, but rather a better way to structure the classes?

推荐答案

我想Player和AI类都有通用的方法为这种纸牌游戏(得到卡, bet,all in ...),它可以继承自基类CardPlayer 或在运行时使用接口组合(参见

I guess both Player and AI class have common methods for that card game (get card, put card, shuffle, bet, all in...) which can be inherited from a base class CardPlayer or composed during runtime using interfaces (see Strategy Pattern).

要将Human Player类接口到GUI,请创建一个合适的接口,例如:

To interface the Human Player class to the GUI, create a suitable interface, for example:

typedef struct PlayerInfoStruct {
    ---player info variables here---
};

class IGUIPlayer {
    virtual PlayerInfoStruct GetPlayerInfo(...) = 0;
    ---other methods exclusive to the GUI-player interaction here---
};

并在您的HumanPlayer中实现该接口继承它:

and implement that interface in your HumanPlayer inheriting it:

class HumanPlayer : public CardPlayer, public IGUIPlayer {
    PlayerInfoStruct GetPlayerInfo() {---method---}
    ---other methods here---
}

,在你的GUI中指向每个人类播放器使用它的界面:

and in your GUI you point to each human player using its interface:

---somewhere in your GUI Class---
IGUIPlayer* humanplayer1 = <pointer to HumanPlayer>;

因此,GUI类取决于IGUIPlayer。 HumanPlayer依赖于IGUIPlayer和CardPlayer。 AIPlayer仅依赖于CardPlayer。没有循环内容!

With that, GUI Class depends on IGUIPlayer. HumanPlayer depends on both IGUIPlayer and CardPlayer. AIPlayer depends only on CardPlayer. No circular stuff!

使用界面意味着您可以随时在GUI中提供AI玩家的信息,只需继承 IGUIPlayer 到AIPlayer并实现纯虚拟方法,而无需修改GUI一个位。我花了一些时间习惯了这一点(我仍然在继续学习),但我不能抵抗它的无力的力量。

Using an interface means that you could, at any moment, provide AI players' info in the GUI, by simply inheriting IGUIPlayer to AIPlayer and implementing the pure virtual methods, without modifying the GUI a single bit. It took me a bit to get used to this (and I still keep on learning), but I can't resist to its inmense power.

这篇关于C ++互相类的依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆