Advertisement
MeGaLiGhT14

4.4 Brave new world

Oct 19th, 2019
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.74 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Lesson4_4
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             int numberCoins = 10;
  10.             int collectedCoins = 0;
  11.  
  12.             var map = GenerationMap(20 , 30 , numberCoins);
  13.  
  14.             int heroX = 2;
  15.             int heroY = 2;
  16.  
  17.             Console.CursorVisible = false;
  18.  
  19.             RenderMap(map);
  20.  
  21.             do
  22.             {
  23.                 Console.SetCursorPosition(heroY , heroX);
  24.                 Console.Write('@');
  25.  
  26.                 MoveHero(map , ref heroX , ref heroY);
  27.  
  28.                 if (map[heroX , heroY] == '$')
  29.                 {
  30.                     collectedCoins++;
  31.                     map[heroX , heroY] = ' ';
  32.                 }
  33.             }
  34.             while (numberCoins != collectedCoins);
  35.         }
  36.  
  37.         private static char[,] GenerationMap(int height , int width , int numberCoins)
  38.         {
  39.             var map = new char[height , width];
  40.  
  41.             var random = new Random();
  42.  
  43.             for (int i = 0; i < map.GetLength(0); i++)
  44.             {
  45.                 for (int j = 0; j < map.GetLength(1); j++)
  46.                 {
  47.                     if (i == 0 || j == 0 || i == map.GetLength(0) - 1 || j == map.GetLength(1) - 1)
  48.                         map[i , j] = '#';
  49.                     else
  50.                         map[i , j] = ' ';
  51.                 }
  52.             }
  53.  
  54.             int coinX;
  55.             int coinY;
  56.  
  57.             bool restartSearch;
  58.  
  59.             for (int i = 0; i < numberCoins; i++)
  60.             {
  61.                 do
  62.                 {
  63.                     restartSearch = false;
  64.  
  65.                     coinX = random.Next(1 , map.GetLength(0) - 1);
  66.                     coinY = random.Next(1 , map.GetLength(1) - 1);
  67.  
  68.                     if (map[coinX , coinY] == ' ')
  69.                         map[coinX , coinY] = '$';
  70.                     else
  71.                         restartSearch = true;
  72.                 }
  73.                 while (restartSearch);
  74.             }
  75.  
  76.             return map;
  77.         }
  78.  
  79.         private static void RenderMap(char[,] map)
  80.         {
  81.             Console.SetCursorPosition(0 , 0);
  82.  
  83.             for (int i = 0; i < map.GetLength(0); i++)
  84.             {
  85.                 for (int j = 0; j < map.GetLength(1); j++)
  86.                 {
  87.                     Console.Write(map[i , j]);
  88.                 }
  89.                 Console.WriteLine();
  90.             }
  91.         }
  92.  
  93.         private static void MoveHero(char[,] map ,ref int heroX , ref int heroY)
  94.         {
  95.             ConsoleKeyInfo userInput = Console.ReadKey();
  96.  
  97.             switch (userInput.Key)
  98.             {
  99.                 case ConsoleKey.UpArrow:
  100.                     if (heroX != 0 && map[heroX - 1 , heroY] != '#')
  101.                     {
  102.                         heroX--;
  103.                         RenderMap(map);
  104.                     }
  105.                     break;
  106.  
  107.                 case ConsoleKey.DownArrow:
  108.                     if (heroX != map.GetLength(0) - 1 && map[heroX + 1 , heroY] != '#')
  109.                     {
  110.                         heroX++;
  111.                         RenderMap(map);
  112.                     }
  113.                     break;
  114.  
  115.                 case ConsoleKey.LeftArrow:
  116.                     if (heroY != 0 && map[heroX , heroY - 1] != '#' && heroY != 0)
  117.                     {
  118.                         heroY--;
  119.                         RenderMap(map);
  120.                     }
  121.                     break;
  122.  
  123.                 case ConsoleKey.RightArrow:
  124.                     if (heroY != map.GetLength(1) - 1 && map[heroX , heroY + 1] != '#')
  125.                     {
  126.                         heroY++;
  127.                         RenderMap(map);
  128.                     }
  129.                     break;
  130.             }
  131.         }
  132.     }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement