• + 1 comment
    function bomberMan(n, gridInit) {
        
        // Grid will have 3 states which are going to get repeated over and over
        // 1. Grid before explosion ::: gridBeforeExplosion
        // 2. Grid full of bombs ::: gridFullOfBombs
        // 3. Grid after explosion ::: gridAfterExplosion
            
        let gridBeforeExplosion = [...gridInit];
        if(n === 1) return gridBeforeExplosion; // Nothing happens at second 1
        
        const gridFullOfBombs = plantBombs(gridBeforeExplosion); 
        if(n % 2 === 0) return gridFullOfBombs.map( v => v.join('')); // Every pair second bomberman fills the grid with bombs
        
        // from this point onwards every unpair number of seconds we alternate between 2 states
        // n = 3, 7, 11, 15, 19, 23, ... will always be the same ( (n - 3) % 4 === 0 )
        // n = 5, 9, 13, 17, 21, 25, ... will always be the same ( (n - 5) % 4 === 0 )
        let gridAfterExplosion = explosion(gridBeforeExplosion, gridFullOfBombs);
        if((n - 3) % 4 === 0) return gridAfterExplosion;
        
        gridBeforeExplosion = [...gridAfterExplosion];
        gridAfterExplosion = explosion(gridBeforeExplosion, gridFullOfBombs);
        
        return gridAfterExplosion;
    }