Advertisement
Rodunskiy

Untitled

May 28th, 2023
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.16 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 symbolStar = '*';
  31.         char symbolWall = '#';
  32.         char symbolCharacter = '@';
  33.         char symbolErasure = ' ';
  34.  
  35.         bool isPlaying = true;
  36.  
  37.         ReadMap(map, out userPositionX, out userPositionY, out allStars, symbolCharacter, symbolStar, symbolErasure);
  38.         DrawMap(map);
  39.  
  40.         while (isPlaying)
  41.         {
  42.             Console.SetCursorPosition(0, 12);
  43.             Console.WriteLine($"Собрано {collectStars}/{allStars}");
  44.  
  45.             DirectionСharacter(ref userDirectionX, ref userDirectionY);
  46.             MovementСharacter(map, ref userPositionX, ref userPositionY, userDirectionX, userDirectionY, symbolWall, symbolCharacter, symbolErasure);
  47.             CollectingStars(map, userPositionX, userPositionY,ref collectStars, symbolStar, symbolErasure);
  48.  
  49.             if (collectStars == allStars)
  50.             {
  51.                 isPlaying = false;
  52.             }
  53.         }
  54.     }
  55.  
  56.     static int CollectingStars(char[,] array, int positionX, int positionY,ref int сollectStars, char symbolStar, char erasure)
  57.     {
  58.         if (array[positionY, positionX] == symbolStar)
  59.         {
  60.             сollectStars++;
  61.             array[positionY, positionX] = erasure;
  62.         }
  63.  
  64.         return сollectStars;
  65.     }
  66.  
  67.     static char[,] ReadMap(char[,] array,out int positionX,out int positionY, out int allStars, char symbolCharacter, char symbolStar, char erasure)
  68.     {
  69.         positionX = 0;
  70.         positionY = 0;
  71.         allStars = 0;
  72.  
  73.         for (int i = 0; i <  array.GetLength(0); i++)
  74.         {
  75.             for (int j = 0; j < array.GetLength(1); j++)
  76.             {
  77.                 if (array[i,j] == symbolCharacter)
  78.                 {
  79.                     positionX = j;
  80.                     positionY = i;
  81.                 }
  82.                 else if (array[i,j] == erasure)
  83.                 {
  84.                     array[i, j] = symbolStar;
  85.                     allStars++;
  86.                 }
  87.             }
  88.         }
  89.  
  90.         return array;
  91.     }
  92.  
  93.     static void DrawMap(char[,] array)
  94.     {
  95.         Console.SetCursorPosition(0, 0);
  96.  
  97.         for (int i = 0; i < array.GetLength(0); i++)
  98.         {
  99.             for (int j = 0; j < array.GetLength(1); j++)
  100.             {
  101.                 Console.Write(array[i, j]);
  102.             }
  103.  
  104.             Console.WriteLine();
  105.         }
  106.     }
  107.  
  108.     static void DirectionСharacter(ref int directionX, ref int directionY)
  109.     {
  110.         const ConsoleKey UpButtonCommand = ConsoleKey.UpArrow;
  111.         const ConsoleKey DownButtonCommand = ConsoleKey.DownArrow;
  112.         const ConsoleKey LeftButtonCommand = ConsoleKey.LeftArrow;
  113.         const ConsoleKey RightButtonCommand = ConsoleKey.RightArrow;
  114.  
  115.         ConsoleKeyInfo charKey = Console.ReadKey();
  116.  
  117.         switch (charKey.Key)
  118.         {
  119.             case UpButtonCommand:
  120.                 directionX = 0;
  121.                 directionY = -1;
  122.                 break;
  123.  
  124.             case DownButtonCommand:
  125.                 directionX = 0;
  126.                 directionY = 1;
  127.                 break;
  128.  
  129.             case LeftButtonCommand:
  130.                 directionX = -1;
  131.                 directionY = 0;
  132.                 break;
  133.  
  134.             case RightButtonCommand:
  135.                 directionX = 1;
  136.                 directionY = 0;
  137.                 break;
  138.         }
  139.     }
  140.  
  141.     static void MovementСharacter(char[,] array, ref int positionX, ref int positionY, int directionX, int directionY, char symbolWall, char symbolCharacter, char erasure)
  142.     {
  143.         if (array[positionY + directionY, positionX + directionX] != symbolWall)
  144.         {
  145.             RenderingСharacter(positionX, positionY, erasure);
  146.             positionX += directionX;
  147.             positionY += directionY;
  148.             RenderingСharacter(positionX, positionY, symbolCharacter);
  149.         }
  150.     }
  151.  
  152.     static void RenderingСharacter(int positionX, int positionY, char symbol)
  153.     {
  154.         Console.SetCursorPosition(positionX, positionY);
  155.         Console.WriteLine(symbol);
  156.     }
  157. }
  158.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement