Dr_Max_Experience

Task 23

Nov 25th, 2021 (edited)
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.29 KB | None | 0 0
  1. using System;
  2. using System.Threading;
  3.  
  4. namespace HomeWorks
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             Console.CursorVisible = false;
  11.  
  12.             bool isPlaying = true;
  13.             int playerPositionY;
  14.             int playerPositionX;
  15.             int playerDirectionY = 0;
  16.             int playerDirectionX = 0;
  17.             int collectDots = 0;
  18.  
  19.             char[,] map = { { '#', '#', '#', '#', '#', '#' ,'#', '#', '#', '#', '#', '#' ,'#', '#', '#', '#', '#', '#' },
  20.                             { '#', '.', '.', '.', '.', '#' ,'.', '.', '.', '.', '.', '.' ,'.', '.', '.', '.', '.', '#' },
  21.                             { '#', '.', '#', '#', '.', '.' ,'.', '.', '#', '#', '#', '#' ,'#', '#', '#', '#', '.', '#' },
  22.                             { '#', '.', '#', '.', '.', '.' ,'#', '.', '.', '.', '.', '.' ,'.', '#', '.', '.', '.', '#' },
  23.                             { '#', '.', '.', '.', '.', '.' ,'#', '.', '.', '@', '.', '#' ,'.', '#', '.', '.', '.', '#' },
  24.                             { '#', '#', '.', '.', '#', '#' ,'#', '#', '#', '.', '.', '#' ,'.', '.', '.', '.', '#', '#' },
  25.                             { '#', '.', '.', '.', '.', '.' ,'#', '.', '.', '.', '.', '#' ,'#', '#', '#', '.', '.', '#' },
  26.                             { '#', '.', '#', '.', '.', '.' ,'#', '.', '.', '.', '.', '.' ,'.', '.', '.', '.', '.', '#' },
  27.                             { '#', '.', '#', '#', '.', '.' ,'.', '.', '#', '#', '.', '.' ,'.', '#', '#', '#', '.', '#' },
  28.                             { '#', '.', '.', '.', '.', '#' ,'.', '.', '.', '.', '.', '#' ,'.', '.', '.', '.', '.', '#' },
  29.                             { '#', '#', '#', '#', '#', '#' ,'#', '#', '#', '#', '#', '#' ,'#', '#', '#', '#', '#', '#' },};
  30.  
  31.             while (isPlaying)
  32.             {
  33.                 DrawMap(map, out playerPositionY, out playerPositionX, collectDots);
  34.  
  35.                 if (Console.KeyAvailable)
  36.                 {
  37.                     ConsoleKeyInfo key = Console.ReadKey(true);
  38.  
  39.                     ChangeDirection(key, ref playerDirectionX, ref playerDirectionY);
  40.                 }
  41.  
  42.                 if (map[playerPositionX + playerDirectionX, playerPositionY + playerDirectionY] != '#')
  43.                 {
  44.                     CollectDots(map, ref collectDots, playerPositionY, playerPositionX, playerDirectionX, playerDirectionY);
  45.                     Move(map, ref playerPositionY, ref playerPositionX, playerDirectionX, playerDirectionY);
  46.                 }
  47.  
  48.                 Thread.Sleep(333);
  49.             }
  50.         }
  51.  
  52.         static void ChangeDirection(ConsoleKeyInfo key, ref int DX, ref int DY)
  53.         {
  54.             switch (key.Key)
  55.             {
  56.                 case ConsoleKey.UpArrow:
  57.                     DX = -1; DY = 0;
  58.                     break;
  59.  
  60.                 case ConsoleKey.DownArrow:
  61.                     DX = 1; DY = 0;
  62.                     break;
  63.  
  64.                 case ConsoleKey.LeftArrow:
  65.                     DX = 0; DY = -1;
  66.                     break;
  67.  
  68.                 case ConsoleKey.RightArrow:
  69.                     DX = 0; DY = 1;
  70.                     break;
  71.             }
  72.         }
  73.  
  74.         static void CollectDots(char[,] map, ref int collectDots, int Y, int X, int DX, int DY)
  75.         {
  76.             if (map[X + DX, Y + DY] == '.')
  77.             {
  78.                 collectDots += 1;
  79.             }
  80.         }
  81.  
  82.         static void Move(char[,] map, ref int Y, ref int X, int DX, int DY)
  83.         {
  84.             if(map[X + DX, Y + DY] != '#')
  85.             {
  86.                 map[X, Y] = ' ';
  87.  
  88.                 Y += DY;
  89.                 X += DX;
  90.             }
  91.  
  92.             map[X, Y] = '@';
  93.         }
  94.  
  95.         static void DrawMap(char[,] map, out int Y, out int X, int collectDots)
  96.         {
  97.             Console.Clear();
  98.  
  99.             Y = 0;
  100.             X = 0;
  101.  
  102.             for (int i = 0; i < map.GetLength(0); i++)
  103.             {
  104.                 for (int j = 0; j < map.GetLength(1); j++)
  105.                 {
  106.                     Console.Write(map[i, j]);
  107.  
  108.                     if (map[i, j] == '@')
  109.                     {
  110.                         X = i;
  111.                         Y = j;
  112.                     }
  113.                 }
  114.  
  115.                 Console.WriteLine();
  116.             }
  117.  
  118.             Console.Write($"Collect dots: {collectDots}");
  119.         }
  120.     }
  121. }
Add Comment
Please, Sign In to add comment