Write me a function that when I pick up rectangles (I can only pick up same colored…
- User
Write me a function that when I pick up rectangles (I can only pick up same colored rectangles) and I put it down, it checks for same colored rectangles in the area (in a row or in a column) and if there are 4 or more rectangles, changes it's backgroundcolor to ""
<script> const boardWidth = 10; const boardHeight = 14; const numFilledRows = 5; let selectedRectangle = null; 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; } }); draggedRectangles = []; click = 0; document.removeEventListener("mousemove", dragRectangle); } - Assistant
The provided code allows you to pick up a group of rectangles with the same color and drop them in a column, but it does not check if there are 4 or more rectangles of the same color in a row or a column.
Here's the function to check for 4 or more rectangles of the same color and change their background color to an empty string:
- User
- Assistant
- User
- Assistant