Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- 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];
- BordersCreatingMapBorders(ref map);
- EditMap(ref map);
- GameOnMap(map);
- }
- static void BordersCreatingMapBorders(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 void GameOnMap(string[,] map)
- {
- (int X, int Y) currentPosition = (0, 0);
- 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);
- break;
- }
- }
- while (true)
- {
- 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]);
- }
- var editPosition = currentPosition;
- ConsoleKeyInfo key = Console.ReadKey();
- if (MovementPlayer(ref editPosition, map, key))
- {
- continue;
- }
- map[currentPosition.Y, currentPosition.X] = " ";
- currentPosition = editPosition;
- map[currentPosition.Y, currentPosition.X] = "@";
- }
- }
- static bool MovementPlayer(ref (int X, int Y) editPosition, string[,] map, 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;
- }
- if (FailedMovementPlayer(editPosition, map))
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- static void EditMap(ref string[,] map)
- {
- (int X, int Y) currentPosition = (1, 1);
- while (true)
- {
- Console.SetCursorPosition(0,30);
- Console.WriteLine("End - построить решотку\nDelete-удалить объект\nPage Down - поставить игрока\nInsert-начать игру");
- bool exit = true;
- 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]);
- }
- Console.SetCursorPosition(currentPosition.X, currentPosition.Y);
- var editPosition = currentPosition;
- ConsoleKeyInfo key = Console.ReadKey();
- if (movementWhenEditing(ref editPosition, map, key, ref exit))
- {
- continue;
- }
- currentPosition = editPosition;
- Editor(currentPosition, ref map, key);
- if (exit == false)
- break;
- }
- }
- static bool movementWhenEditing(ref (int X, int Y) editPosition, string[,] map, ConsoleKeyInfo key, ref bool exit)
- {
- 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;
- case ConsoleKey.Insert:
- exit = false;
- break;
- }
- if (MovementFailed(editPosition, map))
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- static void Editor((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;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement