Необходимо сократить до одного метода с помощью Direction

Рейтинг: 0Ответов: 1Опубликовано: 24.03.2023

Необходимо сократить методы MoveOnX и MoveOnX в один метод

namespace Mazes
{
public static class DiagonalMazeTask
{
    public static void MoveOut(Robot robot, int width, int height)
    {
        if (height > width)
        {
            MoveOnY(robot, width, height);
        }
        else
            MoveOnX(robot, width, height);
    }

    public static void MoveOnY(Robot robot, int width, int height)
    {
        while (!robot.Finished)
        {
            for (int i = 0; i < (height - 3) / (width - 3 + 1); i++)
            {
                robot.MoveTo(Direction.Down);
            }
            if (!robot.Finished)
                robot.MoveTo(Direction.Right);
        }
    }

    public static void MoveOnX(Robot robot, int width, int height)
    {
        while (!robot.Finished)
        {
            for (int i = 0; i < (width - 3) / (height - 3 + 1); i++)
            {
                robot.MoveTo(Direction.Right);
            }
            if (!robot.Finished)
                robot.MoveTo(Direction.Down);
        }
    }
}
}

Ответы

▲ 0
namespace Mazes
{
    public static class DiagonalMazeTask
    {
        public static void MoveOut(Robot robot, int width, int height)
        {
            var direction = width > height ? Direction.Right : Direction.Down;
            var length = width > height ? width : height;
            for (int i = 0; i < length - 3; i++)
            {
                robot.MoveTo(direction);
            }
        }
    }
}