Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Program
- {
- static void Main(string[] args)
- {
- char[,] map;
- char symbolPlayer;
- char symbolWall;
- int positionPlayerX;
- int positionPlayerY;
- CreateMap(out map, out symbolWall);
- Console.WriteLine("Введите символ игрока");
- symbolPlayer = GetSymbol();
- AppointPlayerPlace(map, out positionPlayerX, out positionPlayerY, symbolWall);
- PlayGame(map, positionPlayerX, positionPlayerY, symbolPlayer, symbolWall);
- Console.ReadLine();
- }
- private static void CreateMap(out char[,] map, out char symbolWall)
- {
- const int MapChangeCommand = 1;
- const int ExitCommand = 2;
- bool isCreate = true;
- Console.WriteLine("Введите длину карты");
- int mapLength = GetNumber();
- Console.WriteLine("Введите ширину карты");
- int mapWidth = GetNumber();
- map = new char[mapWidth, mapLength];
- Console.WriteLine("Введите символ стены");
- symbolWall = GetSymbol();
- FillMap(map, symbolWall);
- DrawMap(map);
- while (isCreate)
- {
- Console.WriteLine($"{MapChangeCommand}: Изменить карту\n" +
- $"{ExitCommand}: Выход\n");
- int userInput = GetNumber();
- switch (userInput)
- {
- case MapChangeCommand:
- ChangeMap(map, symbolWall);
- break;
- case ExitCommand:
- isCreate = false;
- break;
- default:
- Console.WriteLine("Неверный ввод");
- break;
- }
- }
- Console.Clear();
- Console.SetCursorPosition(0, 0);
- }
- private static void AppointPlayerPlace(char[,] map, out int positionX, out int positionY, char symbolWall)
- {
- Console.Clear();
- bool isPlayerPlace = true;
- positionX = 0;
- positionY = 0;
- DrawMap(map);
- while (isPlayerPlace)
- {
- Console.WriteLine("Введите позицию по X");
- positionX = GetNumber();
- Console.WriteLine("Введите позицию по Y");
- positionY = GetNumber();
- if (positionX > 0 && positionX < map.GetLength(1) && positionY > 0 && positionY < map.GetLength(0))
- {
- if (map[positionX, positionY] != symbolWall)
- {
- isPlayerPlace = false;
- }
- else
- {
- Console.WriteLine("Неверный ввод");
- }
- }
- }
- Console.Clear();
- Console.SetCursorPosition(0, 0);
- Console.WriteLine("Место выбрано");
- }
- private static void PlayGame(char[,] map, int positionX, int positionY, char symbolPlayer, char symbolWall)
- {
- int directionX;
- int directionY;
- bool isPlaying = true;
- Console.Clear();
- Console.CursorVisible = false;
- DrawMap(map);
- DisplaySymbol(positionY, positionX, symbolPlayer);
- while (isPlaying)
- {
- ConsoleKeyInfo key = GetKey();
- GetDirection(out directionX, out directionY, key);
- if (map[positionX + directionX, positionY + directionY] != symbolWall)
- {
- Move(ref positionX, ref positionY, ref directionX, ref directionY, symbolPlayer);
- }
- System.Threading.Thread.Sleep(200);
- }
- }
- private static void ChangeMap(char[,] map, char symbolWall)
- {
- const ConsoleKey SaveCommand = ConsoleKey.S;
- const ConsoleKey PutWallCommand = ConsoleKey.Spacebar;
- bool isChanged = false;
- bool isSpacebar = false;
- int directionX;
- int directionY;
- int positionX = 1;
- int positionY = 1;
- Console.Clear();
- Console.WriteLine($"Стрелки: Перемещение\n" +
- $"S : Сохранить\n" +
- $"Space : Поставить стенку\n");
- Console.WriteLine("Нажмите любую кнопку для продолжения ...");
- Console.ReadKey();
- Console.Clear();
- Console.CursorVisible = false;
- DrawMap(map);
- DisplaySymbol(positionX, positionY, symbolWall);
- while (isChanged == false)
- {
- ConsoleKeyInfo key = GetKey();
- GetDirection(out directionX, out directionY,key);
- switch (key.Key)
- {
- case PutWallCommand:
- isSpacebar = PutWalls(map,positionX, positionY, symbolWall);
- break;
- case SaveCommand:
- isChanged = true;
- break;
- }
- if (directionY != 0 || directionX != 0)
- {
- if (map[positionX + directionX, positionY + directionY] != symbolWall)
- {
- Move(ref positionX, ref positionY, ref directionX, ref directionY, symbolWall);
- }
- if (isSpacebar)
- {
- Console.Clear();
- DrawMap(map);
- DisplaySymbol(positionX, positionY, symbolWall);
- isSpacebar = false;
- }
- }
- }
- Console.Clear();
- Console.SetCursorPosition(0, 0);
- Console.SetCursorPosition(0, 0);
- Console.WriteLine("Карта изменена");
- Console.CursorVisible = true;
- }
- private static void Move(ref int positionX, ref int positionY, ref int directionX, ref int directionY,char symbolPlayer)
- {
- DisplaySymbol(positionX, positionY);
- positionX += directionX;
- positionY += directionY;
- DisplaySymbol(positionX, positionY, symbolPlayer);
- }
- private static void GetDirection(out int playerDirectionX, out int playerDirectionY, ConsoleKeyInfo key)
- {
- const ConsoleKey UpCommand = ConsoleKey.UpArrow;
- const ConsoleKey DownCommand = ConsoleKey.DownArrow;
- const ConsoleKey LeftCommand = ConsoleKey.LeftArrow;
- const ConsoleKey RightCommand = ConsoleKey.RightArrow;
- playerDirectionX = 0;
- playerDirectionY = 0;
- switch (key.Key)
- {
- case UpCommand:
- playerDirectionX = -1;
- break;
- case DownCommand:
- playerDirectionX = 1;
- break;
- case LeftCommand:
- playerDirectionY = -1;
- break;
- case RightCommand:
- playerDirectionY = 1;
- break;
- }
- }
- private static ConsoleKeyInfo GetKey()
- {
- ConsoleKeyInfo key = new ConsoleKeyInfo();
- if (Console.KeyAvailable)
- {
- key = Console.ReadKey(true);
- }
- return key;
- }
- private static bool PutWalls(char[,] map, int positionX, int positionY, char symbolWall)
- {
- map[positionX, positionY] = symbolWall;
- return true;
- }
- private static void FillMap(char[,] map, char symbolWall)
- {
- for (int i = 0; i < map.GetLength(1); i++)
- {
- map[0, i] = symbolWall;
- }
- for (int i = 1; i < map.GetLength(0)-1; i++)
- {
- map[i, 0] = symbolWall;
- map[i, map.GetLength(1) - 1] = symbolWall;
- }
- for (int i = 0; i < map.GetLength(1); i++)
- {
- map[map.GetLength(0)-1, i] = symbolWall;
- }
- }
- private static void DisplaySymbol(int positionX, int positionY, char symbol = ' ')
- {
- Console.SetCursorPosition(positionY, positionX);
- Console.Write(symbol);
- }
- private static void DrawMap(char[,] 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("");
- }
- }
- private static int GetNumber()
- {
- int number = 0;
- while (int.TryParse(Console.ReadLine(), out number) == false)
- {
- Console.WriteLine("Неверный ввод");
- }
- return number;
- }
- private static char GetSymbol()
- {
- char symbol = ' ';
- while (char.TryParse(Console.ReadLine(), out symbol) == false)
- {
- Console.WriteLine("Неверный ввод");
- }
- return symbol;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement