I want to write a sobics game, can you code it for me in javascript? It is a tetris-like…
- User
I want to write a sobics game, can you code it for me in javascript? It is a tetris-like game, so there is my character on the bottom of a 10 by 10 square, my character's goal is that in each slot there is a differently coloured rectangle and if I match 4 of them (either in a column or in a row or both) they disappear and each disappeared rectangle gives 100 points. There is also a 30 second timer and if it reaches 30 seconds, it resets itself and generates a row of random rectangles.
- Assistant
- User
Great! Now I want you to make a function that if I right click on a column (it doesn't matter where I click, it should pick up the rectangle on the last row in the same column and stick it to 10th row and if I move my mouse it should follow it) and when I click again, it places it down. Please only write down the function
<script> const boardWidth = 10; const boardHeight = 10; 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'); 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]; } </script> - Assistant
- User
Great! Now I want you to make a function that if I left click on a column (it doesn't matter where I click, it should pick up the rectangle on the last row in the same column and stick it to 10th row and if I move my mouse it should follow it) and when I click again, it places it down. Please only write down the javascript part of the code
<script> const boardWidth = 10; const boardHeight = 10; 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'); 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]; } </script> - Assistant
- User
Great! Now I want that when I click on a rectangle it should move it to the 10th row but it should follow my mouse and when I click again, it should place it down in the column that my mouse is in. Only write down the script part of the code.
<script> const boardWidth = 10; const boardHeight = 10; 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'); if (row < numFilledRows) { divRectangle.style.backgroundColor = getRandomColor(); } divRow.appendChild(divRectangle); } gameBoard.appendChild(divRow); } // Add event listener to each column of rectangles const rectangles = document.getElementsByClassName('rectangle'); for (let col = 0; col < boardWidth; col++) { for (let row = 0; row < boardHeight; row++) { const index = row * boardWidth + col; rectangles[index].addEventListener('click', function() { moveRectangleToBottom(index); }); } } } function getRandomColor() { const colors = ['red', 'blue', 'green', 'yellow', 'purple', 'orange']; const randomIndex = Math.floor(Math.random() * colors.length); return colors[randomIndex]; } function moveRectangleToBottom(index) { const rectangles = document.getElementsByClassName('rectangle'); const rectangle = rectangles[index]; const rectangleColor = rectangle.style.backgroundColor; rectangle.style.backgroundColor = ''; for (let row = boardHeight - 1; row >= 0; row--) { const newIndex = row * boardWidth + index % boardWidth; const nextRectangle = rectangles[newIndex]; if (!nextRectangle.style.backgroundColor) { nextRectangle.style.backgroundColor = rectangleColor; return; } } } </script> - Assistant
I've updated your code to include a function called
attachRectanglePickListener(). This function takes care of attaching a left-click event listener to each rectangle, and the related pick-up and place-down logic.Add the following function and modifications in your section:
- User
- Assistant