Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Game Development

Vidit Shah
Vidit Shah
6,037 Points

How are Games like Chess are Coded?

After learning Gaming courses,I came across this question how Do Bots in game can make proper decisions in games Like Chess. Is there Any Algorithm behind the scenes? Nick Pettit

1 Answer

Nick Pettit
STAFF
Nick Pettit
Treehouse Teacher

Hi Vidit,

Creating computer opponents involves creating some kind of artificial intelligence. This can be as basic or as complicated as you want it or need it to be. In fact, the bird character in the Frogs and Logs game is a basic form of AI. Famously, the four ghosts in Pac-Man all have slightly different behaviors - one of them follows the player directly, another is mostly random, and the other two try to position themselves more strategically. Read more here.

Chess is an enormously complex game, so creating a chess AI is quite a large project. In fact, chess AI was the subject of global attention when IBM's supercomputer called Deep Blue defeated chess world champion Garry Kasparov in 1997. Read more here.

There are a lot of ways to create a chess AI (Wikipedia has a good summary here), but most follow the pattern of analyzing the current board, collecting all legal moves, then applying some kind of heuristic analysis to determine which move is the best. Chess is not a solved game, so there's never a "best" move - just the most optimal move given the current circumstances.

This computer analysis can go a lot deeper - for example, the computer can analyze all its current options, then analyze all of its opponent's counter-options, and then analyze all of its options on the next turn, etc etc... This takes a huge amount of processing power, which is why it took a supercomputer in 1997 to beat a chess world champion. There are many variations on this same theme that attempt to optimize the analysis so that the computer doesn't waste time analyzing branches that stem from a bad move at the start.

Vidit Shah
Vidit Shah
6,037 Points

Thank You sir :)

Casey Ydenberg
Casey Ydenberg
15,622 Points

This is nitpicking, but there is sometimes a best move. Deep in the endgame, there are lots of branches of the game that have been solved. In these situations, computers DESTROY human players. Another place where computers can really excel is in the opening, where they simply have huge "books" of openings committed to memory.