Jump to content
  • Advertisement
Sign in to follow this  

Game AI

This topic is 4120 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

If this needs to move to the AI section, please do so. I just was afraid this question was far too basic for that section, though. Regardless, I am interested in the actual structure of a simple game AI. What exactly is it composed of? Logic classes full of switches and ifs? Lets say for example, there are three computer players sitting at a table playing a very simple game of War with cards (Highest card wins each hand and the player only can see what is in his/her given hand + what is out for the current hand). Now each player has to be mindful of the order they are playing. For example, the third to go on a given hand wouldn't throw his king on a bunch of twos if he has a lower card to play. On the other hand, he certainly wouldn't throw a king when the card played ahead of him was an ace. Now, I don't need help with coding all of the actual game rules and dealing. That part I have a grasp on. It's the actual computer AI logic I need the help with. I'm sure I would have no problem hacking something up, but it wouldn't be too advanced (endless switches and if statements). Is there any other more solid, and complex method to use that would be any better? Maybe I need to use switches and ifs, but I am exaggerating just how many in my head. How does game AI change as the card game gets much more advanced (blackjack, poker with other players, etc.) If I had to take a crack at it, I would make PlayHighestCard() and PlayLowestCard() functions and then just (a lot of) simple math to make it work. Seems to bulky. Thanks again.

Share this post

Link to post
Share on other sites
It depends I suppose. If it's a simple enough scenario, you could probably cast out every possible scenario into If then statements.

For complex issues (even reasonably complex, like checkers), it may be better to take a different approach.

For example, the first thing you can do is come up with a heuristic... basically, a set of rules for what, in general, is a "better" move than another. Take a turn based game like checkers again.

Let's make up the following heuristics: (they may or may not be TRUE, but they're what our AI will follow)

1) Moving a piece toward the center is better than moving a piece to the outside.
2) Capturing a piece is required if possible (this is a rule of Checkers, and thus MUST be part of our Heuristic)
3) Moving a piece from the back forward is better than moving a piece that is already further forward.

Using these heuristics (specifically 1 and 2), you can assign "points" to various possible moves. For example, moving a piece up one toward the center where it starts in the center row may be worth 2 points (+1 for rule 1, +1 for rule 2). Moving a piece toward the center where it starts 3 rows behind the aforementioned piece may be worth 4 (+1 for rule 1, +4 for rule 2). The points assigned could be more complex than what I'm presenting here of course, but this is just for an example. (in this example above, a piece gets +1 for rule 1 if it moves toward the center when it moves, and it gets +1 for rule 2 for every square behind the "center line" that it is when it moves forward, and a flat gain of +1 if it moves forward in general.)

Note that rule 2 is an absolute restriction. If there's a scenario where it can "jump" another piece, it will not even bother to calculate the moves above (which only move and don't jump).

Comparing the two, the computer would assume that the second move was more "powerful" and choose that move if those were the only two options.

The idea is to have the computer use whatever time you allot it to examine every possible move using these rules, and then guess what the human's best move is, then what the computers' is for the next move after that etc...

What the computer will be looking for is the best possible move for as far in the future as it's allowed to look using this "rule comparison" approach. (the one where the computer's best move by the end gives him the highest "points" using the rules, and the human's best move by the end gives him the lowest "points" using the rules).

When it runs out of time to process (or hits some theoretical limit of turns set in your program), it will take the values it's calculated for the most recent complete "turn", and use that to pick the best starting move that could lead to that conclusion.

I hope this helps a bit to point you in a good direction (for turn-based games like card games or board games at least). The real trick is finding a heuristic that is accurate (that is, one that will indeed give you better board position if you move to said spot etc...).

Share this post

Link to post
Share on other sites
Thanks for an explanation.

For a game with imperfect information, I assume I would have to structure my rules based on probability of something happening before/after each move.

Also, what is one way to store each possible action? Vector with point values?

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!