-
-
Save cameronpresley/015253bfea6b4a188bb5cf1a87032fdc to your computer and use it in GitHub Desktop.
LTE Mars Rover - Refactor
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 {X=0, Y=0}; | |
} | |
public void MoveForward() | |
{ | |
if (Orienation == Direction.North) { | |
Location = Location.AdjustYBy(1); | |
} | |
if (Orientation == Direction.South) { | |
Location = Location.AdjustYBy(-1); | |
} | |
if (Orientation == Direction.East) { | |
Location = Location.AdjustXBy(1); | |
} | |
if (Orientation == Direction.West) { | |
Location = Location.AdustXBy(-1); | |
} | |
} | |
public void MoveBackward() | |
{ | |
if (Orienation == Direction.North) { | |
Location = Location.AdjustYBy(-1); | |
} | |
if (Orientation == Direction.South) { | |
Location = Location.AdjustYBy(1); | |
} | |
if (Orientation == Direction.East) { | |
Location = Location.AdjustXBy(-1); | |
} | |
if (Orientation == Direction.West) { | |
Location = Location.AdjustXBy(1); | |
} | |
} | |
public void TurnLeft() | |
{ | |
if (Orienation == Direction.North) { | |
Orienation = Direction.West; | |
} | |
else if (Orientation == Direction.West) { | |
Orientation = Direction.South; | |
} | |
else if (Orientation == Direction.South) { | |
Orientation = Direction.East; | |
} | |
else if (Orientation == Direction.East) { | |
Orienation = Direction.North; | |
} | |
} | |
} |
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
private void Execute(Action ifNorth, Action ifSouth, Action ifEast, Action ifWest) | |
{ | |
if (Orientation == Direction.North) { | |
ifNorth(); | |
} | |
else if (Orientation == Direction.South) { | |
ifSouth(); | |
} | |
else if (Orientation == Direction.East) { | |
ifEast(); | |
} | |
else if (Orientation == Direction.West) { | |
ifWest(); | |
} | |
} | |
public void TurnLeft() | |
{ | |
Action ifNorth = () => Orienation = Direction.West; | |
Action ifWest = () => Orientation = Direction.South; | |
Action ifSouth = () => Orientation = Direction.East; | |
Action ifEast = () => Orientation = Direction.North; | |
Execute(ifNorth, ifSouth, ifEast, ifWest); | |
} | |
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); | |
} |
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
internal IMovementStrategy() | |
{ | |
Coordinate MoveForward(Coordinate coordinate); | |
Coordinate MoveBackward(Coordinate coordinate); | |
Direction TurnLeft(); | |
} | |
internal class NorthMovementStrategy : IMovementStrategy | |
{ | |
public Coordinate MoveForward(Coordinate coordinate) | |
{ | |
return coordinate.AdjustYBy(1); | |
} | |
public Coordinate MoveBackward(Coordinate coordinate) | |
{ | |
return coordinate.AdjustYBy(-1); | |
} | |
public Direction TurnLeft() | |
{ | |
return Direction.West; | |
} | |
} | |
private IMovementStrategy GetStrategy() | |
{ | |
if (Orientation == Direction.North) { | |
return new NorthMovementStrategy(); | |
} | |
else if (Orientation == Direction.South) { | |
return new SouthMovementStrategy(); | |
} | |
else if (Orientation == Direction.East) { | |
return new EastMovementStrategy(); | |
} | |
else if (Orientation == Direction.West) { | |
return new WestMovementStrategy(); | |
} | |
} | |
public void TurnLeft() | |
{ | |
var movementStrategy = GetMovementStrategy(); | |
Orientation = movementStrategy.TurnLeft(); | |
} | |
public void MoveForward() | |
{ | |
var movementStrategy = GetMovementStrategy(); | |
Location = movementStrategy.MoveForward(Location); | |
} |
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
private xxx Execute(xxxx) | |
{ | |
if (Orientation == Direction.North) { | |
// | |
} | |
else if (Orientation == Direction.South) { | |
// | |
} | |
else if (Orientation == Direction.East) { | |
// | |
} | |
else if (Orientation == Direction.West) { | |
// | |
} | |
} |
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
private void Execute(Action ifNorth, Action ifSouth, Action ifEast, Action ifWest) | |
{ | |
if (Orientation == Direction.North) { | |
ifNorth(); | |
} | |
else if (Orientation == Direction.South) { | |
ifSouth(); | |
} | |
else if (Orientation == Direction.East) { | |
ifEast(); | |
} | |
else if (Orientation == Direction.West) { | |
ifWest(); | |
} | |
} | |
public void TurnLeft() | |
{ | |
Action ifNorth = () => Orienation = Direction.West; | |
Action ifWest = () => Orientation = Direction.South; | |
Action ifSouth = () => Orientation = Direction.East; | |
Action ifEast = () => Orientation = Direction.North; | |
Execute(ifNorth, ifSouth, ifEast, ifWest); | |
} | |
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 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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment