CCCPaste

ДЗ: Brave new world2.0

using System;

namespace FunctionsHW
{
internal class Program
{
static void Main(string[] args)
{
Console.CursorVisible = false;

char[,] map = {
{ '╔', '═', '╦', '═', '═', '═', '═', '═', '═', '╗' },
{ '║', ' ', '║', '*', '*', '*', '*', '*', '*', '║' },
{ '║', '*', '║', '*', '║', '*', '╔', '═', '═', '╣' },
{ '║', '*', '*', '*', '║', '*', '║', '*', '*', '║' },
{ '║', '*', '║', '*', '║', '*', '║', '*', '╔', '╣' },
{ '║', '*', '║', '*', '║', '*', '║', '*', '╚', '╣' },
{ '║', '*', '║', '*', '║', '*', '║', '*', '*', '║' },
{ '║', '*', '║', '*', '║', '*', '*', '*', '═', '╣' },
{ '║', '*', '║', '*', '║', '*', '║', '*', '*', '║' },
{ '╚', '═', '╩', '═', '╩', '═', '╩', '═', '═', '╝' }
};

char player = '@';
char star = '*';
char road = ' ';

int playerPositionX = 1;
int playerPositionY = 1;
int score = 0;

ConsoleKeyInfo pressedKey;

while (true)
{
Console.Clear();

Console.ForegroundColor = ConsoleColor.Blue;
DrawMap(map);

Console.ForegroundColor = ConsoleColor.Green;
Console.SetCursorPosition(playerPositionX, playerPositionY);
Console.Write(player);

Console.ForegroundColor = ConsoleColor.Red;
Console.SetCursorPosition(0, 11);
Console.Write("Очки: " + score);

pressedKey = Console.ReadKey();

int[] direction = GetDirection(pressedKey);

int nextPlayerPositionX = playerPositionX + direction[0];
int nextPlayerPositionY = playerPositionY + direction[1];

char nextPosition = GetNextPositionChar(map, nextPlayerPositionX, nextPlayerPositionY);

if (nextPosition == star)
{
CollectStar(nextPlayerPositionX, nextPlayerPositionY, road, ref score, ref map);
Move(nextPlayerPositionX, nextPlayerPositionY, ref playerPositionX, ref playerPositionY);
}

if (nextPosition == road)
{
Move(nextPlayerPositionX, nextPlayerPositionY, ref playerPositionX, ref playerPositionY);
}
}
}

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();
}
}

static int[] GetDirection(ConsoleKeyInfo pressedKey)
{
ConsoleKeyInfo upArrow = new ConsoleKeyInfo('↑', ConsoleKey.UpArrow, false, false, false);
ConsoleKeyInfo downArrow = new ConsoleKeyInfo('↓', ConsoleKey.DownArrow, false, false, false);
ConsoleKeyInfo leftArrow = new ConsoleKeyInfo('←', ConsoleKey.LeftArrow, false, false, false);
ConsoleKeyInfo rightArrow = new ConsoleKeyInfo('→', ConsoleKey.RightArrow, false, false, false);

int[] direction = { 0, 0 };

if (pressedKey.Key == upArrow.Key)
direction[1] = -1;
else if (pressedKey.Key == downArrow.Key)
direction[1] = 1;
else if (pressedKey.Key == leftArrow.Key)
direction[0] = -1;
else if (pressedKey.Key == rightArrow.Key)
direction[0] = 1;

return direction;
}
static char GetNextPositionChar(char[,] map, int nextPositionX, int nextPositionY)
{
char nextPosition = map[nextPositionY, nextPositionX];

return nextPosition;
}

static void CollectStar(int positionX, int positionY, char road, ref int score, ref char[,] map)
{
score++;
map[positionY, positionX] = road;
}

static void Move(int positionX, int positionY, ref int playerPositionX, ref int playerPositionY)
{
playerPositionX = positionX;
playerPositionY = positionY;
}
}
}