Advertisement
TwinFrame

Clight_31_BraveNewWorld

May 20th, 2023 (edited)
747
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 12.14 KB | None | 0 0
  1. class Program
  2. {
  3.     static void Main()
  4.     {
  5.         char playerSymbol = '@';
  6.         char wallSymbol = '#';
  7.         char berrySymbol = '%';
  8.         char emptySymbol = ' ';
  9.  
  10.         int widthMap = 0;
  11.         int heightMap = 0;
  12.  
  13.         bool isMapReady = false;
  14.         bool isExit = false;
  15.         ConsoleKey keyInput;
  16.  
  17.         int totalBerries = 0;
  18.  
  19.         char[,] map = new char[0, 0];
  20.  
  21.         while (isExit == false)
  22.         {
  23.             const ConsoleKey CommandCreateMap = ConsoleKey.F1;
  24.             const ConsoleKey CommandStartGame = ConsoleKey.F2;
  25.             const ConsoleKey CommandExit = ConsoleKey.F3;
  26.  
  27.             Console.Clear();
  28.             Console.WriteLine($"{CommandCreateMap} - Создание карты.");
  29.             Console.WriteLine($"{CommandStartGame} - Старт игры.");
  30.             Console.WriteLine($"{CommandExit} - Выход.");
  31.             Console.Write("\nВыберете пункт меню: ");
  32.             keyInput = Console.ReadKey().Key;
  33.  
  34.             switch (keyInput)
  35.             {
  36.                 case CommandCreateMap:
  37.                     CreateMap(playerSymbol, berrySymbol, wallSymbol, emptySymbol, ref map,
  38.                         ref isMapReady, ref totalBerries, ref widthMap, ref heightMap);
  39.                     break;
  40.  
  41.                 case CommandStartGame:
  42.                     TryStartGame(isMapReady, map, playerSymbol, berrySymbol, wallSymbol, emptySymbol, totalBerries);
  43.                     break;
  44.  
  45.                 case CommandExit:
  46.                     isExit = true;
  47.                     break;
  48.  
  49.                 default:
  50.                     ShowMessage("Не корректный ввод номера меню.");
  51.                     break;
  52.             }
  53.  
  54.             ShowMessage("\nДля продолжения нажмите любую клавишу.");
  55.             Console.ReadKey();
  56.         }
  57.     }
  58.  
  59.     static int GetValue(string message, int minValue, int maxValue)
  60.     {
  61.         int value = 0;
  62.  
  63.         bool isCorrectInput = false;
  64.  
  65.         while (isCorrectInput == false)
  66.         {
  67.             Console.Clear();
  68.  
  69.             Console.Write($"{message}. Значения от {minValue} до {maxValue}: ");
  70.             string userInput = Console.ReadLine();
  71.  
  72.             if (Int32.TryParse(userInput, out int tempValue))
  73.             {
  74.                 if (tempValue >= minValue && tempValue <= maxValue)
  75.                 {
  76.                     value = tempValue;
  77.  
  78.                     isCorrectInput = true;
  79.  
  80.                     ShowMessage("\nЗначение корректное.", ConsoleColor.Green);
  81.                 }
  82.                 else
  83.                 {
  84.                     ShowMessage($"\nЗначение превышает {maxValue}.");
  85.                 }
  86.             }
  87.             else
  88.             {
  89.                 ShowMessage("\nНе корректно введёно значение.");
  90.             }
  91.  
  92.             ShowMessage("\nДля продолжения нажмите любую клавишу.");
  93.             Console.ReadKey();
  94.         }
  95.  
  96.         return value;
  97.     }
  98.  
  99.     static bool TryStartGame(bool isMapReady, char[,] map, char playerSymbol, char berrySymbol, char wallSymbol,
  100.         char emptySymbol, int totalBerries)
  101.     {
  102.         if (isMapReady == false)
  103.         {
  104.             ShowMessage("\nКарта ещё не готова");
  105.  
  106.             return false;
  107.         }
  108.         else
  109.         {
  110.             StartGame(map, playerSymbol, berrySymbol, wallSymbol, emptySymbol, totalBerries);
  111.  
  112.             return true;
  113.         }
  114.     }
  115.  
  116.     static void StartGame(char[,] map, char playerSymbol, char berrySymbol, char wallSymbol, char emptySymbol, int totalBerries)
  117.     {
  118.         int playerPositionX;
  119.         int playerPositionY;
  120.         int playerDirectionX = 0;
  121.         int playerDirectionY = 0;
  122.  
  123.         int currentBerries = 0;
  124.  
  125.         bool isPlaying = true;
  126.  
  127.         int gameSpeed = 200;
  128.  
  129.         Console.Clear();
  130.         Console.CursorVisible = false;
  131.  
  132.         DrawMap(map, playerSymbol, out playerPositionX, out playerPositionY);
  133.  
  134.         char[,] currentMap = GetCopyMap(map);
  135.  
  136.         while (isPlaying)
  137.         {
  138.             int menuPositionY = currentMap.GetLength(1) + 3;
  139.             Console.SetCursorPosition(0, menuPositionY);
  140.             Console.WriteLine($"Собрано ягод: {currentBerries}/{totalBerries}");
  141.  
  142.             if (Console.KeyAvailable)
  143.             {
  144.                 ConsoleKeyInfo key = Console.ReadKey(true);
  145.  
  146.                 ChangeDirection(key.Key, ref playerDirectionX, ref playerDirectionY);
  147.             }
  148.  
  149.             if (currentMap[playerPositionX + playerDirectionX, playerPositionY + playerDirectionY] != wallSymbol)
  150.             {
  151.                 Move(playerSymbol, emptySymbol, playerDirectionX, playerDirectionY, ref playerPositionX, ref playerPositionY);
  152.  
  153.                 if (currentMap[playerPositionX, playerPositionY] == berrySymbol)
  154.                 {
  155.                     currentBerries++;
  156.  
  157.                     currentMap[playerPositionX, playerPositionY] = emptySymbol;
  158.                 }
  159.             }
  160.  
  161.             if (currentBerries >= totalBerries)
  162.             {
  163.                 isPlaying = false;
  164.             }
  165.  
  166.             System.Threading.Thread.Sleep(gameSpeed);
  167.         }
  168.  
  169.         Console.Clear();
  170.         ShowMessage("Поздравляем с победой, игра окончена!", ConsoleColor.DarkYellow);
  171.     }
  172.  
  173.     static void Move(int directionX, int directionY, ref int positionX, ref int positionY)
  174.     {
  175.         positionX += directionX;
  176.         positionY += directionY;
  177.  
  178.         Console.SetCursorPosition(positionX, positionY);
  179.     }
  180.  
  181.     static void Move(char playerSymbol, char emptySymbol, int directionX, int directionY, ref int positionX, ref int positionY)
  182.     {
  183.         DrawSymbol(positionX, positionY, emptySymbol);
  184.  
  185.         positionX += directionX;
  186.         positionY += directionY;
  187.  
  188.         DrawSymbol(positionX, positionY, playerSymbol);
  189.     }
  190.  
  191.     static void ChangeDirection(ConsoleKey key, ref int directionX, ref int directionY)
  192.     {
  193.         switch (key)
  194.         {
  195.             case ConsoleKey.UpArrow:
  196.                 directionX = 0;
  197.                 directionY = -1;
  198.                 break;
  199.  
  200.             case ConsoleKey.DownArrow:
  201.                 directionX = 0;
  202.                 directionY = 1;
  203.                 break;
  204.  
  205.             case ConsoleKey.LeftArrow:
  206.                 directionX = -1;
  207.                 directionY = 0;
  208.                 break;
  209.  
  210.             case ConsoleKey.RightArrow:
  211.                 directionX = 1;
  212.                 directionY = 0;
  213.                 break;
  214.         }
  215.     }
  216.  
  217.     static void DrawMap(char[,] map)
  218.     {
  219.         for (int i = 0; i < map.GetLength(0); i++)
  220.         {
  221.             for (int j = 0; j < map.GetLength(1); j++)
  222.             {
  223.                 DrawSymbol(i, j, map[i, j]);
  224.             }
  225.         }
  226.     }
  227.  
  228.     static void DrawMap(char[,] map, char playerSymbol, out int playerPositionX, out int playerPositionY)
  229.     {
  230.         playerPositionX = 0;
  231.         playerPositionY = 0;
  232.  
  233.         for (int i = 0; i < map.GetLength(0); i++)
  234.         {
  235.             for (int j = 0; j < map.GetLength(1); j++)
  236.             {
  237.                 if (map[i, j] == playerSymbol)
  238.                 {
  239.                     playerPositionX = i;
  240.                     playerPositionY = j;
  241.                 }
  242.  
  243.                 DrawSymbol(i, j, map[i, j]);
  244.             }
  245.         }
  246.     }
  247.  
  248.     static void DrawSymbol(int positionX, int positionY, char symbol)
  249.     {
  250.         Console.SetCursorPosition(positionX, positionY);
  251.         Console.Write(symbol);
  252.     }
  253.  
  254.     static char[,] GetCopyMap(char[,] map)
  255.     {
  256.         char[,] tempMap = new char[map.GetLength(0), map.GetLength(1)];
  257.  
  258.         for (int i = 0; i < tempMap.GetLength(0); i++)
  259.         {
  260.             for (int j = 0; j < tempMap.GetLength(1); j++)
  261.             {
  262.                 tempMap[i, j] = map[i, j];
  263.             }
  264.         }
  265.  
  266.         return tempMap;
  267.     }
  268.  
  269.     static void ShowMessage(string message, ConsoleColor color = ConsoleColor.DarkRed)
  270.     {
  271.         ConsoleColor defaultColor = ConsoleColor.White;
  272.  
  273.         Console.ForegroundColor = color;
  274.         Console.WriteLine(message);
  275.         Console.ForegroundColor = defaultColor;
  276.     }
  277.  
  278.     #region Create Map
  279.     static void CreateMap(char playerSymbol, char berrySymbol, char wallSymbol, char emptySymbol,
  280.     ref char[,] map, ref bool isMapReady, ref int totalBerries, ref int widthMap, ref int heightMap)
  281.     {
  282.         const ConsoleKey CommandInsertStartPostition = ConsoleKey.F1;
  283.         const ConsoleKey CommandInsertBerry = ConsoleKey.F2;
  284.         const ConsoleKey CommandDrawWall = ConsoleKey.F3;
  285.         const ConsoleKey CommandDrawEmpty = ConsoleKey.F4;
  286.         const ConsoleKey CommandExit = ConsoleKey.F5;
  287.  
  288.         char positiveAnswer = 'y';
  289.         char negativeAnswer = 'n';
  290.  
  291.         ConsoleKey inputKey;
  292.         int minWidth = 5;
  293.         int maxWidth = 30;
  294.  
  295.         int minHeight = 5;
  296.         int maxHeight = 15;
  297.  
  298.         int cursorPositionX = 1;
  299.         int cursorPositionY = 1;
  300.  
  301.         Console.Clear();
  302.         Console.CursorVisible = true;
  303.  
  304.         if (isMapReady)
  305.         {
  306.             Console.WriteLine($"Карта уже готова, хотите создать новую? Да - \"{positiveAnswer}\"/ Нет - \"{negativeAnswer}\"");
  307.  
  308.             if (Console.ReadLine().ToLower()[0] == positiveAnswer)
  309.             {
  310.                 totalBerries = 0;
  311.  
  312.                 CreateMapBorder(minWidth, maxWidth, minHeight, maxHeight, wallSymbol, ref map, ref widthMap, ref heightMap);
  313.             }
  314.         }
  315.         else
  316.         {
  317.             totalBerries = 0;
  318.  
  319.             CreateMapBorder(minWidth, maxWidth, minHeight, maxHeight, wallSymbol, ref map, ref widthMap, ref heightMap);
  320.         }
  321.  
  322.         isMapReady = false;
  323.  
  324.         while (isMapReady == false)
  325.         {
  326.             Console.Clear();
  327.  
  328.             DrawMap(map);
  329.  
  330.             Console.SetCursorPosition(0, heightMap + 3);
  331.             Console.WriteLine($"{CommandInsertStartPostition} - {playerSymbol} - установить точку старта.");
  332.             Console.WriteLine($"{CommandInsertBerry} - {berrySymbol} - добавить ягоду.");
  333.             Console.WriteLine($"{CommandDrawWall} - {wallSymbol} - нарисовать стену.");
  334.             Console.WriteLine($"{CommandDrawEmpty} - {emptySymbol} - удалить объект.");
  335.             Console.WriteLine($"{CommandExit} - выйти.");
  336.             Console.WriteLine($"\nИспользуйте стрелки для перемещения по карте.");
  337.             Console.SetCursorPosition(cursorPositionX, cursorPositionY);
  338.  
  339.             inputKey = Console.ReadKey().Key;
  340.  
  341.             switch (inputKey)
  342.             {
  343.                 case ConsoleKey.F1:
  344.                     AddStartPoint(cursorPositionX, cursorPositionY, playerSymbol, map);
  345.                     break;
  346.  
  347.                 case ConsoleKey.F2:
  348.                     AddBerry(cursorPositionX, cursorPositionY, berrySymbol, map, ref totalBerries);
  349.                     break;
  350.  
  351.                 case ConsoleKey.F3:
  352.                     AddWall(cursorPositionX, cursorPositionY, wallSymbol, map);
  353.                     break;
  354.  
  355.                 case ConsoleKey.F4:
  356.                     DeleteElement(cursorPositionX, cursorPositionY, berrySymbol, emptySymbol, map, ref totalBerries);
  357.                     break;
  358.  
  359.                 case ConsoleKey.F5:
  360.                     ShowMessage("Карта готова.", ConsoleColor.Green);
  361.                     isMapReady = true;
  362.                     break;
  363.  
  364.                 case ConsoleKey.UpArrow:
  365.                     MoveCursor(inputKey, widthMap, heightMap, ref cursorPositionX, ref cursorPositionY);
  366.                     break;
  367.  
  368.                 case ConsoleKey.DownArrow:
  369.                     MoveCursor(inputKey, widthMap, heightMap, ref cursorPositionX, ref cursorPositionY);
  370.                     break;
  371.  
  372.                 case ConsoleKey.LeftArrow:
  373.                     MoveCursor(inputKey, widthMap, heightMap, ref cursorPositionX, ref cursorPositionY);
  374.                     break;
  375.  
  376.                 case ConsoleKey.RightArrow:
  377.                     MoveCursor(inputKey, widthMap, heightMap, ref cursorPositionX, ref cursorPositionY);
  378.                     break;
  379.  
  380.                 default:
  381.                     ShowMessage("\nНе корректный ввод");
  382.                     break;
  383.             }
  384.         }
  385.     }
  386.  
  387.     static void CreateMapBorder(int minWidth, int maxWidth, int minHeight, int maxHeight, char wallSymbol,
  388.         ref char[,] map, ref int widthMap, ref int heightMap)
  389.     {
  390.         widthMap = GetValue("Введите ширину карты", minWidth, maxWidth);
  391.         heightMap = GetValue("Введите длину карты", minHeight, maxHeight);
  392.  
  393.         map = new char[widthMap, heightMap];
  394.  
  395.         AddHorizontalBorder(0, widthMap, wallSymbol, map);
  396.  
  397.         for (int i = 1; i < heightMap - 1; i++)
  398.         {
  399.             map[0, i] = wallSymbol;
  400.  
  401.             map[widthMap - 1, i] = wallSymbol;
  402.         }
  403.  
  404.         AddHorizontalBorder(heightMap - 1, widthMap, wallSymbol, map);
  405.     }
  406.  
  407.     static void AddHorizontalBorder(int positionY, int length, char wallSymbol, char[,] map)
  408.     {
  409.         for (int i = 0; i < length; i++)
  410.         {
  411.             map[i, positionY] = wallSymbol;
  412.         }
  413.     }
  414.  
  415.     static void AddStartPoint(int positionX, int positionY, char playerSymbol, char[,] map)
  416.     {
  417.         SetSymbol(positionX, positionY, playerSymbol, map);
  418.     }
  419.  
  420.     static void AddBerry(int positionX, int positionY, char berrySymbol, char[,] map, ref int totalBerries)
  421.     {
  422.         SetSymbol(positionX, positionY, berrySymbol, map);
  423.  
  424.         totalBerries++;
  425.     }
  426.  
  427.     static void AddWall(int positionX, int positionY, char wallSymbol, char[,] map)
  428.     {
  429.         SetSymbol(positionX, positionY, wallSymbol, map);
  430.     }
  431.  
  432.     static void DeleteElement(int positionX, int positionY, char berrySymbol, char emptySymbol, char[,] map, ref int totalBerries)
  433.     {
  434.         if (map[positionX, positionY] == berrySymbol)
  435.             totalBerries--;
  436.  
  437.         SetSymbol(positionX, positionY, emptySymbol, map);
  438.     }
  439.  
  440.     static void SetSymbol(int positionX, int positionY, char symbol, char[,] map)
  441.     {
  442.         map[positionX, positionY] = symbol;
  443.     }
  444.  
  445.     static void MoveCursor(ConsoleKey key, int widthMap, int heightMap, ref int positionX, ref int positionY)
  446.     {
  447.         int directionX = 0;
  448.         int directionY = 0;
  449.  
  450.         ChangeDirection(key, ref directionX, ref directionY);
  451.  
  452.         int prePositionX = positionX + directionX;
  453.         int prePositionY = positionY + directionY;
  454.  
  455.         if (prePositionX > 0 && prePositionX < widthMap - 1 && prePositionY > 0 && prePositionY < heightMap - 1)
  456.         {
  457.             Move(directionX, directionY, ref positionX, ref positionY);
  458.         }
  459.     }
  460.     #endregion
  461. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement