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
9 changes: 8 additions & 1 deletion src/common/bitLabel.ts
Original file line number Diff line number Diff line change
@@ -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("");
}
19 changes: 11 additions & 8 deletions src/components/TruthTableView.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import { Box } from "@mui/material";
import { bitLabel } from "../common/bitLabel";
import { Box, type Theme } from "@mui/material";
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 = (muiTheme: Theme) => `1px solid ${muiTheme.palette.divider}`;

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 })),
Expand All @@ -14,10 +20,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 (
Expand All @@ -31,7 +34,7 @@ export function TruthTableView({ table }: { table: TruthTable }) {
<Box
key={key}
component="th"
sx={{ ...cellSx(i), borderBottom: 1, borderColor: "divider" }}
sx={{ ...cellSx(i), borderBottom: divider }}
>
{name}
</Box>
Expand All @@ -53,7 +56,7 @@ export function TruthTableView({ table }: { table: TruthTable }) {
}),
}}
>
{bitLabel(value)}
{labelOf(value)}
</Box>
))}
</tr>
Expand Down
4 changes: 4 additions & 0 deletions src/components/bitNotation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { createContext } from "react";
import type { BitNotation } from "../common/bitLabel";

export const BitNotationContext = createContext<BitNotation>("onOff");
13 changes: 12 additions & 1 deletion src/i18n/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ const stageTexts: Record<TutorialStageId, StageTexts> = {
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: "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": {
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 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: [],
},
};

export const en = {
Expand Down Expand Up @@ -135,6 +145,7 @@ export const en = {
parts: "Parts",
goalHeading: "Goal",
stepsHeading: "Steps",
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",
Expand All @@ -147,7 +158,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",
Expand Down
11 changes: 11 additions & 0 deletions src/i18n/ja.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ export const ja: Messages = {
parts: "部品",
goalHeading: "目標",
stepsHeading: "手順",
digitsLegend: "表の 1 は ON、0 は OFF です。",
check: "確かめる",
passed: "クリア!どの入力の組み合わせでも正しく動きます。",
next: "次の課題へ",
Expand Down Expand Up @@ -142,6 +143,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 のどれかです。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けたの足し算」を部品として使えます。1つで A と B を足し、もう1つでその S と Cin を足します。この回路を「全加算器」といいます。",
steps: [],
},
},
},
componentProperties: {
Expand Down
32 changes: 32 additions & 0 deletions src/i18n/messages.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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 ? (
<button
// biome-ignore lint/suspicious/noArrayIndexKey: bits are identified by their index
Expand Down
Loading
Loading