Advertisement
dmitryEfremov

Untitled

May 16th, 2020
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.10 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. CreatBorders(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. var editPosition = currentPosition;
  26. ConsoleKeyInfo key = Console.ReadKey();
  27. editPosition = GetDirection(editPosition, key);
  28. if (CheckMovement(editPosition, map))
  29. continue;
  30. currentPosition = editPosition;
  31. EditMap(currentPosition, map, key);
  32. if (CheckTransitionToGame(key) == false)
  33. break;
  34. }
  35. var (X, Y) = FindingPlayerCoordinates(map);
  36. Console.CursorVisible = false;
  37. while (true)
  38. {
  39.  
  40. DrawMap(map);
  41. var editPosition = (X, Y);
  42. ConsoleKeyInfo key = Console.ReadKey();
  43. editPosition = GetDirection(editPosition, key);
  44. if (CheckMovementPlayer(editPosition, map))
  45. continue;
  46. map[Y, X] = " ";
  47. (X, Y) = editPosition;
  48. map[Y, X] = "@";
  49. }
  50. }
  51.  
  52. static void CreatBorders(string[,] map)
  53. {
  54. for (int i = 0; i < map.GetLength(0); i++)
  55. {
  56. map[i, 0] = "#";
  57. map[i, map.GetLength(1) - 1] = "#";
  58. }
  59. for (int j = 0; j < map.GetLength(1); j++)
  60. {
  61. map[0, j] = "#";
  62. map[map.GetLength(0) - 1, j] = "#";
  63. }
  64. }
  65.  
  66. static (int, int) FindingPlayerCoordinates(string[,] map)
  67. {
  68. (int X, int Y) currentPosition = (default, default);
  69. for (int i = 0; i < map.GetLength(0); i++)
  70. for (int j = 0; j < map.GetLength(1); j++)
  71. if (map[i, j] == "@")
  72. currentPosition = (j, i);
  73. return currentPosition;
  74. }
  75.  
  76. static (int, int) GetDirection((int X, int Y) editPosition, ConsoleKeyInfo key)
  77. {
  78. switch (key.Key)
  79. {
  80. case ConsoleKey.UpArrow:
  81. editPosition.Y--;
  82. break;
  83. case ConsoleKey.DownArrow:
  84. editPosition.Y++;
  85. break;
  86. case ConsoleKey.LeftArrow:
  87. editPosition.X--;
  88. break;
  89. case ConsoleKey.RightArrow:
  90. editPosition.X++;
  91. break;
  92. }
  93. return editPosition;
  94. }
  95.  
  96. static bool CheckTransitionToGame(ConsoleKeyInfo key)
  97. {
  98. return key.Key != ConsoleKey.Insert;
  99. }
  100.  
  101. static void EditMap((int X, int Y) point, string[,] map, ConsoleKeyInfo key)
  102. {
  103. switch (key.Key)
  104. {
  105. case ConsoleKey.End:
  106. map[point.Y, point.X] = "#";
  107. break;
  108. case ConsoleKey.Delete:
  109. map[point.Y, point.X] = " ";
  110. break;
  111. case ConsoleKey.PageDown:
  112. map[point.Y, point.X] = "@";
  113. break;
  114. }
  115. }
  116.  
  117. static bool CheckMovement((int X, int Y) point, string[,] map)
  118. {
  119. if (point.X <= 0 || point.X >= (map.GetLength(1) - 1))
  120. return true;
  121. if (point.Y <= 0 || point.Y >= (map.GetLength(0) - 1))
  122. return true;
  123. return false;
  124. }
  125.  
  126. static bool CheckMovementPlayer((int X, int Y) point, string[,] map)
  127. {
  128. if (point.X <= 0 || point.X >= (map.GetLength(1) - 1))
  129. return true;
  130. if (point.Y <= 0 || point.Y >= (map.GetLength(0) - 1))
  131. return true;
  132. if (map[point.Y, point.X] == "#")
  133. return true;
  134. return false;
  135. }
  136.  
  137. static void DrawMap(string[,] map)
  138. {
  139. for (int i = 0; i < map.GetLength(0); i++)
  140. for (int j = 0; j < map.GetLength(1); j++)
  141. {
  142. Console.SetCursorPosition(j, i);
  143. Console.Write(map[i, j]);
  144. }
  145. }
  146. }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement