Advertisement
dmitryEfremov

Untitled

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