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 symbolStar = '*';
- char symbolWall = '#';
- char symbolCharacter = '@';
- char symbolErasure = ' ';
- bool isPlaying = true;
- ReadMap(map, out userPositionX, out userPositionY, out allStars, symbolCharacter, symbolStar, symbolErasure);
- DrawMap(map);
- 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, symbolWall, symbolCharacter, symbolErasure);
- CollectingStars(map, userPositionX, userPositionY,ref collectStars, symbolStar, symbolErasure);
- if (collectStars == allStars)
- {
- isPlaying = false;
- }
- }
- }
- static int CollectingStars(char[,] array, int positionX, int positionY,ref int сollectStars, char symbolStar, char erasure)
- {
- if (array[positionY, positionX] == symbolStar)
- {
- сollectStars++;
- array[positionY, positionX] = erasure;
- }
- return сollectStars;
- }
- static char[,] ReadMap(char[,] array,out int positionX,out int positionY, out int allStars, char symbolCharacter, char symbolStar, char erasure)
- {
- 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] == erasure)
- {
- array[i, j] = symbolStar;
- allStars++;
- }
- }
- }
- return array;
- }
- static void DrawMap(char[,] array)
- {
- 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)
- {
- const ConsoleKey UpButtonCommand = ConsoleKey.UpArrow;
- const ConsoleKey DownButtonCommand = ConsoleKey.DownArrow;
- const ConsoleKey LeftButtonCommand = ConsoleKey.LeftArrow;
- const ConsoleKey RightButtonCommand = ConsoleKey.RightArrow;
- ConsoleKeyInfo charKey = Console.ReadKey();
- switch (charKey.Key)
- {
- case UpButtonCommand:
- directionX = 0;
- directionY = -1;
- break;
- case DownButtonCommand:
- directionX = 0;
- directionY = 1;
- break;
- case LeftButtonCommand:
- directionX = -1;
- directionY = 0;
- break;
- case RightButtonCommand:
- 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, char erasure)
- {
- if (array[positionY + directionY, positionX + directionX] != symbolWall)
- {
- RenderingСharacter(positionX, positionY, erasure);
- positionX += directionX;
- positionY += directionY;
- RenderingСharacter(positionX, positionY, symbolCharacter);
- }
- }
- static void RenderingСharacter(int positionX, int positionY, char symbol)
- {
- Console.SetCursorPosition(positionX, positionY);
- Console.WriteLine(symbol);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement