-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add dead-end and infinite-loop detection (#93)
- Loading branch information
Showing
4 changed files
with
133 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<script lang="ts"> | ||
import { createEventDispatcher } from "svelte"; | ||
import { isDeadEnd, type Piles } from "../../utils/pileon"; | ||
import Modal from "../../components/Modal.svelte"; | ||
import IconButton from "../../components/Menu/IconButton.svelte"; | ||
export let piles: Piles; | ||
let show = true; | ||
$: deadEnd = isDeadEnd(piles); | ||
$: piles, (show = true); | ||
const dispatch = createEventDispatcher(); | ||
</script> | ||
|
||
{#if deadEnd && show} | ||
<Modal | ||
title="Dead end" | ||
on:close={() => { | ||
show = false; | ||
}} | ||
> | ||
{#if deadEnd === "dead-end"} | ||
<p>No more possible moves. Undo the last move or start a new game.</p> | ||
{/if} | ||
{#if deadEnd === "infinite-loop"} | ||
<p> | ||
One card can be moved back and forth between two piles. There are no | ||
other possible moves. Undo the last move or start a new game. | ||
</p> | ||
{/if} | ||
<div class="actions"> | ||
<IconButton | ||
icon="Shuffle" | ||
label="Shuffle" | ||
onClick={() => dispatch("shuffle")} | ||
/> | ||
<IconButton icon="Undo" label="Undo" onClick={() => dispatch("undo")} /> | ||
</div> | ||
</Modal> | ||
{/if} | ||
|
||
<style lang="sass"> | ||
.actions | ||
text-align: center | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { Cards } from "two-to-seven-triple-draw"; | ||
import { isDeadEnd } from "../../src/utils/pileon"; | ||
|
||
// ♥♠♦♣ | ||
|
||
describe("isDeadEnd", () => { | ||
it("returns false if there is an empty pile", () => { | ||
const piles = [ | ||
new Cards("8♠ 5♦ J♣ 4♥"), | ||
new Cards("3♠ Q♦ J♠ 4♠"), | ||
[], | ||
]; | ||
expect(isDeadEnd(piles)).toEqual(false); | ||
}); | ||
it("recognizes dead-end", () => { | ||
const piles = [new Cards("8♠ 5♦ J♣ 4♥"), new Cards("3♠ Q♦ J♠ 4♠")]; | ||
expect(isDeadEnd(piles)).toEqual("dead-end"); | ||
}); | ||
it("recognizes infinite-loop", () => { | ||
const piles = [new Cards("K♥ A♣ 7♦"), new Cards("8♠ 5♥ 7♥ 7♠")]; | ||
expect(isDeadEnd(piles)).toEqual("infinite-loop"); | ||
}); | ||
}); |