Advertisement
dmitryEfremov

Untitled

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