Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pla 840 add breakthrough lilalila ws #44

Merged
merged 6 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
node_modules/
dist/

/*.js
/*.d.ts
/*.js.map
build/
7,469 changes: 0 additions & 7,469 deletions package-lock.json

This file was deleted.

33 changes: 12 additions & 21 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,34 +1,25 @@
{
"name": "chessground",
"version": "7.11.1-pstrat2.44",
"version": "7.11.1-pstrat2.45",
"description": "playstrategy.org chess ui, forked from lichess.org",
"type": "commonjs",
"main": "chessground.js",
"typings": "chessground.d.ts",
"main": "dist/chessground.js",
"types": "build/chessground.d.ts",
"dependencies": {},
"devDependencies": {
"@types/chai": "^4.2.12",
"@types/mocha": "^8.0.1",
"@typescript-eslint/eslint-plugin": "^4.32.0",
"@typescript-eslint/parser": "^4.32.0",
"browserify": "^16",
"chai": "^4.2.0",
"esbuild": "^0.21.5",
"eslint": "^7",
"fancy-log": "^1.3.3",
"gulp": "^4",
"gulp-size": "^3",
"gulp-sourcemaps": "^2.6.4",
"gulp-terser": "^1",
"jsdom": "10.0.0",
"jsdom-global": "3.0.2",
"mocha": "^8.1.0",
"prettier": "^2",
"ts-node": "^8.10.2",
"tsify": "^4",
"typescript": "^4.4.4",
"vinyl-buffer": "^1.0.1",
"vinyl-source-stream": "^2",
"watchify": "^3.11.0"
"typescript": "^4.4.4"
},
"scripts": {
"prepare": "npm run compile",
Expand All @@ -39,16 +30,16 @@
"format": "prettier --write .",
"lint": "eslint src/*.ts",
"check-format": "prettier --check .",
"dev": "gulp dev",
"prod": "gulp prod"
"dev": "esbuild src/chessground.ts --bundle --format=esm --outfile=dist/chessground.js",
"prod": "esbuild src/chessground.ts --bundle --format=esm --outfile=dist/chessground.min.js --minify",
"watch": "esbuild src/chessground.ts --bundle --format=esm --watch --outfile=dist/chessground.js"
},
"files": [
"/*.js",
"/*.d.ts",
"/*.js.map",
"/build/**/*.js",
"/build/**/*.d.ts",
"/build/**/*.js.map",
"/assets/*.css",
"/src/*.ts",
"!/gulpfile.js"
"/src/*.ts"
],
"repository": "https://github.com/Mind-Sports-Games/chessground",
"keywords": [
Expand Down
16 changes: 16 additions & 0 deletions src/premove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
const backrank = playerIndex === 'p1' ? '1' : '8';
const files = [];
for (const [key, piece] of pieces) {
if (key[1] === backrank && piece && piece.playerIndex === playerIndex && piece.role === 'r-piece') {

Check warning on line 55 in src/premove.ts

View workflow job for this annotation

GitHub Actions / build

Unnecessary conditional, value is always truthy

Check warning on line 55 in src/premove.ts

View workflow job for this annotation

GitHub Actions / build

Unnecessary conditional, value is always truthy
files.push(util.key2pos(key)[0]);
}
}
Expand Down Expand Up @@ -96,7 +96,7 @@
const backrank = playerIndex === 'p1' ? '2' : '9';
const files = [];
for (const [key, piece] of pieces) {
if (key[1] === backrank && piece && piece.playerIndex === playerIndex && piece.role === 'r-piece') {

Check warning on line 99 in src/premove.ts

View workflow job for this annotation

GitHub Actions / build

Unnecessary conditional, value is always truthy

Check warning on line 99 in src/premove.ts

View workflow job for this annotation

GitHub Actions / build

Unnecessary conditional, value is always truthy
files.push(util.key2pos(key)[0]);
}
}
Expand Down Expand Up @@ -528,6 +528,17 @@
};
}

const breakthroughPawn = (pieces: cg.Pieces, playerIndex: cg.PlayerIndex): Mobility => {
return (x1, y1, x2, y2) => {
if (!diff(x1, x2)) {
// prevents a premove in front of us on a friendly pawn
const pos = util.pos2key([x1, y1 + (playerIndex === 'p1' ? 1 : -1)]) as cg.Key;
if (pieces.has(pos) && pieces.get(pos)?.playerIndex === playerIndex) return false;
}
return diff(x1, x2) < 2 && (playerIndex === 'p1' ? y2 === y1 + 1 : y2 === y1 - 1);
};
};

const noMovement: Mobility = (_, __, ___, ____) => {
return false;
};
Expand All @@ -543,7 +554,7 @@
const piece = pieces.get(key)!;
const role = piece.role;
const playerIndex = piece.playerIndex;
if (!piece) return [];

Check warning on line 557 in src/premove.ts

View workflow job for this annotation

GitHub Actions / build

Unnecessary conditional, value is always falsy

Check warning on line 557 in src/premove.ts

View workflow job for this annotation

GitHub Actions / build

Unnecessary conditional, value is always falsy
const pos = util.key2pos(key);
let mobility: Mobility;

