-
-
Save cameronpresley/41e0e868a13ef9dd33195e2c6f2f052e to your computer and use it in GitHub Desktop.
LTE Mars Rover - WhenTurningRight
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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