algebraic to index func
This commit is contained in:
parent
d1b8b777ac
commit
9032a40b24
1 changed files with 16 additions and 0 deletions
|
|
@ -51,3 +51,19 @@ 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;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue