Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class program
- {
- static void Main(string[] args)
- {
- Console.CursorVisible = false;
- char[,] map =
- {
- {'#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#'},
- {'#',' ',' ',' ',' ',' ',' ',' ',' ','#','@',' ',' ',' ',' ','#'},
- {'#',' ',' ',' ','#','#','#','#',' ','#',' ','#','#','#',' ','#'},
- {'#',' ',' ',' ',' ',' ',' ','#',' ',' ',' ','#',' ',' ',' ','#'},
- {'#',' ',' ',' ',' ','#',' ','#','#',' ','#','#','#',' ','#','#'},
- {'#',' ','#','#',' ','#',' ',' ',' ',' ','#','#',' ',' ','#','#'},
- {'#',' ',' ',' ',' ','#','#','#','#',' ','#','#',' ','#','#','#'},
- {'#',' ','#','#',' ',' ',' ',' ','#',' ',' ',' ',' ','#','#','#'},
- {'#',' ','#','#',' ','#','#',' ','#','#','#',' ','#','#','#','#'},
- {'#',' ','#','#',' ','#','#',' ','#','#','#',' ',' ',' ',' ','#'},
- {'#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#'},
- };
- int userPositionX;
- int userPositionY;
- int userDirectionX = 0;
- int userDirectionY = 0;
- int allStars;
- int collectStars = 0;
- char star = '*';
- char wall = '#';
- char character = '@';
- bool isPlaying = true;
- ReadMap(map, out userPositionX, out userPositionY, out allStars, character, star);
- DrawMap(map, userPositionX, userPositionY);
- while (isPlaying)
- {
- Console.SetCursorPosition(0, 12);
- Console.WriteLine($"Собрано {collectStars}/{allStars}");
- DirectionСharacter(ref userDirectionX, ref userDirectionY);
- MovementСharacter(map, ref userPositionX, ref userPositionY, userDirectionX, userDirectionY, wall, character);
- CollectingStars(map, userPositionX, userPositionY,ref collectStars, star);
- if (collectStars == allStars)
- {
- isPlaying = false;
- }
- }
- }
- static int CollectingStars(char[,] array, int positionX, int positionY,ref int сollectStars, char symbolStar)
- {
- if (array[positionY, positionX] == symbolStar)
- {
- сollectStars++;
- array[positionY, positionX] = ' ';
- }
- return сollectStars;
- }
- static char[,] ReadMap(char[,] array,out int positionX,out int positionY, out int allStars, char symbolCharacter, char symbolStar)
- {
- positionX = 0;
- positionY = 0;
- allStars = 0;
- for (int i = 0; i < array.GetLength(0); i++)
- {
- for (int j = 0; j < array.GetLength(1); j++)
- {
- if (array[i,j] == symbolCharacter)
- {
- positionX = j;
- positionY = i;
- }
- else if (array[i,j] == ' ')
- {
- array[i, j] = symbolStar;
- allStars++;
- }
- }
- }
- return array;
- }
- static void DrawMap(char[,] array, int positionX, int positionY)
- {
- Console.SetCursorPosition(0, 0);
- for (int i = 0; i < array.GetLength(0); i++)
- {
- for (int j = 0; j < array.GetLength(1); j++)
- {
- Console.Write(array[i, j]);
- }
- Console.WriteLine();
- }
- }
- static void DirectionСharacter(ref int directionX, ref int directionY)
- {
- ConsoleKeyInfo charKey = Console.ReadKey();
- switch (charKey.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 MovementСharacter(char[,] array, ref int positionX, ref int positionY, int directionX, int directionY, char symbolWall, char symbolCharacter)
- {
- if (array[positionY + directionY, positionX + directionX] != symbolWall)
- {
- Console.SetCursorPosition(positionX, positionY);
- Console.WriteLine(' ');
- positionX += directionX;
- positionY += directionY;
- Console.SetCursorPosition(positionX, positionY);
- Console.WriteLine(symbolCharacter);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement