Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Threading;
- namespace HomeWorks
- {
- class Program
- {
- static void Main(string[] args)
- {
- Console.CursorVisible = false;
- bool isPlaying = true;
- int playerPositionY;
- int playerPositionX;
- int playerDirectionY = 0;
- int playerDirectionX = 0;
- int collectDots = 0;
- char[,] map = { { '#', '#', '#', '#', '#', '#' ,'#', '#', '#', '#', '#', '#' ,'#', '#', '#', '#', '#', '#' },
- { '#', '.', '.', '.', '.', '#' ,'.', '.', '.', '.', '.', '.' ,'.', '.', '.', '.', '.', '#' },
- { '#', '.', '#', '#', '.', '.' ,'.', '.', '#', '#', '#', '#' ,'#', '#', '#', '#', '.', '#' },
- { '#', '.', '#', '.', '.', '.' ,'#', '.', '.', '.', '.', '.' ,'.', '#', '.', '.', '.', '#' },
- { '#', '.', '.', '.', '.', '.' ,'#', '.', '.', '@', '.', '#' ,'.', '#', '.', '.', '.', '#' },
- { '#', '#', '.', '.', '#', '#' ,'#', '#', '#', '.', '.', '#' ,'.', '.', '.', '.', '#', '#' },
- { '#', '.', '.', '.', '.', '.' ,'#', '.', '.', '.', '.', '#' ,'#', '#', '#', '.', '.', '#' },
- { '#', '.', '#', '.', '.', '.' ,'#', '.', '.', '.', '.', '.' ,'.', '.', '.', '.', '.', '#' },
- { '#', '.', '#', '#', '.', '.' ,'.', '.', '#', '#', '.', '.' ,'.', '#', '#', '#', '.', '#' },
- { '#', '.', '.', '.', '.', '#' ,'.', '.', '.', '.', '.', '#' ,'.', '.', '.', '.', '.', '#' },
- { '#', '#', '#', '#', '#', '#' ,'#', '#', '#', '#', '#', '#' ,'#', '#', '#', '#', '#', '#' },};
- while (isPlaying)
- {
- DrawMap(map, out playerPositionY, out playerPositionX, collectDots);
- if (Console.KeyAvailable)
- {
- ConsoleKeyInfo key = Console.ReadKey(true);
- ChangeDirection(key, ref playerDirectionX, ref playerDirectionY);
- }
- if (map[playerPositionX + playerDirectionX, playerPositionY + playerDirectionY] != '#')
- {
- CollectDots(map, ref collectDots, playerPositionY, playerPositionX, playerDirectionX, playerDirectionY);
- Move(map, ref playerPositionY, ref playerPositionX, playerDirectionX, playerDirectionY);
- }
- Thread.Sleep(333);
- }
- }
- static void ChangeDirection(ConsoleKeyInfo key, ref int DX, ref int DY)
- {
- switch (key.Key)
- {
- case ConsoleKey.UpArrow:
- DX = -1; DY = 0;
- break;
- case ConsoleKey.DownArrow:
- DX = 1; DY = 0;
- break;
- case ConsoleKey.LeftArrow:
- DX = 0; DY = -1;
- break;
- case ConsoleKey.RightArrow:
- DX = 0; DY = 1;
- break;
- }
- }
- static void CollectDots(char[,] map, ref int collectDots, int Y, int X, int DX, int DY)
- {
- if (map[X + DX, Y + DY] == '.')
- {
- collectDots += 1;
- }
- }
- static void Move(char[,] map, ref int Y, ref int X, int DX, int DY)
- {
- if(map[X + DX, Y + DY] != '#')
- {
- map[X, Y] = ' ';
- Y += DY;
- X += DX;
- }
- map[X, Y] = '@';
- }
- static void DrawMap(char[,] map, out int Y, out int X, int collectDots)
- {
- Console.Clear();
- Y = 0;
- X = 0;
- for (int i = 0; i < map.GetLength(0); i++)
- {
- for (int j = 0; j < map.GetLength(1); j++)
- {
- Console.Write(map[i, j]);
- if (map[i, j] == '@')
- {
- X = i;
- Y = j;
- }
- }
- Console.WriteLine();
- }
- Console.Write($"Collect dots: {collectDots}");
- }
- }
- }
Add Comment
Please, Sign In to add comment