M
M
maksimufo2021-09-24 13:29:52
C++ / C#
maksimufo, 2021-09-24 13:29:52

How to simplify the code into one c# method?

Good afternoon, gentlemen
, there is a program
how can I write lines from 8 to 24 in one method?

using System;


namespace Mazes
{
    public static class SnakeMazeTask
    {
        public static void MoveRight(Robot robot, int width, int height)
        {
            for (int i = 0; i < width - 3; i++)
                robot.MoveTo(Direction.Right);
        }

        public static void MoveDown(Robot robot, int width, int height)
        {
            for (int i = 0; i < 2; i++)
                robot.MoveTo(Direction.Down);
        }

        public static void MoveLeft(Robot robot, int width, int height)
        {
            for (int i = 0; i < width - 3; i++)
                robot.MoveTo(Direction.Left);
        }

        public static void MoveOut(Robot robot, int width, int height)
        {
            MoveRight(robot, width, height);
            MoveDown(robot, width, height);
            MoveLeft(robot, width, height);

            if (!robot.Finished)
            {
                MoveDown(robot, width, height);
                MoveOut(robot, width, height);
            }
        }
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
spaceatmoon, 2021-09-24
@spaceatmoon

What's stopping you from doing so?

public static void Move(Robot robot, int width, int height, Direction direct)
        {
            for (int i = 0; i < width - 3; i++)
                robot.MoveTo(direct);
        }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question