Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace braveNewWorld
- {
- internal class Program
- {
- static void Main(string[] args)
- {
- string[,] map;
- int mapWidthSize = 15;
- int mapHeightSize = 10;
- CreateMap(mapWidthSize, mapHeightSize, out map, out int numberOfCoins);
- Work(map, ref numberOfCoins);
- }
- static void CreateMap(int xSize, int ySize, out string[,] map, out int numberOfCoins)
- {
- int numberOfBoarders = 2;
- int heightMapWithoutBoarders = ySize - numberOfBoarders;
- int widthMapWithoutBoarders = xSize - numberOfBoarders;
- int numberOfEmptyFields = (widthMapWithoutBoarders) * (heightMapWithoutBoarders);
- map = new string[ySize, xSize];
- CreateBoarders(map);
- PrintEmptyArea(map);
- PaintArea(map, out numberOfCoins);
- DropOffPlayer(map, ref numberOfCoins);
- }
- static void CreateBoarders(string[,] map)
- {
- string wallSymbol = "#";
- for (int i = 0; i <= map.GetLength(0); i += map.GetLength(0) - 1)
- {
- for (int j = 0; j < map.GetLength(1); j++)
- {
- map[i, j] = wallSymbol;
- }
- }
- for (int j = 0; j <= map.GetLength(1); j += map.GetLength(1) - 1)
- {
- for (int i = 0; i < map.GetLength(0); i++)
- {
- map[i, j] = wallSymbol;
- }
- }
- }
- static void PrintEmptyArea(string[,] map)
- {
- string emptyField = " ";
- for (int i = 1; i < map.GetLength(0) - 1; i++)
- {
- for (int j = 1; j < map.GetLength(1) - 1; j++)
- {
- map[i, j] = emptyField;
- }
- }
- }
- static void PaintArea(string[,] map, out int numberOfCoins)
- {
- string wallSymbol = "#";
- string coinSymbol = "*";
- numberOfCoins = 0;
- for (int i = 1; i < map.GetLength(0) - 1; i++)
- {
- for (int j = 1; j < map.GetLength(1) - 1; j++)
- {
- Console.Write($"Нарисуйте преграды символом {wallSymbol}: ");
- Console.SetCursorPosition(0, 1);
- PrintMap(map);
- Console.SetCursorPosition(j, i + 1);
- map[i, j] = Console.ReadLine();
- if (map[i, j] != wallSymbol)
- {
- map[i, j] = coinSymbol;
- numberOfCoins++;
- }
- Console.Clear();
- }
- }
- }
- 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 DropOffPlayer(string[,] map, ref int numberOfCoins)
- {
- string emptyField = " ";
- string coinSymbol = "*";
- string playerSymbol = "@";
- for (int i = 1; i < map.GetLength(1) - 1; i++)
- {
- for (int j = 1; j < map.GetLength(1) - 1; j++)
- {
- if (map[i, j] == emptyField)
- {
- map[i, j] = playerSymbol;
- return;
- }
- else if (map[i, j] == coinSymbol)
- {
- map[i, j] = playerSymbol;
- numberOfCoins--;
- return;
- }
- }
- }
- }
- static void Work(string[,] map, ref int numberOfCoins)
- {
- int playerMoveXPosition = 0;
- int playerMoveYPosition = 0;
- string emptyField = " ";
- string playerSymbol = "@";
- Console.CursorVisible = false;
- FindPlayer(map, out int playerLocationPositionX, out int playerLocationPositionY);
- map[playerLocationPositionY, playerLocationPositionX] = emptyField;
- while (numberOfCoins > 0)
- {
- Console.SetCursorPosition(0, 0);
- PrintMap(map);
- Console.SetCursorPosition(0, map.GetLength(0) + 2);
- Console.WriteLine(numberOfCoins);
- Console.SetCursorPosition(playerLocationPositionX, playerLocationPositionY);
- Console.Write(playerSymbol);
- ConsoleKeyInfo charKey = Console.ReadKey();
- MovePlayerThroughKeys(ref playerMoveXPosition, ref playerMoveYPosition, charKey);
- DrawMovePlayer(ref map, ref numberOfCoins, ref playerLocationPositionX, ref playerLocationPositionY, ref playerMoveXPosition, ref playerMoveYPosition, playerSymbol, emptyField);
- Console.Clear();
- }
- Console.WriteLine("Уровень пройден.");
- Console.ReadKey();
- }
- static void CollectCoins(string[,] map, ref int numberOfCoins, int playerLocationositionX, int playerLocationPositionY)
- {
- string coinSymbol = "*";
- string emptyField = " ";
- if (map[playerLocationPositionY, playerLocationositionX] == coinSymbol)
- {
- map[playerLocationPositionY, playerLocationositionX] = emptyField;
- numberOfCoins--;
- }
- }
- static void FindPlayer(string[,] map, out int playerLocationPositionX, out int playerLocationPositionY)
- {
- string playerSymbol = "@";
- playerLocationPositionX = 0;
- playerLocationPositionY = 0;
- for (int i = 1; i < map.GetLength(0); i++)
- {
- for (int j = 1; j < map.GetLength(1); j++)
- {
- if (map[i, j] == playerSymbol)
- {
- playerLocationPositionY = i;
- playerLocationPositionX = j;
- return;
- }
- }
- }
- }
- static void DrawMovePlayer(ref string[,] map, ref int numberOfCoins, ref int playerLocationPositionX, ref int playerLocationPositionY, ref int playerMoveXPosition, ref int playerMoveYPosition, string playerSymbol, string emptyField)
- {
- string wallSymbol = "#";
- playerLocationPositionX += playerMoveXPosition;
- playerLocationPositionY += playerMoveYPosition;
- if (map[playerLocationPositionY, playerLocationPositionX] != wallSymbol)
- {
- CollectCoins(map, ref numberOfCoins, playerLocationPositionX, playerLocationPositionY);
- playerMoveXPosition = 0;
- playerMoveYPosition = 0;
- map[playerLocationPositionY, playerLocationPositionX] = playerSymbol;
- }
- else
- {
- playerLocationPositionX -= playerMoveXPosition;
- playerLocationPositionY -= playerMoveYPosition;
- playerMoveXPosition = 0;
- playerMoveYPosition = 0;
- }
- map[playerLocationPositionY, playerLocationPositionX] = emptyField;
- }
- static void MovePlayerThroughKeys(ref int playerMoveXPosition, ref int playerMoveYPosition, ConsoleKeyInfo charKey)
- {
- ConsoleKey moveUp = ConsoleKey.UpArrow;
- ConsoleKey moveDown = ConsoleKey.DownArrow;
- ConsoleKey moveLeft = ConsoleKey.LeftArrow;
- ConsoleKey moveRight = ConsoleKey.RightArrow;
- if (charKey.Key == moveUp)
- {
- playerMoveYPosition = -1;
- }
- else if (charKey.Key == moveDown)
- {
- playerMoveYPosition = 1;
- }
- else if (charKey.Key == moveLeft)
- {
- playerMoveXPosition = -1;
- }
- else if (charKey.Key == moveRight)
- {
- playerMoveXPosition = 1;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment