I\'m a chess enthusiast and a programmer. I recently decided to start making a c
ID: 646293 • Letter: I
Question
I'm a chess enthusiast and a programmer. I recently decided to start making a chess engine using my chess and programming knowledge. So here's my question:
What language (I'm familiar with Java, C++ and Python) and methodology should I adapt while writing a chess engine?
A little guidance would be much appreciated.
Edit:
So I decided to make it in JavaScript. I download this Chess UI from github and now I'm all set! My first step would be to write legal moves for it. So can anybody point me in the right direction? (I'm new to jQuery but have alot of programming experience).
P.S: I'm not trying to make a very efficient engine (I know its way too difficult), I just want to get familiar with the process and learn some new techniques along the way.
Explanation / Answer
2072-rated chess player here. I made this website in pure JavaScript over a weekend. It's not a chess engine (I designed it to create entertaining opening positions as sort of a perverse Chess960 engine), but it's a starting point. The source code is here.
There are a lot of complications involved in making a functional board. These include:
First, figuring out how to represent the basic legal moves. You have to kind of do math with the beginning and ending coordinates. For instance, with rook moves, one of the coordinates has to be the same before and after. With knight moves, the sum of the absolute value of the coordinate changes has to be 3, and both coordinates must change. With bishop moves, either the sum of the coordinates remains the same, or they both increase by the same amount. Pawns are trickiest because you not only have to figure out if they can move two squares or one (check the row and the color instead of storing how many moves they've made) but also have to deal with the whole capture-diagonally, move-forward thing.
Capturing is a challenge because of pawns and check. You can't just say that if a piece moves to another piece's square, then it's a capture. After all, pawns can't move to another piece's square to capture - they have their own special way of capturing.
You have to figure out an efficient way to see if enemy pieces are in the way of a piece's move in order to decide if it's legal or not.
Check is challenging to deal with. After every move, you have to check all the squares that the enemy pieces can go to and see if one of them involves your king and, if so, it's an illegal move.
Castling, en passant, promotion, stalemate, forced draws, repetition - none of these are trivial to handle given the scale of the problem.
All chess engines work by looking at all (possibly a heuristically determined subset) of the legal moves in a position and evaluating numbers to represent their relative values by making those moves and recursively doing the same thing for the resulting positions. Your twin problems here are
How to store this data efficiently
How to proceed with this recursive search - after all, you can't let it go on forever, so you have to put a limit and then figure out how to design your algorithm to do the most optimal and thorough search within that limit. For instance, you want to make sure that it at least comes up with some evaluation for each possible starting move, but you might also want it to spend more time evaluating more promising moves instead of giving an equal amount of time to every move.
This all on top of designing the algorithm in the first place, which there is plenty of information available on.
As for which language to go with (although I guess you already decided on JavaScript), I think it depends more on your goal than anything else. I wanted to make mine online (and get better at JavaScript), so JavaScript was my choice. Any object-oriented programming language will do though.
Once you get comfortable with what you're doing, the following resources will probably prove really helpful:
Chess Programming Wiki
Chess Programming Forum
StockFish on Github
Good luck!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.