DOCTYPE html html lang en head meta charset UTF 8 meta name viewport content width device width initial scale 1 0 title Candy Crush Mini Simple Demo title style canvas border 1px solid black info margin top 10px style head body canvas id gameCanvas width 400 height 400 canvas div id info p Score span id score 0 span p p Current Candy span id currentCandy None span p p Time span id timer 0 span seconds p div script const canvas document getElementById gameCanvas const ctx canvas getContext 2d const candyColors red green blue yellow orange purple const candySize 40 const numRows 8 const numCols 8 const gameTime 60 seconds let grid let selectedCandy null let score 0 let timer gameTime function initializeGrid for let row 0 row numRows row grid row for let col 0 col numCols col grid row col color candyColors Math floor Math random candyColors length x col candySize y row candySize function drawCandies for let row 0 row numRows row for let col 0 col numCols col const candy grid row col ctx fillStyle candy color ctx fillRect candy x candy y candySize candySize function drawText document getElementById score textContent score document getElementById currentCandy textContent selectedCandy grid selectedCandy row selectedCandy col color None document getElementById timer textContent timer function swapCandies row1 col1 row2 col2 const temp grid row1 col1 grid row1 col1 grid row2 col2 grid row2 col2 temp function checkForMatches let matches Horizontal matches for let row 0 row numRows row for let col 0 col numCols 2 col if grid row col color grid row col 1 color grid row col color grid row col 2 color matches push row col row col col 1 row col col 2 Vertical matches for let col 0 col numCols col for let row 0 row numRows 2 row if grid row col color grid row 1 col color grid row col color grid row 2 col color matches push row col row row 1 col row row 2 col Remove matches if matches length 0 matches forEach row col grid row col color null score matches length function handleInput event const rect canvas getBoundingClientRect const mouseX event clientX rect left const mouseY event clientY rect top const col Math floor mouseX candySize const row Math floor mouseY candySize if selectedCandy selectedCandy row col else const row prevRow col prevCol selectedCandy Check if selected candy is adjacent to current candy if Math abs row prevRow Math abs col prevCol 1 row prevRow col prevCol swapCandies row col prevRow prevCol checkForMatches selectedCandy null else selectedCandy row col function update drawCandies drawText function gameLoop update requestAnimationFrame gameLoop function startTimer const interval setInterval if timer 0 timer else clearInterval interval alert Game Over Your score score 1000 function init initializeGrid canvas addEventListener click handleInput startTimer gameLoop init script body html
grid row col color grid row col 2 color matches push row col row col col 1 row col col 2 Vertical matches for let col 0 col numCols col for let row 0 row numRows 2 row if grid row col color grid row 1 col color grid row col color grid row 2 col color matches push row col row row 1 col row row 2 col Remove matches if matches length 0 matches forEach row col grid row col color null score matches length function handleInput event const rect canvas getBoundingClientRect const mouseX event clientX rect left const mouseY event clientY rect top const col Math floor mouseX candySize const row Math floor mouseY candySize if selectedCandy selectedCandy row col else const row prevRow col prevCol selectedCandy Check if selected candy is adjacent to current candy if Math abs row prevRow Math abs col prevCol 1 row prevRow col prevCol swapCandies row col prevRow prevCol checkForMatches selectedCandy null else selectedCandy row col function update drawCandies drawText function gameLoop update requestAnimationFrame gameLoop function startTimer const interval setInterval if timer 0 timer else clearInterval interval alert Game Over Your score score 1000 function init initializeGrid canvas addEventListener click handleInput startTimer gameLoop init script body html