Entry 4916

random ai

   

Submitted by anonymous on June 19, 2010 at 9:49 p.m.
Language: JavaScript. Code size: 861 bytes.

function do_turn(turn, memory){
    var adjacent = [[1,0],[1,-0],[0,1],[0,-1]];
    function get_random_direction(){
        // directions are just integers between 1 and 4
        return Math.floor((4-1)*Math.random()) + 2;
    }
    // randomly move every unit (ignoring if that move is successful or not)
    for(i=0;i<turn.units.length;i++){
        var unit = turn.units[i];
        turn.move(unit.id, get_random_direction());
        // check all adjacent tiles for (enemy) units and attack them
        for(j=0;j<adjacent.length;j++){
            var diff = adjacent[j];
            var x = unit.x + diff[0];
            var y = unit.y + diff[1];
            if (turn.map[x][y].unit && turn.map[x][y].unit.team != turn.team){
                // ATTACK
                turn.attack(unit.id, x, y);
            }
        }
    }
    
}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).