Advertisement
dmitryEfremov

Untitled

May 12th, 2020
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.48 KB | None | 0 0
  1. using System;
  2.  
  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.             BordersCreatingMapBorders(ref map);
  19.             EditMap(ref map);
  20.             GameOnMap(map);
  21.         }
  22.  
  23.         static void BordersCreatingMapBorders(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.         static void GameOnMap(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 (MovementPlayer(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.         static bool MovementPlayer(ref (int X, int Y) editPosition, string[,] map, ConsoleKeyInfo key)
  69.         {
  70.             switch (key.Key)
  71.             {
  72.                 case ConsoleKey.UpArrow:
  73.                     editPosition.Y--;
  74.                     break;
  75.                 case ConsoleKey.DownArrow:
  76.                     editPosition.Y++;
  77.                     break;
  78.                 case ConsoleKey.LeftArrow:
  79.                     editPosition.X--;
  80.                     break;
  81.                 case ConsoleKey.RightArrow:
  82.                     editPosition.X++;
  83.                     break;
  84.             }
  85.             if (FailedMovementPlayer(editPosition, map))
  86.             {
  87.                 return true;
  88.             }
  89.             else
  90.             {
  91.                 return false;
  92.             }
  93.         }
  94.         static void EditMap(ref string[,] map)
  95.         {
  96.             (int X, int Y) currentPosition = (1, 1);
  97.             while (true)
  98.             {
  99.                 Console.SetCursorPosition(0,30);
  100.                 Console.WriteLine("End - построить решотку\nDelete-удалить объект\nPage Down - поставить игрока\nInsert-начать игру");
  101.                 bool exit = true;
  102.                 for (int i = 0; i < map.GetLength(0); i++)
  103.                     for (int j = 0; j < map.GetLength(1); j++)
  104.                     {
  105.                         Console.SetCursorPosition(j, i);
  106.                         Console.Write(map[i, j]);
  107.                     }
  108.  
  109.                 Console.SetCursorPosition(currentPosition.X, currentPosition.Y);
  110.                 var editPosition = currentPosition;
  111.                 ConsoleKeyInfo key = Console.ReadKey();
  112.  
  113.                 if (movementWhenEditing(ref editPosition, map, key, ref exit))
  114.                 {
  115.                     continue;
  116.                 }
  117.                 currentPosition = editPosition;            
  118.                 Editor(currentPosition, ref map, key);
  119.                 if (exit == false)
  120.                     break;
  121.             }
  122.         }
  123.         static bool movementWhenEditing(ref (int X, int Y) editPosition, string[,] map, ConsoleKeyInfo key, ref bool exit)
  124.         {
  125.             switch (key.Key)
  126.             {
  127.                 case ConsoleKey.UpArrow:
  128.                     editPosition.Y--;      
  129.                     break;
  130.                 case ConsoleKey.DownArrow:
  131.                     editPosition.Y++;
  132.                     break;
  133.                 case ConsoleKey.LeftArrow:
  134.                     editPosition.X--;
  135.                     break;
  136.                 case ConsoleKey.RightArrow:
  137.                     editPosition.X++;
  138.                     break;
  139.                 case ConsoleKey.Insert:
  140.                     exit = false;
  141.                     break;
  142.             }
  143.             if (MovementFailed(editPosition, map))
  144.             {
  145.                 return true;
  146.             }
  147.             else
  148.             {
  149.                 return false;
  150.             }
  151.         }
  152.         static void Editor((int X, int Y) point, ref string[,] map, ConsoleKeyInfo key )
  153.         {
  154.             switch (key.Key)
  155.             {
  156.                 case ConsoleKey.End:
  157.                     map[point.Y, point.X] = "#";
  158.                     break;
  159.                 case ConsoleKey.Delete:
  160.                     map[point.Y, point.X] = " ";
  161.                     break;
  162.                 case ConsoleKey.PageDown:
  163.                     map[point.Y, point.X] = "@";
  164.                     break;
  165.             }
  166.         }
  167.         static bool MovementFailed((int X, int Y) point, string[,] map)
  168.         {
  169.             if (point.X <= 0 || point.X >= (map.GetLength(1) - 1))
  170.                 return true;
  171.             if (point.Y <= 0 || point.Y >= (map.GetLength(0) - 1))
  172.                 return true;
  173.             return false;
  174.         }
  175.         static bool FailedMovementPlayer((int X, int Y) point, string[,] map)
  176.         {
  177.             if (point.X <= 0 || point.X >= (map.GetLength(1) - 1))
  178.                 return true;
  179.             if (point.Y <= 0 || point.Y >= (map.GetLength(0) - 1))
  180.                 return true;
  181.             if (map[point.Y, point.X] == "#")
  182.                 return true;
  183.             return false;
  184.         }
  185.     }
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement