Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace Ijunior
- {
- internal class Program
- {
- static void Main(string[] args)
- {
- int mapY = 12;
- int mapX = 24;
- char[,] map = new char[mapY, mapX];
- char wall = '#';
- char emptySpace = ' ';
- char item = '.';
- int totalScore;
- int userScore;
- GenerateMap(map, wall, emptySpace, item, mapY, mapX, out totalScore);
- PlayGame(map, wall, emptySpace, item, ref totalScore, out userScore);
- FinishGame(userScore, totalScore);
- }
- static void GenerateMap(char[,] map, char wall, char emptySpace, char item, int mapY, int mapX, out int totalScore)
- {
- int infoOffset = 2;
- ConsoleKeyInfo pressedKey;
- do
- {
- CreateMapBorders(map, wall, emptySpace);
- CreateVerticalWalls(map, wall, mapY, mapX);
- CreateHorizontalWalls(map, wall, mapY, mapX);
- GenerateItems(map, item, emptySpace, out totalScore);
- DrawMap(map);
- Console.SetCursorPosition(0, map.GetLength(0) + infoOffset);
- Console.Write($"Нажмите любую клавишу для генерации новой карты. " +
- $"\nНажмите {ConsoleKey.Enter}, чтобы начать игру");
- pressedKey = Console.ReadKey();
- Console.Clear();
- } while (pressedKey.Key != ConsoleKey.Enter);
- }
- static void CreateMapBorders(char[,] map, char wall, char emptySpace)
- {
- for (int i = 0; i < map.GetLength(0); i++)
- {
- for (int j = 0; j < map.GetLength(1); j++)
- {
- if (i == 0 || i == map.GetLength(0) - 1 || j == 0 || j == map.GetLength(1) - 1)
- {
- map[i, j] = wall;
- }
- else
- {
- map[i, j] = emptySpace;
- }
- }
- }
- }
- static void CreateVerticalWalls(char[,] map, char wall, int borderBuilingY, int borderBuilingX)
- {
- int pointBuilding = 2;
- borderBuilingY /= 2;
- borderBuilingX -= 2;
- Random random = new Random();
- int minRandomNumber = 5;
- int maxRandomNumber = 10;
- int wallCount = 1;
- for (int k = 0; k < wallCount; k++)
- {
- int indexY = random.Next(pointBuilding, borderBuilingY);
- int indexX = random.Next(pointBuilding, borderBuilingX);
- int wallSize = random.Next(minRandomNumber, maxRandomNumber);
- for (int i = indexY; i < map.GetLength(0) && i < indexY + wallSize; i++)
- {
- for (int j = indexX; j <= indexX; j++)
- {
- map[i, j] = wall;
- }
- }
- }
- }
- static void CreateHorizontalWalls(char[,] map, char wall, int borderBuilingY, int borderBuilingX)
- {
- int pointBuilding = 2;
- borderBuilingY -= 2;
- borderBuilingX -= 2;
- Random random = new Random();
- int minRandomNumber = 5;
- int maxRandomNumber = 10;
- int wallCount = 2;
- for (int k = 0; k < wallCount; k++)
- {
- int indexY = random.Next(pointBuilding, borderBuilingY);
- int indexX = random.Next(pointBuilding, borderBuilingX);
- int wallSize = random.Next(minRandomNumber, maxRandomNumber);
- for (int i = indexY; i <= indexY; i++)
- {
- for (int j = indexX; j < map.GetLength(1) && j < indexX + wallSize; j++)
- {
- map[i, j] = wall;
- }
- }
- }
- }
- static void GenerateItems(char[,] map, char item, char emptySpace, out int totalScore)
- {
- Random random = new Random();
- int minRandomNumber = 0;
- int maxRandomNumber = 20;
- int chanceItem;
- totalScore = 0;
- for (int i = 0; i < map.GetLength(0); i++)
- {
- for (int j = 0; j < map.GetLength(1); j++)
- {
- chanceItem = random.Next(minRandomNumber, maxRandomNumber);
- if (map[i, j] == emptySpace && chanceItem == minRandomNumber)
- {
- map[i, j] = item;
- totalScore++;
- }
- }
- }
- }
- static void DrawMap(char[,] map)
- {
- Console.SetCursorPosition(0, 0);
- 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 PlayGame(char[,] map, char wall, char emptySpace, char item, ref int totalScore, out int userScore)
- {
- char player = '@';
- int userPositionX = 1;
- int userPositionY = 1;
- int infoOffset = 2;
- userScore = 0;
- ConsoleKeyInfo pressedKey;
- do
- {
- Console.SetCursorPosition(0, map.GetLength(0) + infoOffset);
- Console.WriteLine($"Score: {userScore} / {totalScore} \nНажмите {ConsoleKey.Escape} для выхода");
- DrawMap(map);
- Console.SetCursorPosition(userPositionX, userPositionY);
- Console.CursorVisible = false;
- Console.Write(player);
- pressedKey = Console.ReadKey();
- CheckAvailableDirection(pressedKey, ref userPositionX, ref userPositionY, map, wall);
- Console.Clear();
- if (map[userPositionY, userPositionX] == item)
- {
- map[userPositionY, userPositionX] = emptySpace;
- userScore++;
- }
- }while (pressedKey.Key != ConsoleKey.Escape && userScore < totalScore);
- }
- static void CheckAvailableDirection(ConsoleKeyInfo pressedKey, ref int userPositionX, ref int userPositionY, char[,] map, char wall)
- {
- if (pressedKey.Key == ConsoleKey.UpArrow && map[userPositionY - 1, userPositionX] != wall)
- {
- Move(pressedKey, ref userPositionX, ref userPositionY);
- }
- else if (pressedKey.Key == ConsoleKey.DownArrow && map[userPositionY + 1, userPositionX] != wall)
- {
- Move(pressedKey, ref userPositionX, ref userPositionY);
- }
- else if (pressedKey.Key == ConsoleKey.LeftArrow && map[userPositionY, userPositionX - 1] != wall)
- {
- Move(pressedKey, ref userPositionX, ref userPositionY);
- }
- else if (pressedKey.Key == ConsoleKey.RightArrow && map[userPositionY, userPositionX + 1] != wall)
- {
- Move(pressedKey, ref userPositionX, ref userPositionY);
- }
- }
- static void Move(ConsoleKeyInfo pressedKey, ref int userPositionX, ref int userPositionY)
- {
- switch (pressedKey.Key)
- {
- case ConsoleKey.UpArrow:
- userPositionY -= 1;
- userPositionX += 0;
- break;
- case ConsoleKey.DownArrow:
- userPositionY += 1;
- userPositionX += 0;
- break;
- case ConsoleKey.LeftArrow:
- userPositionY += 0;
- userPositionX -= 1;
- break;
- case ConsoleKey.RightArrow:
- userPositionY += 0;
- userPositionX += 1;
- break;
- }
- }
- static void FinishGame(int userScore, int totalScore)
- {
- if(userScore == totalScore)
- {
- Console.WriteLine("Вы победили!");
- }
- else
- {
- Console.WriteLine("Вы проиграли!");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment