Skip to content

Commit

Permalink
Add plyToTurn util function
Browse files Browse the repository at this point in the history
It's defined in three different places in lila.
  • Loading branch information
kraktus committed Nov 26, 2024
1 parent bd1449c commit 62f23b0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/util.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect, test } from '@jest/globals';
import { makeUci, parseUci } from './util.js';
import { makeUci, parseUci, plyToTurn } from './util.js';

test('parse uci', () => {
expect(parseUci('a1a2')).toEqual({ from: 0, to: 8 });
Expand All @@ -12,3 +12,11 @@ test('make uci', () => {
expect(makeUci({ from: 2, to: 3 })).toBe('c1d1');
expect(makeUci({ from: 0, to: 0, promotion: 'pawn' })).toBe('a1a1p');
});

test('plyToTurn', () => {
expect(plyToTurn(1)).toBe(1);
expect(plyToTurn(2)).toBe(1);
expect(plyToTurn(3)).toBe(2);
expect(plyToTurn(4)).toBe(2);
expect(plyToTurn(5)).toBe(3);
});
2 changes: 2 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ export const roleToChar = (role: Role): string => {
}
};

export const plyToTurn = (ply: number): number => Math.floor((ply - 1) / 2) + 1;

export function charToRole(ch: 'p' | 'n' | 'b' | 'r' | 'q' | 'k' | 'P' | 'N' | 'B' | 'R' | 'Q' | 'K'): Role;
export function charToRole(ch: string): Role | undefined;
export function charToRole(ch: string): Role | undefined {
Expand Down

0 comments on commit 62f23b0

Please sign in to comment.