Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace Lesson4_4
- {
- class Program
- {
- static void Main(string[] args)
- {
- int numberCoins = 10;
- int collectedCoins = 0;
- var map = GenerationMap(20 , 30 , numberCoins);
- int heroX = 2;
- int heroY = 2;
- Console.CursorVisible = false;
- RenderMap(map);
- do
- {
- Console.SetCursorPosition(heroY , heroX);
- Console.Write('@');
- MoveHero(map , ref heroX , ref heroY);
- if (map[heroX , heroY] == '$')
- {
- collectedCoins++;
- map[heroX , heroY] = ' ';
- }
- }
- while (numberCoins != collectedCoins);
- }
- private static char[,] GenerationMap(int height , int width , int numberCoins)
- {
- var map = new char[height , width];
- var random = new Random();
- for (int i = 0; i < map.GetLength(0); i++)
- {
- for (int j = 0; j < map.GetLength(1); j++)
- {
- if (i == 0 || j == 0 || i == map.GetLength(0) - 1 || j == map.GetLength(1) - 1)
- map[i , j] = '#';
- else
- map[i , j] = ' ';
- }
- }
- int coinX;
- int coinY;
- bool restartSearch;
- for (int i = 0; i < numberCoins; i++)
- {
- do
- {
- restartSearch = false;
- coinX = random.Next(1 , map.GetLength(0) - 1);
- coinY = random.Next(1 , map.GetLength(1) - 1);
- if (map[coinX , coinY] == ' ')
- map[coinX , coinY] = '$';
- else
- restartSearch = true;
- }
- while (restartSearch);
- }
- return map;
- }
- private static void RenderMap(char[,] map)
- {
- Console.SetCursorPosition(0 , 0);
- for (int i = 0; i < map.GetLength(0); i++)
- {
- for (int j = 0; j < map.GetLength(1); j++)
- {
- Console.Write(map[i , j]);
- }
- Console.WriteLine();
- }
- }
- private static void MoveHero(char[,] map ,ref int heroX , ref int heroY)
- {
- ConsoleKeyInfo userInput = Console.ReadKey();
- switch (userInput.Key)
- {
- case ConsoleKey.UpArrow:
- if (heroX != 0 && map[heroX - 1 , heroY] != '#')
- {
- heroX--;
- RenderMap(map);
- }
- break;
- case ConsoleKey.DownArrow:
- if (heroX != map.GetLength(0) - 1 && map[heroX + 1 , heroY] != '#')
- {
- heroX++;
- RenderMap(map);
- }
- break;
- case ConsoleKey.LeftArrow:
- if (heroY != 0 && map[heroX , heroY - 1] != '#' && heroY != 0)
- {
- heroY--;
- RenderMap(map);
- }
- break;
- case ConsoleKey.RightArrow:
- if (heroY != map.GetLength(1) - 1 && map[heroX , heroY + 1] != '#')
- {
- heroY++;
- RenderMap(map);
- }
- break;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement