lovelyvook

Unit_31

Jul 1st, 2024
794
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.53 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Ijunior
  4. {
  5.     internal class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             int mapY = 12;
  10.             int mapX = 24;
  11.             char[,] map = new char[mapY, mapX];
  12.             char wall = '#';
  13.             char emptySpace = ' ';
  14.             char item = '.';
  15.             int totalScore;
  16.             int userScore;
  17.  
  18.             GenerateMap(map, wall, emptySpace, item, mapY, mapX, out totalScore);
  19.             PlayGame(map, wall, emptySpace, item, ref totalScore, out userScore);
  20.             FinishGame(userScore, totalScore);
  21.         }
  22.  
  23.         static void GenerateMap(char[,] map, char wall, char emptySpace, char item, int mapY, int mapX, out int totalScore)
  24.         {
  25.             int infoOffset = 2;
  26.             ConsoleKeyInfo pressedKey;
  27.  
  28.             do
  29.             {
  30.                 CreateMapBorders(map, wall, emptySpace);
  31.                 CreateVerticalWalls(map, wall, mapY, mapX);
  32.                 CreateHorizontalWalls(map, wall, mapY, mapX);
  33.                 GenerateItems(map, item, emptySpace, out totalScore);
  34.                 DrawMap(map);
  35.  
  36.                 Console.SetCursorPosition(0, map.GetLength(0) + infoOffset);
  37.                 Console.Write($"Нажмите любую клавишу для генерации новой карты. " +
  38.                     $"\nНажмите {ConsoleKey.Enter}, чтобы начать игру");
  39.                 pressedKey = Console.ReadKey();
  40.  
  41.                 Console.Clear();
  42.             } while (pressedKey.Key != ConsoleKey.Enter);
  43.         }
  44.  
  45.         static void CreateMapBorders(char[,] map, char wall, char emptySpace)
  46.         {
  47.             for (int i = 0; i < map.GetLength(0); i++)
  48.             {
  49.                 for (int j = 0; j < map.GetLength(1); j++)
  50.                 {
  51.                     if (i == 0 || i == map.GetLength(0) - 1 || j == 0 || j == map.GetLength(1) - 1)
  52.                     {
  53.                         map[i, j] = wall;
  54.                     }
  55.                     else
  56.                     {
  57.                         map[i, j] = emptySpace;
  58.                     }
  59.                 }
  60.             }
  61.         }
  62.  
  63.         static void CreateVerticalWalls(char[,] map, char wall, int borderBuilingY, int borderBuilingX)
  64.         {
  65.             int pointBuilding = 2;
  66.             borderBuilingY /= 2;
  67.             borderBuilingX -= 2;
  68.             Random random = new Random();
  69.             int minRandomNumber = 5;
  70.             int maxRandomNumber = 10;
  71.             int wallCount = 1;
  72.  
  73.             for (int k = 0; k < wallCount; k++)
  74.             {
  75.                 int indexY = random.Next(pointBuilding, borderBuilingY);
  76.                 int indexX = random.Next(pointBuilding, borderBuilingX);
  77.                 int wallSize = random.Next(minRandomNumber, maxRandomNumber);
  78.  
  79.                 for (int i = indexY; i < map.GetLength(0) && i < indexY + wallSize; i++)
  80.                 {
  81.                     for (int j = indexX; j <= indexX; j++)
  82.                     {
  83.                         map[i, j] = wall;
  84.                     }
  85.                 }
  86.             }
  87.         }
  88.  
  89.         static void CreateHorizontalWalls(char[,] map, char wall, int borderBuilingY, int borderBuilingX)
  90.         {
  91.             int pointBuilding = 2;
  92.             borderBuilingY -= 2;
  93.             borderBuilingX -= 2;
  94.             Random random = new Random();
  95.             int minRandomNumber = 5;
  96.             int maxRandomNumber = 10;
  97.             int wallCount = 2;
  98.  
  99.             for (int k = 0; k < wallCount; k++)
  100.             {
  101.                 int indexY = random.Next(pointBuilding, borderBuilingY);
  102.                 int indexX = random.Next(pointBuilding, borderBuilingX);
  103.                 int wallSize = random.Next(minRandomNumber, maxRandomNumber);
  104.  
  105.                 for (int i = indexY; i <= indexY; i++)
  106.                 {
  107.                     for (int j = indexX; j < map.GetLength(1) && j < indexX + wallSize; j++)
  108.                     {
  109.                         map[i, j] = wall;
  110.                     }
  111.                 }
  112.             }
  113.         }
  114.  
  115.         static void GenerateItems(char[,] map, char item, char emptySpace, out int totalScore)
  116.         {
  117.             Random random = new Random();
  118.             int minRandomNumber = 0;
  119.             int maxRandomNumber = 20;
  120.             int chanceItem;
  121.             totalScore = 0;
  122.  
  123.             for (int i = 0; i < map.GetLength(0); i++)
  124.             {
  125.                 for (int j = 0; j < map.GetLength(1); j++)
  126.                 {
  127.                     chanceItem = random.Next(minRandomNumber, maxRandomNumber);
  128.  
  129.                     if (map[i, j] == emptySpace && chanceItem == minRandomNumber)
  130.                     {
  131.                         map[i, j] = item;
  132.                         totalScore++;
  133.                     }
  134.                 }
  135.             }
  136.         }
  137.  
  138.         static void DrawMap(char[,] map)
  139.         {
  140.             Console.SetCursorPosition(0, 0);
  141.  
  142.             for (int i = 0; i < map.GetLength(0); i++)
  143.             {
  144.                 for (int j = 0; j < map.GetLength(1); j++)
  145.                 {
  146.                     Console.Write(map[i, j]);
  147.                 }
  148.  
  149.                 Console.WriteLine();
  150.             }
  151.         }
  152.  
  153.         static void PlayGame(char[,] map, char wall, char emptySpace, char item, ref int totalScore, out int userScore)
  154.         {
  155.             char player = '@';
  156.             int userPositionX = 1;
  157.             int userPositionY = 1;
  158.             int infoOffset = 2;
  159.             userScore = 0;
  160.             ConsoleKeyInfo pressedKey;
  161.  
  162.             do
  163.             {
  164.                 Console.SetCursorPosition(0, map.GetLength(0) + infoOffset);
  165.                 Console.WriteLine($"Score: {userScore} / {totalScore} \nНажмите {ConsoleKey.Escape} для выхода");
  166.  
  167.                 DrawMap(map);
  168.  
  169.                 Console.SetCursorPosition(userPositionX, userPositionY);
  170.                 Console.CursorVisible = false;
  171.                 Console.Write(player);
  172.  
  173.                 pressedKey = Console.ReadKey();
  174.                 CheckAvailableDirection(pressedKey, ref userPositionX, ref userPositionY, map, wall);
  175.                 Console.Clear();
  176.  
  177.                 if (map[userPositionY, userPositionX] == item)
  178.                 {
  179.                     map[userPositionY, userPositionX] = emptySpace;
  180.                     userScore++;
  181.                 }
  182.             }while (pressedKey.Key != ConsoleKey.Escape && userScore < totalScore);
  183.         }
  184.  
  185.         static void CheckAvailableDirection(ConsoleKeyInfo pressedKey, ref int userPositionX, ref int userPositionY, char[,] map, char wall)
  186.         {
  187.             if (pressedKey.Key == ConsoleKey.UpArrow && map[userPositionY - 1, userPositionX] != wall)
  188.             {
  189.                 Move(pressedKey, ref userPositionX, ref userPositionY);
  190.             }
  191.             else if (pressedKey.Key == ConsoleKey.DownArrow && map[userPositionY + 1, userPositionX] != wall)
  192.             {
  193.                 Move(pressedKey, ref userPositionX, ref userPositionY);
  194.             }
  195.             else if (pressedKey.Key == ConsoleKey.LeftArrow && map[userPositionY, userPositionX - 1] != wall)
  196.             {
  197.                 Move(pressedKey, ref userPositionX, ref userPositionY);
  198.             }
  199.             else if (pressedKey.Key == ConsoleKey.RightArrow && map[userPositionY, userPositionX + 1] != wall)
  200.             {
  201.                 Move(pressedKey, ref userPositionX, ref userPositionY);
  202.             }
  203.         }
  204.  
  205.         static void Move(ConsoleKeyInfo pressedKey, ref int userPositionX, ref int userPositionY)
  206.         {
  207.             switch (pressedKey.Key)
  208.             {
  209.                 case ConsoleKey.UpArrow:
  210.                     userPositionY -= 1;
  211.                     userPositionX += 0;
  212.                     break;
  213.                 case ConsoleKey.DownArrow:
  214.                     userPositionY += 1;
  215.                     userPositionX += 0;
  216.                     break;
  217.                 case ConsoleKey.LeftArrow:
  218.                     userPositionY += 0;
  219.                     userPositionX -= 1;
  220.                     break;
  221.                 case ConsoleKey.RightArrow:
  222.                     userPositionY += 0;
  223.                     userPositionX += 1;
  224.                     break;
  225.             }
  226.         }
  227.  
  228.         static void FinishGame(int userScore, int totalScore)
  229.         {
  230.             if(userScore == totalScore)
  231.             {
  232.                 Console.WriteLine("Вы победили!");
  233.             }
  234.             else
  235.             {
  236.                 Console.WriteLine("Вы проиграли!");
  237.             }
  238.         }
  239.     }
  240. }
Advertisement
Add Comment
Please, Sign In to add comment