Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.ComponentModel.Design;
- namespace ConsoleApp5
- {
- class Program
- {
- static void Main(string[] args)
- {
- Console.SetWindowSize(200, 70);
- Console.WriteLine("Выберите размеры карты");
- Console.Write("Ширина");
- int width = Convert.ToInt32(Console.ReadLine());
- Console.Write("Длина");
- int length = Convert.ToInt32(Console.ReadLine());
- Console.Clear();
- string[,] map = new string[length, width];
- CreatingMapBorders(ref map);
- EditModeOfMap(ref map);
- PlayerMovesAroundMap(map);
- }
- static void CreatingMapBorders(ref string[,] map)
- {
- for (int i = 0; i < map.GetLength(0); i++)
- {
- map[i, 0] = "#";
- map[i, map.GetLength(1) - 1] = "#";
- }
- for (int j = 0; j < map.GetLength(1); j++)
- {
- map[0, j] = "#";
- map[map.GetLength(0) - 1, j] = "#";
- }
- }
- static (int,int) FindingPlayerCoordinates(string[,] map)
- {
- (int X, int Y) currentPosition = (default, default);
- for (int i = 0; i < map.GetLength(0); i++)
- for (int j = 0; j < map.GetLength(1); j++)
- if (map[i, j] == "@")
- currentPosition = (j, i);
- return currentPosition;
- }
- static void PlayerMovesAroundMap(string[,] map)
- {
- var (X,Y) = FindingPlayerCoordinates(map);
- while (true)
- {
- DrawMap(map);
- var editPosition = (X, Y);
- ConsoleKeyInfo key = Console.ReadKey();
- editPosition = DirectionRecognition(editPosition, key);
- if (FailedMovementPlayer(editPosition, map))
- continue;
- map[Y, X] = " ";
- (X, Y) = editPosition;
- map[Y, X] = "@";
- }
- }
- static (int, int) DirectionRecognition((int X, int Y) editPosition, ConsoleKeyInfo key)
- {
- switch (key.Key)
- {
- case ConsoleKey.UpArrow:
- editPosition.Y--;
- break;
- case ConsoleKey.DownArrow:
- editPosition.Y++;
- break;
- case ConsoleKey.LeftArrow:
- editPosition.X--;
- break;
- case ConsoleKey.RightArrow:
- editPosition.X++;
- break;
- }
- return editPosition;
- }
- static void EditModeOfMap(ref string[,] map)
- {
- (int X, int Y) currentPosition = (1, 1);
- while (true)
- {
- Console.SetCursorPosition(0,30);
- Console.WriteLine("End - построить решотку\nDelete-удалить объект\nPage Down - поставить игрока\nInsert-начать игру");
- DrawMap(map);
- Console.SetCursorPosition(currentPosition.X, currentPosition.Y);
- var editPosition = currentPosition;
- ConsoleKeyInfo key = Console.ReadKey();
- editPosition = DirectionRecognition(editPosition, key);
- if (MovementFailed(editPosition, map))
- continue;
- currentPosition = editPosition;
- EditorMap(currentPosition, ref map, key);
- if (CheckingTransitionToGame(key) == false)
- break;
- }
- }
- static bool CheckingTransitionToGame(ConsoleKeyInfo key)
- {
- if(key.Key == ConsoleKey.Insert)
- {
- return false;
- }
- else
- {
- return true;
- }
- }
- static void EditorMap((int X, int Y) point, ref string[,] map, ConsoleKeyInfo key )
- {
- switch (key.Key)
- {
- case ConsoleKey.End:
- map[point.Y, point.X] = "#";
- break;
- case ConsoleKey.Delete:
- map[point.Y, point.X] = " ";
- break;
- case ConsoleKey.PageDown:
- map[point.Y, point.X] = "@";
- break;
- }
- }
- static bool MovementFailed((int X, int Y) point, string[,] map)
- {
- if (point.X <= 0 || point.X >= (map.GetLength(1) - 1))
- return true;
- if (point.Y <= 0 || point.Y >= (map.GetLength(0) - 1))
- return true;
- return false;
- }
- static bool FailedMovementPlayer((int X, int Y) point, string[,] map)
- {
- if (point.X <= 0 || point.X >= (map.GetLength(1) - 1))
- return true;
- if (point.Y <= 0 || point.Y >= (map.GetLength(0) - 1))
- return true;
- if (map[point.Y, point.X] == "#")
- return true;
- return false;
- }
- static void DrawMap(string [,] map)
- {
- for (int i = 0; i < map.GetLength(0); i++)
- for (int j = 0; j < map.GetLength(1); j++)
- {
- Console.SetCursorPosition(j, i);
- Console.Write(map[i, j]);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement