Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Task31._1
- {
- internal class Program
- {
- static void Main(string[] args)
- {
- int heroPositionX;
- int heroPositionY;
- int heroPositionChangeX = 0;
- int heroPositionChangeY = 1;
- int allDots = 0;
- int collectDots = 0;
- int divisionFindHalves = 2;
- int halfDots;
- string youWin = "Вы победили!";
- bool isPlaying = true;
- char[,] map = CreateMap(out heroPositionX, out heroPositionY, ref allDots);
- DrawMap(map);
- while (isPlaying)
- {
- Console.SetCursorPosition(0, 20);
- Console.WriteLine($"Собрано {collectDots}/{allDots}");
- if (Console.KeyAvailable)
- {
- ConsoleKeyInfo key = Console.ReadKey(true);
- ChangeDirection(key, ref heroPositionChangeX, ref heroPositionChangeY, ref isPlaying);
- }
- if (map[heroPositionX + heroPositionChangeX, heroPositionY + heroPositionChangeY] != '#')
- {
- Move(ref heroPositionX, ref heroPositionY, heroPositionChangeX, heroPositionChangeY);
- CollectDots(map, heroPositionX, heroPositionY, ref collectDots);
- }
- System.Threading.Thread.Sleep(50);
- halfDots = allDots / divisionFindHalves;
- Console.SetCursorPosition(0, 21);
- if (collectDots > halfDots)
- {
- isPlaying = false;
- }
- }
- if (isPlaying == false)
- {
- Console.WriteLine(youWin);
- }
- }
- static void ChangeDirection(ConsoleKeyInfo key, ref int heroPositionChangeX, ref int heroPositionChangeY, ref bool isPlaying)
- {
- switch (key.Key)
- {
- case ConsoleKey.UpArrow:
- heroPositionChangeX = -1; heroPositionChangeY = 0;
- break;
- case ConsoleKey.DownArrow:
- heroPositionChangeX = 1; heroPositionChangeY = 0;
- break;
- case ConsoleKey.LeftArrow:
- heroPositionChangeX = 0; heroPositionChangeY = -1;
- break;
- case ConsoleKey.RightArrow:
- heroPositionChangeX = 0; heroPositionChangeY = 1;
- break;
- case ConsoleKey.Escape:
- isPlaying = false;
- break;
- default:
- break;
- }
- }
- static void Move(ref int heroPositionX, ref int heroPositionY, int heroPositionChangeX, int heroPositionChangeY)
- {
- Console.SetCursorPosition(heroPositionY, heroPositionX);
- Console.Write(" ");
- heroPositionX += heroPositionChangeX;
- heroPositionY += heroPositionChangeY;
- Console.SetCursorPosition(heroPositionY, heroPositionX);
- Console.Write('@');
- }
- static void CollectDots(char[,] map, int heroX, int heroY, ref int collectDots)
- {
- if (map[heroX, heroY] == '.')
- {
- collectDots++;
- map[heroX, heroY] = ' ';
- }
- }
- static char[,] CreateMap(out int heroX, out int heroY, ref int allDots)
- {
- int valueObjectMap;
- int wallValue = 0;
- int densityFilling = 5;
- int minValueMap = 5;
- int dotValue = 1;
- int heroValue = 2;
- int lenghtMap = 0;
- int weightMap = 0;
- bool heroAppeared = false;
- heroX = 0;
- heroY = 0;
- Random random = new Random();
- string requestLenghtMap = "Введите длину карты, более 4: ";
- string requestWeightMap = "Введите высоту карты, более 4: ";
- while (lenghtMap < minValueMap)
- {
- Console.Write(requestLenghtMap);
- lenghtMap = Convert.ToInt32(Console.ReadLine());
- }
- while (weightMap < minValueMap)
- {
- Console.Write(requestWeightMap);
- weightMap = Convert.ToInt32(Console.ReadLine());
- }
- Console.Clear();
- char[,] map = new char[weightMap, lenghtMap];
- while (heroAppeared == false)
- {
- for (int i = 0; i < weightMap; i++)
- {
- for (int j = 0; j < lenghtMap; j++)
- {
- if (i == 0 || i == weightMap - 1)
- {
- map[i, j] = '#';
- }
- else if (j == 0 || j == lenghtMap - 1)
- {
- map[i, j] = '#';
- }
- else
- {
- valueObjectMap = random.Next(wallValue, densityFilling);
- if (valueObjectMap == wallValue)
- {
- map[i, j] = '#';
- }
- else if (valueObjectMap == dotValue)
- {
- map[i, j] = '.';
- allDots++;
- }
- else if (valueObjectMap == heroValue && heroAppeared == false)
- {
- map[i, j] = '@';
- heroX = i;
- heroY = j;
- heroAppeared = true;
- }
- }
- }
- }
- }
- return map;
- }
- 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();
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment