Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions src/components/PartDescription.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Box, Typography } from "@mui/material";
import { type Messages, useMessages } from "../i18n";
import type { CCComponent } from "../store/component";
import { definitionByComponentId } from "../store/intrinsics/definitions";
import { truthTableOf } from "../store/intrinsics/truthTable";
import { type TruthTable, truthTableOf } from "../store/intrinsics/truthTable";
import { TruthTableView } from "./TruthTableView";

type Summaries = Messages["parts"]["summaries"];
Expand All @@ -19,18 +19,33 @@ export function partSummary(

export function PartDescription({ component }: { component: CCComponent }) {
const messages = useMessages();
const summary = partSummary(messages, component);
const definition = definitionByComponentId.get(component.id);
const table = definition && truthTableOf(definition);
return (
<PartDescriptionView
name={component.name}
summary={partSummary(messages, component)}
table={definition ? truthTableOf(definition) : null}
/>
);
}

export function PartDescriptionView({
name,
summary,
table,
}: {
name: string;
summary: string | null;
table: TruthTable | null;
}) {
return (
<div>
<Typography
variant="subtitle1"
component="h4"
sx={{ fontWeight: "bold" }}
>
{component.name}
{name}
</Typography>
{summary && <Typography variant="body2">{summary}</Typography>}
{table && (
Expand Down
6 changes: 5 additions & 1 deletion src/i18n/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,13 @@ export const en = {
title: "Both switches",
goal: "Make the lamp Out light up only when both switches A and B are ON. Drag parts from the Parts list onto the editor, and drag from pin to pin to connect them. The play button at the bottom right lets you try the switches. When it works, press Check.",
},
"a-only": {
title: "Only switch A",
goal: "Make the lamp Out light up only when switch A is ON and B is OFF. This time, one part alone is not enough.",
},
xor: {
title: "Only one switch",
goal: "Make the lamp Out light up when exactly one of the switches A and B is ON. It does not light up when both are ON.",
goal: 'Make the lamp Out light up when exactly one of the switches A and B is ON. It does not light up when both are ON. "Only switch A" is ready to use as a part.',
},
},
},
Expand Down
6 changes: 5 additions & 1 deletion src/i18n/ja.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,13 @@ export const ja: Messages = {
title: "両方のスイッチ",
goal: "スイッチ A と B が両方 ON のときだけ、ランプ Out がつくようにしましょう。「部品」の一覧から部品をエディタにドラッグして置き、ピンからピンへドラッグしてつなぎます。右下の再生ボタンで、スイッチを切り替えて試せます。できたら「確かめる」を押してください。",
},
"a-only": {
title: "A だけのスイッチ",
goal: "スイッチ A が ON で、B が OFF のときだけ、ランプ Out がつくようにしましょう。今度は、部品を1つ置くだけではできません。",
},
xor: {
title: "片方だけのスイッチ",
goal: "スイッチ A と B のどちらか片方だけが ON のときに、ランプ Out がつくようにしましょう。両方 ON のときはつきません。",
goal: "スイッチ A と B のどちらか片方だけが ON のときに、ランプ Out がつくようにしましょう。両方 ON のときはつきません。「A だけのスイッチ」を部品として使えます。",
},
},
},
Expand Down
26 changes: 21 additions & 5 deletions src/pages/tutorial/stage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ import {
} from "react-router";
import { valueLabel } from "../../common/bitLabel";
import { ConfirmDialog } from "../../components/ConfirmDialog";
import { PartDescription } from "../../components/PartDescription";
import {
PartDescription,
PartDescriptionView,
} from "../../components/PartDescription";
import { TruthTableView } from "../../components/TruthTableView";
import { type Messages, useMessages } from "../../i18n";
import type CCStore from "../../store";
Expand All @@ -38,8 +41,10 @@ import {
} from "../../tutorial/stageStore";
import {
findTutorialStage,
isStageComponent,
isStagePart,
isStagePinNode,
type StageTitleOf,
type TutorialStage,
tutorialStages,
} from "../../tutorial/stages";
Expand Down Expand Up @@ -96,11 +101,11 @@ function StageView({ stage }: { stage: TutorialStage }) {
const navigate = useNavigate();
const stageIndex = tutorialStages.indexOf(stage);
const nextStage = tutorialStages[stageIndex + 1];
const componentName = texts.stageTexts[stage.id].title;
const titleOf: StageTitleOf = (id) => texts.stageTexts[id].title;
const goalTable = truthTableOfCheck(stage.check);

const [store, setStore] = useState(() =>
getTutorialStageStore(stage, componentName),
getTutorialStageStore(stage, titleOf),
);
const [editedComponentId, setEditedComponentId] = useState(stage.componentId);
const [result, setResult] = useState<CheckResult | null>(null);
Expand Down Expand Up @@ -129,7 +134,7 @@ function StageView({ stage }: { stage: TutorialStage }) {
const resetStage = () => {
setIsConfirmingReset(false);
try {
setStore(resetTutorialStage(stage, componentName));
setStore(resetTutorialStage(stage, titleOf));
setResetCount((count) => count + 1);
setEditedComponentId(stage.componentId);
setResult(null);
Expand Down Expand Up @@ -217,6 +222,16 @@ function StageView({ stage }: { stage: TutorialStage }) {
}
/>
))}
{stage.givenParts.map((id) => (
<PartDescriptionView
key={id}
name={titleOf(id)}
summary={null}
table={truthTableOfCheck(
nullthrows(findTutorialStage(id)).check,
)}
/>
))}
</Stack>
</div>
<Box sx={{ flexGrow: 1 }} />
Expand Down Expand Up @@ -254,7 +269,8 @@ function StageView({ stage }: { stage: TutorialStage }) {
isProtectedNode: (nodeId) => isStagePinNode(store, stage, nodeId),
isOffered,
canCreateComponents: stage.allowsNewComponents,
hasProperties: (componentId) => componentId !== stage.componentId,
hasProperties: (componentId) =>
!isStageComponent(stage, componentId),
}}
onEditComponent={setEditedComponentId}
onClose={() => {
Expand Down
48 changes: 6 additions & 42 deletions src/tutorial/check.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import nullthrows from "nullthrows";
import { describe, expect, it } from "vitest";
import { extractNodesIntoNewComponent } from "../store/extractComponent";
import { and, not, or } from "../store/intrinsics/definitions";
import { and, or } from "../store/intrinsics/definitions";
import {
connectTestNodePins,
createTestStore,
Expand All @@ -14,14 +15,14 @@ import {
truthTableOfCheck,
} from "./check";
import {
findTutorialStage,
setUpTutorialStage,
type TutorialStage,
tutorialStages,
} from "./stages";

function stageStore(stage: TutorialStage) {
const store = createTestStore();
setUpTutorialStage(store, stage, "Lamp");
setUpTutorialStage(store, stage, (id) => id);
const pins = store.componentPins.getManyByComponentId(stage.componentId);
const pinOf = (name: string) => {
const pin = pins.find((p) => p.name === name);
Expand All @@ -44,8 +45,8 @@ function wireGate(stage: TutorialStage, gate: typeof and | typeof or) {
return store;
}

const [andStage, xorStage] = tutorialStages;
if (!andStage || !xorStage) throw new Error("Two stages are expected");
const andStage = nullthrows(findTutorialStage("and"));
const xorStage = nullthrows(findTutorialStage("xor"));

describe("inputCombinations", () => {
it("counts up with the first input as the high bits", () => {
Expand Down Expand Up @@ -116,43 +117,6 @@ describe("checkCombinational", () => {
});
});

it("passes the second stage built from AND, OR and NOT", () => {
const { store, pinOf } = stageStore(xorStage);
const id = xorStage.componentId;
const either = registerTestNode(store, id, or.component.id);
const both = registerTestNode(store, id, and.component.id);
const notBoth = registerTestNode(store, id, not.component.id);
const only = registerTestNode(store, id, and.component.id);
const connect = (
from: Parameters<typeof connectTestNodePins>[2],
to: typeof from,
) => connectTestNodePins(store, id, from, to);
for (const [node, gate] of [
[either, or],
[both, and],
] as const) {
connect(pinOf("A"), testNodePin(store, node, gate.inputPin.A));
connect(pinOf("B"), testNodePin(store, node, gate.inputPin.B));
}
connect(
testNodePin(store, both, and.outputPin.Out),
testNodePin(store, notBoth, not.inputPin.In),
);
connect(
testNodePin(store, either, or.outputPin.Out),
testNodePin(store, only, and.inputPin.A),
);
connect(
testNodePin(store, notBoth, not.outputPin.Out),
testNodePin(store, only, and.inputPin.B),
);
connect(testNodePin(store, only, and.outputPin.Out), pinOf("Out"));

expect(checkCombinational(store, id, xorStage.check)).toEqual({
type: "passed",
});
});

it("passes a circuit grouped into a component of the player's own", async () => {
const store = wireGate(andStage, and);
const gates = store.nodes
Expand Down
65 changes: 65 additions & 0 deletions src/tutorial/circuit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import nullthrows from "nullthrows";
import invariant from "tiny-invariant";
import type { Vector2 } from "../common/vector2";
import type CCStore from "../store";
import type { CCComponentId } from "../store/component";
import type { CCComponentPin } from "../store/componentPin";
import { CCConnectionStore } from "../store/connection";
import { type CCNode, CCNodeStore } from "../store/node";
import type { CCNodePinId } from "../store/nodePin";

/** Builds a circuit inside one component, as the editor would when drawn by hand */
export class StageCircuit {
constructor(
private readonly store: CCStore,
readonly componentId: CCComponentId,
) {}

/** The pin inside the component behind its own pin `name` */
pin(name: string): CCNodePinId {
const componentPin = this.store.componentPins
.getManyByComponentId(this.componentId)
.find((pin) => pin.name === name);
return nullthrows(
componentPin?.implementation,
`The component has no pin named ${name}`,
);
}

place(componentId: CCComponentId, position: Vector2): CCNode {
const node = CCNodeStore.create({
parentComponentId: this.componentId,
componentId,
position,
});
this.store.nodes.register(node);
return node;
}

/** By name only on a stage's component, as intrinsic pins are named for display */
pinOf(node: CCNode, pin: CCComponentPin | string): CCNodePinId {
const nodePin = this.store.nodePins
.getManyByNodeId(node.id)
.find((nodePin) =>
typeof pin === "string"
? this.store.componentPins.get(nodePin.componentPinId)?.name === pin
: nodePin.componentPinId === pin.id,
);
return nullthrows(
nodePin,
`The node has no pin ${typeof pin === "string" ? pin : pin.name}`,
).id;
}

connect(from: CCNodePinId, to: CCNodePinId): void {
const rejection = this.store.connections.register(
CCConnectionStore.create({
parentComponentId: this.componentId,
from,
to,
bentPortion: 0.5,
}),
);
invariant(!rejection, `The connection was rejected: ${rejection?.type}`);
}
}
Loading
Loading