Skip to content

Latest commit

 

History

History
104 lines (62 loc) · 6.41 KB

README.md

File metadata and controls

104 lines (62 loc) · 6.41 KB

chess

Well-optimized chess move validation engine. (not completed)

CLI implementation

Installation

  1. Install Deno if not installed. (brew install deno)
  2. Clone this repository. (git clone [email protected]:hamza-cskn/chess.git)
  3. Run the project: deno run src/bootstrap.ts

Images

image

image

Understanding the responsibilities of the chess validation engine

Must be fast

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.

Must care exceptions

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.

En Passant

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)

image

Promotions

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)

image

King cannot stay threatened

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.

Castling

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)

image

Dead Position

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)

image

Fifty-move rule

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)

Fivefold Repetition Rule

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)

Must be standalone

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.

How it works?

Pre-conditions

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.)

image
  • 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.

image

Otherwise, if the piece and its king are on the same linear line, then we need a vector from king to piece.

image

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.

image

Then we know the side of possible threats. We'll inspect the direction but, here is the end of the pre-conditions.

Ray tracing

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.

image

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.

Todo

  • Fix this scenario
  • Add GitHub Actions workflow for tests.
  • Configure NPM to run the project instead of using ugly deno... cmd.