The Bomberman Game

  • + 0 comments

    Easy Javascript solution:

           const bombGameBoard = (initialState) => {
               const bombedField = initialState.map((row, index) => Array.from({ length: row.length }).fill(BOMB))
               for (let r = 0; r < initialState.length; r++) {
                   for (let c = 0; c < initialState[r].length; c++) {
                       if (initialState[r][c] === BOMB) {
                           bombedField[r][c] = EMPTY_FIELD;
                           if (bombedField[r+1]) {
                               bombedField[r+1][c] = EMPTY_FIELD;
                           }
    
                           if (bombedField[r-1]) {
                               bombedField[r-1][c] = EMPTY_FIELD;
                           }
    
                           if (bombedField[r-1]) {
                               bombedField[r-1][c] = EMPTY_FIELD;
                           }
    
                           if (bombedField[r]?.[c+1]) {
                               bombedField[r][c+1] = EMPTY_FIELD;
                           }
    
                           if (bombedField[r]?.[c-1]) {
                               bombedField[r][c-1] = EMPTY_FIELD;
                           }
                       }
                   }
               }
    
               return bombedField;
           }
    
    
           const BOMB = 'O';
           const EMPTY_FIELD = '.';
    
           function bomberMan(seconds, grid) {
               let initialState = grid.map((row) => Array.from(row));
               const firstCycleBoardState = bombGameBoard(initialState); // 3 7 11
               const secondCycleBoardState = bombGameBoard(firstCycleBoardState); // 5 9 13
               if (seconds === 1) {
                   return grid;
               }
               if (seconds % 4 === 3) {
                   return firstCycleBoardState.map((arr) => arr.join(''));
               }
    
               if (seconds % 4 === 1) {
                   return secondCycleBoardState.map((arr) => arr.join(''));
               }
    
               if (seconds % 2 === 0) {
                   return grid.map((row, index) => Array.from({ length: row.length }).fill(BOMB)).map((arr) => arr.join(''));
               }
           }