Skip to content

Instantly share code, notes, and snippets.

@cameronpresley

cameronpresley/RoverFacingEast.cs Secret

Last active Jun 20, 2020
Embed
What would you like to do?
LTE Mars Rover - WhenTurningRight
public void Execute(Action ifNorth, Action ifSouth, Action ifEast, Action ifWest)
{
switch(Orienation)
{
case Direction.North: ifNorth(); break;
case Direction.South: ifSouth(); break;
case Direction.East: ifEast(); break;
case Direction.West: ifWest(); break;
}
}
public void TurnRight()
{
Action ifNorth = () => Orientation = Direction.East;
Action ifSouth = () => {};
Action ifEast = () => Orientation = Direction.South;
Action ifWest = () => {};
Execute(ifNorth, ifSouth, ifEast, ifWest);
}
public void Execute(Action ifNorth, Action ifSouth, Action ifEast, Action ifWest)
{
switch(Orienation)
{
case Direction.North: ifNorth(); break;
case Direction.South: ifSouth(); break;
case Direction.East: ifEast(); break;
case Direction.West: ifWest(); break;
}
}
public void TurnRight()
{
Action ifNorth = () => Orientation = Direction.East;
Action ifSouth = () => {};
Action ifEast = () => {};
Action ifWest = () => {};
Execute(ifNorth, ifSouth, ifEast, ifWest);
}
public class Rover
{
public Direction Orientation { get; set; }
public Coordinate Location { get; set; }
public Rover()
{
Orientation = Direction.North;
Location = new Coordinate();
}
public void MoveForward()
{
Action ifNorth = () => Location=Location.AdjustYBy(1);
Action ifSouth = () => Location=Location.AdjustYBy(-1);
Action ifEast = () => Location=Location.AdjustXBy(1);
Action ifWest = () => Location=Location.AdjustXBy(-1);
Execute(ifNorth, ifSouth, ifEast, ifWest);
}
public void MoveBackward()
{
Action ifNorth = () => Location=Location.AdjustYBy(-1);
Action ifSouth = () => Location=Location.AdjustYBy(1);
Action ifEast = () => Location=Location.AdjustXBy(-1);
Action ifWest = () => Location=Location.AdjustXBy(1);
Execute(ifNorth, ifSouth, ifEast, ifWest);
}
public void TurnLeft()
{
Action ifNorth = () => Orientation = Direction.West;
Action ifWest = () => Orientation = Direction.South;
Action ifSouth = () => Orientation = Direction.East;
Action ifEast = () => Orientation = Direction.North;
Execute(ifNorth, ifSouth, ifEast, ifWest);
}
public void TurnRight()
{
Action ifNorth = () => Orientation = Direction.East;
Action ifEast = () => Orientation = Direction.South;
Action ifSouth = () => Orientation = Direction.West;
Action ifWest = () => Orientation = Direction.North;
Execute(ifNorth, ifSouth, ifEast, ifWest);
}
private void Execute(Action ifNorth, Action ifSouth, Action ifEast, Action ifWest)
{
switch(Orientation)
{
case Direction.North: ifNorth(); break;
case Direction.South: ifSouth(); break;
case Direction.East: ifEast(); break;
case Direction.West: ifWest(); break;
}
}
}
public void Execute(Action ifNorth, Action ifSouth, Action ifEast, Action ifWest)
{
switch(Orienation)
{
case Direction.North: ifNorth(); break;
case Direction.South: ifSouth(); break;
case Direction.East: ifEast(); break;
case Direction.West: ifWest(); break;
}
}
public void TurnRight()
{
Action ifNorth = () => Orientation = Direction.East;
Action ifSouth = () => Orientation = Direction.West;
Action ifEast = () => Orientation = Direction.South;
Action ifWest = () => Orientation = Direction.North;
Execute(ifNorth, ifSouth, ifEast, ifWest);
}
[TestFixture]
public class WhenTurningRight
{
[Test]
[TestCase(Direction.North, Direction.East, TestName = "AndFacingNorthThenTheRoverFacesEast")]
public void RoverTurningRight(Direction start, Direction expected)
{
var rover = new Rover { Orientation = start };
var initialLocation = rover.Location;
rover.TurnRight();
Assert.AreEqual(expected, rover.Orientation);
Assert.AreEqual(initialLocation, rover.Location);
}
[Test]
[TestCase(Direction.North, Direction.East, TestName = "AndFacingNorthThenTheRoverFacesEast")]
[TestCase(Direction.East, Direction.South, TestName = "AndFacingEastThenTheRoverFacesSouth")]
public void RoverTurningRight(Direction start, Direction expected)
{
var rover = new Rover { Orientation = start };
var initialLocation = rover.Location;
rover.TurnRight();
Assert.AreEqual(expected, rover.Orientation);
Assert.AreEqual(initialLocation, rover.Location);
}
[Test]
[TestCase(Direction.North, Direction.East, TestName = "AndFacingNorthThenTheRoverFacesEast")]
[TestCase(Direction.East, Direction.South, TestName = "AndFacingEastThenTheRoverFacesSouth")]
[TestCase(Direction.South, Direction.West, TestName = "AndFacingSouthThenTheRoverFacesWest")]
[TestCase(Direction.West, Direction.North, TestName = "AndFacingWestThenTheRoverFacesNorth")]
public void RoverTurningRight(Direction start, Direction expected)
{
var rover = new Rover { Orientation = start };
var initialLocation = rover.Location;
rover.TurnRight();
Assert.AreEqual(expected, rover.Orientation);
Assert.AreEqual(initialLocation, rover.Location);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.