algebraic to index func

This commit is contained in:
Satria 2026-07-25 16:41:01 +07:00
commit 9032a40b24

View file

@ -50,4 +50,20 @@ export class Board {
}); });
}); });
} }
}
function algebraicToIndex(algebraic: string): number {
const FILE_TO_INDEX: Record<string, number> = {
a: 0, b: 1, c: 2, d: 3, e: 4, f: 5, g: 6, h: 7,
}
try {
if (algebraic.length !== 2) return -1;
const [file, rank] = algebraic.split("");
const rankIndex = parseInt(rank) - 1;
const fileIndex = FILE_TO_INDEX[file];
return rankIndex * 8 + fileIndex;
} catch {
return -1;
}
} }