I created chess game with python and pygame. Now I'm trying to make Artificial intelligence, but what is actually best way to do it? Some tips, tricks, links?


closed as too broad by bummzack, Jari Komppa, MichaelHouse Jun 12 '14 at 13:21

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.


First, you need a rating function. A rating-function analyzes the board, rates which player currently appears to have an advantage, and outputs it as a positive (white has an advantage) or negative (black has an advantage) number.

A trivial way to do this is to just count the material advantage using the relative value system (pawn: 1, bishop: 3, knight: 3, rook: 5, queen: 9). But a more advanced function would also take into account how well the pieces are positioned (for example by increasing value when the piece threatens or protects other pieces or reducing value when it is threatened itself without being adequately protected).

A mate-position would have a rating of +∞ or -∞, depending on which player mates.

Then you use this function to calculate the rating of every currently possible AI move followed by every possible player response to that move. Then pick the AI move where the enemy response with the worst rating (from the PoV of the AI) is still rated higher than the worst-rated response of every other AI move.

A cheap way to make the AI stronger is to not just explore one move into the future but several moves. But keep in mind that the computation time will increase exponential. A much more efficient and elegant way, however, is usually to improve your rating function so the AI can make better strategic decisions.

This method is called the minmax algorithm, and it can be applied to any turn-based game without information hiding. The only thing you have to adapt to each game is the rating function. In a game like chess with a large number of possible moves each turn, the performance can be enhanced through alpha-beta pruning (not following obviously bad tree branches).

Another enhancement which is implemented by most chess engines is an opening library. Chess opening theory is a very well-researched area which is much too complex to model with one simple algorithm. There are whole books which just explore the strategical aspects of one single chess opening. That's why chess engines do the same thing most chess players do: Learn the most common openings and the usual reactions to them and just play them from memory.


Not the answer you're looking for? Browse other questions tagged or ask your own question.