Today I Learned - Rocky Kev

TIL RXJchess and how it was built

POSTED ON:

TAGS:

Found this React JS Chess game RXJchess

I built this site using React, Redux, RxJs (hence the name!) and a handful of other frontend technologies. I always enjoyed playing chess and over the years racked up a lot of hours on apps like chess.com and lichess. At some point during the pandemic, I decided it would be fun to build my own app with a little more visual flair than what these popular apps generally offer, and so this site was born.

I hope you enjoy RxJChess as much as I enjoyed building it. Please feel free to reach out with any questions, comments, or feedback. I'd especially love to discuss alternate approaches to building a chess engine.

Out of curiosity, I wanted to see how it works:

The Repo: https://github.com/prashanthselvam/rxjchess

Honestly, it's incredible.

I never really thought about how to break Chess into it's rules. So to see someone do it in Javascript/React is incredible.

The website

Ah classic React Helmet.

Via https://github.com/prashanthselvam/rxjchess/blob/main/src/pages/index.js#L21

Drawing the chessboard:

via https://github.com/prashanthselvam/rxjchess/blob/main/src/components/chessboard/index.tsx#L16

Types and Consts

Defining all the pieces.

via https://github.com/prashanthselvam/rxjchess/blob/main/src/pages/index.js#L21

BP1 = Black Pawn 1
BP8 = Black Pawn 8
BN1 = Black (k)Night 1
WB2 = White Bishop 2

I think PROMO_PIECES is when a a pawn gets promoted?

via https://github.com/prashanthselvam/rxjchess/blob/main/src/types/constants.ts#L3

Setting up the Game board:

via https://github.com/prashanthselvam/rxjchess/blob/main/src/types/constants.ts#L235

Game States:
via https://github.com/prashanthselvam/rxjchess/blob/main/src/types/types.d.ts#L6

How each piece moves

This is wild:

export const rookMoves: MoveFunction = (
player,
tileId,
tileMap,
forAttackCalc = false

) => {
const board = _getBoard(player);
// @ts-ignore
const [x, y] = _getRelativePos(player, tileId);

const allLeftMoves = range(x - 1, -1, -1).map((n) => _getTile(board, n, y));
const allRightMoves = range(x + 1, 8).map((n) => _getTile(board, n, y));
const allUpMoves = range(y + 1, 8).map((n) => _getTile(board, x, n));
const allDownMoves = range(y - 1, -1, -1).map((n) => _getTile(board, x, n));

return [
..._getPossibleMoves(allLeftMoves, tileMap, player, forAttackCalc),
..._getPossibleMoves(allRightMoves, tileMap, player, forAttackCalc),
..._getPossibleMoves(allUpMoves, tileMap, player, forAttackCalc),
..._getPossibleMoves(allDownMoves, tileMap, player, forAttackCalc),
];
};

https://github.com/prashanthselvam/rxjchess/blob/main/src/store/moveFunctions.tsx

Determining how Checkmate works

via https://github.com/prashanthselvam/rxjchess/blob/main/src/store/epics/gameEpics.tsx#L200

How the AI works

It does all the AI calculations in a webworker, calls and returns an action

https://github.com/prashanthselvam/rxjchess/blob/769efe43baa63e5fcffe09f6273414cc4c68105c/src/webWorker/aiGame.worker.js

https://www.npmjs.com/package/js-chess-engine


Related TILs

Tagged:

TIL what is npm Script

Despite their high usage they are not particularly well optimized and add about 400ms of overhead. In this article we were able to bring that down to ~22ms.

TIL fancy methods to transform Javascript Objects

You can use Object.entries(), Object.keys(), Object.fromEntries()...

TIL how to hide your JS code

ONE THING TO NOTE: Encrypting a script is stronger than obfuscation, both methods are still not adequate to protect secret content. Honestly, I don't think it's worth it on a production site, and instead just go with pure server-side if you want security. But it's fascinating.