perubahan untuk logika permainan

This commit is contained in:
basilius leta 2025-12-15 20:28:29 +07:00
parent c2eafbbcfb
commit 60de57f1c5

View File

@ -33,7 +33,7 @@ function playSound(name){
SFX[name].play().catch(()=>{});
}
/********** TIMER (unchanged) **********/
// TIMER PERMAINAN
let timerSeconds = 0;
let timerInterval = null;
let timerRunning = false;
@ -48,7 +48,7 @@ function belongsTo(v, player){
return (player===1 && (v===1||v===3)) || (player===2 && (v===2||v===4));
}
/********** RESET BOARD **********/
// RESET BOARD
function resetBoard(){
console.debug("resetBoard()");
board = Array.from({length:size}, ()=>Array(size).fill(0));
@ -135,7 +135,7 @@ function drawMoveHints(moves){
}
}
/********** MOVE LOGIC **********/
// LOGIKA UNTUK MOVE
function tryMove(r1,c1,r2,c2){
console.debug("tryMove", {r1,c1,r2,c2, gameOver, currentTurn, aiEnabled});
if(gameOver) { console.debug("move blocked: gameOver"); return false; }
@ -173,7 +173,7 @@ function tryMove(r1,c1,r2,c2){
} else { console.debug("move blocked: wrong direction for non-king"); return false; }
}
// capture
// MEMAKAN PION
if(Math.abs(dr)===2 && Math.abs(dc)===2){
const mr = r1 + dr/2, mc = c1 + dc/2;
if(!inside(mr,mc)) { console.debug("capture blocked: middle outside"); return false; }
@ -213,7 +213,7 @@ function crownIfNeeded(r,c){
function switchTurn(){ currentTurn = (currentTurn===1?2:1); }
function saveHistory(){ history.push(JSON.parse(JSON.stringify(board))); if(history.length>60) history.shift(); }
/********** GENERATE MOVES **********/
// MEMBUAT GERAKAN
function generateMovesForPiece(r,c,player){
const v = board[r][c];
if(v===0) return [];
@ -248,7 +248,7 @@ function getAllMovesFor(player){
return all;
}
/********** CHECK WIN **********/
// CEK KONDISI MENANG
function checkWinCondition(){
if(gameOver) return true;
let red=0, white=0;
@ -268,7 +268,7 @@ function checkWinCondition(){
return false;
}
/********** AI **********/
// AI MOVE
function aiMakeMove(){
if(!aiEnabled || currentTurn !== aiPlays || gameOver) { console.debug("aiMakeMove blocked", {aiEnabled, currentTurn, aiPlays, gameOver}); return; }
const moves = getAllMovesFor(aiPlays);
@ -288,7 +288,7 @@ function aiMakeMove(){
function triggerAI(){ setTimeout(()=>{ if(aiEnabled && currentTurn===aiPlays && !gameOver) aiMakeMove(); }, 120); }
/********** MOUSE **********/
// MENGKLIK PADA KANVAS
canvas.addEventListener("click", (e)=>{
console.debug("canvas click", {gameOver, aiEnabled, currentTurn});
if(gameOver) return;
@ -319,17 +319,17 @@ canvas.addEventListener("click", (e)=>{
draw();
});
/********** KEY H TOGGLE HINTS **********/
// TOGGLE HINTS DENGAN 'H'
document.addEventListener("keydown", (e)=>{ if(e.key.toLowerCase()==='h'){ hintsEnabled = !hintsEnabled; draw(); } });
/********** BUTTONS **********/
// BUTTONS
const newBtn = document.getElementById("newBtn");
if(newBtn) newBtn.onclick = ()=> resetBoard();
const undoBtn = document.getElementById("undoBtn");
if(undoBtn) undoBtn.onclick = ()=>{ if(history.length>0){ board = history.pop(); draw(); } };
/********** LEADERBOARD (unchanged) **********/
// LEADERBOARD LOAD & SAVE
async function loadLeaderboard(){
const box = document.getElementById("leaderboardList");
if(!box) return;
@ -358,7 +358,7 @@ async function saveResult(result){
}catch(err){ console.error("saveResult error", err); }
}
/********** POPUP **********/
// END GAME POPUP
function showEnd(title, msg, result){
if(document.getElementById("endPopup")) return;
gameOver = true;
@ -375,5 +375,5 @@ function showEnd(title, msg, result){
document.getElementById("popupClose").onclick = ()=> div.remove();
}
/********** START **********/
resetBoard();
// START GAME
resetBoard();