Please build a p5.js program thats a grid of 6 columns and 7 rows. To the left of the…
- User
Please build a p5.js program thats a grid of 6 columns and 7 rows. To the left of the grid is a list of the cells, and each cell corresponds to it's own item on the list. Whenever somebody touches a cell of the grid, a point in Roman numerals is added to the item in the list which corresponds to the cell.
- Assistant
Here's an example of a p5.js program that does what you described:
<!DOCTYPE html> <html> <head> <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.0/p5.js"></script> <style> body { padding: 30px; } </style> </head> <body> <script> const columns = 6; const rows = 7; const cellSize = 50; const points = []; const romanNumerals = ["I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X"]; function setup() { createCanvas(columns * cellSize, rows * cellSize); for (let i = 0; i < columns * rows; i++) { points.push(0); } } function draw() { background(255); textAlign(CENTER, CENTER); for (let x = 0; x < columns; x++) { for (let y = 0; y < rows; y++) { const index = x * rows + y; const px = x * cellSize; const py = y * cellSize; stroke(0); fill(235); rect(px, py, cellSize, cellSize); fill(0); text(points[index], px + cellSize / 2, py + cellSize / 2); } } } function mouseClicked() { const x = floor(mouseX / cellSize); const y = floor(mouseY / cellSize); const index = x * rows + y; if (index < points.length && x < columns && y < rows) { points[index]++; updateList(index); } } function updateList(index) { let list = document.getElementById("cellList"); if (!list) { list = createList(); } const item = document.getElementById("listItem" + index); item.textContent = "Cell " + index + ": " + romanNumerals[points[index] - 1]; } function createList() { const list = document.createElement("ol"); list.id = "cellList"; for (let i = 0; i < points.length; i++) { const item = document.createElement("li"); item.id = "listItem" + i; item.textContent = "Cell " + i + ": 0"; list.appendChild(item); } document.body.appendChild(list); return list; } </script> </body> </html> - User
In this code, please add a separate row above the grid that is numbered from 6 to 1 (backwards), and add a column to the left of the grid that is labels from 1 to 7. The row and column should not be a part of the big grid: const columns = 6; const rows = 7; const cellSize = 50; const points = []; const romanNumerals = ["I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X"];
function setup() { createCanvas(columns * cellSize, rows * cellSize); for (let i = 0; i < columns * rows; i++) { points.push(0); } }
function draw() { background(255); textAlign(CENTER, CENTER); for (let x = 0; x < columns; x++) { for (let y = 0; y < rows; y++) { const index = x * rows + y; const px = x * cellSize; const py = y * cellSize; stroke(0); fill(235); rect(px, py, cellSize, cellSize); fill(0); text(points[index], px + cellSize / 2, py + cellSize / 2); } } }
function mouseClicked() { const x = floor(mouseX / cellSize); const y = floor(mouseY / cellSize); const index = x * rows + y; if (index < points.length && x < columns && y < rows) { points[index]++; updateList(index); } }
- Assistant
- User
Please remove the list and colorize the numbers in the cell.
- Assistant
Here's the updated code that removes the list and colorizes the numbers in the cells:
<!DOCTYPE html> <html> <head> <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.0/p5.js"></script> <style> body { padding: 30px; } </style> </head> <body> <script> const columns = 6; const rows = 7; const cellSize = 50; const offsetX = cellSize; const offsetY = cellSize; const points = []; const romanNumerals = ["I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X"]; function setup() { createCanvas(columns * cellSize + offsetX, rows * cellSize + offsetY); for (let i = 0; i < columns * rows; i++) { points.push(0); } } function draw() { background(255); textAlign(CENTER, CENTER); // Draw header row for (let x = 0; x < columns; x++) { fill(0); text(columns - x, x * cellSize + offsetX + cellSize / 2, cellSize / 2); } // Draw header column for (let y = 0; y < rows; y++) { fill(0); text(y + 1, cellSize / 2, y * cellSize + offsetY + cellSize / 2); } // Draw grid for (let x = 0; x < columns; x++) { for (let y = 0; y < rows; y++) { const index = x * rows + y; const px = x * cellSize + offsetX; const py = y * cellSize + offsetY; stroke(0); fill(235); rect(px, py, cellSize, cellSize); // Colorize text in the cell const hue = map(points[index], 0, 10, 0, 255); fill(hue, 255, 255); if (points[index] > 0) { text(romanNumerals[points[index] - 1], px + cellSize / 2, py + cellSize / 2); } } } } function mouseClicked() { const x = floor((mouseX - offsetX) / cellSize); const y = floor((mouseY - offsetY) / cellSize); const index = x * rows + y; if (index < points.length && x < columns && y < rows) { points[index]++; } } </script> </body> </html>
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.