Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

AI HELP ! Find a "safe" spot on a 2D grid.

Discussion in 'Scripting' started by gmihaylov, Mar 12, 2020.

  1. gmihaylov

    gmihaylov

    Joined:
    Mar 12, 2020
    Posts:
    4
    Hello. ( not sure if I post this in the correct section, mods move it if required)

    I am developing AI for a 2D board game and I am thinking of what is the best way to find a "safe spot" on the 2D grid.

    As an example:

    A human player has figures that can move and then shoot. He can play once with every figure.

    Figure 1 - moves like a Rook, but only 1 space away -> shoots in chosen diagonal

    Figure 2 - moves like a Queen, but only 3 spaces away -> shoots in chosen perpendicular direction

    Figure 3 - moves like a Knight -> shoots all adjacent perpendicular enemies

    AI Figures:

    I have an enemy MAIN unit that should avoid getting hit (if it gets destroyed, the AI will lose the game). That unit can move one space to the LEFT / RIGHT or STAY in its position(basically stays on the same row all the time) And there are two other enemy types, that for now just move and try to attack the player figures(They will not try to protect the MAIN unit )

    My brain has tinkered the following so far :

    1. For each position of the MAIN unit can move to

    2. Get all player figures

    3. Temporary move each figure to each possible movement position

    4. Check possible shoot directions for each position
    • So for player figure 1 I would have to move it to the 4 possible positions, check all shooting directions in each(4 diagonals and check each square along for any AI units that will block the shot)
    I checked with Alpha Beta pruning algorithm for chess, but I think it will be too complex to implement for my game as all units can move each turn and they can also shoot.

    I personally think this is a very straightforward approach and it might be very slow (if there are a lot of figures on the board, say like chess). Are there any smarter ways to do that?
     
  2. WallaceT_MFM

    WallaceT_MFM

    Joined:
    Sep 25, 2017
    Posts:
    394
    If you're feeling adventurous, you could try using Unity's machine learning libraries and self-train models to learn the best way to play.
    But that's probably overkill. Your approach is pretty brute-force, and I'm not sure how smart it would look to a player. It can look one move ahead, but even novice players will probably be able to look ahead 3 or 4 moves. Instead, you could try thinking about making strategies and heuristics for them to use ("control the center", "don't move the same piece twice", "castle when under pressure") and implement those first.
    If you're new to AI, Behavior Trees will be a good place to look for this sort of thing. Some AI devs also like state machines. There are a lot of other possible techniques, but those are a good place to start and should have the most learning resources.
     
  3. gmihaylov

    gmihaylov

    Joined:
    Mar 12, 2020
    Posts:
    4
    Thank you for your reply.

    the thing is, this is for an intake assignment, for a company( i am given limited time). They also require me to write what can be improved at the game(so I think ill write some AI improvement ideas), so I might choose first the brute force way( due to time constraints, and explain what are the potential improvements). The three AI behaviours are explained like :
    1 figure - just move forward if possible and shoot
    2 figure- move to closes enemy and shoot
    3 figure - avoid player shooting at it.

    There is nothing mentioned like "The first two figures should try to barrier the 3rd figure" (if the 3rd figure dies, AI looses)
     
  4. gmihaylov

    gmihaylov

    Joined:
    Mar 12, 2020
    Posts:
    4
    Oh, and I forgot to mention.

    I think the strategies like "don't move the same piece twice", "castle when under pressure" will not apply to this game, as it differs from chess and players can move ALL their pieces once in each turn.
     
  5. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,835
    Most practical AI players for "board games" involve enumerating every single legal move you can make from the current position. (Many then continue with a tree search and check every legal counter-move, and every legal counter-counter-move, etc. But even if you're not doing a tree search, you still almost always enumerate every currently-legal move, unless the game in question has an unusually huge number of legal moves.)

    Since you can move multiple pieces per turn, I would do a quick calculation to estimate how many legal combinations of moves exist to see whether it would be practical to enumerate all of those, too.

    But anyway, once you're enumerating all legal moves anyway, it's probably not very difficult to just keep a count for every space of how many different enemy pieces can potentially attack that space within 1 move, and then choose the smallest available count from within your legal moves.
     
    Yoreki likes this.
  6. gmihaylov

    gmihaylov

    Joined:
    Mar 12, 2020
    Posts:
    4
    So what you mean is to go through all player pawns and number (if that's what you mean by enumerating) each tile with the number of player figures that can potentially 'attack' that tile. Then I can use that calculation for multiple units(that should find a safe spot).