【问题标题】:Algorithm for a turn-by-turn game?回合制游戏的算法?
【发布时间】:2017-07-01 08:35:11
【问题描述】:

我正在构建一个命令行战斗模拟器。我有两个团队:redteamblueteam

它们有 4 个特点:

health
attack
defense
rep

我认为前三个是不言自明的。 rep 是声誉或恐惧因素。用户将为两个团队输入功能。代表最高的团队将首先开始攻击。 damage = attack - defense。这再次从health 中减去。然后另一个团队攻击,这个过程一直持续到团队的health <=0attack <= defense,因为在这种情况下不会造成任何损害。

在这种形式下,游戏非常简单,只有生命值发生变化,其他没有。

我的问题是我目前正在使用数组和一个 while 循环,其中所有逻辑都放在一堆嵌套的 if-else 块中。代码真的很乱。有解决这类问题的算法(和数据结构)吗?

一个

【问题讨论】:

  • “特定类型的问题”是什么意思?您已经为您的特定案例创建了一个算法。
  • 具体是指回合制游戏。
  • 我认为每个回合制游戏与其他游戏相比都有其独特之处。适用于其中大多数的算法可能是:while(true) { for(p : players) { p.take_turn(); if(p.won()) return; } }
  • 有很多!阅读有关 AI、专家系统、最小/最大树、声明性库、AI 库等的更多信息。
  • 恕我直言,最好将一些库用于此类游戏。

标签: algorithm data-structures


【解决方案1】:

我不明白为什么你有一堆嵌套的 if-else 块。如果我误解了问题,请告诉我,否则看看这个(^ 表示 XOR 操作):

// Let the players be x[0] and x[1].

// Get the starting player.
int p = x[0].rep >= x[1].rep ? 0 : 1;

// Loop until the current player loses all its health.
while(x[p].health > 0 && x[p].attack > x[p^1].defense)
{
    // Player p makes an attack, and the
    // other player (p xor 1) takes damage.
    x[p^1].health -= x[p].attack - x[p^1].defense;

    // Switch to the other player.
    p ^= 1;
}

Print(Player p^1 wins);

请注意,您也可以使用 1 - p 代替 p ^ 1 来获得相同的功能。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多