Skip to content

Commit 97f6dfa

Browse files
committed
fix: fen writer bug
1 parent d90edbb commit 97f6dfa

File tree

2 files changed

+127
-52
lines changed

2 files changed

+127
-52
lines changed

src/model.test.ts

Lines changed: 123 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
indexFromCoord,
2121
NumPair,
2222
Position,
23+
positionToFen,
2324
RankFile,
2425
rankFileFromCoord,
2526
rankFileFromRowCol,
@@ -871,52 +872,125 @@ test('fenToPosition', async t => {
871872
}
872873
});
873874

874-
// test('positionToFen', async t => {
875-
// const testCases: { position: Position; expected: string }[] = [
876-
// { position: new Map(), expected: FEN_EMPTY },
877-
// {
878-
// position: new Map([
879-
// ['a8', { id: '', piece: 'r' }],
880-
// ['b8', { id: '', piece: 'n' }],
881-
// ['c8', { id: '', piece: 'b' }],
882-
// ['d8', { id: '', piece: 'q' }],
883-
// ['e8', { id: '', piece: 'k' }],
884-
// ['f8', { id: '', piece: 'b' }],
885-
// ['g8', { id: '', piece: 'n' }],
886-
// ['h8', { id: '', piece: 'r' }],
887-
// ['a7', { id: '', piece: 'p' }],
888-
// ['b7', { id: '', piece: 'p' }],
889-
// ['c7', { id: '', piece: 'p' }],
890-
// ['d7', { id: '', piece: 'p' }],
891-
// ['e7', { id: '', piece: 'p' }],
892-
// ['f7', { id: '', piece: 'p' }],
893-
// ['g7', { id: '', piece: 'p' }],
894-
// ['h7', { id: '', piece: 'p' }],
895-
// ['a2', { id: '', piece: 'P' }],
896-
// ['b2', { id: '', piece: 'P' }],
897-
// ['c2', { id: '', piece: 'P' }],
898-
// ['d2', { id: '', piece: 'P' }],
899-
// ['e2', { id: '', piece: 'P' }],
900-
// ['f2', { id: '', piece: 'P' }],
901-
// ['g2', { id: '', piece: 'P' }],
902-
// ['h2', { id: '', piece: 'P' }],
903-
// ['a1', { id: '', piece: 'R' }],
904-
// ['b1', { id: '', piece: 'N' }],
905-
// ['c1', { id: '', piece: 'B' }],
906-
// ['d1', { id: '', piece: 'Q' }],
907-
// ['e1', { id: '', piece: 'K' }],
908-
// ['f1', { id: '', piece: 'B' }],
909-
// ['g1', { id: '', piece: 'N' }],
910-
// ['h1', { id: '', piece: 'R' }],
911-
// ]),
912-
// expected: FEN_START,
913-
// },
914-
// ];
915-
916-
// for (const tc of testCases) {
917-
// await t.test(`positionToFen`, () => {
918-
// const expected = positionToFen(tc.position);
919-
// assert.strictEqual(expected, tc.expected.trim().split(' ')[0]);
920-
// });
921-
// }
922-
// });
875+
test('positionToFen', async t => {
876+
const testCases: { position: Position; expected: string }[] = [
877+
{ position: new Map(), expected: FEN_EMPTY },
878+
{
879+
position: new Map([
880+
['a8', { id: '', piece: 'r' }],
881+
['b8', { id: '', piece: 'n' }],
882+
['c8', { id: '', piece: 'b' }],
883+
['d8', { id: '', piece: 'q' }],
884+
['e8', { id: '', piece: 'k' }],
885+
['f8', { id: '', piece: 'b' }],
886+
['g8', { id: '', piece: 'n' }],
887+
['h8', { id: '', piece: 'r' }],
888+
['a7', { id: '', piece: 'p' }],
889+
['b7', { id: '', piece: 'p' }],
890+
['c7', { id: '', piece: 'p' }],
891+
['d7', { id: '', piece: 'p' }],
892+
['e7', { id: '', piece: 'p' }],
893+
['f7', { id: '', piece: 'p' }],
894+
['g7', { id: '', piece: 'p' }],
895+
['h7', { id: '', piece: 'p' }],
896+
['a2', { id: '', piece: 'P' }],
897+
['b2', { id: '', piece: 'P' }],
898+
['c2', { id: '', piece: 'P' }],
899+
['d2', { id: '', piece: 'P' }],
900+
['e2', { id: '', piece: 'P' }],
901+
['f2', { id: '', piece: 'P' }],
902+
['g2', { id: '', piece: 'P' }],
903+
['h2', { id: '', piece: 'P' }],
904+
['a1', { id: '', piece: 'R' }],
905+
['b1', { id: '', piece: 'N' }],
906+
['c1', { id: '', piece: 'B' }],
907+
['d1', { id: '', piece: 'Q' }],
908+
['e1', { id: '', piece: 'K' }],
909+
['f1', { id: '', piece: 'B' }],
910+
['g1', { id: '', piece: 'N' }],
911+
['h1', { id: '', piece: 'R' }],
912+
]),
913+
expected: FEN_START,
914+
},
915+
{
916+
position: new Map([
917+
['a8', { id: '', piece: 'r' }],
918+
['c8', { id: '', piece: 'b' }],
919+
['d8', { id: '', piece: 'q' }],
920+
['e8', { id: '', piece: 'k' }],
921+
['f8', { id: '', piece: 'b' }],
922+
['g8', { id: '', piece: 'n' }],
923+
['h8', { id: '', piece: 'r' }],
924+
['c7', { id: '', piece: 'p' }],
925+
['d7', { id: '', piece: 'p' }],
926+
['f7', { id: '', piece: 'p' }],
927+
['g7', { id: '', piece: 'p' }],
928+
['h7', { id: '', piece: 'p' }],
929+
['a2', { id: '', piece: 'P' }],
930+
['b2', { id: '', piece: 'P' }],
931+
['c2', { id: '', piece: 'P' }],
932+
['d2', { id: '', piece: 'P' }],
933+
['f2', { id: '', piece: 'P' }],
934+
['g2', { id: '', piece: 'P' }],
935+
['h2', { id: '', piece: 'P' }],
936+
['a1', { id: '', piece: 'R' }],
937+
['b1', { id: '', piece: 'N' }],
938+
['c1', { id: '', piece: 'B' }],
939+
['d1', { id: '', piece: 'Q' }],
940+
['e1', { id: '', piece: 'K' }],
941+
['h1', { id: '', piece: 'R' }],
942+
['e4', { id: '', piece: 'P' }],
943+
['e5', { id: '', piece: 'p' }],
944+
['f3', { id: '', piece: 'N' }],
945+
['c6', { id: '', piece: 'n' }],
946+
['a6', { id: '', piece: 'p' }],
947+
['b5', { id: '', piece: 'p' }],
948+
['b3', { id: '', piece: 'B' }],
949+
]),
950+
expected: 'r1bqkbnr/2pp1ppp/p1n5/1p2p3/4P3/1B3N2/PPPP1PPP/RNBQK2R',
951+
},
952+
{
953+
position: new Map([
954+
['a8', { id: '', piece: 'r' }],
955+
['d8', { id: '', piece: 'q' }],
956+
['e8', { id: '', piece: 'k' }],
957+
['f8', { id: '', piece: 'b' }],
958+
['b7', { id: '', piece: 'p' }],
959+
['c7', { id: '', piece: 'p' }],
960+
['e7', { id: '', piece: 'n' }],
961+
['c6', { id: '', piece: 'n' }],
962+
['e6', { id: '', piece: 'b' }],
963+
['f6', { id: '', piece: 'p' }],
964+
['h6', { id: '', piece: 'r' }],
965+
['a5', { id: '', piece: 'p' }],
966+
['d5', { id: '', piece: 'p' }],
967+
['g5', { id: '', piece: 'p' }],
968+
['h5', { id: '', piece: 'p' }],
969+
['d4', { id: '', piece: 'P' }],
970+
['b3', { id: '', piece: 'P' }],
971+
['f3', { id: '', piece: 'N' }],
972+
['g3', { id: '', piece: 'P' }],
973+
['a2', { id: '', piece: 'P' }],
974+
['b2', { id: '', piece: 'B' }],
975+
['c2', { id: '', piece: 'P' }],
976+
['d2', { id: '', piece: 'N' }],
977+
['e2', { id: '', piece: 'P' }],
978+
['f2', { id: '', piece: 'P' }],
979+
['g2', { id: '', piece: 'B' }],
980+
['h2', { id: '', piece: 'P' }],
981+
['a1', { id: '', piece: 'R' }],
982+
['d1', { id: '', piece: 'Q' }],
983+
['f1', { id: '', piece: 'R' }],
984+
['g1', { id: '', piece: 'K' }],
985+
]),
986+
expected: 'r2qkb2/1pp1n3/2n1bp1r/p2p2pp/3P4/1P3NP1/PBPNPPBP/R2Q1RK1',
987+
},
988+
];
989+
990+
for (const tc of testCases) {
991+
await t.test(`positionToFen`, () => {
992+
const expected = positionToFen(tc.position);
993+
assert.strictEqual(expected, tc.expected.trim().split(' ')[0]);
994+
});
995+
}
996+
});

src/model.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,17 +97,18 @@ export function positionToFen(position: Position): string {
9797
}
9898
fen += pd.piece;
9999
}
100-
if ((i + 1) % 8 === 0 && i < 63) {
100+
if ((i + 1) % 8 === 0) {
101101
if (emptySquareCount > 0) {
102102
fen += emptySquareCount.toString();
103103
emptySquareCount = 0;
104104
}
105-
fen += '/';
105+
if (i < COORDS.length - 1) {
106+
fen += '/';
107+
}
106108
}
107109
}
108110
return fen;
109111
}
110-
111112
export function indexFromCoord(coord: Coord | null, orientation: Color): number {
112113
if (coord === null) {
113114
return SQUARE_NONE;

0 commit comments

Comments
 (0)