<!DOCTYPE html>

<html lang="id">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">

<title>ONET BUAH - MALIK89</title>

<style>

*{margin:0;padding:0;box-sizing:border-box;-webkit-tap-highlight-color:transparent}

body{background:#1a1a2e;color:#fff;font-family:Arial,sans-serif;text-align:center;overflow:hidden;user-select:none}

.top{background:#16213e;padding:10px;display:flex;justify-content:space-between;align-items:center}

.top b{font-size:18px}.top b span{color:#FFC107}

.info{display:flex;justify-content:center;gap:10px;padding:10px;flex-wrap:wrap}

.pill{background:#0f3460;padding:8px 14px;border-radius:20px;font-weight:bold;font-size:14px}

.pill.red{background:#e94560}

#board{margin:5px auto;background:#0f3460;padding:6px;border-radius:12px;display:grid;gap:4px;width:98vw;max-width:480px;aspect-ratio:1/1.15}

.tile{background:#fff;border-radius:8px;display:flex;align-items:center;justify-content:center;font-size:26px;cursor:pointer;transition:0.1s;border:2px solid transparent}

.tile:active{transform:scale(0.9)}

.tile.selected{background:#FFC107;border-color:#fff;transform:scale(1.05);box-shadow:0 0 10px #FFC107}

.tile.empty{background:transparent;pointer-events:none}

.tile.hint{animation:blink 0.5s 3}

@keyframes blink{50%{background:#00ff88}}

.btns{display:flex;justify-content:center;gap:10px;margin:8px}

button{background:#FFC107;border:none;padding:12px 18px;border-radius:20px;font-weight:bold;font-size:14px;cursor:pointer}

button.blue{background:#00d4ff;color:#000}

button.green{background:#25D366;color:#fff}

#msg{position:fixed;inset:0;background:rgba(0,0,0,0.85);display:none;align-items:center;justify-content:center;flex-direction:column;z-index:99;padding:20px}

#msg h1{font-size:38px;margin-bottom:10px}

#msg p{margin-bottom:18px;color:#ddd;line-height:1.5}

.footer{font-size:11px;color:#888;margin-top:4px}

canvas{position:fixed;inset:0;pointer-events:none;z-index:50}

</style>

</head>

<body>

<canvas id="lineCanvas"></canvas>

<div class="top"><b>MALIK<span>89</span> ONET</b><span>🍎🍇🍊</span></div>

<div class="info">

  <div class="pill">⭐ Skor: <span id="skor">0</span></div>

  <div class="pill">🏆 Level: <span id="level">1</span></div>

  <div class="pill red">⏰ <span id="timer">120</span> Detik</div>

</div>

<div id="board"></div>

<div class="btns">

  <button class="blue" onclick="showHint()">💡 HINT (<span id="hintCount">3</span>)</button>

  <button onclick="shuffleBoard()">🔀 ACAK</button>

  <button class="green" onclick="restartGame()">🔄 ULANG</button>

</div>

<div class="footer">MALIK89.COM - Game Onet Buah | Pasangkan Buah Yang Sama Sampai Habis!</div>


<div id="msg"><h1 id="msgTitle">MENANG! 🎉</h1><p id="msgText"></p><button onclick="nextLevel()">➡️ LEVEL BERIKUTNYA</button><br><button class="blue" onclick="restartGame()">🔄 MAIN LAGI</button></div>


<script>

let ROWS=6, COLS=6, board=[], selected=null, skor=0, level=1, timeLeft=120, timerInt=null, hintCount=3, pairsLeft=0;

const BUAH=['🍎','🍇','🍊','🍓','🥝','🍌','🍍','🍉','🥭','🍒','🍑','🥥'];

const boardEl=document.getElementById('board');

const canvas=document.getElementById('lineCanvas');

const ctx=canvas.getContext('2d');


function resizeCanvas(){canvas.width=innerWidth;canvas.height=innerHeight} window.onresize=resizeCanvas; resizeCanvas();


function initBoard(){

  let total=ROWS*COLS;

  let typesNeeded=total/2;

  let arr=[];

  for(let i=0;i<typesNeeded;i++){let f=BUAH[i%BUAH.length];arr.push(f,f)}

  // acak

  for(let i=arr.length-1;i>0;i--){let j=Math.floor(Math.random()*(i+1));[arr[i],arr[j]]=[arr[j],arr[i]]}

  board=[];

  let k=0;

  for(let r=0;r<ROWS;r++){board[r]=[];for(let c=0;c<COLS;c++){board[r][c]=arr[k++] }}

  pairsLeft=typesNeeded;

  selected=null;

  render();

}

function render(){

  boardEl.style.gridTemplateColumns=`repeat(${COLS},1fr)`;

  boardEl.innerHTML='';

  for(let r=0;r<ROWS;r++) for(let c=0;c<COLS;c++){

    let d=document.createElement('div');

    d.className='tile'+(board[r][c]===null?' empty':'')+(selected&&selected.r===r&&selected.c===c?' selected':'');

    d.textContent=board[r][c]||'';

    d.onclick=()=>clickTile(r,c);

    d.id=`t-${r}-${c}`;

    boardEl.appendChild(d);

  }

  document.getElementById('skor').textContent=skor;

  document.getElementById('level').textContent=level;

  document.getElementById('timer').textContent=timeLeft;

  document.getElementById('hintCount').textContent=hintCount;

}

function clickTile(r,c){

  if(board[r][c]===null) return;

  playClick();

  if(!selected){selected={r,c};render();return}

  if(selected.r===r&&selected.c===c){selected=null;render();return}

  if(board[selected.r][selected.c]===board[r][c]){

    let path=findPath(selected,{r,c});

    if(path){

      drawLine(path);

      let a=selected,b={r,c};

      setTimeout(()=>{

        board[a.r][a.c]=null; board[b.r][b.c]=null;

        skor+=100; pairsLeft--; selected=null; ctx.clearRect(0,0,canvas.width,canvas.height);

        playMatch(); render();

        if(pairsLeft===0) winGame();

        else if(!hasMove()){alert('Tidak ada pasangan! Diacak otomatis'); shuffleBoard()}

      },350);

      return;

    }

  }

  // gagal

  selected={r,c}; render();

}

function findPath(a,b){

  // BFS dengan max 2 belokan, dengan border kosong di luar board

  let R=ROWS+2, C=COLS+2;

  let get=(r,c)=>{ if(r===0||r===ROWS+1||c===0||c===COLS+1) return null; return board[r-1][c-1]; }

  let visited=Array.from({length:R},()=>Array(C).fill(999));

  let q=[{r:a.r+1,c:a.c+1,dir:-1,turn:0,path:[a]}];

  visited[a.r+1][a.c+1]=0;

  let dirs=[[-1,0],[1,0],[0,-1],[0,1]];

  while(q.length){

    let cur=q.shift();

    for(let d=0;d<4;d++){

      let nr=cur.r+dirs[d][0], nc=cur.c+dirs[d][1];

      if(nr<0||nr>=R||nc<0||nc>=C) continue;

      let nt=cur.turn + (cur.dir!==-1&&cur.dir!==d?1:0);

      if(nt>2) continue;

      if(nr===b.r+1&&nc===b.c+1){

        return [...cur.path,{r:b.r,c:b.c}];

      }

      if(get(nr,nc)!==null) continue;

      if(visited[nr][nc]<=nt) continue;

      visited[nr][nc]=nt;

      q.push({r:nr,c:nc,dir:d,turn:nt,path:[...cur.path,{r:nr-1,c:nc-1}]});

    }

  }

  return null;

}

function getTileCenter(r,c){

  let el=document.getElementById(`t-${r}-${c}`);

  if(!el) return {x:innerWidth/2,y:200};

  let rect=el.getBoundingClientRect();

  return {x:rect.left+rect.width/2,y:rect.top+rect.height/2};

}

function drawLine(path){

  ctx.clearRect(0,0,canvas.width,canvas.height);

  ctx.strokeStyle='#FFC107';ctx.lineWidth=5;ctx.lineJoin='round';ctx.lineCap='round';

  ctx.beginPath();

  path.forEach((p,i)=>{let pos=getTileCenter(p.r,p.c); if(i===0)ctx.moveTo(pos.x,pos.y); else ctx.lineTo(pos.x,pos.y)});

  ctx.stroke();

}

function hasMove(){

  for(let r1=0;r1<ROWS;r1++)for(let c1=0;c1<COLS;c1++) if(board[r1][c1]) for(let r2=0;r2<ROWS;r2++)for(let c2=0;c2<COLS;c2++) if(board[r2][c2]&&!(r1===r2&&c1===c2)&&board[r1][c1]===board[r2][c2]&&findPath({r:r1,c:c1},{r:r2,c:c2})) return true;

  return false;

}

function showHint(){

  if(hintCount<=0){alert('Hint habis!');return}

  for(let r1=0;r1<ROWS;r1++)for(let c1=0;c1<COLS;c1++) if(board[r1][c1]) for(let r2=0;r2<ROWS;r2++)for(let c2=0;c2<COLS;c2++) if(board[r2][c2]&&board[r1][c1]===board[r2][c2]&&!(r1===r2&&c1===c2)&&findPath({r:r1,c:c1},{r:r2,c:c2})){

    hintCount--; document.getElementById(`t-${r1}-${c1}`).classList.add('hint'); document.getElementById(`t-${r2}-${c2}`).classList.add('hint'); render(); return;

  }

}

function shuffleBoard(){

  let vals=[]; for(let r=0;r<ROWS;r++)for(let c=0;c<COLS;c++) if(board[r][c]) vals.push(board[r][c]);

  for(let i=vals.length-1;i>0;i--){let j=Math.floor(Math.random()*(i+1));[vals[i],vals[j]]=[vals[j],vals[i]]}

  let k=0; for(let r=0;r<ROWS;r++)for(let c=0;c<COLS;c++) if(board[r][c]) board[r][c]=vals[k++];

  selected=null; render();

}

function startTimer(){

  clearInterval(timerInt);

  timerInt=setInterval(()=>{

    timeLeft--; document.getElementById('timer').textContent=timeLeft;

    if(timeLeft<=0){clearInterval(timerInt); showMsg('WAKTU HABIS! 😭','Skor kamu: '+skor+'<br>Yuk coba lagi, pasti bisa!',false)}

  },1000);

}

function winGame(){

  clearInterval(timerInt);

  let bonus=timeLeft*10; skor+=bonus;

  showMsg('MENANG! 🎉🏆','Luar biasa bos! Level '+level+' selesai!<br>Skor: '+skor+' | Bonus Waktu: +'+bonus,true);

}

function showMsg(t,txt,isWin){

  document.getElementById('msgTitle').textContent=t;

  document.getElementById('msgText').innerHTML=txt;

  document.getElementById('msg').style.display='flex';

  if(isWin) playWin();

}

function nextLevel(){

  document.getElementById('msg').style.display='none';

  level++; timeLeft=120+level*10; hintCount=3;

  if(level===2){ROWS=7;COLS=6} if(level>=3){ROWS=8;COLS=6}

  initBoard(); startTimer();

}

function restartGame(){

  document.getElementById('msg').style.display='none';

  skor=0; level=1; ROWS=6; COLS=6; timeLeft=120; hintCount=3;

  initBoard(); startTimer();

}

let audioCtx=null;

function playClick(){try{audioCtx=audioCtx||new (window.AudioContext||window.webkitAudioContext)();let o=audioCtx.createOscillator(),g=audioCtx.createGain();o.connect(g);g.connect(audioCtx.destination);o.frequency.value=600;g.gain.value=0.1;o.start();o.stop(audioCtx.currentTime+0.08)}catch(e){}}

function playMatch(){try{audioCtx=audioCtx||new (window.AudioContext||window.webkitAudioContext)();[523,659,784].forEach((f,i)=>{let o=audioCtx.createOscillator(),g=audioCtx.createGain();o.connect(g);g.connect(audioCtx.destination);o.frequency.value=f;g.gain.value=0.12;o.start(audioCtx.currentTime+i*0.08);o.stop(audioCtx.currentTime+i*0.08+0.15)})}catch(e){}}

function playWin(){try{audioCtx=audioCtx||new (window.AudioContext||window.webkitAudioContext)();[523,659,784,1046].forEach((f,i)=>{let o=audioCtx.createOscillator(),g=audioCtx.createGain();o.connect(g);g.connect(audioCtx.destination);o.frequency.value=f;g.gain.value=0.15;o.start(audioCtx.currentTime+i*0.15);o.stop(audioCtx.currentTime+i*0.15+0.3)})}catch(e){}}

restartGame();

</script>

</body>

</html>

Tidak ada komentar:

Posting Komentar

 <!DOCTYPE html> <html lang="id"> <head> <meta charset="UTF-8"> <meta name="viewport...