Untitled

 avatar
unknown
plain_text
4 years ago
5.1 kB
5
Indexable
var matrix = [
    [0,1,0,0,0,0],
    [0,0,0,0,0,1],
    [0,1,0,0,0,0],
    [0,1,0,0,0,0],
    [0,0,0,0,1,0],
    [1,0,0,0,0,0]
]


var boomHint = [];

var countBoom = function() {
    for(var i = 0; i < 6; i++) {
        for(var j = 0; j < 6; j++) {
            if(matrix[i][j]) {               
                if(!boomHint[i]) {
                    boomHint[i] = [];
                } 
                if(boomHint[i][j] !== undefined) {
                    boomHint[i][j]
                } else {
                    boomHint[i].push(null);
                }
            }
            else {
                var total = 0;
                for(var a = -1; a <= 1; a++) {
                    for(var b = -1; b <= 1; b++) {
                        var test = matrix[i+a]?.[j+b];
                        if(test) {
                            total++;
                        }
                    }
                }
                if(!boomHint[i]) {
                    boomHint[i] = [];
                } 
                if(boomHint[i][j] !== undefined) {
                    boomHint[i][j];
                } else {
                    boomHint[i].push(total);
                }
            }
        }
    }
}

function checkBoom(a, b, event) {
    if(matrix[a][b]) {
        let rows = document.querySelectorAll('tr');
        for(let i = 0; i < rows.length; i++) {
            let currentRow = rows[i];
            let cells = currentRow.querySelectorAll('td');
            for(let j = 0; j < cells.length; j++) {
                let isBoom = matrix[i][j] == 1;
                let hintNumber = boomHint[i][j];
                if(isBoom) {
                    cells[j].querySelector('.boom').style.display = 'block';
                }
                if(hintNumber != null && hintNumber > 0) {

                }
            }               
        }
        /*alert("Game over! Play again?");
        
         */setTimeout(function() {
            var r = confirm("Game over! Play again?");
            if (r == true) {
                init();
            } else {
                return;
            }
        }, 1)
        
    } else {
        //console.log('show thong tin hint');// nay de choi game tiep moiws biet nos show the nao
        if(boomHint[a][b] > 0) {
            event.target;
            event.target.querySelector('.number').innerText = boomHint[a][b];
            event.target.querySelector('.number').style.display = 'block';
        }
        if(boomHint[a][b] == 0) {
            //event.target;
            event.target.style.background = 'green';
            for(let i= a; i < 6; i++) {
                for(let j = b; j < 6; j++) {
                    if(boomHint[i][j] == 0) {            
                        document.querySelectorAll('table tr:nth-child('+(i+1)+') td')[j].style.background = 'green';
                    } else {
                        break;
                    }
                }
                for(let j = b; j >= 0; j--) {
                    if(boomHint[i][j] == 0) {
                        document.querySelectorAll('table tr:nth-child('+(i+1)+') td')[j].style.background = 'green';
                    } else {
                        break;
                    }
                }
            }
            
            for(let i= a; i >= 0; i--) {
                for(let j = b; j < 6; j++) {
                    if(boomHint[i][j] == 0) {            
                        document.querySelectorAll('table tr:nth-child('+(i+1)+') td')[j].style.background = 'green';
                    } else {
                        break;
                    }
                }
                for(let j = b; j >= 0; j--) {
                    if(boomHint[i][j] == 0) {
                        document.querySelectorAll('table tr:nth-child('+(i+1)+') td')[j].style.background = 'green';
                    } else {
                        break;
                    }
                }
            }
        }
    }
}


function init() {
    randomBoom() ;
    // TODO: an het may cai boom voi number lai
    //let rows = document.querySelectorAll('td');
    //    for(let i = 0; i < rows.length; i++) {
    //        let currentRow = rows[i];
            let cells = document.querySelectorAll('td');
            for(let j = 0; j < cells.length; j++) {
                cells[j].querySelector('.boom').style.display = 'none';
                cells[j].querySelector('.number').style.display = 'none';
                cells[j].querySelector('.number').innerText = "";
                cells[j].style.background = 'rgb(135, 194, 233)';
                console.log(j);
            }               
    //    }
    // TODO: tao random booom
    countBoom();
    
}

init();

function randomBoom() {
    for(var i = 0; i < 6; i++) {
        for(var j = 0; j < 6; j++) {
            matrix[i][j] = (Math.random()>=0.5)? 1 : 0;
        }
    }
}
randomBoom() ;
Editor is loading...