Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 3

#region Using Directives

using RumbaMumbaRobots.Paths;
using RumbaMumbaRobots.Printing;
using RumbaMumbaRobots.Robot;

#endregion
namespace RumbaMumbaRobots.CleaningAlgorithm
{
/// <summary>
///
/// </summary>
class HardwareRobotMainRoomCleaner : IHardwareRobotRoomCleaner
{
#region Private Variables

/// <summary>
///
/// </summary>
private int _hardwareRobotTotalMovement;
/// <summary>
///
/// </summary>
private readonly RobotPath _parentRobotPath;
/// <summary>
///
/// </summary>
private readonly IHardwareRobotDirection _hardwareRobotDirection;
/// <summary>
///
/// </summary>
private readonly IHardwareRobotMovement _hardwareRobotMovement;
/// <summary>
///
/// </summary>
private readonly IHardwareRobotObstacleDetector
_hardwareRobotObstacleDetector;
/// <summary>
///
/// </summary>
private readonly IRobotMovementPrinter _robotMovementPrinter;

#endregion

#region Class Constuctors

/// <summary>
///
/// </summary>
/// <param name="hardwareRobotDirection"></param>
/// <param name="hardwareRobotMovement"></param>
/// <param name="hardwareRobotObstacleDetector"></param>
/// <param name="robotMovementPrinter"></param>
public HardwareRobotMainRoomCleaner(IHardwareRobotDirection
hardwareRobotDirection,
IHardwareRobotMovement
hardwareRobotMovement,
IHardwareRobotObstacleDetector
hardwareRobotObstacleDetector,
IRobotMovementPrinter robotMovementPrinter)
{
_hardwareRobotDirection = hardwareRobotDirection;
_hardwareRobotMovement = hardwareRobotMovement;
_hardwareRobotObstacleDetector = hardwareRobotObstacleDetector;
_robotMovementPrinter = robotMovementPrinter;

_parentRobotPath = new RootRobotPath("Cell0");


}

#endregion

#region Method Implementations

/// <summary>
///
/// </summary>
public void CleanRoom()
{
var cellNo = 1;
const string cell = "Cell";
//1) Rotate left.
_hardwareRobotDirection.TurnLeft();

//2) If there isn�t an obstacle and the robot was never there, walk.
if (!_hardwareRobotObstacleDetector.IsObstacle())
{
var cellName = cell + cellNo;
if (!_parentRobotPath.IsVisited(cellName))
{
//Check if the robot has been there. if not, walk
_hardwareRobotMovement.Walk();

var _leafRobotPath = new LeafRobotPath(cellName);


_parentRobotPath.Add(_leafRobotPath);

++cellNo;
}
}
else
{
//3) If not, rotate right.
_hardwareRobotDirection.TurnRight();
}

//4) If there isn�t an obstacle and the robot was never there, walk.
if (!_hardwareRobotObstacleDetector.IsObstacle())
{
var cellName = cell + cellNo;
if (!_parentRobotPath.IsVisited(cellName))
{
//Check if the robot has been there. if not, walk
_hardwareRobotMovement.Walk();

var _leafRobotPath = new LeafRobotPath(cellName);


_parentRobotPath.Add(_leafRobotPath);
++cellNo;
}
}
else
{
//5) If not, rotate right.
_hardwareRobotDirection.TurnRight();
}

//6) If there isn�t an obstacle and the robot was never there, walk.
if (!_hardwareRobotObstacleDetector.IsObstacle())
{
var cellName = cell + cellNo;
if (!_parentRobotPath.IsVisited(cellName))
{
//Check if the robot has been there. if not, walk
_hardwareRobotMovement.Walk();

var _leafRobotPath = new LeafRobotPath(cellName);


_parentRobotPath.Add(_leafRobotPath);

++cellNo;
}
}
else
{
//7) If not, the room is cleaned.
}
_hardwareRobotTotalMovement = _hardwareRobotMovement.TotalMovements;
}
/// <summary>
///
/// </summary>
public void PrintPath()
{
//Print the path
_robotMovementPrinter.Print(_parentRobotPath.GetVisitedPathsDetails());
}
/// <summary>
///
/// </summary>
public void PrintTotalMovement()
{
_robotMovementPrinter.Print(string.Format("The Total movements is:
{0}.", _hardwareRobotTotalMovement));
}

#endregion
}
}

You might also like