From 62f23b0d9d6c1d53a5cd0de3619b67fbdadc2fd0 Mon Sep 17 00:00:00 2001 From: kraktus Date: Tue, 26 Nov 2024 19:32:25 +0100 Subject: [PATCH] Add `plyToTurn` util function It's defined in three different places in lila. --- src/util.test.ts | 10 +++++++++- src/util.ts | 2 ++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/util.test.ts b/src/util.test.ts index b1bc79aa..b0beb8f3 100644 --- a/src/util.test.ts +++ b/src/util.test.ts @@ -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 }); @@ -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); +}); diff --git a/src/util.ts b/src/util.ts index 1b68ba02..b054bc26 100644 --- a/src/util.ts +++ b/src/util.ts @@ -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 {