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