Advertisement
Guest User

Bomberman IZZI

a guest
May 20th, 2019
344
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. using System;
  2. using System.Diagnostics;
  3. using System.Linq;
  4.  
  5. namespace HackerRankProjects
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. string[] inputStringArray = new string[6];
  12. inputStringArray[0] = ".......";
  13. inputStringArray[1] = "...O...";
  14. inputStringArray[2] = "....O..";
  15. inputStringArray[3] = ".......";
  16. inputStringArray[4] = "OO.....";
  17. inputStringArray[5] = "OO.....";
  18. string[] test = bomberMan(3, inputStringArray);
  19. Console.ReadKey();
  20. }
  21.  
  22. const char BOMB = 'O';
  23. const string CLEAN = ".";
  24.  
  25. // Complete the bomberMan function below.
  26. static string[] bomberMan(int n, string[] grid)
  27. {
  28. var stopTimer = Stopwatch.StartNew();
  29.  
  30. var columnSize = grid[0].Length;
  31. bool explodeTime = false;
  32.  
  33. string[] checkpoint = new string[grid.Length];
  34. string[] gridOriginal = new string[grid.Length];
  35. grid.CopyTo(gridOriginal, 0);
  36. grid.CopyTo(checkpoint, 0);
  37.  
  38. if (n % 2 == 0)
  39. {
  40. PlantAll(grid, columnSize);
  41. return grid;
  42. }
  43.  
  44. int numberCycles = 0;
  45. n = n % 32;
  46.  
  47. for (int secs = 2; secs <= n; secs++)
  48. {
  49. if (explodeTime)
  50. {
  51. for (int line = 0; line < grid.Length; line++)
  52. {
  53. for (int column = 0; column < columnSize; column++)
  54. {
  55. var itsGonnaExplode = checkpoint[line][column] == BOMB;
  56. if (itsGonnaExplode)
  57. ExplodeArea(grid, line, column, columnSize, checkpoint);
  58. }
  59. }
  60.  
  61. grid.CopyTo(checkpoint, 0);
  62. }
  63. else
  64. {
  65. PlantAll(grid, columnSize);
  66. }
  67.  
  68. numberCycles++;
  69. if (Enumerable.SequenceEqual(gridOriginal, grid))
  70. {
  71. n = n % numberCycles;
  72. secs = 2;
  73. }
  74.  
  75. explodeTime = !explodeTime;
  76. }
  77.  
  78. stopTimer.Stop();
  79. Console.WriteLine(stopTimer.ElapsedMilliseconds);
  80.  
  81. return grid;
  82. }
  83.  
  84. private static void ExplodeArea(string[] grid, int line, int column, int columnSize, string[] checkpoint)
  85. {
  86. grid[line] = grid[line].Remove(column, 1).Insert(column, CLEAN);
  87.  
  88. //Has plant for both sides.
  89. if (column > 0 && column < columnSize - 1)
  90. {
  91. if (checkpoint[line][column - 1] != BOMB)
  92. grid[line] = grid[line].Remove(column - 1, 1).Insert(column - 1, CLEAN);
  93.  
  94. if (checkpoint[line][column + 1] != BOMB)
  95. grid[line] = grid[line].Remove(column + 1, 1).Insert(column + 1, CLEAN);
  96. }
  97. else if (column > 0)
  98. {
  99. if (checkpoint[line][column - 1] != BOMB)
  100. grid[line] = grid[line].Remove(column - 1, 1).Insert(column - 1, CLEAN);
  101. }
  102. else if (column < columnSize - 1)
  103. {
  104. if (checkpoint[line][column + 1] != BOMB)
  105. grid[line] = grid[line].Remove(column + 1, 1).Insert(column + 1, CLEAN);
  106. }
  107.  
  108. //Has plant for up and down
  109. if (line > 0 && line < grid.Length - 1)
  110. {
  111. if (checkpoint[line - 1][column] != BOMB)
  112. grid[line - 1] = grid[line - 1].Remove(column, 1).Insert(column, CLEAN);
  113.  
  114. if (checkpoint[line + 1][column] != BOMB)
  115. grid[line + 1] = grid[line + 1].Remove(column, 1).Insert(column, CLEAN);
  116. }
  117. else if (line > 0)
  118. {
  119. if (checkpoint[line - 1][column] != BOMB)
  120. grid[line - 1] = grid[line - 1].Remove(column, 1).Insert(column, CLEAN);
  121. }
  122. else if (line < grid.Length - 1)
  123. {
  124. if (checkpoint[line + 1][column] != BOMB)
  125. grid[line + 1] = grid[line + 1].Remove(column, 1).Insert(column, CLEAN);
  126. }
  127. }
  128.  
  129. private static void PlantAll(string[] grid, int lenght)
  130. {
  131. for (int i = 0; i < grid.Length; i++)
  132. grid[i] = new string('O', lenght);
  133. }
  134. }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement