Can you make me a function that checks whenever there is a gap in the columns? So in…
- User
Can you make me a function that checks whenever there is a gap in the columns? So in column 5 there is 3 rectangles than a gap than 1 more rectangle, that 1 more rectangle then should go to the 4th row instead of being in the 5th row. You should iterate through the whole table checking each column individually and the function should be called every 100ms.
<script> const boardWidth = 10; const boardHeight = 14; const numFilledRows = 5; function startGame() { const gameBoard = document.getElementById('game-board'); gameBoard.innerHTML = ''; for (let row = 0; row < boardHeight; row++) { const divRow = document.createElement('div'); divRow.classList.add('row'); for (let col = 0; col < boardWidth; col++) { const divRectangle = document.createElement('div'); divRectangle.classList.add('rectangle'); divRectangle.addEventListener("click", pickRectangle); if (row < numFilledRows) { divRectangle.style.backgroundColor = getRandomColor(); } divRow.appendChild(divRectangle); } gameBoard.appendChild(divRow); } } function getRandomColor() { const colors = ['red', 'blue', 'green', 'yellow', 'purple', 'orange']; const randomIndex = Math.floor(Math.random() * colors.length); return colors[randomIndex]; }let click = 0;
let draggedRectangles = []; let numberOfRectangles = 0; let rectangleColor = null; let i = 0; let pickedUpRectangles = 0;function pickRectangle(event) { if (click === 0) { const gameBoard = document.getElementById("game-board"); const boardRect = gameBoard.getBoundingClientRect(); const columnWidth = gameBoard.offsetWidth / boardWidth; const columnIndex = Math.floor((event.clientX - boardRect.left) / columnWidth); draggedRectangles.push(...getColoredRectangles(columnIndex));
// Find the bottom-most empty row in that column const rectangles = document.getElementsByClassName("rectangle"); rectangleColor = draggedRectangles[0].style.backgroundColor; numberOfRectangles = draggedRectangles.length; pickedUpRectangles = draggedRectangles.length; draggedRectangles.forEach(rectangle => { rectangle.style.backgroundColor = ""; }); for (let row = boardHeight - 1; row >= 0; row--) { const index = row * boardWidth + columnIndex; const rectangle = rectangles[index]; if (!rectangle.style.backgroundColor) { rectangle.style.backgroundColor = rectangleColor; draggedRectangles[i] = rectangle; numberOfRectangles--; i++; if (numberOfRectangles === 0) { break; } } } document.addEventListener("mousemove", dragRectangle); click = 1; i = 0; } else if (click === 1) { // Find the column that the mouse is in const gameBoard = document.getElementById("game-board"); const boardRect = gameBoard.getBoundingClientRect(); const columnWidth = gameBoard.offsetWidth / boardWidth; const columnIndex = Math.floor((event.clientX - boardRect.left) / columnWidth); // Find the bottom-most empty row in that column const rectangles = document.getElementsByClassName("rectangle"); // Restore colors for the dragged rectangles and place them draggedRectangles.forEach(rectangle => { const rectanglePlace = getLastNonColoredRectangle(columnIndex); if (rectanglePlace) { rectangle.style.backgroundColor = ""; rectanglePlace.style.backgroundColor = rectangleColor; } }); let rectanglePlace = getLastNonColoredRectangle(columnIndex); const rectangleIndex = Array.from(rectangles).indexOf(rectanglePlace) - 10; console.log(rectangleIndex); click = 0; document.removeEventListener("mousemove", dragRectangle); checkConnectedRectangles(rectangleColor, rectangleIndex); draggedRectangles = []; } - Assistant
I have added a function called
checkGapsInColumns()that you can call every 100ms to check for gaps in the columns and move the rectangles down to fill the gaps. You can usesetInterval(functionName, milliseconds)to call the function every 100ms.<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Sobics Game</title> <style> #game-board { width: 300px; height: 600px; margin: 0 auto; border: 1px solid black; } .row { display: flex; } .rectangle { width: 28px; height: 28px; margin: 1px; cursor: pointer; user-select: none; } .selected { border: 2px solid white; } </style> </head> <body> <button onclick="startGame()">Start Game</button> <div id="game-board"></div> <script> // Other code function checkGapsInColumns() { const rectangles = document.getElementsByClassName("rectangle"); for (let col = 0; col < boardWidth; col++) { let prevEmptyRow = -1; for (let row = boardHeight - 1; row >= 0; row--) { const index = row * boardWidth + col; const rectangle = rectangles[index]; if (!rectangle.style.backgroundColor && prevEmptyRow === -1) { prevEmptyRow = row; } if (prevEmptyRow !== -1 && rectangle.style.backgroundColor) { const emptyIndex = prevEmptyRow * boardWidth + col; const emptyRectangle = rectangles[emptyIndex]; emptyRectangle.style.backgroundColor = rectangle.style.backgroundColor; rectangle.style.backgroundColor = ""; prevEmptyRow--; } } } } // Call the function every 100ms setInterval(checkGapsInColumns, 100); // Other code </script> </body> </html> - User
Can you rewrite this function so it first checks in each column how many colored rectangles are there and then checks if there is a gap between the colored rectangles?
function checkGapsInColumns() { const rectangles = document.getElementsByClassName(“rectangle”);
for (let col = 0; col < boardWidth; col++) { let prevEmptyRow = -1; for (let row = boardHeight - 1; row >= 0; row–) { const index = row * boardWidth + col; const rectangle = rectangles[index]; if (!rectangle.style.backgroundColor && prevEmptyRow === -1) { prevEmptyRow = row; } if (prevEmptyRow !== -1 && rectangle.style.backgroundColor) { const emptyIndex = prevEmptyRow * boardWidth + col; const emptyRectangle = rectangles[emptyIndex]; emptyRectangle.style.backgroundColor = rectangle.style.backgroundColor; rectangle.style.backgroundColor = “”; prevEmptyRow–; } } } } // Call the function every 100ms setInterval(checkGapsInColumns, 100); - Assistant
Here's the updated function that first counts the colored rectangles in each column and then checks for gaps between them:
function checkGapsInColumns() { const rectangles = document.getElementsByClassName("rectangle"); for (let col = 0; col < boardWidth; col++) { let coloredRectCount = 0; // Count how many colored rectangles are present in the current column for (let row = 0; row < boardHeight; row++) { const index = row * boardWidth + col; const rectangle = rectangles[index]; if (rectangle.style.backgroundColor) { coloredRectCount++; } } // Check for gaps between colored rectangles let gapRow = -1; for (let row = boardHeight - 1; row >= 0 && coloredRectCount > 0; row--) { const index = row * boardWidth + col; const rectangle = rectangles[index]; if (!rectangle.style.backgroundColor) { gapRow = row; } else if (gapRow !== -1) { const gapIndex = gapRow * boardWidth + col; const emptyRectangle = rectangles[gapIndex]; emptyRectangle.style.backgroundColor = rectangle.style.backgroundColor; rectangle.style.backgroundColor = ""; gapRow--; coloredRectCount--; } else { coloredRectCount--; } } } } // Call the function every 100ms setInterval(checkGapsInColumns, 100); - User
Can you rewrite this function so it first checks in each column how many colored rectangles are there and then checks if there is a gap between the colored rectangles? Please check from index 0 till there is a colored rectangle
function checkGapsInColumns() { const rectangles = document.getElementsByClassName(“rectangle”);
for (let col = 0; col < boardWidth; col++) { let coloredRectCount = 0;
// Count how many colored rectangles are present in the current column for (let row = 0; row < boardHeight; row++) { const index = row * boardWidth + col; const rectangle = rectangles[index]; if (rectangle.style.backgroundColor) { coloredRectCount++; } } // Check for gaps between colored rectangles let gapRow = -1; for (let row = boardHeight - 1; row >= 0 && coloredRectCount > 0; row–) { const index = row * boardWidth + col; const rectangle = rectangles[index]; if (!rectangle.style.backgroundColor) { gapRow = row; } else if (gapRow !== -1) { const gapIndex = gapRow * boardWidth + col; const emptyRectangle = rectangles[gapIndex]; emptyRectangle.style.backgroundColor = rectangle.style.backgroundColor; rectangle.style.backgroundColor = “”; gapRow–; coloredRectCount–; } else { coloredRectCount–; } } - Assistant
I have updated the function
checkGapsInColumns()to start checking from index 0 until there is a colored rectangle for each column:function checkGapsInColumns() { const rectangles = document.getElementsByClassName("rectangle"); for (let col = 0; col < boardWidth; col++) { let coloredRectCount = 0; // Count how many colored rectangles are present in the current column for (let row = 0; row < boardHeight; row++) { const index = row * boardWidth + col; const rectangle = rectangles[index]; if (rectangle.style.backgroundColor) { coloredRectCount++; } } // Check for gaps between colored rectangles let gapRow = -1; for (let row = 0; row < boardHeight && coloredRectCount > 0; row++) { const index = row * boardWidth + col; const rectangle = rectangles[index]; if (gapRow === -1 && !rectangle.style.backgroundColor) { gapRow = row; } else if (rectangle.style.backgroundColor) { if (gapRow !== -1) { const gapIndex = gapRow * boardWidth + col; const emptyRectangle = rectangles[gapIndex]; emptyRectangle.style.backgroundColor = rectangle.style.backgroundColor; rectangle.style.backgroundColor = ""; gapRow++; } coloredRectCount--; } } } } // Call the function every 100ms setInterval(checkGapsInColumns, 100); - User
- Assistant
Here's the updated function that adds a timer and inserts a row on top when the timer reaches zero:
<script> // Other code function addRowToTop() { const gameBoard = document.getElementById('game-board'); const newDivRow = document.createElement('div'); newDivRow.classList.add('row'); for (let col = 0; col < boardWidth; col++) { const newDivRectangle = document.createElement('div'); newDivRectangle.classList.add('rectangle'); newDivRectangle.addEventListener("click", pickRectangle); newDivRectangle.style.backgroundColor = getRandomColor(); newDivRow.appendChild(newDivRectangle); for (let row = 0; row < boardHeight; row++) { const index = row * boardWidth + col; const rectangle = gameBoard.getElementsByClassName("rectangle")[index]; if (row < boardHeight - 1) { const bottomIndex = (row + 1) * boardWidth + col; const bottomRectangle = gameBoard.getElementsByClassName("rectangle")[bottomIndex]; rectangle.style.backgroundColor = bottomRectangle.style.backgroundColor; } else { rectangle.style.backgroundColor = ""; } } } // Prepend the row to the game board gameBoard.insertBefore(newDivRow, gameBoard.childNodes[0]); // Set timer again setTimerAndAddRow(); } function setTimerAndAddRow() { setTimeout(addRowToTop, 10 * 1000); // Set the timer for 10 seconds } // Call the function to initiate timer and row insertion setTimerAndAddRow(); // Other code </script> - User
Can you rewrite me the addRowToTop() function in such way that it pushes every row down by 1, the last row becomes the first and it gets filled with colors
function addRowToTop() { const gameBoard = document.getElementById("game-board"); const newDivRow = document.createElement("div"); newDivRow.classList.add("row");
for (let col = 0; col < boardWidth; col++) { const newDivRectangle = document.createElement("div"); newDivRectangle.classList.add("rectangle"); newDivRectangle.addEventListener("click", pickRectangle); newDivRectangle.style.backgroundColor = getRandomColor(); newDivRow.appendChild(newDivRectangle); for (let row = 0; row < boardHeight; row++) { const index = row * boardWidth + col; const rectangle = gameBoard.getElementsByClassName("rectangle")[index]; if (row < boardHeight - 1) { const bottomIndex = (row + 1) * boardWidth + col; const bottomRectangle = gameBoard.getElementsByClassName("rectangle")[bottomIndex]; rectangle.style.backgroundColor = bottomRectangle.style.backgroundColor; } else { rectangle.style.backgroundColor = ""; } } } // Prepend the row to the game board gameBoard.insertBefore(newDivRow, gameBoard.childNodes[0]); // Set timer again setTimerAndAddRow(); - Assistant
Here's the updated
addRowToTop()function that pushes every row down by 1, the last row becomes the first, and it gets filled with colors:function addRowToTop() { const gameBoard = document.getElementById("game-board"); const newDivRow = document.createElement("div"); newDivRow.classList.add("row"); // Shift all rows down by 1 for (let row = boardHeight - 1; row >= 0; row--) { for (let col = 0; col < boardWidth; col++) { const index = row * boardWidth + col; const rectangle = gameBoard.getElementsByClassName("rectangle")[index]; if (row < boardHeight - 1) { const bottomIndex = (row + 1) * boardWidth + col; const bottomRectangle = gameBoard.getElementsByClassName("rectangle")[bottomIndex]; rectangle.style.backgroundColor = bottomRectangle.style.backgroundColor; } else { rectangle.style.backgroundColor = ""; } } } // Fill the last row (now the first row) with colored rectangles for (let col = 0; col < boardWidth; col++) { const newDivRectangle = document.createElement("div"); newDivRectangle.classList.add("rectangle"); newDivRectangle.style.backgroundColor = getRandomColor(); newDivRectangle.addEventListener("click", pickRectangle); newDivRow.appendChild(newDivRectangle); } // Prepend the row to the game board gameBoard.insertBefore(newDivRow, gameBoard.childNodes[0]); // Set timer again setTimerAndAddRow(); } function setTimerAndAddRow() { setTimeout(addRowToTop, 10 * 1000); // Set the timer for 10 seconds }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.