包含“检查位置”是否更有效? “移动函数”中的函数 用于游戏,还是作为外部功能?

发布于 2024-07-24 02:56:09 字数 621 浏览 6 评论 0原文

我正在用 C++ 创建一个游戏(说到这里,我使用的代码重要吗?),它可以被宽松地描述为棋盘游戏,我想知道这两个“检查角色是否越界”中的哪一个功能更高效:

一:

int main()
{
    //display board
    //get player input
    //move player
    //if player is out of bounds,
      //force player back into bounds
}

//int main()
{
    //same thing, except without last two lines.
}
void move(char input)
{
    //if input does not place player out of bounds
      //move player according to input
}

本质上,第一个移动每个实体,然后检查所有实体位置并相应地重置它们,第二个确保玩家移动不会将他移出界外,而不是等待直到循环结束。

我想知道这两个(系统?)中哪一个比另一个更高效或更快,或者,如果它们都相同,那么哪一个是更好的编码风格?

I'm creating a game in C++ (speaking of which, does the code I use matter?), which coudl be loosely described as a board game, and I'm wondering which of these two "check if character is out of bounds" functions is more efficient:

ONE:

int main()
{
    //display board
    //get player input
    //move player
    //if player is out of bounds,
      //force player back into bounds
}

TWO

//int main()
{
    //same thing, except without last two lines.
}
void move(char input)
{
    //if input does not place player out of bounds
      //move player according to input
}

Essentially, the first one moves every entity, then checks all of the entities positions and resets them accordingly, and the second one makes sure that the players move does not move him out of bounds, reather than waiting until the end of the loop.

I would like to know which of these two (systems?) is more efficient or faster than the other, or, if they're both equal, which one of these would be a better coding style?

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

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

发布评论

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

评论(3

迷你仙 2024-07-31 02:56:09

在移动之前我会确保每次移动都是合法的。 这样你就可以保持理智的状态,而不是尝试某些东西并使“板”处于无效状态。 以面向对象的方式它也更有意义。 该对象不能做出无效的移动,因此该移动是可信的。

I would make sure that ever move is legal before moving it. That way you keep a sane state instead of trying something and leaving the "board" in an invalid state. It also makes more sense in an object oriented way. The object can not make an invalid move therefore the move can be trusted.

尐偏执 2024-07-31 02:56:09

后者更快(不执行和撤消)。 我也认为它是清洁工,但这是一个观点问题。

The latter is faster (Not doing AND undoing). I would also think that it is the cleaner, but that's a matter of opinion.

如果没有你 2024-07-31 02:56:09

我想,在移动之前进行检查会更快,除非一个人出界的可能性非常低。

关于样式,我建议不要从 move() 方法内部检查边界,即使该检查被提取到其自己的方法中,因为这给了您两个“更改 move() 方法的理由”沿线(从而打破单一职责;有些人只在班级级别强制执行这一点,但是我推荐它作为你可以逃脱惩罚的方法)。

Checking before moving would be faster, save, I suppose, the odds of a person going out of bounds being remarkably low.

Regarding style, I recommend not checking the boundaries from within the move() method, even if that check is extracted into its own method, as that gives you two "reasons to change" the move() method down the line (thus breaking Single Responsibility; some people only enforce this at the class level, but I recommend it for methods when you can get away with it).

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