如何创建一个好的程序结构? [英] How to create a good program structure ?

查看:151
本文介绍了如何创建一个好的程序结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个我写的纸牌游戏程序。

我有 - myhand,mytable和AIhand,AItable。

在桌子上正在进行战斗。

到现在为止只有mytable I写作和使用。

但AItable?

我应该写一个Table类(一个用于我)和一个用于AI吗?

我应该为我和AI制作一个单独的表(类或对象)吗?

你有样本供我查看吗 - 类似案例?

有没有办法创建像迷你引擎这样的东西,它只包含游戏的核心以及附加到其上的其他细节,如图形,对象等?

谢谢。



我的尝试:



i多年来一直在思考这个主题... :)

I have a card game program that I write.
I have - myhand,mytable, and AIhand,AItable.
on the table the fight is taking place.
until now only mytable I write and use.
But AItable?
Should I write a Table class (one for)me and (one for)AI?
Should I make a single table(class or object) for both me and AI?
Do you have a sample for me to look over - a similar case?
Is there a way to create something like a "mini-engine" that will contain only the core of the game and the rest of the details like graphics,objects,etc to be attached to it?
thanks.

What I have tried:

i am thinking at this subject for many years ... :)

推荐答案

在这种情况下你想要做的是声明和实现一个接口。播放器表和AI表都具有常见的功能,但可能以不同的方式实现;这是接口派上用场的地方。你甚至可以定义一个实现接口部分的基类,对于两者来说都是一样的(比如Draw())。



这是一个快速的样本这是如何工作的,用一个愚蠢的psudocode片段来展示如何使用它。



What you want to do in situations like this is to declare and implement an interface. Both the player table and AI table will have features that are common, but might be implemented differently; this is where interfaces come in handy. You can even define a base class that implements the part of the interface that could be the same for both (such as Draw()).

Here's a quick sample of how that works, with a goofy psudocode snippet to show how to use it.

public interface ICardHand
{
   int Draw();
   ICardResult PlayCard(ICard card);
}

public abstract class CardHand
{
   public int Draw()
   {
      ... //logic to draw card
      return numberOfCardsInHand;
   }
}

public class PlayerHand : CardHand, ICardHand
{
   public ICardResult PlayCard(ICard card)
   {
      ... //get the card from the UI thread, etc
      return card.Result;
   }
}

public class AIHand : CardHand, ICardHand
{
   public ICardResult PlayCard(ICard card)
   {
      ... //perform card selection through voodoo and whatnot
      return card.Result;
   }
}

public class CardTable
{
   public IEnumerable<icardhand> Players { get; protected set; }
   
   public void Deal()
   {
      foreach(var hand in Players)
      {
         var count = 0;
         while(count < 7)
         {
            count = hand.Draw();
         }
      }
   }
   ...
}





请记住,我在5分钟内写了这个,没有任何背景。这只是为了向您展示如何针对界面进行编码,而不是使用(我有问题需要这个警告,这就是我给它的原因)。



这也使您可以灵活地添加选项。如果您针对接口进行编码,那么AI是控制它还是人类也无关紧要。您可以使用相同的代码库作为单人游戏运行多人游戏。



至于你的上一个问题,你应该抽象你的游戏引擎足够无论资产如何管理都无关紧要。这将为您提供选项,以防您想使用不同的GUI机制或由于系统更改而需要更改图形(例如DirectX版本更新)。



无论如何,只需要开始思考如何构建游戏类以及如何让它们彼此相关。把它煮成一个结构的原始,这将有助于你定义你的接口。



祝你好运!



Please bear in mind that I wrote this in 5 minutes with no context whatsoever. This is solely to show you how to code against an interface, not for use (I've had questions that required this caveat, which is why I'm giving it).

This also gives you the flexibility of adding options down the road. If you code against an interface, it doesn't matter if an AI is controlling it or a human. You could use the same code base to run a multi-player game as a single player one.

As for your last question, you should abstract your game engine enough that it doesn't matter how the assets are managed. This will give you options, in case you want to use a different GUI mechanism or need to change out the graphics due to system changes (such as DirectX version updates).

Anyway, just start thinking about how you can structure your game classes and how you want them to relate to each other. Boil it down to as primal of a structure as you can, and that will help you define your interfaces.

Best of luck!


这篇关于如何创建一个好的程序结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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