Advertisement
nikitaTheSlayer

Lesson: map

Apr 20th, 2020
392
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.14 KB | None | 0 0
  1. using System;
  2.  
  3. namespace CSLight_4._3
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             Console.CursorVisible = false;
  10.  
  11.             char[,] map = CreateMap();
  12.  
  13.             int positionX = 1, positionY = 1;
  14.             int positionDX = 0, positionDY = 0;
  15.             int mapHeight, mapWidth;
  16.  
  17.             string answerUser;
  18.             Console.Write("Нарисовать свою карту? (да/нет): ");
  19.             answerUser = Console.ReadLine();
  20.  
  21.             if (answerUser.ToLower() == "нет".ToLower())
  22.             {
  23.                 Console.Clear();
  24.                 ShowMap(map, positionX, positionY);
  25.                 while (true)
  26.                 {
  27.                     if (Console.KeyAvailable)
  28.                     {
  29.  
  30.                         ConsoleKeyInfo key = Console.ReadKey(true);
  31.                         ChangeDirection(key, ref positionDX, ref positionDY);
  32.                         if (map[positionX + positionDX, positionY + positionDY] != '#')
  33.                         {
  34.                             MoveCharacter(map, ref positionX, ref positionY, positionDX, positionDY, key, '@', ' ');
  35.                             positionDX = 0;
  36.                             positionDY = 0;
  37.                         }
  38.                     }
  39.                 }
  40.             }
  41.             else if (answerUser.ToLower() == "да".ToLower())
  42.             {
  43.                 Console.Write("Ведите ширину карты: ");
  44.                 mapWidth = Convert.ToInt32(Console.ReadLine());
  45.                 Console.Write("Ведите высоту карты карты: ");
  46.                 mapHeight = Convert.ToInt32(Console.ReadLine());
  47.  
  48.                 Console.Write("Введите символ полей карты: ");
  49.                 char symbol = Convert.ToChar(Console.ReadLine());
  50.  
  51.                 Console.Write("Введите символ персонажа: ");
  52.                 char persSymbol = Convert.ToChar(Console.ReadLine());
  53.  
  54.                 char[,] newMap = new char[mapHeight, mapWidth];
  55.                 Console.Clear();
  56.  
  57.                 WriteMap(newMap, symbol, persSymbol, positionX, positionY);
  58.                 while (true)
  59.                 {
  60.  
  61.                     if (Console.KeyAvailable)
  62.                     {
  63.                         ConsoleKeyInfo key = Console.ReadKey(true);
  64.                         ChangeDirection(key, ref positionDX, ref positionDY);
  65.  
  66.                         if (newMap[positionX + positionDX, positionY + positionDY] != symbol)
  67.                         {
  68.                             MoveCharacter(newMap, ref positionX, ref positionY, positionDX, positionDY, key, persSymbol, symbol);
  69.                             positionDX = 0;
  70.                             positionDY = 0;
  71.                         }
  72.                     }
  73.                 }
  74.             }
  75.         }
  76.  
  77.         static void ChangeDirection(ConsoleKeyInfo key, ref int DX, ref int DY)
  78.         {
  79.             switch (key.Key)
  80.             {
  81.                 case ConsoleKey.UpArrow:
  82.                     DX = -1; DY = 0;
  83.                     break;
  84.                 case ConsoleKey.DownArrow:
  85.                     DX = 1; DY = 0;
  86.                     break;
  87.                 case ConsoleKey.LeftArrow:
  88.                     DX = 0; DY = -1;
  89.                     break;
  90.                 case ConsoleKey.RightArrow:
  91.                     DX = 0; DY = 1;
  92.                     break;
  93.             }
  94.         }
  95.  
  96.         static void MoveCharacter(char[,] map, ref int posX, ref int posY, int posDX, int posDY, ConsoleKeyInfo key, char persSymbol, char symbol)
  97.         {
  98.             Console.SetCursorPosition(posY, posX);
  99.             Console.Write(map[posX, posY]);
  100.  
  101.             switch (key.Key)
  102.             {
  103.                 case ConsoleKey.Enter:
  104.                     map[posX, posY] = symbol;
  105.                     break;
  106.             }
  107.            
  108.             posX += posDX;
  109.             posY += posDY;
  110.  
  111.             Console.SetCursorPosition(posY, posX);
  112.             Console.Write(persSymbol);
  113.         }
  114.  
  115.         static void WriteMap(char[,] newMap, char symbol, char persSymbol, int userX, int userY)
  116.         {
  117.             Console.SetCursorPosition(0, 0);
  118.             for (int i = 0; i < newMap.GetLength(0); i++)
  119.             {
  120.                 for (int j = 0; j < newMap.GetLength(1); j++)
  121.                 {
  122.                     if (newMap[i, j] == i || newMap[i, j] == symbol)
  123.                     {
  124.                         newMap[i, j] = symbol;
  125.                         Console.Write(newMap[i, j]);
  126.                     }
  127.                     else if (i == (newMap.GetLength(0) - 1))
  128.                     {
  129.                         newMap[i, j] = symbol;
  130.                         Console.Write(newMap[i, j]);
  131.                     }
  132.                     else if (j == 0)
  133.                     {
  134.                         newMap[i, j] = symbol;
  135.                         Console.Write(newMap[i, j]);
  136.                     }
  137.                     else if (j == newMap.GetLength(1) - 1)
  138.                     {
  139.                         newMap[i, j] = symbol;
  140.                         Console.Write(newMap[i, j]);
  141.                     }
  142.                     else if (newMap[i, j] != i)
  143.                     {
  144.                         newMap[i, j] = ' ';
  145.                         Console.Write(newMap[i, j]);
  146.                     }
  147.  
  148.                 }
  149.                 Console.WriteLine();
  150.             }
  151.             Console.SetCursorPosition(userX, userY);
  152.             Console.WriteLine(persSymbol);
  153.         }
  154.  
  155.         static char[,] CreateMap()
  156.         {
  157.             Console.SetCursorPosition(0, 0);
  158.  
  159.             char[,] map =
  160.             {
  161.                 {'#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#'},
  162.                 {'#',' ',' ',' ',' ',' ',' ',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
  163.                 {'#',' ',' ','#',' ',' ',' ',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
  164.                 {'#',' ',' ','#',' ',' ',' ',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
  165.                 {'#',' ',' ','#',' ','#',' ',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
  166.                 {'#',' ',' ','#',' ','#',' ',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
  167.                 {'#',' ',' ','#',' ','#',' ',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
  168.                 {'#',' ',' ','#',' ','#',' ',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
  169.                 {'#',' ',' ','#',' ','#',' ',' ','#',' ',' ',' ',' ',' ','#','#','#','#','#','#'},
  170.                 {'#',' ',' ',' ',' ','#',' ',' ','#',' ',' ',' ','#',' ',' ',' ',' ',' ',' ','#'},
  171.                 {'#',' ',' ','#',' ','#',' ',' ','#',' ',' ',' ','#',' ',' ',' ',' ',' ',' ','#'},
  172.                 {'#',' ',' ','#',' ',' ',' ',' ','#',' ',' ',' ','#',' ',' ',' ',' ',' ',' ','#'},
  173.                 {'#',' ',' ','#',' ','#',' ',' ','#',' ',' ',' ','#',' ',' ',' ',' ',' ',' ','#'},
  174.                 {'#',' ',' ',' ',' ','#',' ',' ','#',' ',' ',' ','#',' ',' ',' ',' ',' ',' ','#'},
  175.                 {'#',' ',' ','#',' ','#',' ',' ',' ',' ',' ',' ','#',' ',' ',' ',' ',' ',' ','#'},
  176.                 {'#',' ',' ','#',' ',' ',' ',' ',' ',' ',' ',' ','#',' ',' ',' ',' ',' ',' ','#'},
  177.                 {'#',' ',' ','#',' ',' ',' ',' ',' ',' ',' ',' ','#',' ',' ',' ',' ',' ',' ','#'},
  178.                 {'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#',' ',' ',' ',' ',' ',' ','#'},
  179.                 {'#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#',' ',' ',' ',' ',' ',' ','#'},
  180.                 {'#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#'}
  181.             };
  182.             return map;
  183.         }
  184.  
  185.         static void ShowMap(char[,] map, int userX, int userY)
  186.         {
  187.             Console.SetCursorPosition(0, 0);
  188.             for (int i = 0; i < map.GetLength(0); i++)
  189.             {
  190.                 for (int j = 0; j < map.GetLength(1); j++)
  191.                 {
  192.                     Console.Write(map[i, j]);
  193.                 }
  194.                 Console.WriteLine();
  195.             }
  196.             Console.SetCursorPosition(userX, userY);
  197.             Console.Write('@');
  198.         }
  199.     }
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement