6
\$\begingroup\$

I am making an air hockey game for Android using AndEngine and its Box2D extension.

How would I make an AI for an air hockey game? For it to work the AI would not only need to move its paddle side to side to defend but also move forward for an attack. What kind of code would I need to implement this?

\$\endgroup\$
1
  • \$\begingroup\$ Not sure if it's excessive, but look into fuzzy logic. \$\endgroup\$
    – Sidar
    Mar 3, 2013 at 18:01

1 Answer 1

11
\$\begingroup\$

The mechanics for air hockey are not particularly deep, so it wouldn't take much to generate reasonable behavior. Think of the movement of the paddle as comprised of several different types of movement:

  • Wander - to some extent people simply move the paddle around, both left and right and forward and back to keep the enemy guessing.
  • Defend - blocking the goal is important (and more important than not blocking the goal) but you want the left right wandering to "lure" the opponent into trying to take a shot.
  • Attack - attempt to score a goal.

So first you need some code that allows for performing the above three behaviors. You'll need code that can take a shot, etc. One of the tricks here is going to be generating the "desired" vector of the paddle that would create a goal when it hits the puck (assuming it isn't blocked). You're going to have to anticipate the position of the puck based on its velocity and anticipate a location far enough out in time that the appropriate velocity of the paddle can be accomplished. Note that this "attack" pattern can be tweaked so it's a defense pattern. I.e., think of the "desired" vector of the paddle being a resulting velocity from its interaction with the puck such that the puck goes anywhere but the AI's own goal. (So in fact, the "Defend" behavior above might be subdivided into two: (1) knock the puck away from its velocity toward the goal; (2) just get in the way of the puck and the goal.) I'd probably start with implementing #2 as it's the most simple, but will still force you to deal with anticipating the results of the puck's velocity.

Once you have your behaviors, rather than have a single one of them control the paddle at any given time, try having all three control the paddle simultaneously, but with different relative weights. So you might say at one moment, the movement is 60% attack, 20% wander, and 20% defend. Average together those movement vectors to determine the paddle's movement.

Next, you'll want to play with the relative weights at different times. The question is, under what circumstances when playing air-hockey do you lean more toward one of the other tactics than another? When somebody inadvertently takes a very slow shot at you, that would seem to be an opportunity for a very aggressive attack. On the other hand, when the puck is coming in fast and toward the goal, that would be a time when you'd have to play as defensively as possible. When the puck is on the other side of the table (the enemy's side), that's a time when "wandering behavior" is more appropriate. Etc. So you'll want to look at the puck to influence the weights.

You might also want to take into consideration the enemy paddle location to determine the weights. (I.e., if the player paddle is far away from defending its own goal, you might want that to increase the % of attack.)

Next, add some error. The computer shouldn't play perfectly, it should slip up a little to make it "feel" more natural. For example, you're going to need to have the AI anticipate the puck location (both for attack and defense), but add the wiggle room to have it anticipate a little early or late.

Finally, for brownie points, you could implement some adaptive behavior. This is probably over-the-top for an airhockey game, but what I mean is, you can have the AI tweak the relative weights of its behaviors based on its past success or failure. When playing a game, rather than have forced percentage weights based on the above circumstances, tweak them a little each game randomly. Then look at the results: was the AI winning more when it played with a particular weighting distribution? If so, use that as the new default and start tweak-randomizing from there.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .