简体   繁体   English

Javascript根据先前的数据预测下一步

[英]Javascript predict next move based on previous data

I'm learning to make a simple ai to predict the direction that the player will punch next turn. 我正在学习做一个简单的AI来预测玩家下一回合打卡的方向。 there is only 3 direction, left, right and front. 只有3个方向,左,右和前。

Currently I can only predict the next move based on which direction is the most punched. 目前,我只能根据哪个方向打得最厉害来预测下一步。 Here is my code 这是我的代码

function Actor(name) {
    this.name = name
}

var r = 'right'
var l = 'left'
var f = 'front'

Actor.prototype.punch = function(direct, target) {
    if (this == player) {
        if (!target) target = enemy
        console.log(this.name + ' hit ' + target.name + ' on ' + direct)
        player_log.push(direct)
        console.log(target.name + ' block to the ' + mode(player_log))
    }
    console.log(this.name + ' hit ' + target.name + ' on ' + direct)
}

var player = new Actor('Player')
var enemy = new Actor('Enemy')

var player_log = []

function mode(arr) {
    if (arr.length == 0) return null

    var map = {}
    var max = 1
    var maxEl = arr[0]
    for (var i = 0; i < arr.length; i++) {
        var el = arr[i]

        if (!map[el]) {
            map[el] = 1
        } else {
            map[el]++
        }

        if (map[el] > max) {
            max = map[el]
            maxEl = el
        }
    }

    return maxEl
}

Now, what I want is, if the player have some combo, for example punch right, right, left, right, right, front. 现在,我想要的是,如果玩家有一些连击,例如右击,右击,左击,右击,右击,前击。 What I need to do so that the ai will recognize this combo pattern after a few times the player used this combo. 我需要做的是,让AI在玩家使用此连击几次后即可识别出该连击模式。 So that the same combo won't work anymore and the player must find new combo that the ai didn't recognize yet. 这样同一个连击将不再起作用,玩家必须找到AI尚未识别的新连击。

Also I'm new in this kind of programming. 我也是这种编程的新手。 I need the algorithm, not just the code. 我需要算法,而不仅仅是代码。 And what else I should learn if I want to develop this ai? 如果我想开发这种人工智能,我还应该学什么? I know perhaps there's a lot of library that can do this, but I want to know the basic first before using some library. 我知道也许有很多库可以做到这一点,但是我想在使用某些库之前先了解基本知识。

Genuinely spotting patterns is very complicated. 真正发现模式非常复杂。 Like - research team complicated. 像-研究小组复杂。

However, you could do a simplistic version by recording all the moves so far, then gathering together the last n moves and looking for them in the existing move list in that order. 但是,您可以通过记录到目前为止的所有移动,然后将最后n个移动收集在一起,并按现有顺序在现有移动列表中查找来进行简化。 If you find them (and not just at the end), then you can make a guess that the next move will be the same as the one in the list. 如果找到了它们(而不仅仅是在结尾处),那么您可以猜测下一步将与列表中的相同。 The more times you find the sequence and the next move is the same, the more sure you can be. 找到顺序的次数越多,下一步的动作相同,就越有把握。

Moving on, you could save the times between the moves and start comparing them as well, but that's going to complicate things quite a lot. 继续前进,您可以节省移动之间的时间并开始比较它们,但这会使事情复杂化。

Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM