简体   繁体   中英

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. there is only 3 direction, left, right and front.

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. So that the same combo won't work anymore and the player must find new combo that the ai didn't recognize yet.

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. 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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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