Skip to content
This repository has been archived by the owner on Dec 5, 2024. It is now read-only.

Commit

Permalink
Rewrite in TypeScript (#14)
Browse files Browse the repository at this point in the history
* Start conversion to TypeScript

* Complete TypeScript rewrite

* Use plain static members for game data

* Update readme.md

* Fix merge error

* Use CommonJS import for test

* Fix compile errors

* Update index.ts

* Cleanup

* Export `points`

* Simplify error

* Remove getColour error

* Remove check altogether

Not the best option but I'm not sure how to supress this otherwise

* Update type labels

* Fix type errors

* Version

* No longer Node 13+ specific

* Change browser script

* Minor cleanup
  • Loading branch information
Nixinova authored Jul 7, 2021
1 parent db10327 commit bed79b3
Show file tree
Hide file tree
Showing 30 changed files with 364 additions and 317 deletions.
6 changes: 3 additions & 3 deletions browser.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
if (typeof global === 'undefined') global = {};
import exports from './index.js';
if (typeof window !== "undefined") window.fenFuncs = exports;
if (typeof window !== "undefined") {
window.fenFuncs = require('./src/index.js');
}
21 changes: 0 additions & 21 deletions index.js

This file was deleted.

32 changes: 19 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 8 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fenfurnace",
"version": "0.3.1",
"version": "0.3.1-next",
"description": "A chess engine designed to work entirely from a FEN position.",
"keywords": [
"chess",
Expand All @@ -13,21 +13,19 @@
"repository": "git+https://github.com/CarbonChess/FenFurnace.git",
"license": "ISC",
"author": "CarbonChess",
"type": "module",
"main": "index.js",
"main": "src/index.js",
"files": [
"**/*.js"
"src/**/*.d.ts",
"src/**/*.js"
],
"scripts": {
"test": "node test",
"compile": "npx webpack --entry ./browser.js -o ./dist"
"test": "tsc && node test",
"compile": "tsc && npx webpack --entry ./src/browser.js -o ./dist"
},
"dependencies": {},
"devDependencies": {
"webpack": "^5.41.1",
"typescript": "~4.3.5",
"webpack": "^5.42.0",
"webpack-cli": "^4.7.2"
},
"engines": {
"node": ">=13"
}
}
16 changes: 9 additions & 7 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ FenFurnace is available [on npm](https://www.npmjs.org/package/fenfurnace):
- Check that a given move obeys the rules of chess.
- `validation.pieceInWay(startCell, endCell)`
- Check if there are any pieces between two cells.
- `validation.makeMove(startCell, endCell)`
- `makeMove(startCell, endCell)`
- Attempt to move a piece; returns `false` if invalid.
- `undoMove()`
- Undoes and returns the last move.
Expand All @@ -36,20 +36,22 @@ FenFurnace is available [on npm](https://www.npmjs.org/package/fenfurnace):
- Checks whether a given colour (`'w'` or `'b'`) is currently in check.
- `gameEndingStatus(colourId)`
- Check the game has concluded and the result of the game (`'checkmate'`, `'stalemate'`, or `false`) for a given colour (`'w'` or `'b'`).
- `points()`
- Return an object containing points scores for white (`w`) and black (`b`).

### Variables

The following values are given in import `gameData`:
- `castling`: `{ w: { k, q }, b: { k, q } }` (each boolean)
- `boardArray` (array)
- `enpassantSquare` (`null` or string)
- `enpassantSquare` (cell string or `-`)
- `moveList` (array)
- `currentTurn` (`null` or string)
- `halfMoveCount` (int)
- `moveNumber` (int or string)
- `promotionPiece` (string)
- `logList` (array)
- `currentTurn` (`null` or `w` or `b`)
- `halfMoveCount` (int)
- `moveNumber` (int)
- `promotionPiece` (pieceId string)

## Build

Build code for browser use using `npm run compile`.
Bundle local code for browser use with `npm run compile`.
24 changes: 0 additions & 24 deletions src/board/create-board.js

This file was deleted.

23 changes: 23 additions & 0 deletions src/board/create-board.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import gameData from '../variables';
import { Cell, Colour } from '../types';

export default function createBoardArray(fenString: string): void {
const fenParts = fenString.split(' ');

const currentBoard = fenParts[0].split('/');
gameData.currentTurn = fenParts[1] as Colour;

gameData.castling = { w: { k: false, q: false }, b: { k: false, q: false } };
const castleString = fenParts[2];
for (let i = 0; i < castleString.length; i++) {
const colour = castleString[i] === castleString[i].toUpperCase() ? 'w' : 'b' as Colour;
const side = castleString[i].toLowerCase() as ('k' | 'q');
gameData.castling[colour][side] = true;
}

gameData.enpassantSquare = fenParts[3].toUpperCase() as Cell;
gameData.halfMoveCount = +fenParts[4];
gameData.moveNumber = +fenParts[5];

gameData.boardArray = currentBoard.map(val => val.replace(/[0-9]/g, n => '-'.repeat(+n)));
}
10 changes: 5 additions & 5 deletions src/board/create-fen.js → src/board/create-fen.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import gameData from '../variables.js';
import gameData from '../variables';

export default function createFenFromBoardArray() {
export default function createFenFromBoardArray(): string {
let fenString = '';
let blankSquares = 0;

//go through array until end
for (let i in gameData.boardArray) {
//loop through board array to create fen
for (let i = 0; i < gameData.boardArray.length; i++) {
const row = gameData.boardArray[i];

for (let j = 0; j < row.length; j++) {
Expand All @@ -24,7 +24,7 @@ export default function createFenFromBoardArray() {
if (blankSquares > 0) fenString += blankSquares;
blankSquares = 0;

if (i !== '7') fenString += '\/';
if (i !== 7) fenString += '\/';
}

//current turn
Expand Down
6 changes: 3 additions & 3 deletions src/board/setup-board.js → src/board/setup-board.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import gameData from '../variables.js';
import createBoardArray from './create-board.js';
import gameData from '../variables';
import createBoardArray from './create-board';

export default function setupBoard() {
export default function setupBoard(): void {
createBoardArray('rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1');
gameData.moveList = [];
gameData.logList = [];
Expand Down
11 changes: 0 additions & 11 deletions src/board/undo.js

This file was deleted.

11 changes: 11 additions & 0 deletions src/board/undo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import gameData from '../variables';
import createBoardArray from './create-board';

export default function undoMove(): string | undefined {
if (gameData.moveList.length === 0) return;
gameData.moveList.pop();
gameData.logList.pop();
const lastMove = gameData.moveList[gameData.moveList.length - 1];
createBoardArray(lastMove);
return lastMove;
}
15 changes: 0 additions & 15 deletions src/eval/evaluation.js

This file was deleted.

16 changes: 0 additions & 16 deletions src/eval/material-diff.js

This file was deleted.

8 changes: 0 additions & 8 deletions src/helpers.js

This file was deleted.

14 changes: 14 additions & 0 deletions src/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Cell, Colour } from './types';

export function indexToLetter(n: number): string {
// 1-indexed; 0x40=uppercase, 0x60=lowercase
return String.fromCharCode(n + 0x40);
}

export function invertColour(colour: Colour): Colour {
return colour === 'b' ? 'w' : 'b';
}

export function coordsToCell(x: number, y: number): Cell {
return indexToLetter(x) + y as Cell;
}
23 changes: 23 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import gameData from './variables';
import setupBoard from './board/setup-board';
import createBoard from './board/create-board';
import points from './points';
import * as validation from './validation/validation';
import makeMove from './validation/make-move';
import findAllMoves from './validation/all-moves';
import isCheck from './validation/is-check';
import gameEndingStatus from './validation/ending';
import undoMove from './board/undo';

export = {
gameData,
setupBoard,
createBoard,
points,
makeMove,
validation,
findAllMoves,
isCheck,
gameEndingStatus,
undoMove,
}
Loading

0 comments on commit bed79b3

Please sign in to comment.