Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace brave_new_world
- {
- internal class Program
- {
- static void Main(string[] args)
- {
- string[,] map;
- int mapWidthSize = 15;
- int mapHeightSize = 10;
- map = PaintMap(mapWidthSize, mapHeightSize, out int numberOfCoins);
- MoveAroundMap(map, numberOfCoins);
- }
- static string[,] PaintMap(int xSize, int ySize, out int numberOfCoins)
- {
- numberOfCoins = 0;
- double thirtyPercent = 0.3;
- string playerSymbol = "X";
- string wallSymbol = "#";
- string coinSymbol = "*";
- string emptyField = " ";
- string[,] map = new string[ySize, xSize];
- int numberOfBoarders = 2;
- int heightMapWithoutBoarders = ySize - numberOfBoarders;
- int widthMapWithoutBoarders = xSize - numberOfBoarders;
- int numberOfEmptyFields = (widthMapWithoutBoarders) * (heightMapWithoutBoarders);
- int maxNumberOfWalls = Convert.ToInt32(numberOfEmptyFields * thirtyPercent);
- for (int i = 0; i < ySize; i++)
- {
- for (int j = 0; j < xSize; j++)
- {
- if (i == 0 || i == ySize - 1 || j == 0 || j == xSize - 1)
- {
- map[i, j] = wallSymbol;
- }
- }
- }
- for (int i = 1; i < ySize - 1; i++)
- {
- for (int j = 1; j < xSize - 1; j++)
- {
- map[i, j] = emptyField;
- }
- }
- for (int i = 1; i < ySize - 1; i++)
- {
- for (int j = 1; j < xSize - 1; j++)
- {
- Console.Write($"Нарисуйте преграды символом {wallSymbol}, монеты символом {coinSymbol} и пустые поля пробелом: ");
- Console.SetCursorPosition(0, 1);
- PrintMap(map);
- Console.SetCursorPosition(0, ySize + 2);
- Console.WriteLine($"Осталось преград: {maxNumberOfWalls}");
- Console.SetCursorPosition(j, i + 1);
- map[i, j] = Console.ReadLine();
- if (map[i, j] == emptyField)
- {
- map[i, j] = " ";
- }
- else if (map[i, j] == wallSymbol)
- {
- maxNumberOfWalls--;
- }
- else if (map[i, j] == coinSymbol)
- {
- numberOfCoins++;
- }
- else if (maxNumberOfWalls == 0)
- {
- map[i, j] = coinSymbol;
- }
- else
- {
- map[i, j] = emptyField;
- }
- Console.Clear();
- }
- }
- for (int i = 1; i < ySize - 1; i++)
- {
- for (int j = 1; j < xSize - 1; j++)
- {
- if (map[i, j] == emptyField)
- {
- map[i, j] = playerSymbol;
- break;
- }
- }
- break;
- }
- return map;
- }
- static void PrintMap(string[,] map)
- {
- 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();
- }
- }
- static void MoveAroundMap(string[,] map, int numberOfCoins)
- {
- string emptyField = " ";
- string playerSymbol = "X";
- Console.CursorVisible = false;
- PlayerLocation(map, out int playerLocationX, out int playerLocationY);
- map[playerLocationY, playerLocationX] = emptyField;
- while (numberOfCoins > 0)
- {
- Console.SetCursorPosition(0, 0);
- PrintMap(map);
- Console.SetCursorPosition(0, map.GetLength(0) + 2);
- Console.WriteLine(numberOfCoins);
- Console.SetCursorPosition(playerLocationX, playerLocationY);
- Console.Write(playerSymbol);
- ConsoleKeyInfo charKey = Console.ReadKey();
- ProcessInput(map, ref playerLocationX, ref playerLocationY, charKey);
- CollectingCoins(map, ref numberOfCoins, playerLocationX, playerLocationY);
- Console.Clear();
- }
- Console.WriteLine("Уровень пройден.");
- Console.ReadKey();
- }
- static void CollectingCoins(string[,] map, ref int numberOfCoins, int playerLocationX, int playerLocationY)
- {
- string coinSymbol = "*";
- string emptyField = " ";
- if (map[playerLocationY, playerLocationX] == coinSymbol)
- {
- map[playerLocationY, playerLocationX] = emptyField;
- numberOfCoins--;
- }
- }
- static void PlayerLocation(string[,] map, out int playerLocationX, out int playerLocationY)
- {
- string playerSymbol = "X";
- playerLocationX = 0;
- playerLocationY = 0;
- for (int i = 1; i < map.GetLength(0) - 1; i++)
- {
- for (int j = 1; j < map.GetLength(1) - 1; j++)
- {
- if (map[i, j] == playerSymbol)
- {
- playerLocationY = i;
- playerLocationX = j;
- break;
- }
- }
- break;
- }
- }
- static void ProcessInput(string[,] map, ref int playerLocationX, ref int playerLocationY, ConsoleKeyInfo charKey)
- {
- string wallSymbol = "#";
- if (charKey.Key == ConsoleKey.UpArrow && map[playerLocationY - 1, playerLocationX] != wallSymbol)
- {
- playerLocationY--;
- }
- else if (charKey.Key == ConsoleKey.DownArrow && map[playerLocationY + 1, playerLocationX] != wallSymbol)
- {
- playerLocationY++;
- }
- else if (charKey.Key == ConsoleKey.LeftArrow && map[playerLocationY, playerLocationX - 1] != wallSymbol)
- {
- playerLocationX--;
- }
- else if (charKey.Key == ConsoleKey.RightArrow && map[playerLocationY, playerLocationX + 1] != wallSymbol)
- {
- playerLocationX++;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement