• Looking for something?

Cheating AI from Hardcourt – Plan it out

Hardcourt

Hardcourt

Working with AI is a real pain, but I say, it most likely depends on what your game needs, what you want it to do. Most developers who are new would quickly ask the magical word “how?” or “how to start?”. If you haven’t had experience or good experience with AI patterns and behaviors, hear me out on how I did it with this game.

With my first game, Hardcourt, it was also my first time to encounter programming (or should I say cheating) artificial intelligence (AI). I myself didn’t have proper educational background with AI patterns and behaviors else haven’t even read anything about it when I made that game (I still had dial-up before, so no online tutorials too). With the first build of Hardcourt there were no AI so no playing with CPU, it was just a 2 player on a single PC game. After some time I decided to make it into a much complete game and had been finalized as it is now. I’ve had received good reviews with how the AI performs. It can totally beat the players up (esp with quick play, hard mode).

AI was the most troubling part as I thought; I didn’t even have an idea where to begin with. Now let me share a little knowledge on how I have dealt with this so called “artificial intelligence” and cheated my way through it. This shall be some multi-part discussion on the step by step methods when I dealt with them.


Note: Starting here, we reference the character AI (which is the CPU controlled enemies) as our player or the primary character; hence the human player will be referenced as the enemy. So don’t confuse yourselves.

Charts:

Flow charts were the first logical programming I had; it can clearly display your problem’s logic through proper data flow. So basically, what I wanted was the AI to do all the same things that a human can and would do (just the common and usual behaviors):

  1. go after the ball
  2. shoot
  3. steal the ball
  4. go offensive (attack)

… it does look simple, but where do we start?

Basic Behaviors

First thing to do is look for actions which least require external forces/object interaction but executes the aim of the game.

These steps are primarily available for those “race to finish” games where the primary aim is achieved without requiring going after opponents. This would be good for certain sports games, racing, who-is-faster, etc games. For hardcourt, since the game is basketball, the primary aim is to shoot into the hoops. So that would include going after the ball and shooting the ball.

Basic Chart - Get, move shoot

Basic Chart - Get, move shoot

To explain the chart through words:

  1. See if our character has the ball or not,
  2. if not, get the ball and check again if the ball was acquired (»1)
  3. else if yes, shoot the ball,
  4. then check if the ball was acquired again (»1)

It’s actually a very simple loop but provides the main action required to fulfill the game’s primary purpose. Upon testing this basic behavior, the character shall go after & get the ball, shoot, do get the ball again and repeat the process.

Expanding for more Interactions

The next step is to consider the external factors. This is where the character considers the status or actions of another character. It can either be to support allies or to go after enemies depending of certain conditions.

Case: What if the enemy has possession of the ball? – Then steal the ball.

Interactive Chart - Steal the ball

Interactive Chart - Steal the ball

From this case after knowing that the ball is not in the character’s possession we have to determine where the ball specifically is.

  1. So the enemy has the ball:
  2. if so, we have to steal it
  3. else if not, we just go after and get the ball.
  4. Either way we loop again with checking if we have the ball’s possession.

So basically we determine the ball’s condition, our character doesn’t just chase it, and he has to steal it if it’s in the enemy’s possession. In case you’re wondering why we did not consider getting the ball directly after stealing the ball is because there will chances that stealing the ball would simply fail and the enemy still has its possession.

Case: The enemy doesn’t have the ball and we don’t want the enemy to get a hold of it. – go offensive, attack!

On this next expansion, we decide to make our character more aggressive and attempt to stop the enemy to from gaining the ball.

Interactive Chart 2 - Go offensive, attack!

Interactive Chart 2 - Go offensive, attack!

Now we made it clearer that for stealing the ball we attack the enemy. For this, before going after the ball, we determine if the enemy is closer than the ball is.

  1. Check if enemy is closer than the ball,
  2. if so, attack him
  3. else if not, we go after ball first.
  4. Either way we loop again with checking if we have the ball’s possession.

With this chart expansion, our AI now considers the enemy position compared to the ball’s position and determines which action to do. It might not be the wisest decision making but it would comprehend that fact that the enemy is offensive in a way.

Now our AI behavior chart is complete enough to be competitive… or at the least it does what it has to. We can even add some more like evading the enemy when taking a shot, even simply evading enemy attacks or other more complex actions/reactions from various events and circumstances. But with hardcourt, this is all that I would have needed. Although if you’ll consider more decision making for your AI with more specific situations, you’ll be making a better AI and might even be more intelligent than the human.

You want to see these behaviors in action? – analyze and see for yourself: play hardcourt now!

Classic (2 comments) | Fb Comments
Share your thoughts, socialize!