AI for a grid-based board game

0 favourites
  • 6 posts
From the Asset Store
Grid - snap to grid
$1 USD
80% off
Snap to visible grid - perfect solution for any game genre
  • This is for my board game. I am now at the final stage before it can be considered a working prototype. This is also the hardest stage: creating the computer behavior.

    I've created AI systems in the past, and computer behavior is an area that greatly fascinates me. Here are my notes regarding how the computer should behave.

    Computer program selection order:

    1. Computer uses programs in order of highest range (ComputerPrograms.Range) to shortest range.

    Priority order:

    1. Destroy player programs if they are in range and capable of being killed by a computer program this turn.

    2. (Default) Move toward and attack the nearest player program.

    Local variables:

    TargetPlayerProgram: The UID of the player program targeted by the computer program.

    Destruction potential check:

    1. Loop through each player program and compare its active parts (PlayerPrograms.PartsActive) to the currently selected computer program's damage (ComputerPrograms.Damage).

    2. If a player program is able to be killed by the currently selected computer program (PlayerPrograms.PartsActive = ComputerPrograms.Damage), then set its UID to TargetPlayerProgram.

    Highlight all movable squares (call function "HighlightMove") and pick the highlighted square closest to the player program, then do a range calculation for X and Y (squares are 64px in size, so closest square X + ComputerPrograms.Range * 64 >= PlayerPrograms.X OR PlayerProgramParts.X; repeat for Y).

    3. If no player program is able to be killed this turn, proceed to 'Default behavior'

    4. If range calculation succeeds, then move computer program to nearest square and attack.

    5. If range calculation fails, then proceed to 'Default behavior'

    Default behavior:

    1. Loop through each player program and compare distance along X and Y axes (distance(PlayerPrograms, ComputerPrograms)). Set closest player program to TargetPlayerProgram.

    2. Move computer program to the closest square.

    3. Try to attack the player program. Call 'AttackHighlight' and check if target program is overlapping an attack indicator.

    If so, then deal damage. If not, then do nothing (set ComputerPrograms.Finished = true).

    This thread is intended to keep all my questions in a single location instead of spreading them out over several 'How To' posts in the forum.

    First issue: How do I pick the program with the highest range? In the case of conflicts, I am seeking to just pick randomly among the ones that are tied for the highest.

    <img src="http://i.imgur.com/kzSfBUF.png" border="0">

    BoardGame.capx

    Basically you have to compare the variable ComputerPrograms.Range. ComputerPrograms is a family, and Range is a family instance variable. I'm having trouble figuring out how I would compare family members against each other.

  • Currently working at a similar AI picking problem for collision prevention.

    Got the stage, that you need to store an ID of the family Instance to compare "Range" against.

    Hope this helps.

  • I understand I have to store an ID of the family instance.

    However, in terms of the comparison, how would I reference two objects within a family in the same block of code?

  • This is a solution (although a little sloppy).

    Cool project, by the way. Please let me know if there is anything else I can do to help.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • I just realized that the previous capx will work if you only want the ComputerPrograms.Range with highest value.

    If you have multiple instances of family members, it will be better to store the UIDs in a 1D array, sorted from highest range to lowest range. This way the AI will start with UID at array position=0 (highest range), then array position=1, and so on.

    This is the CAPX

    The text instances are only there so you can see the array. I initialized all the elements on the array to some negative number. When you implement the function to read the instances from the array, you can stop when the function reads a number < 0.

    Hope that this helps!

  • makotto, that does it, thanks!

    <img src="http://i.imgur.com/MtArfvA.png" border="0" />

    I've also confirmed the array contents in the debugger.

    I'll get to work on the default behavior next and let you know if I have any issues. AI is a tough subject!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)