A Chess Engine has to perform the following high level functions:
1. Take a Chess Position
2. Find the best move in the position for the player whose side it is to move (some parameters maybe provided to the chess engine like "take 1 minute maximum time to find the best move)
3. Display the best move
How will it be decided whether a move is best for a player?
Well, a chess game would end if either:
1. White Checkmates (1-0)
2. Black Checkmates (0-1)
3. Draw (1/2-1/2)
Hence, if it is white's turn to move, the engine should return a move which leads to 1-0 or next best is 1/2-1/2 or if there is no way that white can avoid checkmate it should return any legal move.
However, its not possible for every position to be evaluated based on only the above 3 results.
Hence, rather than evaluating upto the final result, the position maybe evaluated with a probability score.
E.g. In the position if there is a move that wins the opponents queen, then the probability of winning against the opponent increases.
Hence, it should be the best move and there should be no need to evaluate till the end.
Therefore, alongwith the best move a chess engine also displays a score of the position.
This score should relate with the probability to win.
Higher the score, higher is the probability to win.
E.g. if in a chess game you are a pawn up then your chances of winning is lesser than if you would have been a whole queen up.
Based on the above mechanism of a chess engine we can say that there would be following algorithms atleast to be developed in a chess engine:
1. Move Generation - A chess engine should know all rules of chess (e.g. a bishop moves diagonally and rook moves straight) to generate all the legal moves of a chess position.
2. Evaluate - A chess engine should be able to evaluate a chess position with a probability score of winning.
3. Move making and takeback - A chess engine should be able to change the existing position by making any of its legal move and get a new position.
It should also be able to takeback the move played and return to the previous position.
Why take-back is important?
Well the answer lies in the next algorithm called search
4. Search - Search is what an engine does to find the best move of the position.
It will make a legal move,evaluate the position and record the score,takeback the move and do the same for all other legal moves.
Thus, it will loop across all legal moves to find the best move based on the recorded score.
Then it has to return the best move and its score.