Skip to content

Commit

Permalink
Merge pull request #6 from Mind-Sports-Games/57-add-5-check-variant-c…
Browse files Browse the repository at this point in the history
…hess

57 add 5 check variant chess
  • Loading branch information
JamesHeppell authored Jan 5, 2022
2 parents 68fe1b6 + 110b863 commit 2f88195
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "chessops",
"version": "0.8.1-pstrat3.1",
"version": "0.8.1-pstrat3.2",
"description": "Chess and chess variant rules and operations",
"keywords": [
"chess",
Expand Down
3 changes: 3 additions & 0 deletions src/compat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export function lichessVariantRules(
| 'fromPosition'
| 'kingOfTheHill'
| 'threeCheck'
| 'fiveCheck'
| 'atomic'
| 'horde'
| 'racingKings'
Expand All @@ -72,6 +73,8 @@ export function lichessVariantRules(
return 'chess';
case 'threeCheck':
return '3check';
case 'fiveCheck':
return '5check';
case 'kingOfTheHill':
return 'kingofthehill';
case 'racingKings':
Expand Down
6 changes: 3 additions & 3 deletions src/fen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,13 @@ export function parseRemainingChecks(part: string): Result<RemainingChecks, FenE
if (parts.length === 3 && parts[0] === '') {
const white = parseSmallUint(parts[1]);
const black = parseSmallUint(parts[2]);
if (!defined(white) || white > 3 || !defined(black) || black > 3)
if (!defined(white) || white > 5 || !defined(black) || black > 5)
return Result.err(new FenError(InvalidFen.RemainingChecks));
return Result.ok(new RemainingChecks(3 - white, 3 - black));
return Result.ok(new RemainingChecks(5 - white, 5 - black)); //Old notation counting down, not used in games, therefore using highest check mode 5
} else if (parts.length === 2) {
const white = parseSmallUint(parts[0]);
const black = parseSmallUint(parts[1]);
if (!defined(white) || white > 3 || !defined(black) || black > 3)
if (!defined(white) || white > 5 || !defined(black) || black > 5)
return Result.err(new FenError(InvalidFen.RemainingChecks));
return Result.ok(new RemainingChecks(white, black));
} else return Result.err(new FenError(InvalidFen.RemainingChecks));
Expand Down
4 changes: 4 additions & 0 deletions src/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,10 @@ export class RemainingChecks {
return new RemainingChecks(3, 3);
}

static fiveCheck(): RemainingChecks {
return new RemainingChecks(5, 5);
}

clone(): RemainingChecks {
return new RemainingChecks(this.white, this.black);
}
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ export const RULES = [
'antichess',
'kingofthehill',
'3check',
'5check',
'atomic',
'horde',
'racingkings',
Expand Down
44 changes: 44 additions & 0 deletions src/variant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,46 @@ export class ThreeCheck extends Chess {
}
}

export class FiveCheck extends Chess {
protected constructor() {
super('5check');
}

static default(): FiveCheck {
const pos = super.default();
pos.remainingChecks = RemainingChecks.fiveCheck();
return pos;
}

static fromSetup(setup: Setup): Result<FiveCheck, PositionError> {
return super.fromSetup(setup).map(pos => {
pos.remainingChecks = setup.remainingChecks ? setup.remainingChecks.clone() : RemainingChecks.fiveCheck();
return pos;
});
}

clone(): FiveCheck {
return super.clone() as FiveCheck;
}

hasInsufficientMaterial(color: Color): boolean {
return this.board.pieces(color, 'k-piece').equals(this.board[color]);
}

isVariantEnd(): boolean {
return !!this.remainingChecks && (this.remainingChecks.white <= 0 || this.remainingChecks.black <= 0);
}

variantOutcome(_ctx?: Context): Outcome | undefined {
if (this.remainingChecks) {
for (const color of COLORS) {
if (this.remainingChecks[color] <= 0) return { winner: color };
}
}
return;
}
}

class RacingKings extends Chess {
protected constructor() {
super('racingkings');
Expand Down Expand Up @@ -618,6 +658,8 @@ export function defaultPosition(rules: Rules): Position {
return KingOfTheHill.default();
case '3check':
return ThreeCheck.default();
case '5check':
return FiveCheck.default();
case 'crazyhouse':
return Crazyhouse.default();
case 'linesofaction':
Expand Down Expand Up @@ -645,6 +687,8 @@ export function setupPosition(rules: Rules, setup: Setup): Result<Position, Posi
return KingOfTheHill.fromSetup(setup);
case '3check':
return ThreeCheck.fromSetup(setup);
case '5check':
return FiveCheck.fromSetup(setup);
case 'crazyhouse':
return Crazyhouse.fromSetup(setup);
case 'linesofaction':
Expand Down
3 changes: 3 additions & 0 deletions test/variant.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ const insufficientMaterial: [Rules, string, boolean, boolean][] = [
['3check', '8/5k2/8/8/8/8/3K4/8 w - - 3+3', true, true],
['3check', '8/5k2/8/8/8/8/3K2N1/8 w - - 3+3', false, true],

['5check', '8/5k2/8/8/8/8/3K4/8 w - - 5+5', true, true],
['5check', '8/5k2/8/8/8/8/3K2N1/8 w - - 5+5', false, true],

['crazyhouse', '8/5k2/8/8/8/8/3K2N1/8[] w - -', true, true],
['crazyhouse', '8/5k2/8/8/8/5B2/3KB3/8[] w - -', false, false],

Expand Down

0 comments on commit 2f88195

Please sign in to comment.