114 lines
5.7 KiB
Vue
114 lines
5.7 KiB
Vue
<script setup lang="ts">
|
|
import type { ChallengeCheck } from '~/composables/useEditor'
|
|
import { tableMarkdown, type TruthTable } from '~/lib/logic/analyze'
|
|
import { apiId, isTable } from '~/lib/logic/puzzles'
|
|
|
|
const ed = useEditor()
|
|
const def = ed.def
|
|
|
|
// The challenge or puzzle this circuit is for.
|
|
const challenge = computed(() => {
|
|
const t = ed.task.value
|
|
const c = t?.challenge ?? t?.puzzle
|
|
return t && c ? { id: t.id, name: c.name, goal: c.goal, inputs: c.inputs, outputs: c.outputs, puzzle: t.puzzle } : undefined
|
|
})
|
|
const solved = computed(() => !!challenge.value && ed.project.value.solved.includes(challenge.value.id))
|
|
const board = computed(() => (challenge.value?.puzzle ? ed.boards[challenge.value.id] : undefined))
|
|
// What the puzzle asks for, as a table to read.
|
|
const target = computed<TruthTable | null>(() => {
|
|
const p = challenge.value?.puzzle
|
|
if (!p) return null
|
|
return { inputs: p.inputs, outputs: p.outputs, rows: p.steps.map(s => ({ ins: p.inputs.map(l => s.set[l] ?? 0), outs: p.outputs.map(l => s.expect[l] ?? 0), settled: true })) }
|
|
})
|
|
watch(
|
|
() => challenge.value?.puzzle,
|
|
p => {
|
|
if (p && !ed.boards[p.id]) void ed.loadBoard(p)
|
|
},
|
|
{ immediate: true },
|
|
)
|
|
const showTarget = ref(true)
|
|
const puzzleLink = computed(() => (challenge.value?.puzzle ? `${location.origin}/puzzle/${apiId(challenge.value.puzzle.id)}` : ''))
|
|
|
|
const table = ref<TruthTable | { error: string } | null>(null)
|
|
watch([() => ed.sim.value, () => def.value.id], () => (table.value = ed.table()), { immediate: true })
|
|
|
|
const check = ref<ChallengeCheck | null>(null)
|
|
const bestHere = computed(() => (challenge.value ? ed.project.value.best?.[challenge.value.id] : undefined))
|
|
watch(() => def.value.id, () => (check.value = null))
|
|
watch(() => ed.sim.value, () => (check.value = null))
|
|
function runCheck() {
|
|
check.value = ed.checkChallenge()
|
|
}
|
|
</script>
|
|
|
|
<template>
|
|
<section v-if="challenge" class="challenge" :class="{ solved }">
|
|
<h3>
|
|
<Icon :name="solved ? 'lucide:badge-check' : 'lucide:flag'" class="size-4" />
|
|
{{ challenge.puzzle ? 'Puzzle' : 'Challenge' }}: {{ challenge.name }}
|
|
</h3>
|
|
<p>{{ challenge.goal }}</p>
|
|
<p class="pins">Pins: {{ challenge.inputs.join(', ') }} → {{ challenge.outputs.join(', ') }}</p>
|
|
<template v-if="target">
|
|
<button class="section-toggle mt-2" @click="showTarget = !showTarget">
|
|
<Icon :name="showTarget ? 'lucide:chevron-down' : 'lucide:chevron-right'" class="size-4" />
|
|
{{ isTable(challenge.puzzle!) ? 'Target truth table' : `Target: ${target.rows.length} steps from power-on` }}
|
|
</button>
|
|
<div v-if="showTarget" class="target-table">
|
|
<EditorTruthTable :table="target" />
|
|
</div>
|
|
</template>
|
|
<button class="btn btn-primary w-full" @click="runCheck"><Icon name="lucide:play" class="size-4" /> Check</button>
|
|
<p v-if="check" class="result" :class="check.pass ? 'pass' : 'fail'">
|
|
<Icon :name="check.pass ? 'lucide:circle-check' : 'lucide:circle-x'" class="size-4 shrink-0" />
|
|
{{ check.message }}
|
|
</p>
|
|
<p class="score">
|
|
<Icon name="lucide:cpu" class="size-4 shrink-0" />
|
|
<span>
|
|
{{ ed.transistors(def.id).toLocaleString() }} transistors now<template v-if="bestHere !== undefined">, best <strong>{{ bestHere.toLocaleString() }}</strong></template>
|
|
</span>
|
|
</p>
|
|
<p v-if="check?.pass && check.record !== undefined" class="result pass">
|
|
<Icon name="lucide:trophy" class="size-4 shrink-0" /> New record! {{ check.record - check.transistors }} fewer than before ({{ check.record }}).
|
|
</p>
|
|
<p v-else-if="check?.pass && check.best === check.transistors" class="hint">Fewer transistors is better: 4 per NAND gate. Can you make it smaller?</p>
|
|
<div v-if="check?.pass && (check.unlocked?.length || check.nextUp?.length)" class="unlock">
|
|
<Icon name="lucide:sparkles" class="size-4 shrink-0" />
|
|
<div>
|
|
<p v-if="check.unlocked?.length"><strong>Unlocked:</strong> {{ check.unlocked.join(', ') }}</p>
|
|
<p v-if="check.nextUp?.length"><strong>New challenges:</strong> {{ check.nextUp.join(', ') }}</p>
|
|
</div>
|
|
</div>
|
|
<template v-if="challenge.puzzle">
|
|
<p v-if="challenge.puzzle.author !== undefined" class="hint mt-2">The author's solution uses {{ challenge.puzzle.author.toLocaleString() }} transistors.</p>
|
|
<EditorScoreChart v-if="board?.players" :summary="board" :mine="bestHere" />
|
|
<p v-else class="hint mt-2">No one has solved it yet. Be the first.</p>
|
|
<button class="btn btn-ghost mt-2 w-full" title="Copy the link to this puzzle" @click="ed.copyText(puzzleLink, 'Puzzle link copied')">
|
|
<Icon name="lucide:link" class="size-4" /> Copy puzzle link
|
|
</button>
|
|
</template>
|
|
</section>
|
|
|
|
<section>
|
|
<div class="flex items-center gap-1">
|
|
<h3 class="grow">Truth table</h3>
|
|
<template v-if="table && !('error' in table)">
|
|
<button class="icon-btn" title="Download as CSV" @click="ed.exportTable(table)"><Icon name="lucide:download" class="size-3.5" /></button>
|
|
<button class="icon-btn" title="Copy as a Markdown table" @click="ed.copyText(tableMarkdown(table), 'Truth table copied')">
|
|
<Icon name="lucide:clipboard-copy" class="size-3.5" />
|
|
</button>
|
|
</template>
|
|
<button class="icon-btn" title="Full screen" @click="ed.dialog.value = 'table'"><Icon name="lucide:maximize-2" class="size-3.5" /></button>
|
|
</div>
|
|
<template v-if="table">
|
|
<p v-if="'error' in table" class="hint">{{ table.error }}</p>
|
|
<template v-else>
|
|
<p v-if="ed.circuitStats.value.sequential" class="hint">This circuit stores state, so the table depends on the order the rows are tried in.</p>
|
|
<EditorTruthTable :table="table" />
|
|
</template>
|
|
</template>
|
|
</section>
|
|
</template>
|