Well-optimized chess move validation engine. (not completed)
- Install Deno if not installed. (
brew install deno
) - Clone this repository. (
git clone [email protected]:hamza-cskn/chess.git
) - Run the project:
deno run src/bootstrap.ts
Chess engines(I mean engines that find best moves) like Stockfish and AI software like AlphaZero, have to calculate billions of legal moves. So, one of the responsibilities of a chess validation engine is to work fast.
Chess looks like easy to code. However, I can say it is not. It has strange exceptions that make me tired. Let's see the rules of chess.
A unique chess rule allows a pawn to capture an opponent's pawn as it moves forward two squares from the starting position, preventing potential exploitation and adding tactical possibilities to the game. (wikipedia)
The rule in chess where a pawn can be promoted to any other piece upon reaching the opposite end of the board, provides an opportunity to enhance its power and strategic options.(wikipedia)
A fundamental chess principle states that if the king is in check, the player must make a move that removes the threat; failure to do so would result in an illegal position.
A strategic maneuver in chess where the king and one of the rooks move together to establish the king's safety, involving a unique set of rules governing their movement and ensuring the king's protection. (wikipedia)
Refers to a state in chess where neither player can checkmate the opponent due to insufficient material or the inability to create a winning position, resulting in a draw. (wikipedia)
A chess regulation that declares a draw if no captures are made and no pawn is moved within the last fifty moves, preventing excessively long games and ensuring progress in the gameplay.(wikipedia)
The fivefold repetition rule requires the arbiter to intervene and declare the game drawn if the same position occurs five times, needing no claim by the players. (wikipedia)
Validating a chess move does not matter alone. The engine is a core library and should be able to be used anywhere. That is why a chess validation engine must be standalone, allowing people to integrate it into their systems.
We can understand any 2 squares on the same linear (diagonal, horizontal, or vertical) line using only math functions. (We're using them because they are fast.)
-
Vertical Check - if (x1 === x2) then two square are on same vertical line.
-
Horizontal Check - if (y1 === y2) then two square are on same horizontal line.
-
Diagonal Check - if (abs(x1 - x2) === abs(y1 - y2)) then two square are on same diagonal line.
If any piece and its king are not on the same linear line, then that is impossible to create any threat to its king. Otherwise, we have to check the target direction and king direction. If they are not vertical axes, then that is impossible to create any threat to its king.
Otherwise, if the piece and its king are on the same linear line, then we need a vector from king to piece.
We have to normalize the vector. Set 1 to all positive coordinates, set -1 to all negative coordinates, and leave 0 to all 0 coordinates of it.
Then we know the side of possible threats. We'll inspect the direction but, here is the end of the pre-conditions.
There is 3 ray tracer piece,
- Queen
- Bishop
- Rook
The pieces can go a few squares forward in one move. However, they cannot jump over pieces, you know. So we have to track a linear line to calculate legal moves of them. An important part of the pieces is they can discover check (wikipedia).
If the scenario has passed pre-conditions, this method must verify the movement. The pre-conditions provide a direction from the king to the possible threatening square. We only need to check this direction.
In this example, firstly c5 square will be checked. secondly, d5 will be ignored because it is the piece that will be moved. thirdly e5, and in the end, the iteration will be stopped at f5 because we hit a piece. If our king is in range of the piece we hit, then we can say our rook is not able to move to d7.
- Fix this scenario
- Add GitHub Actions workflow for tests.
- Configure NPM to run the project instead of using ugly deno... cmd.