Advertisement
Rodunskiy

Untitled

May 27th, 2023
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.68 KB | None | 0 0
  1. class program
  2. {
  3.     static void Main(string[] args)
  4.     {
  5.         Console.CursorVisible = false;
  6.         char[,] map =
  7.         {
  8.             {'#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#'},
  9.             {'#',' ',' ',' ',' ',' ',' ',' ',' ','#','@',' ',' ',' ',' ','#'},
  10.             {'#',' ',' ',' ','#','#','#','#',' ','#',' ','#','#','#',' ','#'},
  11.             {'#',' ',' ',' ',' ',' ',' ','#',' ',' ',' ','#',' ',' ',' ','#'},
  12.             {'#',' ',' ',' ',' ','#',' ','#','#',' ','#','#','#',' ','#','#'},
  13.             {'#',' ','#','#',' ','#',' ',' ',' ',' ','#','#',' ',' ','#','#'},
  14.             {'#',' ',' ',' ',' ','#','#','#','#',' ','#','#',' ','#','#','#'},
  15.             {'#',' ','#','#',' ',' ',' ',' ','#',' ',' ',' ',' ','#','#','#'},
  16.             {'#',' ','#','#',' ','#','#',' ','#','#','#',' ','#','#','#','#'},
  17.             {'#',' ','#','#',' ','#','#',' ','#','#','#',' ',' ',' ',' ','#'},
  18.             {'#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#'},
  19.         };
  20.  
  21.         int userPositionX;
  22.         int userPositionY;
  23.  
  24.         int userDirectionX = 0;
  25.         int userDirectionY = 0;
  26.  
  27.         int allStars;
  28.         int collectStars = 0;
  29.  
  30.         char star = '*';
  31.         char wall = '#';
  32.         char character = '@';
  33.  
  34.         bool isPlaying = true;
  35.  
  36.         ReadMap(map, out userPositionX, out userPositionY, out allStars, character, star);
  37.         DrawMap(map, userPositionX, userPositionY);
  38.  
  39.         while (isPlaying)
  40.         {
  41.             Console.SetCursorPosition(0, 12);
  42.             Console.WriteLine($"Собрано {collectStars}/{allStars}");
  43.  
  44.             DirectionСharacter(ref userDirectionX, ref userDirectionY);
  45.             MovementСharacter(map, ref userPositionX, ref userPositionY, userDirectionX, userDirectionY, wall, character);
  46.             CollectingStars(map, userPositionX, userPositionY,ref collectStars, star);
  47.  
  48.             if (collectStars == allStars)
  49.             {
  50.                 isPlaying = false;
  51.             }
  52.         }
  53.     }
  54.  
  55.     static int CollectingStars(char[,] array, int positionX, int positionY,ref int сollectStars, char symbolStar)
  56.     {
  57.         if (array[positionY, positionX] == symbolStar)
  58.         {
  59.             сollectStars++;
  60.             array[positionY, positionX] = ' ';
  61.         }
  62.  
  63.         return сollectStars;
  64.     }
  65.  
  66.     static char[,] ReadMap(char[,] array,out int positionX,out int positionY, out int allStars, char symbolCharacter, char symbolStar)
  67.     {
  68.         positionX = 0;
  69.         positionY = 0;
  70.         allStars = 0;
  71.  
  72.         for (int i = 0; i <  array.GetLength(0); i++)
  73.         {
  74.             for (int j = 0; j < array.GetLength(1); j++)
  75.             {
  76.                 if (array[i,j] == symbolCharacter)
  77.                 {
  78.                     positionX = j;
  79.                     positionY = i;
  80.                 }
  81.                 else if (array[i,j] == ' ')
  82.                 {
  83.                     array[i, j] = symbolStar;
  84.                     allStars++;
  85.                 }
  86.             }
  87.         }
  88.  
  89.         return array;
  90.     }
  91.  
  92.     static void DrawMap(char[,] array, int positionX, int positionY)
  93.     {
  94.         Console.SetCursorPosition(0, 0);
  95.  
  96.         for (int i = 0; i < array.GetLength(0); i++)
  97.         {
  98.             for (int j = 0; j < array.GetLength(1); j++)
  99.             {
  100.                 Console.Write(array[i, j]);
  101.             }
  102.  
  103.             Console.WriteLine();
  104.         }
  105.     }
  106.  
  107.     static void DirectionСharacter(ref int directionX, ref int directionY)
  108.     {
  109.         ConsoleKeyInfo charKey = Console.ReadKey();
  110.  
  111.         switch (charKey.Key)
  112.         {
  113.             case ConsoleKey.UpArrow:
  114.                 directionX = 0;
  115.                 directionY = -1;
  116.                 break;
  117.  
  118.             case ConsoleKey.DownArrow:
  119.                 directionX = 0;
  120.                 directionY = 1;
  121.                 break;
  122.  
  123.             case ConsoleKey.LeftArrow:
  124.                 directionX = -1;
  125.                 directionY = 0;
  126.                 break;
  127.  
  128.             case ConsoleKey.RightArrow:
  129.                 directionX = 1;
  130.                 directionY = 0;
  131.                 break;
  132.         }
  133.     }
  134.  
  135.     static void MovementСharacter(char[,] array, ref int positionX, ref int positionY, int directionX, int directionY, char symbolWall, char symbolCharacter)
  136.     {
  137.         if (array[positionY + directionY, positionX + directionX] != symbolWall)
  138.         {
  139.             Console.SetCursorPosition(positionX, positionY);
  140.             Console.WriteLine(' ');
  141.             positionX += directionX;
  142.             positionY += directionY;
  143.             Console.SetCursorPosition(positionX, positionY);
  144.             Console.WriteLine(symbolCharacter);
  145.         }
  146.     }
  147. }
  148.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement