首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C++ AI设计问题

C++ AI设计问题
EN

Stack Overflow用户
提问于 2009-08-21 02:26:46
回答 5查看 1.2K关注 0票数 3

我目前正在为MMORPG编写一个机器人。不过,目前我正试图弄清楚如何很好地实现这一点。设计问题与以正确的顺序施放字符拼写有关。这里有一个简单的例子来说明我需要做些什么。这与强制转换无关,但要按正确的顺序执行。我知道如何简单地随机施放它们,通过检查哪个技能还没有施放,但在GUI中显示的顺序是正确的,而不是真正的。

注意:技能数量可能会有所不同,但并不总是3,最多10。

Charactername < foobar >有3个技能。

技能1:名字( random1 )冷却时间( 1000毫秒)施法持续时间( 500毫秒)

技能2:名字( random2 )冷却时间( 1500毫秒)施法持续时间( 700毫秒)

技能3:名字( random3 )冷却时间( 2000毫秒)施法持续时间( 900毫秒)

我真的不知道如何实现这一点,如果有人有什么想法,请随意分享。我知道大多数人不喜欢在游戏中作弊的想法,我也不喜欢,我也不是真的在玩这个游戏,但这对我来说是一个有趣的领域。

谢谢。

EN

回答 5

Stack Overflow用户

发布于 2009-08-25 06:10:02

这是在冒险进入更多的“智能代理”领域。考虑为你的人工智能建立一个计划数据库。你的施放火球法术计划可能会有一个点燃之火法术的先决条件,这个法术本身也可能有一个成功使用创造气泡法术的先决条件。选择一个计划需要满足所有的前提条件,所以如果你的AI可以创造一个气泡,但不能点燃这个气泡,那么这个计划就失败了,他们不得不做一些其他的事情(也许是重试)。

票数 3
EN

Stack Overflow用户

发布于 2009-08-25 07:14:01

也许从某个事件处理程序中,你想要决定施展什么咒语。也许你可以从这个咒语开始:

代码语言:javascript
复制
public class Caster
{
    private readonly ICastable[] _spells;
    private int _canCastAt;

    public Caster(ICastable[] spells)
    {
        _spells = spells;
        _canCastAt = -1;
    }

    public string GetNextSpellToCast(int currentTime)
    {
        if (currentTime < _canCastAt)
            return null;

        for (int i = 0; i < _spells.Length; i++)
        {
            int durationOfCast = _spells[i].AttemptCast(currentTime);

            if (durationOfCast > 0)
            {
                _canCastAt = currentTime + durationOfCast;
                return _spells[i].GetName();
            }
        }

        return null;
    }
}

施法者会施展法术:

代码语言:javascript
复制
public interface ICastable
{
    string GetName();
    int AttemptCast(int msCurrentTime);
}

你描述了一种特殊的咒语:

代码语言:javascript
复制
public class ChanneledSpell : ICastable
{
    private readonly string _name;
    private readonly int _castDuration;
    private readonly int _castCooldown;
    private int _lastCast;

    public ChanneledSpell(string name, int castDuration, int castCooldown)
    {
        Debug.Assert(castDuration < castCooldown);  // a reasonable assumption the tests makes

        _name = name;
        _castDuration = castDuration;
        _castCooldown = castCooldown;
        _lastCast = -_castCooldown;
    }

    public string GetName()
    {
        return _name;
    }

    public int AttemptCast(int msCurrentTime)
    {
        if (msCurrentTime > _lastCast + _castCooldown)
        {
            _lastCast = msCurrentTime;
            return _castDuration;
        }

        return 0;
    }
}

我看到这个标记为C++,这个答案是C#,尽管我只使用了C++中可用的语言结构,所以它应该是一个简单的翻译。我不能轻易翻译的是一些测试,

代码语言:javascript
复制
[TestFixture]
public class SpellTest
{
    [Test]
    public void TestCanCastOnStartup()
    {
        var sut = new ChanneledSpell(Some.String(), Some.PositiveNonzeroInteger(), Some.PositiveNonzeroInteger());

        int result = sut.AttemptCast(Some.PositiveNonzeroInteger());

        Assert.IsTrue(CastWasMade(result));
    }

    [Test]
    public void TestCantCastUntilCooldown()
    {
        int firstCast = Some.PositiveNonzeroInteger();
        int duration = Some.PositiveNonzeroInteger();
        int cooldown = duration + Some.PositiveNonzeroInteger();  // assuming spell duration is less then cooldown

        var sut = new ChanneledSpell(Some.String(), duration, cooldown);

        int ignored = sut.AttemptCast(firstCast);
        int secondCastAttempt = sut.AttemptCast(firstCast + cooldown - 1);
        int thirdCastAttempt = sut.AttemptCast(firstCast + cooldown + 1);

        Assert.IsFalse(CastWasMade(secondCastAttempt));
        Assert.IsTrue(CastWasMade(thirdCastAttempt));
    }

    [Test]
    public void TestReportsTimeOnCast()
    {
        int duration = Some.PositiveNonzeroInteger();
        int firstCastTime = Some.PositiveNonzeroInteger();

        var sut = new ChanneledSpell(Some.String(), duration, Some.PositiveNonzeroInteger());

        int firstCastAttempt = sut.AttemptCast(firstCastTime);

        Assert.AreEqual(duration, firstCastAttempt);
    }

    private bool CastWasMade(int result)
    {
        return result > 0;
    }
}
票数 2
EN

Stack Overflow用户

发布于 2009-08-21 02:33:13

也许您想要一个包含计划任务的队列。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1309813

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档