Skip to content

Instantly share code, notes, and snippets.

@cameronpresley
Last active June 20, 2020 00:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cameronpresley/41e0e868a13ef9dd33195e2c6f2f052e to your computer and use it in GitHub Desktop.
Save cameronpresley/41e0e868a13ef9dd33195e2c6f2f052e to your computer and use it in GitHub Desktop.
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