The Bomberman Game

  • + 0 comments

    C#:

    public static List<string> bomberMan(int n, List<string> grid)
        {
            if(n == 1)
                return grid;
            List<string> stuffedGrid = new List<string>();
            List<string> detonationGrid = new List<string>();
            string str1 = "";
            for(int j = 0; j < grid[0].Length; j++)
            {
                str1+="O";
            }
            for(int i = 0; i < grid.Count; i++)
            {
                stuffedGrid.Add(str1);
            }
            if(n % 2 == 0)
                return stuffedGrid;
                
            else if(n % 4 == 3)
            {
                detonationGrid = getBombLocations(stuffedGrid, grid);
            }               
            else
            {
                detonationGrid = getBombLocations(stuffedGrid, getBombLocations(stuffedGrid,grid));
            }
            return detonationGrid;
        }
        public static List<string> getBombLocations(List<string> stuffedGrid, List<string> grid)
        {
            List<string> detonatingGrid = new List<string>();
            detonatingGrid.AddRange(stuffedGrid);
            char[] temp = new char[grid[0].Length];
            for(int i = 0; i < grid.Count; i++)
            {
                string str = grid[i];
                for(int  j = 0; j < str.Length; j++)
                {
                    if(str[j] == 'O')
                    {
                         temp = detonatingGrid[i].ToCharArray();
                         temp[j] = '.';
                         string replaceBomb = new string(temp);
                         detonatingGrid[i] = replaceBomb;
                        if(j != str.Length-1){
                            temp = detonatingGrid[i].ToCharArray();
                            temp[j+1] = '.';
                            string replaceRight = new string(temp);
                            detonatingGrid[i] = replaceRight;
                        }
                        if(i != grid.Count-1){
                            temp = detonatingGrid[i+1].ToCharArray();
                            temp[j] = '.';
                            string replaceDown = new string(temp);
                            detonatingGrid[i+1] = replaceDown;
                        }
                        if(j != 0){
                            temp = detonatingGrid[i].ToCharArray();
                            temp[j-1] = '.';
                            string replaceLeft = new string(temp);
                            detonatingGrid[i] = replaceLeft;
                        }
                        if(i != 0){
                            temp = detonatingGrid[i-1].ToCharArray();
                            temp[j] = '.';
                            string replaceUp = new string(temp);
                            detonatingGrid[i-1] = replaceUp;
                        }
                    }
                } 
            }
            return detonatingGrid;
        }