There seems to be an overwhelming amount of info out there on AI of all kinds, and it's kind hard to digest at once.

For my testing purposes, I'm creating a "typical stateless AI", as described in this article, and now I need to implement the following functions for a 2D square based game board:


For example, I can run my own run away function like this:

    [self debugMessage];

    int bestTileIndex = 0;
    int largestDistance = 0;
    int distance = 0;

        for(NSNumber* tileNumber in validMoves)
            distance = [self distanceToTargetFromTile:tileNumber.intValue];
            if(distance >largestDistance)
                largestDistance = distance;
                bestTileIndex = tileNumber.intValue;
    //move to best tile


but this method only checks the distance from 1 target, which may not be the best if there are multiple enemies on the game board.

Where can I find good/efficient implementations of algorithms for searching a 2D grid for these functions? For example finding a point that is furthest away from multiple opponents and is accessible within a certain number of moves.

Other functions that come to mind is picking the best/weakest target, moving towards a specific target while accounting for variable terrain cost, etc

I'm certain someone has already done this before.


closed as off-topic by Sean Middleditch, Josh Dec 17 '13 at 5:41

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions that are about "which tech to use" are outside the scope of the site. For more information, see this meta post" – Sean Middleditch, Josh
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    \$\begingroup\$ Voted to close as requests for libraries or existing code (and anything asking for an open-ended list of answers and not a single concrete answer) is off-topic for GDSE. You may consider rewording the question or asking new ones to ask for the algorithm (not just an implementation) for solving a specific problem. \$\endgroup\$ – Sean Middleditch Dec 17 '13 at 1:53

I don't give you a plain code, but I give you idea and some food for brain. I think it depend on the game, for example if you have a large board with big amount of tile this one thing, but if you have 10x10 board, there is nothing bad to iterate through the tiles for 4 or 8 (with diagonals) directions. iOS devices extremely productive and this won't overload cpu and don't take a long time to go through all possible moves. I made such a thing when I make my reversi game Fresh Reversi, you can check out free version.

For example, you can create a method to go to one direction from your AI player (let's go right), this will be a cycle, and we go while we not away from board or if we found player. So, each iteration you can count tiles for distance and so on. After you find player you can save this possible move to dictionary and call another method for another direction and so on. Hope you got my idea. This is not panacea, but I think it's good for board game type.


Not the answer you're looking for? Browse other questions tagged or ask your own question.