Advertisement
dmitryEfremov

Untitled

Apr 30th, 2020
381
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.78 KB | None | 0 0
  1. using System;
  2. namespace ConsoleApp5
  3. {
  4.     class Program
  5.     {
  6.         static void Main(string[] args)
  7.         {
  8.             Console.WriteLine("Выберите размеры карты");
  9.             Console.Write("Ширина");
  10.             int width = Convert.ToInt32(Console.ReadLine());
  11.             Console.Write("Длина");
  12.             int length = Convert.ToInt32(Console.ReadLine());
  13.             Console.Clear();
  14.             string[,] map = new string[length, width];
  15.             Borders(map, width, length);
  16.             DrawingMap(map);
  17.         }
  18.  
  19.         static void Borders(string[,] map, int width, int length)
  20.         {
  21.             for (int i = 0; i < length; i++)
  22.             {
  23.                 for (int j = 0; j < width; j++)
  24.                 {
  25.                     map[i, j]= " ";
  26.                 }
  27.             }
  28.             for (int i = 0; i < length; i++)
  29.             {
  30.                 map[i, 0] = "#";
  31.                 map[i, width -1 ] = "#";
  32.             }
  33.             for (int j = 0; j < width; j++)
  34.             {
  35.                 map[0, j] = "#";
  36.                 map[length-1, j] = "#";
  37.             }
  38.  
  39.             for (int i = 0; i < length; i++)
  40.             {
  41.                 for (int j = 0; j < width; j++)
  42.                 {                  
  43.                     Console.Write(map[i, j]);
  44.                 }
  45.                 Console.WriteLine();
  46.             }
  47.         }
  48.         static void DrawingMap(string[,] map)
  49.         {
  50.             int cursorX = 1 , cursorY = 1;
  51.             Console.SetCursorPosition(cursorY, cursorX);
  52.             while (true)
  53.             {
  54.                 ConsoleKeyInfo key = Console.ReadKey();
  55.                 switch (key.Key)
  56.                 {
  57.                     case ConsoleKey.UpArrow:
  58.                         if (map[cursorX, cursorY-1] != "#")
  59.                         {
  60.                             Console.SetCursorPosition(cursorX, --cursorY);
  61.                         }                      
  62.                         break;
  63.                     case ConsoleKey.DownArrow:
  64.                         if (map[cursorX, cursorY+1] != "#")
  65.                         {
  66.                             Console.SetCursorPosition(cursorX, ++cursorY);
  67.                         }                        
  68.                         break;
  69.                     case ConsoleKey.LeftArrow:
  70.                         Console.SetCursorPosition(--cursorX, cursorY);
  71.                         break;
  72.                     case ConsoleKey.RightArrow:
  73.                         Console.SetCursorPosition(++cursorX, cursorY);
  74.                         break;
  75.                     default:
  76.                         Console.WriteLine("Вы нажали не ту клавишу!");
  77.                         break;
  78.                 }
  79.             }
  80.         }
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement