Skip to content

Commit

Permalink
Fix false infinite-loop detection (#103)
Browse files Browse the repository at this point in the history
  • Loading branch information
kangasta authored Oct 20, 2023
1 parent dab03fd commit 10f172d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
1 change: 1 addition & 0 deletions src/utils/pileon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ const getIsInfiniteLoop =
if (nextPossibleMoves.length - nextPossibleMovesIfLoop.length === 1) {
const nextSourceCards = nextPiles[source].slice(-2);
if (
nextPiles[target][0]?.value !== cards[0].value &&
nextSourceCards.length === 2 &&
nextSourceCards.every((i) => i.value === cards[0].value)
) {
Expand Down
22 changes: 13 additions & 9 deletions test/utils/pileon.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,25 @@ describe("isDeadEnd", () => {
expect(t).toEqual(false);
expect(c).toHaveLength(0);
});
it("returns false if there is no infinite loop", () => {
const piles = [new Cards("2♣ 3♥ 10♣ 10♦"), new Cards("8♣ 10♥")];
const [t, c] = isDeadEnd(piles);
expect(t).toEqual(false);
expect(c).toHaveLength(0);
});
it.each([[["2♣ 3♥ 10♣ 10♦", "8♣ 10♥"]], [["A♦", "5♦ A♠ A♥ A♣"]]])(
"returns false if there is no infinite loop %s",
(piles: string[]) => {
const [t, c] = isDeadEnd(piles.map((i) => new Cards(i)));
expect(t).toEqual(false);
expect(c).toHaveLength(0);
},
);
it("recognizes dead end", () => {
const piles = [new Cards("8♠ 5♦ J♣ 4♥"), new Cards("3♠ Q♦ J♠ 4♠")];
const [t, c] = isDeadEnd(piles);
expect(t).toEqual("dead-end");
expect(c).toHaveLength(0);
});
it("recognizes infinite loop", () => {
const piles = [new Cards("K♥ A♣ 7♦"), new Cards("8♠ 5♥ 7♥ 7♠")];
const [t, c] = isDeadEnd(piles);
it.each([
[["K♥ A♣ 7♦", "8♠ 5♥ 7♥ 7♠"]],
[["K♥ A♣ 7♦ 7♠", "7♣ 5♥ 7♥"]],
])("recognizes infinite loop %s", (piles: string[]) => {
const [t, c] = isDeadEnd(piles.map((i) => new Cards(i)));
expect(t).toEqual("infinite-loop");
expect(c).toEqual(new Cards("7♠"));
});
Expand Down

0 comments on commit 10f172d

Please sign in to comment.