diff options
author | elioat <elioat@tilde.institute> | 2025-05-05 21:01:35 -0400 |
---|---|---|
committer | elioat <elioat@tilde.institute> | 2025-05-05 21:01:35 -0400 |
commit | f74e0ee83e3bfb6a1e51c62855e0847b46a33815 (patch) | |
tree | 15e374acef791ac909c9e5bda84a1fc4d9cc9438 | |
parent | 55ff39c80a6b36aa670d153f74c947b62c324e0c (diff) | |
download | tour-f74e0ee83e3bfb6a1e51c62855e0847b46a33815.tar.gz |
*
-rw-r--r-- | html/kgame/script.js | 60 |
1 files changed, 50 insertions, 10 deletions
diff --git a/html/kgame/script.js b/html/kgame/script.js index fcb4df4..a11d5f5 100644 --- a/html/kgame/script.js +++ b/html/kgame/script.js @@ -270,49 +270,89 @@ document.addEventListener('DOMContentLoaded', () => { if (assignMatch) { const indexExpr = assignMatch[1].trim(); const valueExpr = assignMatch[2].trim(); + const steps = []; + // Parse and evaluate indices + steps.push(`1. Evaluating indices expression: ${indexExpr}`); const indices = evaluateExpression(tokenize(indexExpr)); + steps.push(` → Generated ${indices.length} indices`); + + // Parse and evaluate values + steps.push(`2. Evaluating values expression: ${valueExpr}`); const values = evaluateExpression(tokenize(valueExpr)); + steps.push(` → Generated ${Array.isArray(values) ? values.length : 1} values`); const indicesArray = Array.isArray(indices) ? indices : [indices]; const valuesArray = Array.isArray(values) ? values : [values]; if (indicesArray.length === 0) { - setOutput("Warning: Assignment applied to empty index list.", "info"); + setOutput("Warning: Assignment applied to empty index list.", "info", steps); return; } // Vectorized assignment const assignments = indicesArray.reduce((count, idx, i) => { - // Convert scalar index to 2D coordinates const row = Math.floor(idx / GRID_SIZE); const col = idx % GRID_SIZE; if (row >= 0 && row < GRID_SIZE && col >= 0 && col < GRID_SIZE) { const valueToAssign = valuesArray[i % valuesArray.length]; - console.log(`Assigning at [${row},${col}]: value=${valueToAssign}, modulo2=${valueToAssign % 2}`); - // Use modulo 2 directly for binary values G[row][col] = valueToAssign % 2; return count + 1; } return count; }, 0); - setOutput(`OK. Performed ${assignments} assignments.`, "success"); + steps.push(`3. Assignment complete:`); + steps.push(` → Applied ${assignments} assignments to the grid`); + steps.push(` → Each value was taken modulo 2 to ensure binary (0/1) values`); + + setOutput(`OK. Performed ${assignments} assignments.`, "success", steps); } else { const result = evaluateExpression(tokenize(code)); - setOutput(`Evaluated: ${JSON.stringify(result)}`, "info"); + setOutput(`Evaluated: ${JSON.stringify(result)}`, "info", [ + `1. Evaluated expression: ${code}`, + `2. Result: ${JSON.stringify(result)}` + ]); } } catch (error) { - setOutput(`Error: ${error.message}`, "error"); + setOutput(`Error: ${error.message}`, "error", [ + `1. Error occurred while executing: ${code}`, + `2. Error details: ${error.message}` + ]); console.error("K execution error:", error); } } // --- Output Helper --- - function setOutput(message, type = "info") { // type: info, success, error - output.textContent = message; - output.className = type; // Use CSS classes for styling + function setOutput(message, type = "info", steps = []) { + const outputDiv = document.getElementById('output'); + + // Create a container for the message and steps + const container = document.createElement('div'); + container.className = type; + + // Add the main message + const messageDiv = document.createElement('div'); + messageDiv.textContent = message; + container.appendChild(messageDiv); + + // Add steps if provided + if (steps.length > 0) { + const stepsDiv = document.createElement('div'); + stepsDiv.className = 'steps'; + steps.forEach(step => { + const stepDiv = document.createElement('div'); + stepDiv.className = 'step'; + stepDiv.textContent = step; + stepsDiv.appendChild(stepDiv); + }); + container.appendChild(stepsDiv); + } + + // Clear previous output and add new content + outputDiv.innerHTML = ''; + outputDiv.appendChild(container); } // --- Event Listeners --- |