Skip to content

Commit

Permalink
Merge pull request #33 from Mind-Sports-Games/pla-560-support-analysi…
Browse files Browse the repository at this point in the history
…s-for-multi-action-games

Update Go FEN parsing to include PassCount
  • Loading branch information
JamesHeppell authored Oct 23, 2024
2 parents 4965d40 + 06021f3 commit 985bc1c
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "stratops",
"version": "0.8.1-pstrat5.1",
"version": "0.8.1-pstrat5.2",
"description": "Strategy game rules and operations",
"keywords": [
"chess",
Expand Down
4 changes: 2 additions & 2 deletions src/fen.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@ test.each(['6,2s,2s,2s,2s,2s,2s/6,5S,5[5S,2s] 2/2/2 2 w 5 1 25'])('parse and mak
});

test.each([
// 'SSS16/19/5S1S11/4S1S4S7/11s7/6s3ss7/S8S2s1S4/4s5Ss2s2S1/4sS7S5/8s9S/3sS3S3S2s3/7s1S1Ss5S/8Ss9/3sS7s2ss2/5s2s2s7/7sS10/4S6S1s5/8S10/16sss[SSSSSSSSSSssssssssss] b - 280 345 0 0 75 29',
'19/19/19/19/19/19/19/19/19/19/19/19/19/19/19/19/19/19/19[SSSSSSSSSSssssssssss] b - 0 75 0 0 75 1',
// 'SSS16/19/5S1S11/4S1S4S7/11s7/6s3ss7/S8S2s1S4/4s5Ss2s2S1/4sS7S5/8s9S/3sS3S3S2s3/7s1S1Ss5S/8Ss9/3sS7s2ss2/5s2s2s7/7sS10/4S6S1s5/8S10/16sss[SSSSSSSSSSssssssssss] b - 280 345 0 0 75 0 29',
'19/19/19/19/19/19/19/19/19/19/19/19/19/19/19/19/19/19/19[SSSSSSSSSSssssssssss] b - 0 75 0 0 75 0 1',
])('parse and make go fens', fen => {
const setup = parseFen('go19x19')(fen).unwrap();
expect(makeFen('go19x19')(setup, { promoted: true })).toEqual(fen);
Expand Down
11 changes: 8 additions & 3 deletions src/fen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export enum InvalidFen {
Fullmoves = 'ERR_FULLMOVES',
PlayerScore = 'ERR_PLAYER_SCORE',
PlayerCaptures = 'ERR_PLAYER_CAPTURES',
PassCount = 'ERR_PASS_COUNT',
Ko = 'ERR_KO',
BackgammonScore = 'ERR_BACKGAMMON_SCORE',
}
Expand Down Expand Up @@ -330,6 +331,7 @@ const parseFenUint = (err: () => Error) => (part: fp.Option<string>): Result<num

const parseScore = parseFenUint(fenErr(InvalidFen.PlayerScore));
const parseCaptures = parseFenUint(fenErr(InvalidFen.PlayerCaptures));
const parsePassCount = parseFenUint(fenErr(InvalidFen.PassCount));

const parseFenSquare = (rules: Rules) => (part: fp.Option<string>): Result<fp.Option<Square>, FenError> =>
fp.pipe(
Expand Down Expand Up @@ -397,7 +399,7 @@ const parseKo = (part: fp.Option<string>): Result<fp.Option<number>> => {
const parseGoFen = (rules: Rules) => (fen: string): Result<Setup, FenError> => {
const [boardAndPockets, ...parts] = fen.split(' ');

if (parts.length !== 8) {
if (parts.length !== 9) {
return Result.err(new FenError(InvalidFen.Fen));
}

Expand All @@ -411,9 +413,10 @@ const parseGoFen = (rules: Rules) => (fen: string): Result<Setup, FenError> => {
parseCaptures(parts[4]),
parseCaptures(parts[5]),
parseScore(parts[6]),
parseFullMoves(parts[7]),
parsePassCount(parts[7]),
parseFullMoves(parts[8]),
])
.map(([{ board, pockets }, turn, ko, p1Score, p2Score, p1Captures, p2Captures, komi, fullmoves]) => ({
.map(([{ board, pockets }, turn, ko, p1Score, p2Score, p1Captures, p2Captures, komi, passCount, fullmoves]) => ({
...defaultSetup(),
board,
pockets,
Expand All @@ -424,6 +427,7 @@ const parseGoFen = (rules: Rules) => (fen: string): Result<Setup, FenError> => {
p2Captures,
ko,
komi,
passCount,
fullmoves,
}));
};
Expand All @@ -442,6 +446,7 @@ export const makeGoFen = (rules: Rules) => (setup: Setup, opts?: FenOpts): strin
int(setup.p1Captures),
int(setup.p2Captures),
int(setup.komi),
int(setup.passCount),
int(setup.fullmoves),
].join(' ');
};
Expand Down
31 changes: 28 additions & 3 deletions src/fp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,19 @@ export function resultZip<A, B, C, D, E, F, G, H, I, Err extends Error>([a, b, c
br.Result<H, Err>,
br.Result<I, Err>,
]): br.Result<[A, B, C, D, E, F, G, H, I], Err>;
export function resultZip<Err extends Error>([a, b, c, d, e, f, g, h, i]: readonly [
export function resultZip<A, B, C, D, E, F, G, H, I, J, Err extends Error>([a, b, c, d, e, f, g, h, i, j]: readonly [
br.Result<A, Err>,
br.Result<B, Err>,
br.Result<C, Err>,
br.Result<D, Err>,
br.Result<E, Err>,
br.Result<F, Err>,
br.Result<G, Err>,
br.Result<H, Err>,
br.Result<I, Err>,
br.Result<J, Err>,
]): br.Result<[A, B, C, D, E, F, G, H, I, J], Err>;
export function resultZip<Err extends Error>([a, b, c, d, e, f, g, h, i, j]: readonly [
br.Result<unknown, Err>,
br.Result<unknown, Err>?,
br.Result<unknown, Err>?,
Expand All @@ -127,8 +139,9 @@ export function resultZip<Err extends Error>([a, b, c, d, e, f, g, h, i]: readon
br.Result<unknown, Err>?,
br.Result<unknown, Err>?,
br.Result<unknown, Err>?,
br.Result<unknown, Err>?,
]): unknown {
const args = [a, b, c, d, e, f, g, h, i].filter(Option.isSome) as br.Result<unknown, Err>[];
const args = [a, b, c, d, e, f, g, h, i, j].filter(Option.isSome) as br.Result<unknown, Err>[];
switch (args.length) {
case 1:
return a.map(a => [a]);
Expand All @@ -154,7 +167,7 @@ export function resultZip<Err extends Error>([a, b, c, d, e, f, g, h, i]: readon
)
)
);
default:
case 9:
return a.chain(a =>
b!.chain(b =>
c!.chain(c =>
Expand All @@ -164,6 +177,18 @@ export function resultZip<Err extends Error>([a, b, c, d, e, f, g, h, i]: readon
)
)
);
default:
return a.chain(a =>
b!.chain(b =>
c!.chain(c =>
d!.chain(d =>
e!.chain(e =>
f!.chain(f => g!.chain(g => h!.chain(h => i!.chain(i => j!.map(j => [a, b, c, d, e, f, g, h, i, j])))))
)
)
)
)
);
}
}

Expand Down
1 change: 1 addition & 0 deletions src/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ export interface Setup {
p2Captures?: fp.Option<number>;
ko?: fp.Option<number>;
komi?: fp.Option<number>;
passCount?: fp.Option<number>;
unusedDice?: fp.Option<string>;
usedDice?: fp.Option<string>;
lastMove?: fp.Option<Move>;
Expand Down

0 comments on commit 985bc1c

Please sign in to comment.