RotNot / src /lib /puzzles /mathequation.ts
deploy
Deploy RotNot
9580dce
Raw History Blame Contribute Delete
12.1 kB
/**
* Math Equation puzzle generator.
* Generates fill-in-the-blank arithmetic equations where the player
* must determine the missing number (always a positive integer).
*
* Difficulty levels:
* easy: single operation, small numbers (answer 1–20)
* medium: two operations, moderate numbers (answer 1–50)
* hard: three operations or mixed, larger numbers (answer 1–100)
*/
export type MathDifficulty = "easy" | "medium" | "hard";
/** Where the unknown appears in the equation */
export type UnknownPosition = "left" | "right" | "middle";
export interface MathEquationPuzzle {
/** LaTeX-style display string, e.g. "? + 7 = 15" */
equation: string;
/** The correct integer answer */
answer: number;
/** Difficulty level */
difficulty: MathDifficulty;
/** Human-readable hint about how to approach the puzzle */
hint: string;
/** The number of operations involved */
operationCount: number;
}
// ── Helpers ────────────────────────────────────────────────────────────────────
function randomInt(min: number, max: number): number {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
/** Pick a random element from an array */
function pick<T>(arr: T[]): T {
return arr[Math.floor(Math.random() * arr.length)];
}
type BinaryOp = "+" | "-" | "Γ—" | "Γ·";
// ── Easy: single-operation equations ─────────────────────────────────────────
/**
* Generate an easy single-operation puzzle.
* Answer is always a positive integer in [1, 20].
*/
function generateEasy(): MathEquationPuzzle {
const ops: BinaryOp[] = ["+", "-", "Γ—", "Γ·"];
// Try up to 100 times to find a valid configuration
for (let attempt = 0; attempt < 100; attempt++) {
const op = pick(ops);
const answer = randomInt(1, 20);
let equation: string;
let hint: string;
switch (op) {
case "+": {
// ? + b = answer OR a + ? = answer
const b = randomInt(1, 20);
const result = answer + b;
if (Math.random() < 0.5) {
equation = `? + ${b} = ${result}`;
hint = `Subtract ${b} from ${result}`;
} else {
equation = `${b} + ? = ${result}`;
hint = `Subtract ${b} from ${result}`;
}
return { equation, answer, difficulty: "easy", hint, operationCount: 1 };
}
case "-": {
// ? - b = answer (answer + b is the left side)
const b = randomInt(1, 20);
const left = answer + b;
if (Math.random() < 0.5) {
// ? - b = answer
equation = `? - ${b} = ${answer}`;
hint = `Add ${b} to ${answer}`;
} else {
// left - ? = answer β†’ ? = left - answer
const unknown = left - answer;
if (unknown < 1 || unknown > 30) continue;
equation = `${left} - ? = ${answer}`;
hint = `Subtract ${answer} from ${left}`;
return { equation, answer: unknown, difficulty: "easy", hint, operationCount: 1 };
}
return { equation, answer, difficulty: "easy", hint, operationCount: 1 };
}
case "Γ—": {
// ? Γ— b = answer where answer is a product
const b = randomInt(2, 10);
const product = answer * b;
if (product > 100) continue;
if (Math.random() < 0.5) {
equation = `? Γ— ${b} = ${product}`;
hint = `Divide ${product} by ${b}`;
return { equation, answer, difficulty: "easy", hint, operationCount: 1 };
} else {
equation = `${b} Γ— ? = ${product}`;
hint = `Divide ${product} by ${b}`;
return { equation, answer, difficulty: "easy", hint, operationCount: 1 };
}
}
case "Γ·": {
// ? Γ· b = answer β†’ ? = answer Γ— b
const b = randomInt(2, 10);
const dividend = answer * b;
if (dividend > 100) continue;
if (Math.random() < 0.5) {
equation = `? Γ· ${b} = ${answer}`;
hint = `Multiply ${answer} by ${b}`;
return { equation, answer: dividend, difficulty: "easy", hint, operationCount: 1 };
} else {
// dividend Γ· ? = answer β†’ ? = b
equation = `${dividend} Γ· ? = ${answer}`;
hint = `Divide ${dividend} by ${answer}`;
return { equation, answer: b, difficulty: "easy", hint, operationCount: 1 };
}
}
}
}
// Fallback: simple addition
const b = randomInt(1, 15);
const answer = randomInt(1, 15);
return {
equation: `? + ${b} = ${answer + b}`,
answer,
difficulty: "easy",
hint: `Subtract ${b} from ${answer + b}`,
operationCount: 1,
};
}
// ── Medium: two-operation equations ──────────────────────────────────────────
/**
* Generate a medium two-operation puzzle.
* Form: (? OP1 b) OP2 c = result OR a OP1 (? OP2 c) = result
* Answer is a positive integer in [1, 50].
*/
function generateMedium(): MathEquationPuzzle {
for (let attempt = 0; attempt < 200; attempt++) {
const answer = randomInt(1, 50);
// Choose a two-operation template
const template = randomInt(1, 4);
if (template === 1) {
// (? + b) Γ— c = result
const b = randomInt(1, 20);
const c = randomInt(2, 6);
const inner = answer + b; // ? + b
const result = inner * c;
if (result > 300 || inner < 1) continue;
return {
equation: `(? + ${b}) Γ— ${c} = ${result}`,
answer,
difficulty: "medium",
hint: `Divide ${result} by ${c}, then subtract ${b}`,
operationCount: 2,
};
}
if (template === 2) {
// ? Γ— b + c = result
const b = randomInt(2, 10);
const c = randomInt(1, 20);
const result = answer * b + c;
if (result > 300) continue;
return {
equation: `? Γ— ${b} + ${c} = ${result}`,
answer,
difficulty: "medium",
hint: `Subtract ${c} from ${result}, then divide by ${b}`,
operationCount: 2,
};
}
if (template === 3) {
// ? Γ— b - c = result
const b = randomInt(2, 10);
const c = randomInt(1, 20);
const result = answer * b - c;
if (result < 1) continue;
return {
equation: `? Γ— ${b} - ${c} = ${result}`,
answer,
difficulty: "medium",
hint: `Add ${c} to ${result}, then divide by ${b}`,
operationCount: 2,
};
}
if (template === 4) {
// (a - ?) Γ— b = result β†’ ? = a - result/b
const b = randomInt(2, 8);
const a = randomInt(answer + 1, answer + 30);
const result = (a - answer) * b;
if (result > 300 || result < 1) continue;
return {
equation: `(${a} - ?) Γ— ${b} = ${result}`,
answer,
difficulty: "medium",
hint: `Divide ${result} by ${b}, then subtract from ${a}`,
operationCount: 2,
};
}
}
// Fallback
const b = randomInt(2, 8);
const c = randomInt(1, 10);
const answer = randomInt(1, 20);
const result = answer * b + c;
return {
equation: `? Γ— ${b} + ${c} = ${result}`,
answer,
difficulty: "medium",
hint: `Subtract ${c} from ${result}, then divide by ${b}`,
operationCount: 2,
};
}
// ── Hard: three-operation or complex equations ────────────────────────────────
/**
* Generate a hard three-operation puzzle.
* Forms:
* (? + a) Γ— b - c = result
* ? Γ— a + b Γ— c = result
* (? - a) Γ— (b + c) = result
* Answer is a positive integer in [1, 100].
*/
function generateHard(): MathEquationPuzzle {
for (let attempt = 0; attempt < 300; attempt++) {
const answer = randomInt(1, 100);
const template = randomInt(1, 5);
if (template === 1) {
// (? + a) Γ— b - c = result
const a = randomInt(1, 20);
const b = randomInt(2, 8);
const c = randomInt(1, 30);
const inner = answer + a;
const result = inner * b - c;
if (result < 1 || result > 500) continue;
return {
equation: `(? + ${a}) Γ— ${b} - ${c} = ${result}`,
answer,
difficulty: "hard",
hint: `Add ${c} to ${result}, divide by ${b}, then subtract ${a}`,
operationCount: 3,
};
}
if (template === 2) {
// ? Γ— a + b Γ— c = result
const a = randomInt(2, 10);
const b = randomInt(2, 10);
const c = randomInt(2, 10);
const bc = b * c;
const result = answer * a + bc;
if (result > 1000) continue;
return {
equation: `? Γ— ${a} + ${b} Γ— ${c} = ${result}`,
answer,
difficulty: "hard",
hint: `First compute ${b} Γ— ${c} = ${bc}, subtract from ${result}, then divide by ${a}`,
operationCount: 3,
};
}
if (template === 3) {
// (? - a) Γ— b + c = result
const a = randomInt(1, 30);
const b = randomInt(2, 8);
const c = randomInt(1, 20);
const inner = answer - a;
if (inner < 1) continue;
const result = inner * b + c;
if (result > 500) continue;
return {
equation: `(? - ${a}) Γ— ${b} + ${c} = ${result}`,
answer,
difficulty: "hard",
hint: `Subtract ${c} from ${result}, divide by ${b}, then add ${a}`,
operationCount: 3,
};
}
if (template === 4) {
// ? Γ— a - b Γ— c = result
const a = randomInt(3, 12);
const b = randomInt(2, 8);
const c = randomInt(2, 8);
const bc = b * c;
const result = answer * a - bc;
if (result < 1 || result > 1000) continue;
return {
equation: `? Γ— ${a} - ${b} Γ— ${c} = ${result}`,
answer,
difficulty: "hard",
hint: `Add ${bc} (= ${b} Γ— ${c}) to ${result}, then divide by ${a}`,
operationCount: 3,
};
}
if (template === 5) {
// (? + a) Γ— (b - c) = result β€” b > c required
const a = randomInt(1, 20);
const c = randomInt(1, 5);
const b = c + randomInt(2, 6);
const factor = b - c;
const inner = answer + a;
const result = inner * factor;
if (result > 500 || result < 1) continue;
return {
equation: `(? + ${a}) Γ— (${b} - ${c}) = ${result}`,
answer,
difficulty: "hard",
hint: `Compute ${b} - ${c} = ${factor}, divide ${result} by ${factor}, then subtract ${a}`,
operationCount: 3,
};
}
}
// Fallback
const a = randomInt(2, 8);
const b = randomInt(1, 15);
const answer = randomInt(1, 20);
const result = answer * a + b;
return {
equation: `? Γ— ${a} + ${b} = ${result}`,
answer,
difficulty: "hard",
hint: `Subtract ${b} from ${result}, then divide by ${a}`,
operationCount: 2,
};
}
// ── Public API ─────────────────────────────────────────────────────────────────
/**
* Generate a math equation puzzle for the given difficulty.
*/
export function generateMathEquation(
difficulty: MathDifficulty,
): MathEquationPuzzle {
switch (difficulty) {
case "easy": return generateEasy();
case "medium": return generateMedium();
case "hard": return generateHard();
}
}
/**
* Validate a player's answer for a math equation puzzle.
* Returns true if the integer answer matches exactly.
*/
export function validateMathEquation(
puzzle: MathEquationPuzzle,
playerAnswer: number,
): boolean {
return Number.isInteger(playerAnswer) && playerAnswer === puzzle.answer;
}
/**
* Score a math equation attempt.
* Returns 1.0 for correct, 0.0 for incorrect (binary scoring).
*/
export function scoreMathEquation(
puzzle: MathEquationPuzzle,
playerAnswer: number,
): number {
return validateMathEquation(puzzle, playerAnswer) ? 1.0 : 0.0;
}