C#,代表,纸牌游戏

发布于 2024-12-16 10:59:46 字数 359 浏览 5 评论 0原文

我将使用委托编写一个 Black Jack 游戏,根据我的一位朋友的说法,您可以通过一个 PlayerEventArgs:EventArgs 类来管理所有事件(如绘制和保持)来完成此任务。如何?

我可以从 GUI 类创建一个引用上述类并调用 PlayerEventArgs 类内的方法的委托吗?或者如何执行此操作?

我应该有两个事件处理程序,例如:GUI 类中的 PlayerDrawEventHandler 和 PlayerHoldEventHandler ???

public event EventHandler<PlayerEventArgs> PlayerTurnCardEventHandler;

I m going to write a Black Jack game using delegates, according to a friend of mine you can accomplish this by having one PlayerEventArgs:EventArgs class that manages all events as draw and hold. How?

Can I from my GUI-class create a delegate that references the above class and call for the methods inside the PlayerEventArgs class or how do I do this?

Should I have two EventHandlers eg: PlayerDrawEventHandler and PlayerHoldEventHandler in GUI-class???

public event EventHandler<PlayerEventArgs> PlayerTurnCardEventHandler;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

◇流星雨 2024-12-23 10:59:46

是的,对于不同的操作有两个不同的事件处理程序是很好的。

解耦你的代码。

创建一个接口,例如 IPlayer,其中定义了事件。

他们创建一个实现 IPlayer 接口的 Player 对象。

创建一个管理器类(在您的例子中是游戏的引擎)创建一个实例
玩家类别并订阅事件。

Yes, it's good to have two different event handlers for different actions.

Decouple your code.

Create an Interface, like IPlayer that has the events defined in it.

Them create a Player object that implements the IPlayer interface.

Create a manager class ( in your case the engine of the game ) create an instance of the
Player class and subscribe to the events.

橘虞初梦 2024-12-23 10:59:46

你可以实现这样的东西:

public interface IMessage
{
}

public class PlayerDraw: IMessage
{
    public AnyType Arguments { get; set; }
}

public interface IMessageHandler<T> where T: IMessage
{
    void Handle(T message);
}

private class MessageHandlers: 
   IMessageHandler<PlayerDraw>, 
   IMessageHandler<PlayerHold>
{
    public void Handle(PlayerDraw message)
    {
        // Use your message here;
    }

    public void Handle(PlayerHold message)
    {
        // Use your message here;
    }

    ...
}

public class GameManager
{
    private readonly Dictionary<Type, Action<IMessage>> _messageRoutes = 
        new Dictionary<Type, Action<IMessage>>();

    public void RegisterHandler<T>(Action<T> handler) where T : IMessage
    { ... }

    public void Send<T>(T message) where T : IMessage
    { ... }
}

现在你需要一个地方来注册你的消息处理程序:

var messages = new MessageHandlers();
GameManager.RegisterHandler<PlayerDraw>(messages.Handle);
GameManager.RegisterHandler<PlayerHold>(messages.Handle);

并以这种方式使用你的机制:

GameManager.Send<PlayerDraw>(new PlayerDraw(Arguments))

You can implement something like this:

public interface IMessage
{
}

public class PlayerDraw: IMessage
{
    public AnyType Arguments { get; set; }
}

public interface IMessageHandler<T> where T: IMessage
{
    void Handle(T message);
}

private class MessageHandlers: 
   IMessageHandler<PlayerDraw>, 
   IMessageHandler<PlayerHold>
{
    public void Handle(PlayerDraw message)
    {
        // Use your message here;
    }

    public void Handle(PlayerHold message)
    {
        // Use your message here;
    }

    ...
}

public class GameManager
{
    private readonly Dictionary<Type, Action<IMessage>> _messageRoutes = 
        new Dictionary<Type, Action<IMessage>>();

    public void RegisterHandler<T>(Action<T> handler) where T : IMessage
    { ... }

    public void Send<T>(T message) where T : IMessage
    { ... }
}

Now you need a place to register your message handlers:

var messages = new MessageHandlers();
GameManager.RegisterHandler<PlayerDraw>(messages.Handle);
GameManager.RegisterHandler<PlayerHold>(messages.Handle);

And use your mechanism in such way:

GameManager.Send<PlayerDraw>(new PlayerDraw(Arguments))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文