From f6c3f40c94ed3e64a4dd5871196d51712f18be6c Mon Sep 17 00:00:00 2001 From: Rn86222 Date: Fri, 20 Nov 2026 00:37:24 +0900 Subject: [PATCH 1/6] feat(tutorial): add the half adder and full adder stages Stage 4, "Adding one digit", introduces binary: ON is 1 and OFF is 0, and A + B is output as the twos digit C and the ones digit S, from And and Xor. Stage 5, "Adding with a carry", adds Cin too, from Or and two of stage 4's part. The stage factory no longer assumes the two switches and one lamp of the first stages. Co-Authored-By: Claude Opus 5.5 --- src/i18n/en.ts | 10 ++++ src/i18n/ja.ts | 10 ++++ src/tutorial/check.test.ts | 15 +++++ src/tutorial/stages.ts | 116 +++++++++++++++++++++++++++++++++---- 4 files changed, 140 insertions(+), 11 deletions(-) diff --git a/src/i18n/en.ts b/src/i18n/en.ts index 62463c1..80d5db5 100644 --- a/src/i18n/en.ts +++ b/src/i18n/en.ts @@ -33,6 +33,16 @@ const stageTexts: Record = { 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.', steps: [], }, + "half-adder": { + title: "Adding one digit", + goal: "Think of ON as 1 and OFF as 0. Then A + B is 0, 1 or 2. In binary, numbers are written with only 0 and 1, so these are 00, 01 and 10: 2 is written 10. Output the twos digit to C and the ones digit to S. C is called the carry and S the sum, and this circuit is called a half adder.", + steps: [], + }, + "full-adder": { + title: "Adding with a carry", + goal: 'Output A + B + Cin as a two-digit binary number to C and S. Cin is the carry coming from the digit below. The answer is 0 to 3, and 3 is 11 in binary. "Adding one digit" is ready to use as a part. This circuit is called a full adder.', + steps: [], + }, }; export const en = { diff --git a/src/i18n/ja.ts b/src/i18n/ja.ts index 1dd86fd..cd76b8a 100644 --- a/src/i18n/ja.ts +++ b/src/i18n/ja.ts @@ -142,6 +142,16 @@ export const ja: Messages = { goal: "スイッチ A と B のどちらか片方だけが ON のときに、ランプ Out がつくようにしましょう。両方 ON のときはつきません。「A だけのスイッチ」を部品として使えます。", steps: [], }, + "half-adder": { + title: "1けたの足し算", + goal: "ON を 1、OFF を 0 と考えると、A+B は 0、1、2 のどれかです。0 と 1 だけで数を書く2進数では、これを 00、01、10 と書きます(2 は 10 です)。2の位を C に、1の位を S に出しましょう。C を「桁上がり」、S を「和」といい、この回路を「半加算器」といいます。", + steps: [], + }, + "full-adder": { + title: "桁上がりつきの足し算", + goal: "A+B+Cin の答えを、2けたの2進数で C と S に出しましょう。Cin は、下のけたから来る桁上がりです。答えは 0 から 3 で、3 は2進数で 11 です。「1けたの足し算」を部品として使えます。この回路を「全加算器」といいます。", + steps: [], + }, }, }, componentProperties: { diff --git a/src/tutorial/check.test.ts b/src/tutorial/check.test.ts index b8411d3..687a25c 100644 --- a/src/tutorial/check.test.ts +++ b/src/tutorial/check.test.ts @@ -79,6 +79,21 @@ describe("truthTableOfCheck", () => { }); }); + it("lists several outputs in the order the stage names them", () => { + const halfAdder = nullthrows(findTutorialStage("half-adder")); + + expect(truthTableOfCheck(halfAdder.check)).toEqual({ + inputs: ["A", "B"], + outputs: ["C", "S"], + rows: [ + { inputs: [false, false], outputs: [false, false] }, + { inputs: [false, true], outputs: [false, true] }, + { inputs: [true, false], outputs: [false, true] }, + { inputs: [true, true], outputs: [true, false] }, + ], + }); + }); + it("gives no table when a pin carries more than one bit", () => { const wideInput: CombinationalCheck = { inputs: [{ name: "A", bitWidth: 2 }], diff --git a/src/tutorial/stages.ts b/src/tutorial/stages.ts index 560fde6..2ca389d 100644 --- a/src/tutorial/stages.ts +++ b/src/tutorial/stages.ts @@ -6,12 +6,25 @@ import { CCComponentStore, } from "../store/component"; import type { CCComponentPin } from "../store/componentPin"; -import { and, input, not, or, output } from "../store/intrinsics/definitions"; +import { + and, + input, + not, + or, + output, + xor, +} from "../store/intrinsics/definitions"; import { type CCNodeId, CCNodeStore } from "../store/node"; import type { CombinationalCheck } from "./check"; import { StageCircuit } from "./circuit"; -export const tutorialStageIds = ["and", "a-only", "xor"] as const; +export const tutorialStageIds = [ + "and", + "a-only", + "xor", + "half-adder", + "full-adder", +] as const; export type TutorialStageId = (typeof tutorialStageIds)[number]; export type StageTitleOf = (id: TutorialStageId) => string; @@ -34,31 +47,47 @@ const gateParts = [and, or, not].map((gate) => gate.component.id); const bit = (value: boolean) => [value]; const isOn = (value: readonly boolean[] | undefined) => nullthrows(value?.[0]); -function twoSwitchesStage( +const oneBitInputs = (...names: string[]) => + names.map((name) => ({ name, bitWidth: 1 })); + +function defineStage( id: TutorialStageId, - definition: Pick & + definition: Pick & Partial>, - lampIsOn: (a: boolean, b: boolean) => boolean, ): TutorialStage { return { id, componentId: `tutorial-${id}` as CCComponentId, givenParts: [], - ...definition, allowsNewComponents: false, + ...definition, + }; +} + +function twoSwitchesStage( + id: TutorialStageId, + definition: Pick & + Partial>, + lampIsOn: (a: boolean, b: boolean) => boolean, +): TutorialStage { + return defineStage(id, { + ...definition, check: { - inputs: [ - { name: "A", bitWidth: 1 }, - { name: "B", bitWidth: 1 }, - ], + inputs: oneBitInputs("A", "B"), outputs: ["Out"], expected: (values) => ({ Out: bit(lampIsOn(isOn(values.A), isOn(values.B))), }), }, - }; + }); } +/** The two digits of `total` in binary, as the carry C and the sum S */ +const carryAndSum = (total: number) => ({ + C: bit(total >= 2), + S: bit(total % 2 === 1), +}); + const andStage = twoSwitchesStage( "and", { @@ -128,10 +157,75 @@ const xorStage = twoSwitchesStage( (a, b) => a !== b, ); +const halfAdderStage = defineStage("half-adder", { + parts: [and.component.id, xor.component.id], + check: { + inputs: oneBitInputs("A", "B"), + outputs: ["C", "S"], + expected: (values) => + carryAndSum(Number(isOn(values.A)) + Number(isOn(values.B))), + }, + solve(circuit) { + const carry = circuit.place(and.component.id, { x: 0, y: -100 }); + const sum = circuit.place(xor.component.id, { x: 0, y: 100 }); + for (const [node, gate] of [ + [carry, and], + [sum, xor], + ] as const) { + circuit.connect(circuit.pin("A"), circuit.pinOf(node, gate.inputPin.A)); + circuit.connect(circuit.pin("B"), circuit.pinOf(node, gate.inputPin.B)); + } + circuit.connect(circuit.pinOf(carry, and.outputPin.Out), circuit.pin("C")); + circuit.connect(circuit.pinOf(sum, xor.outputPin.Out), circuit.pin("S")); + }, +}); + +const fullAdderStage = defineStage("full-adder", { + parts: [or.component.id], + givenParts: ["half-adder"], + check: { + inputs: oneBitInputs("A", "B", "Cin"), + outputs: ["C", "S"], + expected: (values) => + carryAndSum( + Number(isOn(values.A)) + + Number(isOn(values.B)) + + Number(isOn(values.Cin)), + ), + }, + solve(circuit) { + const first = circuit.place(halfAdderStage.componentId, { + x: -100, + y: -100, + }); + const second = circuit.place(halfAdderStage.componentId, { + x: 60, + y: 100, + }); + const either = circuit.place(or.component.id, { x: 170, y: -130 }); + circuit.connect(circuit.pin("A"), circuit.pinOf(first, "A")); + circuit.connect(circuit.pin("B"), circuit.pinOf(first, "B")); + circuit.connect(circuit.pinOf(first, "S"), circuit.pinOf(second, "A")); + circuit.connect(circuit.pin("Cin"), circuit.pinOf(second, "B")); + circuit.connect( + circuit.pinOf(first, "C"), + circuit.pinOf(either, or.inputPin.A), + ); + circuit.connect( + circuit.pinOf(second, "C"), + circuit.pinOf(either, or.inputPin.B), + ); + circuit.connect(circuit.pinOf(either, or.outputPin.Out), circuit.pin("C")); + circuit.connect(circuit.pinOf(second, "S"), circuit.pin("S")); + }, +}); + export const tutorialStages: readonly TutorialStage[] = [ andStage, aOnlyStage, xorStage, + halfAdderStage, + fullAdderStage, ]; export function findTutorialStage(id: string | undefined) { From 2ddbc94774bf30b8902fd2eead40bea706cc9e67 Mon Sep 17 00:00:00 2001 From: Rn86222 Date: Fri, 20 Nov 2026 00:41:17 +0900 Subject: [PATCH 2/6] fix(tutorial): explain why 2 is 10 in binary, and say how the full adder is built - Stage 4 compares binary carrying with 9 + 1 becoming 10. - Stage 5 says to add A and B with one half adder, then Cin with another. - The English failure message lists three inputs as "A, B and Cin". - Both adder stages share one check, and stage 5's solution is laid out with Or level with C. Co-Authored-By: Claude Opus 5.5 --- src/i18n/en.ts | 6 ++--- src/i18n/ja.ts | 4 ++-- src/tutorial/stages.ts | 52 +++++++++++++++++------------------------- 3 files changed, 26 insertions(+), 36 deletions(-) diff --git a/src/i18n/en.ts b/src/i18n/en.ts index 80d5db5..d2b99ac 100644 --- a/src/i18n/en.ts +++ b/src/i18n/en.ts @@ -35,12 +35,12 @@ const stageTexts: Record = { }, "half-adder": { title: "Adding one digit", - goal: "Think of ON as 1 and OFF as 0. Then A + B is 0, 1 or 2. In binary, numbers are written with only 0 and 1, so these are 00, 01 and 10: 2 is written 10. Output the twos digit to C and the ones digit to S. C is called the carry and S the sum, and this circuit is called a half adder.", + goal: "Think of ON as 1 and OFF as 0. Then A + B is 0, 1 or 2. Binary writes numbers with only 0 and 1: just as 9 + 1 carries over to 10 in everyday numbers, 1 + 1 carries over to 10 in binary. So 0, 1 and 2 are 00, 01 and 10 in two digits. Output the twos digit to C and the ones digit to S. C is called the carry and S the sum, and this circuit is called a half adder.", steps: [], }, "full-adder": { title: "Adding with a carry", - goal: 'Output A + B + Cin as a two-digit binary number to C and S. Cin is the carry coming from the digit below. The answer is 0 to 3, and 3 is 11 in binary. "Adding one digit" is ready to use as a part. This circuit is called a full adder.', + goal: 'Output A + B + Cin as a two-digit binary number to C and S. Cin is the carry coming up from the next lower digit. The answer is 0 to 3, and 3 is 11 in binary. "Adding one digit" is ready to use as a part: add A and B with one, then add Cin to its S with another. This circuit is called a full adder.', steps: [], }, }; @@ -157,7 +157,7 @@ export const en = { expected: string, actual: string | null, ) => - `When ${inputs.map(({ name, value }) => `${name} is ${value}`).join(" and ")}, ${pinName} should be ${expected}, but it ${actual === null ? "has no value" : `is ${actual}`}.`, + `When ${listOf(inputs.map(({ name, value }) => `${name} is ${value}`))}, ${pinName} should be ${expected}, but it ${actual === null ? "has no value" : `is ${actual}`}.`, simulationFailed: (detail: string) => `The circuit could not be simulated. ${detail}`, resetStage: "Reset this stage", diff --git a/src/i18n/ja.ts b/src/i18n/ja.ts index cd76b8a..20814a8 100644 --- a/src/i18n/ja.ts +++ b/src/i18n/ja.ts @@ -144,12 +144,12 @@ export const ja: Messages = { }, "half-adder": { title: "1けたの足し算", - goal: "ON を 1、OFF を 0 と考えると、A+B は 0、1、2 のどれかです。0 と 1 だけで数を書く2進数では、これを 00、01、10 と書きます(2 は 10 です)。2の位を C に、1の位を S に出しましょう。C を「桁上がり」、S を「和」といい、この回路を「半加算器」といいます。", + goal: "ON を 1、OFF を 0 と考えると、A+B は 0、1、2 のどれかです。2進数では、0 と 1 だけで数を書きます。ふだんの数で 9+1 が繰り上がって 10 になるように、2進数では 1+1 が繰り上がって 10 になります。そのため、0、1、2 は2けたで 00、01、10 です。2の位を C に、1の位を S に出しましょう。C を「桁上がり」、S を「和」といい、この回路を「半加算器」といいます。", steps: [], }, "full-adder": { title: "桁上がりつきの足し算", - goal: "A+B+Cin の答えを、2けたの2進数で C と S に出しましょう。Cin は、下のけたから来る桁上がりです。答えは 0 から 3 で、3 は2進数で 11 です。「1けたの足し算」を部品として使えます。この回路を「全加算器」といいます。", + goal: "A+B+Cin の答えを、2けたの2進数で C と S に出しましょう。Cin は、下のけたから来る桁上がりです。答えは 0 から 3 で、3 は2進数で 11 です。「1けたの足し算」を部品として使えます。1つで A と B を足し、もう1つでその S と Cin を足します。この回路を「全加算器」といいます。", steps: [], }, }, diff --git a/src/tutorial/stages.ts b/src/tutorial/stages.ts index 2ca389d..afffda1 100644 --- a/src/tutorial/stages.ts +++ b/src/tutorial/stages.ts @@ -50,10 +50,12 @@ const isOn = (value: readonly boolean[] | undefined) => nullthrows(value?.[0]); const oneBitInputs = (...names: string[]) => names.map((name) => ({ name, bitWidth: 1 })); +type StageDefinition = Pick & + Partial>; + function defineStage( id: TutorialStageId, - definition: Pick & - Partial>, + definition: StageDefinition, ): TutorialStage { return { id, @@ -66,8 +68,7 @@ function defineStage( function twoSwitchesStage( id: TutorialStageId, - definition: Pick & - Partial>, + definition: Omit, lampIsOn: (a: boolean, b: boolean) => boolean, ): TutorialStage { return defineStage(id, { @@ -82,11 +83,17 @@ function twoSwitchesStage( }); } -/** The two digits of `total` in binary, as the carry C and the sum S */ -const carryAndSum = (total: number) => ({ - C: bit(total >= 2), - S: bit(total % 2 === 1), -}); +/** Adds up the inputs that are ON, and outputs the total as two binary digits C and S */ +function addingCheck(...inputNames: string[]): CombinationalCheck { + return { + inputs: oneBitInputs(...inputNames), + outputs: ["C", "S"], + expected: (values) => { + const total = inputNames.filter((name) => isOn(values[name])).length; + return { C: bit(total >= 2), S: bit(total % 2 === 1) }; + }, + }; +} const andStage = twoSwitchesStage( "and", @@ -159,12 +166,7 @@ const xorStage = twoSwitchesStage( const halfAdderStage = defineStage("half-adder", { parts: [and.component.id, xor.component.id], - check: { - inputs: oneBitInputs("A", "B"), - outputs: ["C", "S"], - expected: (values) => - carryAndSum(Number(isOn(values.A)) + Number(isOn(values.B))), - }, + check: addingCheck("A", "B"), solve(circuit) { const carry = circuit.place(and.component.id, { x: 0, y: -100 }); const sum = circuit.place(xor.component.id, { x: 0, y: 100 }); @@ -183,26 +185,14 @@ const halfAdderStage = defineStage("half-adder", { const fullAdderStage = defineStage("full-adder", { parts: [or.component.id], givenParts: ["half-adder"], - check: { - inputs: oneBitInputs("A", "B", "Cin"), - outputs: ["C", "S"], - expected: (values) => - carryAndSum( - Number(isOn(values.A)) + - Number(isOn(values.B)) + - Number(isOn(values.Cin)), - ), - }, + check: addingCheck("A", "B", "Cin"), solve(circuit) { const first = circuit.place(halfAdderStage.componentId, { - x: -100, + x: -140, y: -100, }); - const second = circuit.place(halfAdderStage.componentId, { - x: 60, - y: 100, - }); - const either = circuit.place(or.component.id, { x: 170, y: -130 }); + const second = circuit.place(halfAdderStage.componentId, { x: 0, y: 100 }); + const either = circuit.place(or.component.id, { x: 150, y: -100 }); circuit.connect(circuit.pin("A"), circuit.pinOf(first, "A")); circuit.connect(circuit.pin("B"), circuit.pinOf(first, "B")); circuit.connect(circuit.pinOf(first, "S"), circuit.pinOf(second, "A")); From a7f385ff731ec437343b28fc4a9b6fda4e4f6954 Mon Sep 17 00:00:00 2001 From: Rn86222 Date: Fri, 20 Nov 2026 01:22:39 +0900 Subject: [PATCH 3/6] fix: draw the underline of a truth table's first output column in the divider colour In that header cell, the colour of the left border came before the bottom border, whose shorthand reset its colour to black. Each border now carries its colour. Co-Authored-By: Claude Opus 5.5 --- src/components/TruthTableView.tsx | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/components/TruthTableView.tsx b/src/components/TruthTableView.tsx index 0cf74a8..29f8dcf 100644 --- a/src/components/TruthTableView.tsx +++ b/src/components/TruthTableView.tsx @@ -1,8 +1,10 @@ -import { Box } from "@mui/material"; +import { Box, type Theme } from "@mui/material"; import { bitLabel } from "../common/bitLabel"; import type { TruthTable } from "../store/intrinsics/truthTable"; import { theme } from "../style"; +const divider = (theme: Theme) => `1px solid ${theme.palette.divider}`; + export function TruthTableView({ table }: { table: TruthTable }) { const columns = [ ...table.inputs.map((name) => ({ key: `in-${name}`, name })), @@ -14,10 +16,7 @@ export function TruthTableView({ table }: { table: TruthTable }) { py: 0.25, textAlign: "center", ...(table.inputs.length > 0 && - column === table.inputs.length && { - borderLeft: 1, - borderColor: "divider", - }), + column === table.inputs.length && { borderLeft: divider }), }) as const; return ( @@ -31,7 +30,7 @@ export function TruthTableView({ table }: { table: TruthTable }) { {name} From cacf2aff9cf5df68de2e02817625c5636e42faad Mon Sep 17 00:00:00 2001 From: Rn86222 Date: Fri, 20 Nov 2026 01:27:41 +0900 Subject: [PATCH 4/6] feat(tutorial): write truth tables in 1 and 0 from the stage that teaches binary From stage 4 on, the goal asks to read C and S as a binary number, so ON and OFF in the tables had to be translated in the head. A stage now says how its tables write a bit, and the stage page passes it to every table on it, including the side panel's. A line under the goal table says that 1 is ON and 0 is OFF. Co-Authored-By: Claude Opus 5.5 --- src/common/bitLabel.ts | 9 +- src/components/TruthTableView.tsx | 9 +- src/i18n/en.ts | 1 + src/i18n/ja.ts | 1 + src/pages/tutorial/stage.tsx | 316 +++++++++++++++--------------- src/tutorial/stages.test.ts | 10 + src/tutorial/stages.ts | 7 +- 7 files changed, 196 insertions(+), 157 deletions(-) diff --git a/src/common/bitLabel.ts b/src/common/bitLabel.ts index b40e885..a99c590 100644 --- a/src/common/bitLabel.ts +++ b/src/common/bitLabel.ts @@ -1,11 +1,18 @@ import type { SimulationValue } from "../store/simulation"; +/** How a table writes a bit: digits once binary has been taught */ +export type BitNotation = "onOff" | "digits"; + export function bitLabel(bit: boolean): string { return bit ? "ON" : "OFF"; } +export function bitDigit(bit: boolean): string { + return bit ? "1" : "0"; +} + export function valueLabel(value: SimulationValue): string { const [bit] = value; if (value.length === 1 && bit !== undefined) return bitLabel(bit); - return value.map((b) => (b ? "1" : "0")).join(""); + return value.map(bitDigit).join(""); } diff --git a/src/components/TruthTableView.tsx b/src/components/TruthTableView.tsx index 29f8dcf..95977b9 100644 --- a/src/components/TruthTableView.tsx +++ b/src/components/TruthTableView.tsx @@ -1,11 +1,16 @@ import { Box, type Theme } from "@mui/material"; -import { bitLabel } from "../common/bitLabel"; +import { createContext, useContext } from "react"; +import { type BitNotation, bitDigit, bitLabel } from "../common/bitLabel"; import type { TruthTable } from "../store/intrinsics/truthTable"; import { theme } from "../style"; const divider = (theme: Theme) => `1px solid ${theme.palette.divider}`; +export const BitNotationContext = createContext("onOff"); + export function TruthTableView({ table }: { table: TruthTable }) { + const labelOf = + useContext(BitNotationContext) === "digits" ? bitDigit : bitLabel; const columns = [ ...table.inputs.map((name) => ({ key: `in-${name}`, name })), ...table.outputs.map((name) => ({ key: `out-${name}`, name })), @@ -52,7 +57,7 @@ export function TruthTableView({ table }: { table: TruthTable }) { }), }} > - {bitLabel(value)} + {labelOf(value)} ))} diff --git a/src/i18n/en.ts b/src/i18n/en.ts index d2b99ac..641f766 100644 --- a/src/i18n/en.ts +++ b/src/i18n/en.ts @@ -145,6 +145,7 @@ export const en = { parts: "Parts", goalHeading: "Goal", stepsHeading: "Steps", + digitsLegend: "In the table, 1 is ON and 0 is OFF.", check: "Check", passed: "Cleared! The circuit works for every combination of the inputs.", next: "Next stage", diff --git a/src/i18n/ja.ts b/src/i18n/ja.ts index 20814a8..8a2f3ec 100644 --- a/src/i18n/ja.ts +++ b/src/i18n/ja.ts @@ -103,6 +103,7 @@ export const ja: Messages = { parts: "部品", goalHeading: "目標", stepsHeading: "手順", + digitsLegend: "表の 1 は ON、0 は OFF です。", check: "確かめる", passed: "クリア!どの入力の組み合わせでも正しく動きます。", next: "次の課題へ", diff --git a/src/pages/tutorial/stage.tsx b/src/pages/tutorial/stage.tsx index a500e9b..148d617 100644 --- a/src/pages/tutorial/stage.tsx +++ b/src/pages/tutorial/stage.tsx @@ -22,7 +22,10 @@ import { PartDescription, PartDescriptionView, } from "../../components/PartDescription"; -import { TruthTableView } from "../../components/TruthTableView"; +import { + BitNotationContext, + TruthTableView, +} from "../../components/TruthTableView"; import { type Messages, useMessages } from "../../i18n"; import type CCStore from "../../store"; import { definitionByComponentId } from "../../store/intrinsics/definitions"; @@ -154,165 +157,172 @@ function StageView({ stage }: { stage: TutorialStage }) { : stage.componentId; return ( -
- - - - - {texts.stages} - -
- - {texts.stageNumber(stageIndex + 1)} - - +
+ + + - {stageTexts.title} - -
-
- {texts.goalHeading} - {stageTexts.goal} - {goalTable && ( - - - + + {texts.stages} + +
+ + {texts.stageNumber(stageIndex + 1)} + + + {stageTexts.title} + +
+
+ {texts.goalHeading} + {stageTexts.goal} + {goalTable && ( + + + {stage.tableNotation === "digits" && ( + + {texts.digitsLegend} + + )} + + )} +
+ {stageTexts.steps.length > 0 && ( +
+ {texts.stepsHeading} + + {stageTexts.steps.map((step) => ( + + {step} + + ))} + +
)} -
- {stageTexts.steps.length > 0 && (
- {texts.stepsHeading} - - {stageTexts.steps.map((step) => ( - - {step} - + {messages.parts.ofStage} + + {stage.parts.map((partId) => ( + ))} - + {stage.givenParts.map((id) => ( + + ))} +
- )} -
- {messages.parts.ofStage} - - {stage.parts.map((partId) => ( - - ))} - {stage.givenParts.map((id) => ( - - ))} - -
- - {hasResetFailed && ( - {texts.resetStageFailed} - )} - - - - {/* Outside the scrolling, so that Check and its result stay in view */} - - - {result?.type === "passed" && ( - <> - {texts.passed} - - - )} - {result && result.type !== "passed" && ( - - {describeFailure(result, store, messages)} - - )} - - - - - - - {texts.parts} - - + + {hasResetFailed && ( + {texts.resetStageFailed} + )} + + + + {/* Outside the scrolling, so that Check and its result stay in view */} + + + {result?.type === "passed" && ( + <> + {texts.passed} + + + )} + {result && result.type !== "passed" && ( + + {describeFailure(result, store, messages)} + + )} + - isStagePinNode(store, stage, nodeId), - isOffered, - canCreateComponents: stage.allowsNewComponents, - hasProperties: (componentId) => - !isStageComponent(stage, componentId), - }} - onEditComponent={setEditedComponentId} - onClose={() => { - if (shownComponentId === stage.componentId) navigate("/tutorial"); - else setEditedComponentId(stage.componentId); - }} - /> - - {isConfirmingReset && ( - setIsConfirmingReset(false)} - > - {texts.resetStageDialog.body} - - )} -
+ + + + {texts.parts} + + + + + isStagePinNode(store, stage, nodeId), + isOffered, + canCreateComponents: stage.allowsNewComponents, + hasProperties: (componentId) => + !isStageComponent(stage, componentId), + }} + onEditComponent={setEditedComponentId} + onClose={() => { + if (shownComponentId === stage.componentId) navigate("/tutorial"); + else setEditedComponentId(stage.componentId); + }} + /> + + {isConfirmingReset && ( + setIsConfirmingReset(false)} + > + {texts.resetStageDialog.body} + + )} +
+ ); } diff --git a/src/tutorial/stages.test.ts b/src/tutorial/stages.test.ts index fbb7f37..aa77176 100644 --- a/src/tutorial/stages.test.ts +++ b/src/tutorial/stages.test.ts @@ -31,6 +31,16 @@ const aOnlyStage = nullthrows(findTutorialStage("a-only")); const titleOf = (id: TutorialStageId) => `Stage ${id}`; describe("tutorialStages", () => { + it("writes tables in digits from the stage that teaches binary on", () => { + const binaryFrom = tutorialStages.findIndex((s) => s.id === "half-adder"); + + expect(tutorialStages.map((s) => s.tableNotation)).toEqual( + tutorialStages.map((_, index) => + index < binaryFrom ? "onOff" : "digits", + ), + ); + }); + it("hands over only the parts of earlier stages", () => { for (const [index, later] of tutorialStages.entries()) { for (const id of later.givenParts) { diff --git a/src/tutorial/stages.ts b/src/tutorial/stages.ts index afffda1..c16f718 100644 --- a/src/tutorial/stages.ts +++ b/src/tutorial/stages.ts @@ -1,4 +1,5 @@ import nullthrows from "nullthrows"; +import type { BitNotation } from "../common/bitLabel"; import type CCStore from "../store"; import { type CCComponent, @@ -37,6 +38,7 @@ export type TutorialStage = { /** earlier stages whose components the stage hands over, built and ready to use */ givenParts: readonly TutorialStageId[]; allowsNewComponents: boolean; + tableNotation: BitNotation; /** also the input and output pins the stage starts with */ check: CombinationalCheck; solve(circuit: StageCircuit): void; @@ -51,7 +53,7 @@ const oneBitInputs = (...names: string[]) => names.map((name) => ({ name, bitWidth: 1 })); type StageDefinition = Pick & - Partial>; + Partial>; function defineStage( id: TutorialStageId, @@ -62,6 +64,7 @@ function defineStage( componentId: `tutorial-${id}` as CCComponentId, givenParts: [], allowsNewComponents: false, + tableNotation: "onOff", ...definition, }; } @@ -166,6 +169,7 @@ const xorStage = twoSwitchesStage( const halfAdderStage = defineStage("half-adder", { parts: [and.component.id, xor.component.id], + tableNotation: "digits", check: addingCheck("A", "B"), solve(circuit) { const carry = circuit.place(and.component.id, { x: 0, y: -100 }); @@ -185,6 +189,7 @@ const halfAdderStage = defineStage("half-adder", { const fullAdderStage = defineStage("full-adder", { parts: [or.component.id], givenParts: ["half-adder"], + tableNotation: "digits", check: addingCheck("A", "B", "Cin"), solve(circuit) { const first = circuit.place(halfAdderStage.componentId, { From c202fbcafc681a5f9fcd67bace0398e72d136765 Mon Sep 17 00:00:00 2001 From: Rn86222 Date: Fri, 20 Nov 2026 01:28:41 +0900 Subject: [PATCH 5/6] fix(tutorial): say in stage 4 that the tables switch to 1 and 0 from there Co-Authored-By: Claude Opus 5.5 --- src/i18n/en.ts | 2 +- src/i18n/ja.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/i18n/en.ts b/src/i18n/en.ts index 641f766..a1cfa4a 100644 --- a/src/i18n/en.ts +++ b/src/i18n/en.ts @@ -35,7 +35,7 @@ const stageTexts: Record = { }, "half-adder": { title: "Adding one digit", - goal: "Think of ON as 1 and OFF as 0. Then A + B is 0, 1 or 2. Binary writes numbers with only 0 and 1: just as 9 + 1 carries over to 10 in everyday numbers, 1 + 1 carries over to 10 in binary. So 0, 1 and 2 are 00, 01 and 10 in two digits. Output the twos digit to C and the ones digit to S. C is called the carry and S the sum, and this circuit is called a half adder.", + goal: "From here on, think of ON as 1 and OFF as 0, and the tables write them that way too. Then A + B is 0, 1 or 2. Binary writes numbers with only 0 and 1: just as 9 + 1 carries over to 10 in everyday numbers, 1 + 1 carries over to 10 in binary. So 0, 1 and 2 are 00, 01 and 10 in two digits. Output the twos digit to C and the ones digit to S. C is called the carry and S the sum, and this circuit is called a half adder.", steps: [], }, "full-adder": { diff --git a/src/i18n/ja.ts b/src/i18n/ja.ts index 8a2f3ec..f792712 100644 --- a/src/i18n/ja.ts +++ b/src/i18n/ja.ts @@ -145,7 +145,7 @@ export const ja: Messages = { }, "half-adder": { title: "1けたの足し算", - goal: "ON を 1、OFF を 0 と考えると、A+B は 0、1、2 のどれかです。2進数では、0 と 1 だけで数を書きます。ふだんの数で 9+1 が繰り上がって 10 になるように、2進数では 1+1 が繰り上がって 10 になります。そのため、0、1、2 は2けたで 00、01、10 です。2の位を C に、1の位を S に出しましょう。C を「桁上がり」、S を「和」といい、この回路を「半加算器」といいます。", + goal: "ここからは ON を 1、OFF を 0 と考え、表もそう書きます。すると、A+B は 0、1、2 のどれかです。2進数では、0 と 1 だけで数を書きます。ふだんの数で 9+1 が繰り上がって 10 になるように、2進数では 1+1 が繰り上がって 10 になります。そのため、0、1、2 は2けたで 00、01、10 です。2の位を C に、1の位を S に出しましょう。C を「桁上がり」、S を「和」といい、この回路を「半加算器」といいます。", steps: [], }, "full-adder": { From cbe02bc02c852ddfcc8b3ad0f3a132f12c329286 Mon Sep 17 00:00:00 2001 From: Rn86222 Date: Fri, 20 Nov 2026 01:30:12 +0900 Subject: [PATCH 6/6] fix(tutorial): name the left and right digits in stage 4, and address the review - Stage 4 says the twos digit is the left one and the ones digit the right one. - The English legend speaks of the tables, as a page has several. - The message on a wrong output is tested in both languages, with two and three inputs. - The notation context lives in its own file, so the table keeps fast refresh. - The play-mode value uses bitDigit, and two comments that went stale or only restated the code are trimmed. Co-Authored-By: Claude Opus 5.5 --- src/components/TruthTableView.tsx | 9 +++--- src/components/bitNotation.ts | 4 +++ src/i18n/en.ts | 4 +-- src/i18n/ja.ts | 2 +- src/i18n/messages.test.ts | 32 +++++++++++++++++++ .../Node/components/Interface/Value/index.tsx | 4 +-- src/pages/tutorial/stage.tsx | 6 ++-- src/tutorial/check.ts | 2 +- src/tutorial/stages.ts | 1 - 9 files changed, 48 insertions(+), 16 deletions(-) create mode 100644 src/components/bitNotation.ts diff --git a/src/components/TruthTableView.tsx b/src/components/TruthTableView.tsx index 95977b9..7cd6869 100644 --- a/src/components/TruthTableView.tsx +++ b/src/components/TruthTableView.tsx @@ -1,12 +1,11 @@ import { Box, type Theme } from "@mui/material"; -import { createContext, useContext } from "react"; -import { type BitNotation, bitDigit, bitLabel } from "../common/bitLabel"; +import { useContext } from "react"; +import { bitDigit, bitLabel } from "../common/bitLabel"; import type { TruthTable } from "../store/intrinsics/truthTable"; import { theme } from "../style"; +import { BitNotationContext } from "./bitNotation"; -const divider = (theme: Theme) => `1px solid ${theme.palette.divider}`; - -export const BitNotationContext = createContext("onOff"); +const divider = (muiTheme: Theme) => `1px solid ${muiTheme.palette.divider}`; export function TruthTableView({ table }: { table: TruthTable }) { const labelOf = diff --git a/src/components/bitNotation.ts b/src/components/bitNotation.ts new file mode 100644 index 0000000..cbcb618 --- /dev/null +++ b/src/components/bitNotation.ts @@ -0,0 +1,4 @@ +import { createContext } from "react"; +import type { BitNotation } from "../common/bitLabel"; + +export const BitNotationContext = createContext("onOff"); diff --git a/src/i18n/en.ts b/src/i18n/en.ts index a1cfa4a..ff6dd0c 100644 --- a/src/i18n/en.ts +++ b/src/i18n/en.ts @@ -35,7 +35,7 @@ const stageTexts: Record = { }, "half-adder": { title: "Adding one digit", - goal: "From here on, think of ON as 1 and OFF as 0, and the tables write them that way too. Then A + B is 0, 1 or 2. Binary writes numbers with only 0 and 1: just as 9 + 1 carries over to 10 in everyday numbers, 1 + 1 carries over to 10 in binary. So 0, 1 and 2 are 00, 01 and 10 in two digits. Output the twos digit to C and the ones digit to S. C is called the carry and S the sum, and this circuit is called a half adder.", + goal: "From here on, think of ON as 1 and OFF as 0, and the tables write them that way too. Then A + B is 0, 1 or 2. Binary writes numbers with only 0 and 1: just as 9 + 1 carries over and becomes 10 in everyday numbers, 1 + 1 carries over and becomes 10 in binary. So 0, 1 and 2 are 00, 01 and 10 in two digits. Output the left digit (the twos digit) to C and the right digit (the ones digit) to S. C is called the carry and S the sum, and this circuit is called a half adder.", steps: [], }, "full-adder": { @@ -145,7 +145,7 @@ export const en = { parts: "Parts", goalHeading: "Goal", stepsHeading: "Steps", - digitsLegend: "In the table, 1 is ON and 0 is OFF.", + digitsLegend: "In the tables, 1 is ON and 0 is OFF.", check: "Check", passed: "Cleared! The circuit works for every combination of the inputs.", next: "Next stage", diff --git a/src/i18n/ja.ts b/src/i18n/ja.ts index f792712..ab50e73 100644 --- a/src/i18n/ja.ts +++ b/src/i18n/ja.ts @@ -145,7 +145,7 @@ export const ja: Messages = { }, "half-adder": { title: "1けたの足し算", - goal: "ここからは ON を 1、OFF を 0 と考え、表もそう書きます。すると、A+B は 0、1、2 のどれかです。2進数では、0 と 1 だけで数を書きます。ふだんの数で 9+1 が繰り上がって 10 になるように、2進数では 1+1 が繰り上がって 10 になります。そのため、0、1、2 は2けたで 00、01、10 です。2の位を C に、1の位を S に出しましょう。C を「桁上がり」、S を「和」といい、この回路を「半加算器」といいます。", + goal: "ここからは ON を 1、OFF を 0 と考え、表もそう書きます。すると、A+B は 0、1、2 のどれかです。2進数では、0 と 1 だけで数を書きます。ふだんの数で 9+1 が繰り上がって 10 になるように、2進数では 1+1 が繰り上がって 10 になります。そのため、0、1、2 は2けたで 00、01、10 です。左のけた(2の位)を C に、右のけた(1の位)を S に出しましょう。C を「桁上がり」、S を「和」といい、この回路を「半加算器」といいます。", steps: [], }, "full-adder": { diff --git a/src/i18n/messages.test.ts b/src/i18n/messages.test.ts index e7db275..2bf764c 100644 --- a/src/i18n/messages.test.ts +++ b/src/i18n/messages.test.ts @@ -100,6 +100,38 @@ describe("Japanese messages", () => { }); }); +describe("the message on a wrong output", () => { + const inputs = [ + { name: "A", value: "ON" }, + { name: "B", value: "OFF" }, + { name: "Cin", value: "ON" }, + ]; + + it("lists the inputs as a sentence in English", () => { + expect( + messagesByLanguage.en.tutorial.wrongOutput(inputs, "C", "ON", "OFF"), + ).toBe( + "When A is ON, B is OFF and Cin is ON, C should be ON, but it is OFF.", + ); + expect( + messagesByLanguage.en.tutorial.wrongOutput( + inputs.slice(0, 2), + "C", + "ON", + null, + ), + ).toBe("When A is ON and B is OFF, C should be ON, but it has no value."); + }); + + it("lists the inputs in Japanese", () => { + expect( + messagesByLanguage.ja.tutorial.wrongOutput(inputs, "C", "ON", "OFF"), + ).toBe( + "A が ON、B が OFF、Cin が ON のとき、C は ON になるはずが、OFF でした。", + ); + }); +}); + describe("English messages", () => { it("lists the inputs reset with their bit widths", () => { expect( diff --git a/src/pages/edit/Editor/renderer/Node/components/Interface/Value/index.tsx b/src/pages/edit/Editor/renderer/Node/components/Interface/Value/index.tsx index 2fac409..ae55061 100644 --- a/src/pages/edit/Editor/renderer/Node/components/Interface/Value/index.tsx +++ b/src/pages/edit/Editor/renderer/Node/components/Interface/Value/index.tsx @@ -1,5 +1,5 @@ import clsx from "clsx"; -import { bitLabel } from "../../../../../../../../common/bitLabel"; +import { bitDigit, bitLabel } from "../../../../../../../../common/bitLabel"; import type { SimulationValue } from "../../../../../../../../store/simulation"; import { stringifySimulationValue } from "../../../../../store/slices/core"; import { CCComponentEditorRendererNodeInterfaceRendererValueEditButton } from "./EditButton"; @@ -46,7 +46,7 @@ export function CCComponentEditorRendererNodeInterfaceRendererValue({ > {value.map((bit, index) => { const className = clsx(styles.bit, bit && styles.on); - const label = isSingleBit ? bitLabel(bit) : bit ? "1" : "0"; + const label = isSingleBit ? bitLabel(bit) : bitDigit(bit); return onChange ? (