Expand Down Expand Up @@ -1131,6 +1142,11 @@
}
break;

case 'breakthroughtroyka':
case 'minibreakthroughtroyka':
mobility = breakthroughPawn(pieces, playerIndex);
break;

// Variants using standard pieces and additional fairy pieces like S-chess, Capablanca, etc.
default:
switch (role) {
Expand Down
14 changes: 8 additions & 6 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,15 @@ export type Variant =
| 'go19x19'
| 'backgammon'
| 'nackgammon'
| 'breakthroughtroyka'
| 'minibreakthroughtroyka'
| undefined;
export type PlayerIndex = typeof playerIndexs[number];
export type Letter = typeof letters[number];
export type StoneCount = typeof stoneCounts[number];
export type PlayerIndex = (typeof playerIndexs)[number];
export type Letter = (typeof letters)[number];
export type StoneCount = (typeof stoneCounts)[number];
export type Role = `${Letter}-piece` | `p${Letter}-piece` | `s${StoneCount}-piece`;
export type File = typeof files[number];
export type Rank = typeof ranks19[number];
export type File = (typeof files)[number];
export type Rank = (typeof ranks19)[number];
export type Key = 'a0' | `${File}${Rank}`;
export type FEN = string;
export type Pos = [number, number];
Expand All @@ -81,7 +83,7 @@ export type NumberPair = [number, number];

export type NumberQuad = [number, number, number, number];

export type Orientation = typeof orientations[number];
export type Orientation = (typeof orientations)[number];
export type TransformToP1 = (_: Pos, bt: BoardDimensions) => Pos;
export type TranslateBase = (pos: Pos, xFactor: number, yFactor: number, bt: BoardDimensions) => NumberPair;

Expand Down
102 changes: 102 additions & 0 deletions tests/premove.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,3 +214,105 @@ describe('premove() test', () => {
expect(premoves).to.have.members(expected);
});
});

describe('premove() test', () => {
it('breakthroughTroyka pawns can premove forward diagonally and in front of them when there is no piece', () => {
const state = defaults() as State;
configure(state, {
dimensions: { width: 8, height: 8 },
variant: 'breakthroughtroyka',
fen: 'pppppppp/pppppppp/8/8/8/8/PPPPPPPP/PPPPPPPP',
});

const expected = ['e6', 'f6', 'g6'];
const premoves = premove(
state.pieces,
'f7',
state.premovable.castle,
state.dimensions,
state.variant,
state.chess960
);
expect(premoves).to.have.members(expected);
});

it('breakthroughTroyka pawns on the edge can not premove outside the board', () => {
const state = defaults() as State;
configure(state, {
dimensions: { width: 8, height: 8 },
variant: 'breakthroughtroyka',
fen: 'pppppppp/pppppppp/8/8/8/8/PPPPPPPP/PPPPPPPP',
});

const expected = ['g6', 'h6'];
const premoves = premove(
state.pieces,
'h7',
state.premovable.castle,
state.dimensions,
state.variant,
state.chess960
);
expect(premoves).to.have.members(expected);
});

it('breakthroughTroyka pawns can premove to opponent pawn in front of them', () => {
const state = defaults() as State;
configure(state, {
dimensions: { width: 8, height: 8 },
variant: 'breakthroughtroyka',
fen: 'pppppppp/ppp1pppp/8/4p3/4P3/8/PPPP1PPP/PPPPPPPP',
});

const expected = ['d4', 'e4', 'f4'];
const premoves = premove(
state.pieces,
'e5',
state.premovable.castle,
state.dimensions,
state.variant,
state.chess960
);
expect(premoves).to.have.members(expected);
});

it('breakthroughTroyka pawns can not premove to friendly pawn in front of them', () => {
const state = defaults() as State;
configure(state, {
dimensions: { width: 8, height: 8 },
variant: 'breakthroughtroyka',
fen: 'pppppppp/ppp1p1pp/4p3/4p3/4P3/4P3/PPP2PPP/PPPPPPPP',
});

const expected = ['d5', 'f5'];
const premoves = premove(
state.pieces,
'e6',
state.premovable.castle,
state.dimensions,
state.variant,
state.chess960
);
expect(premoves).to.have.members(expected);
});

it('breakthroughTroyka pawns can premove to friendly and opponent pawn in diagonal', () => {
const state = defaults() as State;
configure(state, {
dimensions: { width: 8, height: 8 },
variant: 'breakthroughtroyka',
fen: 'pppppppp/ppp1p1pp/8/4p3/3pPP2/4P3/PPP3PP/PPPPPPPP',
});

const expected = ['d4', 'e4', 'f4'];
const premoves = premove(
state.pieces,
'e5',
state.premovable.castle,
state.dimensions,
state.variant,
state.chess960
);
expect(premoves).to.have.members(expected);
});
});
4 changes: 3 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
"filesGlob": ["src/*.ts"],
"exclude": [],
"compilerOptions": {
"outDir": "./",
"outDir": "./build/",
"rootDir": "src",
"baseUrl": "src",
"declaration": true,
"removeComments": true,
"noEmitOnError": false,
Expand Down
Loading
Loading