Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Program
- {
- static void Main()
- {
- char playerSymbol = '@';
- char wallSymbol = '#';
- char berrySymbol = '%';
- char emptySymbol = ' ';
- int widthMap = 0;
- int heightMap = 0;
- bool isMapReady = false;
- bool isExit = false;
- ConsoleKey keyInput;
- int totalBerries = 0;
- char[,] map = new char[0, 0];
- while (isExit == false)
- {
- const ConsoleKey CommandCreateMap = ConsoleKey.F1;
- const ConsoleKey CommandStartGame = ConsoleKey.F2;
- const ConsoleKey CommandExit = ConsoleKey.F3;
- Console.Clear();
- Console.WriteLine($"{CommandCreateMap} - Создание карты.");
- Console.WriteLine($"{CommandStartGame} - Старт игры.");
- Console.WriteLine($"{CommandExit} - Выход.");
- Console.Write("\nВыберете пункт меню: ");
- keyInput = Console.ReadKey().Key;
- switch (keyInput)
- {
- case CommandCreateMap:
- CreateMap(playerSymbol, berrySymbol, wallSymbol, emptySymbol, ref map,
- ref isMapReady, ref totalBerries, ref widthMap, ref heightMap);
- break;
- case CommandStartGame:
- TryStartGame(isMapReady, map, playerSymbol, berrySymbol, wallSymbol, emptySymbol, totalBerries);
- break;
- case CommandExit:
- isExit = true;
- break;
- default:
- ShowMessage("Не корректный ввод номера меню.");
- break;
- }
- ShowMessage("\nДля продолжения нажмите любую клавишу.");
- Console.ReadKey();
- }
- }
- static int GetValue(string message, int minValue, int maxValue)
- {
- int value = 0;
- bool isCorrectInput = false;
- while (isCorrectInput == false)
- {
- Console.Clear();
- Console.Write($"{message}. Значения от {minValue} до {maxValue}: ");
- string userInput = Console.ReadLine();
- if (Int32.TryParse(userInput, out int tempValue))
- {
- if (tempValue >= minValue && tempValue <= maxValue)
- {
- value = tempValue;
- isCorrectInput = true;
- ShowMessage("\nЗначение корректное.", ConsoleColor.Green);
- }
- else
- {
- ShowMessage($"\nЗначение превышает {maxValue}.");
- }
- }
- else
- {
- ShowMessage("\nНе корректно введёно значение.");
- }
- ShowMessage("\nДля продолжения нажмите любую клавишу.");
- Console.ReadKey();
- }
- return value;
- }
- static bool TryStartGame(bool isMapReady, char[,] map, char playerSymbol, char berrySymbol, char wallSymbol,
- char emptySymbol, int totalBerries)
- {
- if (isMapReady == false)
- {
- ShowMessage("\nКарта ещё не готова");
- return false;
- }
- else
- {
- StartGame(map, playerSymbol, berrySymbol, wallSymbol, emptySymbol, totalBerries);
- return true;
- }
- }
- static void StartGame(char[,] map, char playerSymbol, char berrySymbol, char wallSymbol, char emptySymbol, int totalBerries)
- {
- int playerPositionX;
- int playerPositionY;
- int playerDirectionX = 0;
- int playerDirectionY = 0;
- int currentBerries = 0;
- bool isPlaying = true;
- int gameSpeed = 200;
- Console.Clear();
- Console.CursorVisible = false;
- DrawMap(map, playerSymbol, out playerPositionX, out playerPositionY);
- char[,] currentMap = GetCopyMap(map);
- while (isPlaying)
- {
- int menuPositionY = currentMap.GetLength(1) + 3;
- Console.SetCursorPosition(0, menuPositionY);
- Console.WriteLine($"Собрано ягод: {currentBerries}/{totalBerries}");
- if (Console.KeyAvailable)
- {
- ConsoleKeyInfo key = Console.ReadKey(true);
- ChangeDirection(key.Key, ref playerDirectionX, ref playerDirectionY);
- }
- if (currentMap[playerPositionX + playerDirectionX, playerPositionY + playerDirectionY] != wallSymbol)
- {
- Move(playerSymbol, emptySymbol, playerDirectionX, playerDirectionY, ref playerPositionX, ref playerPositionY);
- if (currentMap[playerPositionX, playerPositionY] == berrySymbol)
- {
- currentBerries++;
- currentMap[playerPositionX, playerPositionY] = emptySymbol;
- }
- }
- if (currentBerries >= totalBerries)
- {
- isPlaying = false;
- }
- System.Threading.Thread.Sleep(gameSpeed);
- }
- Console.Clear();
- ShowMessage("Поздравляем с победой, игра окончена!", ConsoleColor.DarkYellow);
- }
- static void Move(int directionX, int directionY, ref int positionX, ref int positionY)
- {
- positionX += directionX;
- positionY += directionY;
- Console.SetCursorPosition(positionX, positionY);
- }
- static void Move(char playerSymbol, char emptySymbol, int directionX, int directionY, ref int positionX, ref int positionY)
- {
- DrawSymbol(positionX, positionY, emptySymbol);
- positionX += directionX;
- positionY += directionY;
- DrawSymbol(positionX, positionY, playerSymbol);
- }
- static void ChangeDirection(ConsoleKey key, ref int directionX, ref int directionY)
- {
- switch (key)
- {
- case ConsoleKey.UpArrow:
- directionX = 0;
- directionY = -1;
- break;
- case ConsoleKey.DownArrow:
- directionX = 0;
- directionY = 1;
- break;
- case ConsoleKey.LeftArrow:
- directionX = -1;
- directionY = 0;
- break;
- case ConsoleKey.RightArrow:
- directionX = 1;
- directionY = 0;
- break;
- }
- }
- static void DrawMap(char[,] map)
- {
- for (int i = 0; i < map.GetLength(0); i++)
- {
- for (int j = 0; j < map.GetLength(1); j++)
- {
- DrawSymbol(i, j, map[i, j]);
- }
- }
- }
- static void DrawMap(char[,] map, char playerSymbol, out int playerPositionX, out int playerPositionY)
- {
- playerPositionX = 0;
- playerPositionY = 0;
- for (int i = 0; i < map.GetLength(0); i++)
- {
- for (int j = 0; j < map.GetLength(1); j++)
- {
- if (map[i, j] == playerSymbol)
- {
- playerPositionX = i;
- playerPositionY = j;
- }
- DrawSymbol(i, j, map[i, j]);
- }
- }
- }
- static void DrawSymbol(int positionX, int positionY, char symbol)
- {
- Console.SetCursorPosition(positionX, positionY);
- Console.Write(symbol);
- }
- static char[,] GetCopyMap(char[,] map)
- {
- char[,] tempMap = new char[map.GetLength(0), map.GetLength(1)];
- for (int i = 0; i < tempMap.GetLength(0); i++)
- {
- for (int j = 0; j < tempMap.GetLength(1); j++)
- {
- tempMap[i, j] = map[i, j];
- }
- }
- return tempMap;
- }
- static void ShowMessage(string message, ConsoleColor color = ConsoleColor.DarkRed)
- {
- ConsoleColor defaultColor = ConsoleColor.White;
- Console.ForegroundColor = color;
- Console.WriteLine(message);
- Console.ForegroundColor = defaultColor;
- }
- #region Create Map
- static void CreateMap(char playerSymbol, char berrySymbol, char wallSymbol, char emptySymbol,
- ref char[,] map, ref bool isMapReady, ref int totalBerries, ref int widthMap, ref int heightMap)
- {
- const ConsoleKey CommandInsertStartPostition = ConsoleKey.F1;
- const ConsoleKey CommandInsertBerry = ConsoleKey.F2;
- const ConsoleKey CommandDrawWall = ConsoleKey.F3;
- const ConsoleKey CommandDrawEmpty = ConsoleKey.F4;
- const ConsoleKey CommandExit = ConsoleKey.F5;
- char positiveAnswer = 'y';
- char negativeAnswer = 'n';
- ConsoleKey inputKey;
- int minWidth = 5;
- int maxWidth = 30;
- int minHeight = 5;
- int maxHeight = 15;
- int cursorPositionX = 1;
- int cursorPositionY = 1;
- Console.Clear();
- Console.CursorVisible = true;
- if (isMapReady)
- {
- Console.WriteLine($"Карта уже готова, хотите создать новую? Да - \"{positiveAnswer}\"/ Нет - \"{negativeAnswer}\"");
- if (Console.ReadLine().ToLower()[0] == positiveAnswer)
- {
- totalBerries = 0;
- CreateMapBorder(minWidth, maxWidth, minHeight, maxHeight, wallSymbol, ref map, ref widthMap, ref heightMap);
- }
- }
- else
- {
- totalBerries = 0;
- CreateMapBorder(minWidth, maxWidth, minHeight, maxHeight, wallSymbol, ref map, ref widthMap, ref heightMap);
- }
- isMapReady = false;
- while (isMapReady == false)
- {
- Console.Clear();
- DrawMap(map);
- Console.SetCursorPosition(0, heightMap + 3);
- Console.WriteLine($"{CommandInsertStartPostition} - {playerSymbol} - установить точку старта.");
- Console.WriteLine($"{CommandInsertBerry} - {berrySymbol} - добавить ягоду.");
- Console.WriteLine($"{CommandDrawWall} - {wallSymbol} - нарисовать стену.");
- Console.WriteLine($"{CommandDrawEmpty} - {emptySymbol} - удалить объект.");
- Console.WriteLine($"{CommandExit} - выйти.");
- Console.WriteLine($"\nИспользуйте стрелки для перемещения по карте.");
- Console.SetCursorPosition(cursorPositionX, cursorPositionY);
- inputKey = Console.ReadKey().Key;
- switch (inputKey)
- {
- case ConsoleKey.F1:
- AddStartPoint(cursorPositionX, cursorPositionY, playerSymbol, map);
- break;
- case ConsoleKey.F2:
- AddBerry(cursorPositionX, cursorPositionY, berrySymbol, map, ref totalBerries);
- break;
- case ConsoleKey.F3:
- AddWall(cursorPositionX, cursorPositionY, wallSymbol, map);
- break;
- case ConsoleKey.F4:
- DeleteElement(cursorPositionX, cursorPositionY, berrySymbol, emptySymbol, map, ref totalBerries);
- break;
- case ConsoleKey.F5:
- ShowMessage("Карта готова.", ConsoleColor.Green);
- isMapReady = true;
- break;
- case ConsoleKey.UpArrow:
- MoveCursor(inputKey, widthMap, heightMap, ref cursorPositionX, ref cursorPositionY);
- break;
- case ConsoleKey.DownArrow:
- MoveCursor(inputKey, widthMap, heightMap, ref cursorPositionX, ref cursorPositionY);
- break;
- case ConsoleKey.LeftArrow:
- MoveCursor(inputKey, widthMap, heightMap, ref cursorPositionX, ref cursorPositionY);
- break;
- case ConsoleKey.RightArrow:
- MoveCursor(inputKey, widthMap, heightMap, ref cursorPositionX, ref cursorPositionY);
- break;
- default:
- ShowMessage("\nНе корректный ввод");
- break;
- }
- }
- }
- static void CreateMapBorder(int minWidth, int maxWidth, int minHeight, int maxHeight, char wallSymbol,
- ref char[,] map, ref int widthMap, ref int heightMap)
- {
- widthMap = GetValue("Введите ширину карты", minWidth, maxWidth);
- heightMap = GetValue("Введите длину карты", minHeight, maxHeight);
- map = new char[widthMap, heightMap];
- AddHorizontalBorder(0, widthMap, wallSymbol, map);
- for (int i = 1; i < heightMap - 1; i++)
- {
- map[0, i] = wallSymbol;
- map[widthMap - 1, i] = wallSymbol;
- }
- AddHorizontalBorder(heightMap - 1, widthMap, wallSymbol, map);
- }
- static void AddHorizontalBorder(int positionY, int length, char wallSymbol, char[,] map)
- {
- for (int i = 0; i < length; i++)
- {
- map[i, positionY] = wallSymbol;
- }
- }
- static void AddStartPoint(int positionX, int positionY, char playerSymbol, char[,] map)
- {
- SetSymbol(positionX, positionY, playerSymbol, map);
- }
- static void AddBerry(int positionX, int positionY, char berrySymbol, char[,] map, ref int totalBerries)
- {
- SetSymbol(positionX, positionY, berrySymbol, map);
- totalBerries++;
- }
- static void AddWall(int positionX, int positionY, char wallSymbol, char[,] map)
- {
- SetSymbol(positionX, positionY, wallSymbol, map);
- }
- static void DeleteElement(int positionX, int positionY, char berrySymbol, char emptySymbol, char[,] map, ref int totalBerries)
- {
- if (map[positionX, positionY] == berrySymbol)
- totalBerries--;
- SetSymbol(positionX, positionY, emptySymbol, map);
- }
- static void SetSymbol(int positionX, int positionY, char symbol, char[,] map)
- {
- map[positionX, positionY] = symbol;
- }
- static void MoveCursor(ConsoleKey key, int widthMap, int heightMap, ref int positionX, ref int positionY)
- {
- int directionX = 0;
- int directionY = 0;
- ChangeDirection(key, ref directionX, ref directionY);
- int prePositionX = positionX + directionX;
- int prePositionY = positionY + directionY;
- if (prePositionX > 0 && prePositionX < widthMap - 1 && prePositionY > 0 && prePositionY < heightMap - 1)
- {
- Move(directionX, directionY, ref positionX, ref positionY);
- }
- }
- #endregion
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement