SaNik74

BraweNewWorld

Jun 4th, 2024 (edited)
504
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.40 KB | None | 0 0
  1. namespace braveNewWorld
  2. {
  3.     internal class Program
  4.     {
  5.         static void Main(string[] args)
  6.         {
  7.             string[,] map;
  8.             int mapWidthSize = 15;
  9.             int mapHeightSize = 10;
  10.  
  11.             CreateMap(mapWidthSize, mapHeightSize, out map, out int numberOfCoins);
  12.             Work(map, ref numberOfCoins);
  13.         }
  14.  
  15.         static void CreateMap(int xSize, int ySize, out string[,] map, out int numberOfCoins)
  16.         {
  17.             int numberOfBoarders = 2;
  18.             int heightMapWithoutBoarders = ySize - numberOfBoarders;
  19.             int widthMapWithoutBoarders = xSize - numberOfBoarders;
  20.             int numberOfEmptyFields = (widthMapWithoutBoarders) * (heightMapWithoutBoarders);
  21.             map = new string[ySize, xSize];
  22.  
  23.             CreateBoarders(map);
  24.             PrintEmptyArea(map);
  25.             PaintArea(map, out numberOfCoins);
  26.             DropOffPlayer(map, ref numberOfCoins);
  27.         }
  28.  
  29.         static void CreateBoarders(string[,] map)
  30.         {
  31.             string wallSymbol = "#";
  32.  
  33.             for (int i = 0; i <= map.GetLength(0); i += map.GetLength(0) - 1)
  34.             {
  35.                 for (int j = 0; j < map.GetLength(1); j++)
  36.                 {
  37.                     map[i, j] = wallSymbol;
  38.                 }
  39.             }
  40.  
  41.             for (int j = 0; j <= map.GetLength(1); j += map.GetLength(1) - 1)
  42.             {
  43.                 for (int i = 0; i < map.GetLength(0); i++)
  44.                 {
  45.                     map[i, j] = wallSymbol;
  46.                 }
  47.             }
  48.         }
  49.  
  50.         static void PrintEmptyArea(string[,] map)
  51.         {
  52.             string emptyField = " ";
  53.  
  54.             for (int i = 1; i < map.GetLength(0) - 1; i++)
  55.             {
  56.                 for (int j = 1; j < map.GetLength(1) - 1; j++)
  57.                 {
  58.                     map[i, j] = emptyField;
  59.                 }
  60.             }
  61.         }
  62.  
  63.         static void PaintArea(string[,] map, out int numberOfCoins)
  64.         {
  65.             string wallSymbol = "#";
  66.             string coinSymbol = "*";
  67.             numberOfCoins = 0;
  68.  
  69.             for (int i = 1; i < map.GetLength(0) - 1; i++)
  70.             {
  71.                 for (int j = 1; j < map.GetLength(1) - 1; j++)
  72.                 {
  73.                     Console.Write($"Нарисуйте преграды символом {wallSymbol}: ");
  74.                     Console.SetCursorPosition(0, 1);
  75.  
  76.                     PrintMap(map);
  77.  
  78.                     Console.SetCursorPosition(j, i + 1);
  79.  
  80.                     map[i, j] = Console.ReadLine();
  81.  
  82.                     if (map[i, j] != wallSymbol)
  83.                     {
  84.                         map[i, j] = coinSymbol;
  85.                         numberOfCoins++;
  86.                     }
  87.  
  88.                     Console.Clear();
  89.                 }
  90.             }
  91.         }
  92.  
  93.         static void PrintMap(string[,] map)
  94.         {
  95.             for (int i = 0; i < map.GetLength(0); i++)
  96.             {
  97.                 for (int j = 0; j < map.GetLength(1); j++)
  98.                 {
  99.                     Console.Write(map[i, j]);
  100.                 }
  101.  
  102.                 Console.WriteLine();
  103.             }
  104.         }
  105.  
  106.         static void DropOffPlayer(string[,] map, ref int numberOfCoins)
  107.         {
  108.             string emptyField = " ";
  109.             string coinSymbol = "*";
  110.             string playerSymbol = "@";
  111.  
  112.             for (int i = 1; i < map.GetLength(1) - 1; i++)
  113.             {
  114.                 for (int j = 1; j < map.GetLength(1) - 1; j++)
  115.                 {
  116.                     if (map[i, j] == emptyField)
  117.                     {
  118.                         map[i, j] = playerSymbol;
  119.                         return;
  120.                     }
  121.                     else if (map[i, j] == coinSymbol)
  122.                     {
  123.                         map[i, j] = playerSymbol;
  124.                         numberOfCoins--;
  125.                         return;
  126.                     }
  127.                 }
  128.             }
  129.         }
  130.  
  131.         static void Work(string[,] map, ref int numberOfCoins)
  132.         {
  133.             int playerMoveXPosition = 0;
  134.             int playerMoveYPosition = 0;
  135.             string emptyField = " ";
  136.             string playerSymbol = "@";
  137.  
  138.             Console.CursorVisible = false;
  139.  
  140.             FindPlayer(map, out int playerLocationPositionX, out int playerLocationPositionY);
  141.  
  142.             map[playerLocationPositionY, playerLocationPositionX] = emptyField;
  143.  
  144.             while (numberOfCoins > 0)
  145.             {
  146.                 Console.SetCursorPosition(0, 0);
  147.  
  148.                 PrintMap(map);
  149.  
  150.                 Console.SetCursorPosition(0, map.GetLength(0) + 2);
  151.                 Console.WriteLine(numberOfCoins);
  152.                 Console.SetCursorPosition(playerLocationPositionX, playerLocationPositionY);
  153.                 Console.Write(playerSymbol);
  154.  
  155.                 ConsoleKeyInfo charKey = Console.ReadKey();
  156.  
  157.                 MovePlayerThroughKeys(ref playerMoveXPosition, ref playerMoveYPosition, charKey);
  158.                 DrawMovePlayer(ref map, ref numberOfCoins, ref playerLocationPositionX, ref playerLocationPositionY, ref playerMoveXPosition, ref playerMoveYPosition, playerSymbol, emptyField);
  159.  
  160.                 Console.Clear();
  161.             }
  162.  
  163.             Console.WriteLine("Уровень пройден.");
  164.             Console.ReadKey();
  165.         }
  166.  
  167.         static void CollectCoins(string[,] map, ref int numberOfCoins, int playerLocationositionX, int playerLocationPositionY)
  168.         {
  169.             string coinSymbol = "*";
  170.             string emptyField = " ";
  171.  
  172.             if (map[playerLocationPositionY, playerLocationositionX] == coinSymbol)
  173.             {
  174.                 map[playerLocationPositionY, playerLocationositionX] = emptyField;
  175.                 numberOfCoins--;
  176.             }
  177.         }
  178.  
  179.         static void FindPlayer(string[,] map, out int playerLocationPositionX, out int playerLocationPositionY)
  180.         {
  181.             string playerSymbol = "@";
  182.  
  183.             playerLocationPositionX = 0;
  184.             playerLocationPositionY = 0;
  185.  
  186.             for (int i = 1; i < map.GetLength(0); i++)
  187.             {
  188.                 for (int j = 1; j < map.GetLength(1); j++)
  189.                 {
  190.                     if (map[i, j] == playerSymbol)
  191.                     {
  192.                         playerLocationPositionY = i;
  193.                         playerLocationPositionX = j;
  194.                         return;
  195.                     }
  196.                 }
  197.             }
  198.         }
  199.  
  200.         static void DrawMovePlayer(ref string[,] map, ref int numberOfCoins, ref int playerLocationPositionX, ref int playerLocationPositionY, ref int playerMoveXPosition, ref int playerMoveYPosition, string playerSymbol, string emptyField)
  201.         {
  202.             string wallSymbol = "#";
  203.  
  204.             playerLocationPositionX += playerMoveXPosition;
  205.             playerLocationPositionY += playerMoveYPosition;
  206.  
  207.             if (map[playerLocationPositionY, playerLocationPositionX] != wallSymbol)
  208.             {
  209.                 CollectCoins(map, ref numberOfCoins, playerLocationPositionX, playerLocationPositionY);
  210.  
  211.                 playerMoveXPosition = 0;
  212.                 playerMoveYPosition = 0;
  213.  
  214.                 map[playerLocationPositionY, playerLocationPositionX] = playerSymbol;
  215.             }
  216.             else
  217.             {
  218.                 playerLocationPositionX -= playerMoveXPosition;
  219.                 playerLocationPositionY -= playerMoveYPosition;
  220.  
  221.                 playerMoveXPosition = 0;
  222.                 playerMoveYPosition = 0;
  223.             }
  224.  
  225.             map[playerLocationPositionY, playerLocationPositionX] = emptyField;
  226.         }
  227.  
  228.         static void MovePlayerThroughKeys(ref int playerMoveXPosition, ref int playerMoveYPosition, ConsoleKeyInfo charKey)
  229.         {
  230.             ConsoleKey moveUp = ConsoleKey.UpArrow;
  231.             ConsoleKey moveDown = ConsoleKey.DownArrow;
  232.             ConsoleKey moveLeft = ConsoleKey.LeftArrow;
  233.             ConsoleKey moveRight = ConsoleKey.RightArrow;
  234.  
  235.             if (charKey.Key == moveUp)
  236.             {
  237.                 playerMoveYPosition = -1;
  238.             }
  239.             else if (charKey.Key == moveDown)
  240.             {
  241.                 playerMoveYPosition = 1;
  242.             }
  243.             else if (charKey.Key == moveLeft)
  244.             {
  245.                 playerMoveXPosition = -1;
  246.             }
  247.             else if (charKey.Key == moveRight)
  248.             {
  249.                 playerMoveXPosition = 1;
  250.             }
  251.         }
  252.     }
  253. }
Advertisement
Add Comment
Please, Sign In to add comment