Advertisement
dmitryEfremov

Untitled

May 13th, 2020
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.41 KB | None | 0 0
  1. using System;
  2. using System.ComponentModel.Design;
  3.  
  4. namespace ConsoleApp5
  5. {
  6. class Program
  7. {
  8. static void Main(string[] args)
  9. {
  10. Console.SetWindowSize(200, 70);
  11. Console.WriteLine("Выберите размеры карты");
  12. Console.Write("Ширина");
  13. int width = Convert.ToInt32(Console.ReadLine());
  14. Console.Write("Длина");
  15. int length = Convert.ToInt32(Console.ReadLine());
  16. Console.Clear();
  17. string[,] map = new string[length, width];
  18. CreatingMapBorders(map);
  19. (int X, int Y) currentPosition = (1, 1);
  20. while (true)
  21. {
  22. Console.SetCursorPosition(0, 30);
  23. Console.WriteLine("End - построить решотку\nDelete-удалить объект\nPage Down - поставить игрока\nInsert-начать игру");
  24. DrawMap(map);
  25. Console.SetCursorPosition(currentPosition.X, currentPosition.Y);
  26. var editPosition = currentPosition;
  27. ConsoleKeyInfo key = Console.ReadKey();
  28. editPosition = DirectionRecognition(editPosition, key);
  29. if (MovementFailed(editPosition, map))
  30. continue;
  31. currentPosition = editPosition;
  32. MapEditor(currentPosition, map, key);
  33. if (CheckingTransitionToGame(key) == false)
  34. break;
  35. }
  36. var (X, Y) = FindingPlayerCoordinates(map);
  37. while (true)
  38. {
  39. DrawMap(map);
  40. var editPosition = (X, Y);
  41. ConsoleKeyInfo key = Console.ReadKey();
  42. editPosition = DirectionRecognition(editPosition, key);
  43. if (FailedMovementPlayer(editPosition, map))
  44. continue;
  45. map[Y, X] = " ";
  46. (X, Y) = editPosition;
  47. map[Y, X] = "@";
  48. }
  49. }
  50.  
  51. static void CreatingMapBorders(string[,] map)
  52. {
  53. for (int i = 0; i < map.GetLength(0); i++)
  54. {
  55. map[i, 0] = "#";
  56. map[i, map.GetLength(1) - 1] = "#";
  57. }
  58. for (int j = 0; j < map.GetLength(1); j++)
  59. {
  60. map[0, j] = "#";
  61. map[map.GetLength(0) - 1, j] = "#";
  62. }
  63. }
  64.  
  65. static (int,int) FindingPlayerCoordinates(string[,] map)
  66. {
  67. (int X, int Y) currentPosition = (default, default);
  68. for (int i = 0; i < map.GetLength(0); i++)
  69. for (int j = 0; j < map.GetLength(1); j++)
  70. if (map[i, j] == "@")
  71. currentPosition = (j, i);
  72. return currentPosition;
  73. }
  74.  
  75. static (int, int) DirectionRecognition((int X, int Y) editPosition, ConsoleKeyInfo key)
  76. {
  77. switch (key.Key)
  78. {
  79. case ConsoleKey.UpArrow:
  80. editPosition.Y--;
  81. break;
  82. case ConsoleKey.DownArrow:
  83. editPosition.Y++;
  84. break;
  85. case ConsoleKey.LeftArrow:
  86. editPosition.X--;
  87. break;
  88. case ConsoleKey.RightArrow:
  89. editPosition.X++;
  90. break;
  91. }
  92. return editPosition;
  93. }
  94.  
  95. static bool CheckingTransitionToGame(ConsoleKeyInfo key)
  96. {
  97. if(key.Key == ConsoleKey.Insert)
  98. {
  99. return false;
  100. }
  101. else
  102. {
  103. return true;
  104. }
  105. }
  106.  
  107. static void MapEditor((int X, int Y) point, string[,] map, ConsoleKeyInfo key )
  108. {
  109. switch (key.Key)
  110. {
  111. case ConsoleKey.End:
  112. map[point.Y, point.X] = "#";
  113. break;
  114. case ConsoleKey.Delete:
  115. map[point.Y, point.X] = " ";
  116. break;
  117. case ConsoleKey.PageDown:
  118. map[point.Y, point.X] = "@";
  119. break;
  120. }
  121. }
  122.  
  123. static bool MovementFailed((int X, int Y) point, string[,] map)
  124. {
  125. if (point.X <= 0 || point.X >= (map.GetLength(1) - 1))
  126. return true;
  127. if (point.Y <= 0 || point.Y >= (map.GetLength(0) - 1))
  128. return true;
  129. return false;
  130. }
  131.  
  132. static bool FailedMovementPlayer((int X, int Y) point, string[,] map)
  133. {
  134. if (point.X <= 0 || point.X >= (map.GetLength(1) - 1))
  135. return true;
  136. if (point.Y <= 0 || point.Y >= (map.GetLength(0) - 1))
  137. return true;
  138. if (map[point.Y, point.X] == "#")
  139. return true;
  140. return false;
  141. }
  142.  
  143. static void DrawMap(string [,] map)
  144. {
  145. for (int i = 0; i < map.GetLength(0); i++)
  146. for (int j = 0; j < map.GetLength(1); j++)
  147. {
  148. Console.SetCursorPosition(j, i);
  149. Console.Write(map[i, j]);
  150. }
  151. }
  152. }
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement