Guest User

Untitled

a guest
Jul 21st, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.48 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace LevelGenerator_02
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. int[,] levelArray = new int[20, 20]; //Setting size of level
  13. //Populating initial level array with numbers 0-399
  14. int index = -1;
  15. int increacing = 0;
  16. double counter = 0;
  17. for (index = 0; increacing < 20 * 20; index++)
  18. {
  19.  
  20. levelArray[((int)counter), index] = increacing; //Loop to fill the array
  21. //Console.Write(levelArray[((int)counter), index] + "|");
  22. counter = counter + .05;
  23. increacing = increacing + 1;
  24.  
  25. if (index == 19)
  26. {
  27. index = -1;
  28. }
  29.  
  30. }
  31. //Console.ReadKey(); //Finish populating level array with initial numbers.
  32.  
  33. //--------------------------------------------------------------------------------------------------------------------------------
  34. //--------------------------------------------------------------------------------------------------------------------------------
  35. //--------------------------------------------------------------------------------------------------------------------------------
  36.  
  37. Random randomVariable = new Random(); //Begin the random level generation
  38. int startingPoint = randomVariable.Next(0, 19);
  39. int endingPoint = randomVariable.Next(0, 19);
  40. int blank = 2000;
  41. int wall = 3000;
  42. int start = 4000;
  43. int end = 5000;
  44. int currentPositionUpDown = 0;
  45. int currentPositionLeftRight = startingPoint;
  46. int randomDirection = randomVariable.Next(0,3);
  47. levelArray[0,startingPoint] = start;
  48. levelArray[19, endingPoint] = end;
  49. int errorControl = 0;
  50. int randomLevelSize = randomVariable.Next(500, 700);
  51. int currentLevelSize = 0;
  52.  
  53. do{
  54. if (randomDirection == 0 && currentPositionLeftRight-2 <19 && currentPositionLeftRight-2 >0 && levelArray[(currentPositionUpDown), (currentPositionLeftRight - 2)] <= 400) //Move left 2
  55. {
  56. currentPositionLeftRight = currentPositionLeftRight - 2;
  57. levelArray[(currentPositionUpDown), (currentPositionLeftRight)] = blank;
  58. currentPositionLeftRight = currentPositionLeftRight + 1;
  59. levelArray[(currentPositionUpDown), (currentPositionLeftRight)] = blank;
  60. currentPositionLeftRight = currentPositionLeftRight - 1;
  61. errorControl = 0;
  62. }
  63. else if (randomDirection == 1 && currentPositionLeftRight+2 <19 && currentPositionLeftRight+2 >0 && levelArray[(currentPositionUpDown), (currentPositionLeftRight + 2)] <= 400) //Move right 2
  64. {
  65. currentPositionLeftRight = currentPositionLeftRight + 2;
  66. levelArray[(currentPositionUpDown), (currentPositionLeftRight)] = blank;
  67. currentPositionLeftRight = currentPositionLeftRight - 1;
  68. levelArray[(currentPositionUpDown), (currentPositionLeftRight)] = blank;
  69. currentPositionLeftRight = currentPositionLeftRight + 1;
  70. errorControl = 0;
  71. }
  72. else if (randomDirection == 2 && currentPositionUpDown+2 <19 && currentPositionUpDown+2 >0 && levelArray[(currentPositionUpDown +2), (currentPositionLeftRight)] <= 400) //Move down 2
  73. {
  74. currentPositionUpDown = currentPositionUpDown + 2;
  75. levelArray[(currentPositionUpDown), (currentPositionLeftRight)] = blank;
  76. currentPositionUpDown = currentPositionUpDown - 1;
  77. levelArray[(currentPositionUpDown), (currentPositionLeftRight)] = blank;
  78. currentPositionUpDown = currentPositionUpDown + 1;
  79. errorControl = 0;
  80. }
  81. else if (randomDirection == 2 && currentPositionUpDown-2 <19 && currentPositionUpDown-2 >0 && levelArray[(currentPositionUpDown -2), (currentPositionLeftRight)] <= 400) //Move up 2
  82. {
  83. currentPositionUpDown = currentPositionUpDown - 2;
  84. levelArray[(currentPositionUpDown), (currentPositionLeftRight)] = blank;
  85. currentPositionUpDown = currentPositionUpDown + 1;
  86. levelArray[(currentPositionUpDown), (currentPositionLeftRight)] = blank;
  87. currentPositionUpDown = currentPositionUpDown - 1;
  88. errorControl = 0;
  89. }
  90.  
  91. errorControl = errorControl + 1;
  92. if (errorControl >= 5)
  93. {
  94. if (randomDirection == 0 && currentPositionLeftRight - 2 <19 && currentPositionLeftRight - 2 >0 && levelArray[(currentPositionUpDown), (currentPositionLeftRight - 2)] == blank) //Move left 2
  95. {
  96. currentPositionLeftRight = currentPositionLeftRight - 2;
  97. levelArray[(currentPositionUpDown), (currentPositionLeftRight)] = blank;
  98. currentPositionLeftRight = currentPositionLeftRight + 1;
  99. levelArray[(currentPositionUpDown), (currentPositionLeftRight)] = blank;
  100. currentPositionLeftRight = currentPositionLeftRight - 1;
  101. errorControl = 0;
  102. }
  103. else if (randomDirection == 1 && currentPositionLeftRight + 2 <19 && currentPositionLeftRight + 2 >0 && levelArray[(currentPositionUpDown), (currentPositionLeftRight + 2)] <= blank) //Move right 2
  104. {
  105. currentPositionLeftRight = currentPositionLeftRight + 2;
  106. levelArray[(currentPositionUpDown), (currentPositionLeftRight)] = blank;
  107. currentPositionLeftRight = currentPositionLeftRight - 1;
  108. levelArray[(currentPositionUpDown), (currentPositionLeftRight)] = blank;
  109. currentPositionLeftRight = currentPositionLeftRight + 1;
  110. errorControl = 0;
  111. }
  112. else if (randomDirection == 2 && currentPositionUpDown + 2 <19 && currentPositionUpDown + 2 >0 && levelArray[(currentPositionUpDown+2), (currentPositionLeftRight)] <= blank) //Move down 2
  113. {
  114. currentPositionUpDown = currentPositionUpDown + 2;
  115. levelArray[(currentPositionUpDown), (currentPositionLeftRight)] = blank;
  116. currentPositionUpDown = currentPositionUpDown - 1;
  117. levelArray[(currentPositionUpDown), (currentPositionLeftRight)] = blank;
  118. currentPositionUpDown = currentPositionUpDown + 1;
  119. errorControl = 0;
  120. }
  121. else if (randomDirection == 2 && currentPositionUpDown - 2 <19 && currentPositionUpDown - 2 >0 && levelArray[(currentPositionUpDown-2), (currentPositionLeftRight)] <= blank) //Move up 2
  122. {
  123. currentPositionUpDown = currentPositionUpDown - 2;
  124. levelArray[(currentPositionUpDown), (currentPositionLeftRight)] = blank;
  125. currentPositionUpDown = currentPositionUpDown + 1;
  126. levelArray[(currentPositionUpDown), (currentPositionLeftRight)] = blank;
  127. currentPositionUpDown = currentPositionUpDown - 1;
  128. errorControl = 0;
  129. }
  130. }
  131. randomDirection = randomVariable.Next(0, 3);
  132. currentLevelSize = currentLevelSize + 1;
  133.  
  134.  
  135. } while (levelArray[19, endingPoint] == end && currentLevelSize != randomLevelSize);
  136.  
  137. //--------------------------------------------------------------------------------------------------------------------------------
  138. //--------------------------------------------------------------------------------------------------------------------------------
  139. //--------------------------------------------------------------------------------------------------------------------------------
  140.  
  141. increacing = 0; //Writing out the level to console.
  142. double printCounter = 0;
  143. int[,] finalArray = new int[20, 20];
  144. for (index = 0; increacing < 20 * 20; index++)
  145. {
  146. increacing = increacing + 1;
  147. if (levelArray[(int)printCounter, index] == blank)
  148. {
  149. Console.Write(" ");
  150. }
  151. else if (levelArray[(int)printCounter, index] == wall)
  152. {
  153. Console.Write("|");
  154. }
  155. else if (levelArray[(int)printCounter, index] == start)
  156. {
  157. Console.Write("S ");
  158. }
  159. else if (levelArray[(int)printCounter, index] == end)
  160. {
  161. Console.Write("E ");
  162. }
  163. else if (levelArray[(int)printCounter, index] <= 400)
  164. {
  165. Console.Write("__");
  166. }
  167. printCounter = printCounter + .05;
  168. if (index == 19)
  169. {
  170. index = -1;
  171. Console.WriteLine();
  172. }
  173. }
  174. Console.ReadKey();
  175. }
  176. }
  177. }
Add Comment
Please, Sign In to add comment