Advertisement
SaNik74

Brave new world

May 13th, 2024 (edited)
814
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.87 KB | None | 0 0
  1. namespace brave_new_world
  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.             map = PaintMap(mapWidthSize, mapHeightSize, out int numberOfCoins);
  11.             MoveAroundMap(map, numberOfCoins);
  12.         }
  13.  
  14.         static string[,] PaintMap(int xSize, int ySize, out int numberOfCoins)
  15.         {
  16.             numberOfCoins = 0;
  17.             double thirtyPercent = 0.3;
  18.             string playerSymbol = "X";
  19.             string wallSymbol = "#";
  20.             string coinSymbol = "*";
  21.             string emptyField = " ";
  22.             string[,] map = new string[ySize, xSize];
  23.             int numberOfBoarders = 2;
  24.             int heightMapWithoutBoarders = ySize - numberOfBoarders;
  25.             int widthMapWithoutBoarders = xSize - numberOfBoarders;
  26.             int numberOfEmptyFields = (widthMapWithoutBoarders) * (heightMapWithoutBoarders);
  27.             int maxNumberOfWalls = Convert.ToInt32(numberOfEmptyFields * thirtyPercent);
  28.  
  29.             for (int i = 0; i < ySize; i++)
  30.             {
  31.                 for (int j = 0; j < xSize; j++)
  32.                 {
  33.                     if (i == 0 || i == ySize - 1 || j == 0 || j == xSize - 1)
  34.                     {
  35.                         map[i, j] = wallSymbol;
  36.                     }
  37.                 }
  38.             }
  39.  
  40.             for (int i = 1; i < ySize - 1; i++)
  41.             {
  42.                 for (int j = 1; j < xSize - 1; j++)
  43.                 {
  44.                     map[i, j] = emptyField;
  45.                 }
  46.             }
  47.  
  48.             for (int i = 1; i < ySize - 1; i++)
  49.             {
  50.                 for (int j = 1; j < xSize - 1; j++)
  51.                 {
  52.                     Console.Write($"Нарисуйте преграды символом {wallSymbol}, монеты символом {coinSymbol} и пустые поля пробелом: ");
  53.                     Console.SetCursorPosition(0, 1);
  54.                     PrintMap(map);
  55.                     Console.SetCursorPosition(0, ySize + 2);
  56.                     Console.WriteLine($"Осталось преград: {maxNumberOfWalls}");
  57.                     Console.SetCursorPosition(j, i + 1);
  58.                     map[i, j] = Console.ReadLine();
  59.  
  60.                     if (map[i, j] == emptyField)
  61.                     {
  62.                         map[i, j] = " ";
  63.                     }
  64.                     else if (map[i, j] == wallSymbol)
  65.                     {
  66.                         maxNumberOfWalls--;
  67.                     }
  68.                     else if (map[i, j] == coinSymbol)
  69.                     {
  70.                         numberOfCoins++;
  71.                     }
  72.                     else if (maxNumberOfWalls == 0)
  73.                     {
  74.                         map[i, j] = coinSymbol;
  75.                     }
  76.                     else
  77.                     {
  78.                         map[i, j] = emptyField;
  79.                     }
  80.  
  81.                     Console.Clear();
  82.                 }
  83.             }
  84.  
  85.             for (int i = 1; i < ySize - 1; i++)
  86.             {
  87.                 for (int j = 1; j < xSize - 1; j++)
  88.                 {
  89.                     if (map[i, j] == emptyField)
  90.                     {
  91.                         map[i, j] = playerSymbol;
  92.                         break;
  93.                     }
  94.                 }
  95.  
  96.                 break;
  97.             }
  98.  
  99.             return map;
  100.         }
  101.  
  102.         static void PrintMap(string[,] map)
  103.         {
  104.             for (int i = 0; i < map.GetLength(0); i++)
  105.             {
  106.                 for (int j = 0; j < map.GetLength(1); j++)
  107.                 {
  108.                     Console.Write(map[i, j]);
  109.                 }
  110.                 Console.WriteLine();
  111.             }
  112.         }
  113.  
  114.         static void MoveAroundMap(string[,] map, int numberOfCoins)
  115.         {
  116.             string emptyField = " ";
  117.             string playerSymbol = "X";
  118.  
  119.             Console.CursorVisible = false;
  120.  
  121.             PlayerLocation(map, out int playerLocationX, out int playerLocationY);
  122.  
  123.             map[playerLocationY, playerLocationX] = emptyField;
  124.  
  125.             while (numberOfCoins > 0)
  126.             {
  127.                 Console.SetCursorPosition(0, 0);
  128.                 PrintMap(map);
  129.                 Console.SetCursorPosition(0, map.GetLength(0) + 2);
  130.                 Console.WriteLine(numberOfCoins);
  131.                 Console.SetCursorPosition(playerLocationX, playerLocationY);
  132.                 Console.Write(playerSymbol);
  133.                 ConsoleKeyInfo charKey = Console.ReadKey();
  134.                 ProcessInput(map, ref playerLocationX, ref playerLocationY, charKey);
  135.                 CollectingCoins(map, ref numberOfCoins, playerLocationX, playerLocationY);
  136.                 Console.Clear();
  137.             }
  138.  
  139.             Console.WriteLine("Уровень пройден.");
  140.             Console.ReadKey();
  141.         }
  142.  
  143.         static void CollectingCoins(string[,] map, ref int numberOfCoins, int playerLocationX, int playerLocationY)
  144.         {
  145.             string coinSymbol = "*";
  146.             string emptyField = " ";
  147.  
  148.             if (map[playerLocationY, playerLocationX] == coinSymbol)
  149.             {
  150.                 map[playerLocationY, playerLocationX] = emptyField;
  151.                 numberOfCoins--;
  152.             }
  153.         }
  154.  
  155.         static void PlayerLocation(string[,] map, out int playerLocationX, out int playerLocationY)
  156.         {
  157.             string playerSymbol = "X";
  158.             playerLocationX = 0;
  159.             playerLocationY = 0;
  160.  
  161.             for (int i = 1; i < map.GetLength(0) - 1; i++)
  162.             {
  163.                 for (int j = 1; j < map.GetLength(1) - 1; j++)
  164.                 {
  165.                     if (map[i, j] == playerSymbol)
  166.                     {
  167.                         playerLocationY = i;
  168.                         playerLocationX = j;
  169.                         break;
  170.                     }
  171.                 }
  172.  
  173.                 break;
  174.             }
  175.         }
  176.  
  177.         static void ProcessInput(string[,] map, ref int playerLocationX, ref int playerLocationY, ConsoleKeyInfo charKey)
  178.         {
  179.             string wallSymbol = "#";
  180.  
  181.             if (charKey.Key == ConsoleKey.UpArrow && map[playerLocationY - 1, playerLocationX] != wallSymbol)
  182.             {
  183.                 playerLocationY--;
  184.             }
  185.             else if (charKey.Key == ConsoleKey.DownArrow && map[playerLocationY + 1, playerLocationX] != wallSymbol)
  186.             {
  187.                 playerLocationY++;
  188.             }
  189.             else if (charKey.Key == ConsoleKey.LeftArrow && map[playerLocationY, playerLocationX - 1] != wallSymbol)
  190.             {
  191.                 playerLocationX--;
  192.             }
  193.             else if (charKey.Key == ConsoleKey.RightArrow && map[playerLocationY, playerLocationX + 1] != wallSymbol)
  194.             {
  195.                 playerLocationX++;
  196.             }
  197.         }
  198.     }
  199. }
  200.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement