Advertisement
VodVas

Brave new world

Sep 7th, 2023 (edited)
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.55 KB | Software | 0 0
  1. namespace Brave_new_world
  2. {
  3.     internal class Program
  4.     {
  5.         static void Main(string[] args)
  6.         {
  7.             Console.CursorVisible = false;
  8.  
  9.             char[,] map =
  10.             {
  11.                 {'#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#'},
  12.                 {'#',' ',' ',' ','#',' ','X',' ',' ',' ',' ',' ','#',' ',' ','X','#'},
  13.                 {'#',' ',' ',' ','#',' ',' ',' ',' ',' ',' ',' ','#',' ',' ',' ','#'},
  14.                 {'#',' ',' ',' ','#',' ',' ',' ',' ',' ',' ',' ','#',' ',' ',' ','#'},
  15.                 {'#',' ','X',' ','#',' ',' ',' ',' ',' ',' ',' ','#',' ',' ',' ','#'},
  16.                 {'#',' ',' ',' ','#',' ',' ','#','#','#','#',' ','#',' ',' ',' ','#'},
  17.                 {'#',' ',' ',' ','#',' ',' ','#',' ',' ',' ',' ','#',' ',' ',' ','#'},
  18.                 {'#',' ',' ',' ','#',' ','X','#',' ',' ',' ',' ','#',' ',' ',' ','#'},
  19.                 {'#',' ',' ',' ',' ',' ',' ','#',' ','X',' ',' ','#',' ',' ',' ','#'},
  20.                 {'#',' ',' ',' ',' ',' ',' ','#',' ',' ',' ',' ','#',' ',' ',' ','#'},
  21.                 {'#',' ','X',' ',' ',' ',' ','#',' ',' ',' ',' ','#',' ',' ',' ','#'},
  22.                 {'#',' ',' ',' ',' ',' ',' ','#',' ',' ',' ',' ',' ',' ','X',' ','#'},
  23.                 {'#',' ',' ',' ',' ',' ',' ','#',' ',' ',' ',' ',' ',' ',' ',' ','#'},
  24.                 {'#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#'}
  25.             };
  26.  
  27.             int userPositionX = 6;
  28.             int userPositionY = 6;
  29.             int directionX = 0;
  30.             int directionY = 0;
  31.  
  32.             char[] bag = new char[1];
  33.  
  34.             char user = '@';
  35.             char treasure = 'X';
  36.             char emptyTreasure = 'O';
  37.             char wall = '#';
  38.  
  39.             bool isRun = true;
  40.  
  41.             while (isRun)
  42.             {
  43.                 CreateBag(bag);
  44.  
  45.                 DrawMap(map);
  46.  
  47.                 Console.SetCursorPosition(userPositionY, userPositionX);
  48.                 Console.WriteLine(user);
  49.  
  50.                 GetDirection(ref directionX, ref directionY);
  51.  
  52.                 if (map[userPositionX + directionX, userPositionY + directionY] != wall)
  53.                 {
  54.                     Movement(ref userPositionY, ref userPositionX, directionY, directionX);
  55.                 }
  56.  
  57.                 ExpansionBag(ref map, ref bag, ref userPositionX, ref userPositionY, treasure, emptyTreasure);
  58.                 Console.Clear();
  59.             }
  60.         }
  61.  
  62.         static void DrawMap(char[,] map)
  63.         {
  64.             Console.SetCursorPosition(0, 0);
  65.  
  66.             for (int i = 0; i < map.GetLength(0); i++)
  67.             {
  68.                 for (int j = 0; j < map.GetLength(1); j++)
  69.                 {
  70.                     Console.Write(map[i, j]);
  71.                 }
  72.  
  73.                 Console.WriteLine();
  74.             }
  75.         }
  76.  
  77.         static void CreateBag(char[] bag)
  78.         {
  79.             Console.SetCursorPosition(0, 15);
  80.             Console.Write("Сумка: ");
  81.  
  82.             for (int i = 0; i < bag.Length; i++)
  83.             {
  84.                 Console.Write(bag[i] + " ");
  85.             }
  86.         }
  87.  
  88.         static void ExpansionBag(ref char[,] map, ref char[] bag, ref int userX, ref int userY, char treasure, char emptyTreasure)
  89.         {
  90.             if (map[userX, userY] == treasure)
  91.             {
  92.                 map[userX, userY] = emptyTreasure;
  93.  
  94.                 char[] tempBag = new char[bag.Length + 1];
  95.  
  96.                 for (int i = 0; i < bag.Length; i++)
  97.                 {
  98.                     tempBag[i] = bag[i];
  99.                 }
  100.  
  101.                 tempBag[tempBag.Length - 1] = treasure;
  102.                 bag = tempBag;
  103.             }
  104.         }
  105.  
  106.         static void Movement(ref int positionX, ref int positionY, int directionX, int directionY)
  107.         {
  108.             positionX += directionX;
  109.             positionY += directionY;
  110.         }
  111.  
  112.         static void GetDirection(ref int directionX, ref int directionY)
  113.         {
  114.             directionX = 0;
  115.             directionY = 0;
  116.  
  117.             ConsoleKeyInfo charKey = Console.ReadKey();
  118.  
  119.             switch (charKey.Key)
  120.             {
  121.                 case ConsoleKey.UpArrow:
  122.                     directionX--;
  123.                     break;
  124.  
  125.                 case ConsoleKey.DownArrow:
  126.                     directionX++;
  127.                     break;
  128.  
  129.                 case ConsoleKey.LeftArrow:
  130.                     directionY--;
  131.                     break;
  132.  
  133.                 case ConsoleKey.RightArrow:
  134.                     directionY++;
  135.                     break;
  136.             }
  137.         }
  138.     }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement