{{ message }}

Instantly share code, notes, and snippets.

# cameronpresley/AutoProperty.cs Secret

Last active Jun 9, 2020
Mars Rover - Implementing Rover - Moving Forward
 // Given this definition of Rover public class Rover { public Coordinate Location {get; set} } // This is syntactic sugar for the following public class Rover { private Coordinate _location; public Coordinate Location { get => _location; set => _location = value; } }
 public struct Coordinate { public int X {get; set;} public int Y {get; set;} } public struct Coordinate { public int X {get; set;} public int Y {get; set;} public Coordinate AdjustXBy(int adjustment) { return new Coordinate {X = X+adjustment, Y=Y}; } public Coordinate AdjustYBy(int adjustment) { return new Coordinate {X=X, Y=Y+adjustment}; } }
 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 (Orientation == Direction.North) { Location = new Coordinate { X = Location.X, Y = Location.Y + 1 }; } if (Orientation == Direction.South) { Location = new Coordinate { X = Location.X, Y = Location.Y - 1}; } if (Direciton == Direction.East) { Location = new Coordinate {X = Location.X + 1, Y = Location.Y}; } } public void MoveForward() { if (Orientation == Direction.North) { Location = new Coordinate { X = Location.X, Y = Location.Y + 1 }; } if (Orientation == Direction.South) { Location = new Coordinate { X = Location.X, Y = Location.Y - 1}; } if (Direciton == Direction.East) { Location = Location.AdjustXBy(1); } } public void MoveForward() { if (Orientation == Direction.North) { Location = Location.AdjustYBy(1); } if (Orientation == Direction.South) { Location = Location.AdjustYBy(-1); } if (Direciton == Direction.East) { Location = Location.AdjustXBy(1); } } }
 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 (Orientation == Direction.North) { Location = new Coordinate { X = Location.X, Y = Location.Y + 1 }; } else { Location = new Coordinate { X = Location.X, Y = Location.Y - 1}; } } }
 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 (Orientation == Direction.North) { Location = Location.AdjustYBy(1); } if (Orientation == Direction.South) { Location = Location.AdjustYBy(-1); } if (Direciton == Direction.East) { Location = Location.AdjustXBy(1); } if (Orientation == Direction.West) { Location = Location.AdjustXBy(-1); } } }
 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 (Orientation == Direction.North) { Location = Location.AdjustYBy(1); } if (Orientation == Direction.South) { Location = Location.AdjustYBy(-1); } if (Direciton == Direction.East) { Location = Location.AdjustXBy(1); } if (Orientation == Direction.West) { Location = Location.AdjustXBy(-1); } } }
 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() { Location.Y+=1; } }
 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() { Location = new Coordinate { X = Location.X, Y = Location.Y + 1 }; } }
 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() { } }
 // Let's create a location var location = new Coordinate {X = 0, Y = 0}; // And now let's have newLocation have the same value, NOTE: This isn't a reference to location! var newLocation = location; // And if we check if both are equal, they are! Console.WriteLine(location.Equals(newLocation)); // True // Now let's change location's X value location.X = 200; // That works just fine, but if we check what newLocation is, we see that it's stil (0, 0) Console.WriteLine(\$"newLocation = ({newLocation.X}, {newLocation.Y})"); // Which means when we compare, they're not the same! Console.WriteLine(location.Equals(newLocation)); // False
 [TestFixture] public class WhenMovingForward() { [Test] public void AndFacingNorthThenYIncreasesByOne() { // Arrange var rover = new Rover() { Orientation = Direction.North}; // Act rover.MoveForward(); // Assert Assert.AreEqual(1, rover.Coordinate.Y); } [Test] public void AndFacingNorthThenYIncreasesByOne() { // Arrange var rover = new Rover {Orientation = Direction.North}; // Act rover.MoveForward(); // Assert var expectedLocation = new Coordinate { X = 0, Y = 1}; Assert.AreEqual(expectedLocation, rover.Location); Assert.AreEqual(Direction.North, rover.Orientation); } [Test] public void AndFacingNorthThenYIncreasesByOne() { // Arrange var rover = new Rover { Orientation = Direction.North, Location = new Coordinate {X = 0, Y = 0}, }; // Act rover.MoveForward(); // Assert var expectedLocation = new Coordinate { X = 0, Y = 1}; Assert.AreEqual(expectedLocation, rover.Location); Assert.AreEqual(Direction.North, rover.Orientation); } [Test] public void AndFacingNorthThenYIncreasesByOne() { // Arrange var rover = new Rover {Orientation = Direction.North}; var initialLocation = rover.Location; // capturing the inital location // Act rover.MoveForward(); // Assert var expectedLocation = new Coordinate { X = initialLocation.X, Y = initialLocation.Y+1}; Assert.AreEqual(expectedLocation, rover.Location); Assert.AreEqual(Direction.North, rover.Orientation); } [Test] public void AndFacingSouthThenYDecreasesByOne() { // Arrange var rover = new Rover {Orientation = Direction.South}; var initialLocation = rover.Location; // capturing the inital location // Act rover.MoveForward(); // Assert var expectedLocation = new Coordinate { X = initialLocation.X, Y = initialLocation.Y-1}; Assert.AreEqual(expectedLocation, rover.Location); Assert.AreEqual(Direction.South, rover.Orientation); } [Test] public void AndFacingEastThenXIncreasesByOne() { // Arrange var rover = new Rover {Orientation = Direction.East}; var initialLocation = rover.Location; // Act rover.MoveForward(); // Assert var expectedLocation = new Coordinate { X = initialLocation.X+1, Y = initialLocation.Y}; Assert.AreEqual(expectedLocation, rover.Location); Assert.AreEqual(Direction.East, rover.Orientation); } [Test] public void AndFacingWestThenXDecreasesByOne() { // Arrange var rover = new Rover {Orientation = Direction.West}; var initialLocation = rover.Location; // Act rover.MoveForward(); // Assert var expectedLocation = new Coordinate { X = initialLocation.X-1, Y = initialLocation.Y}; Assert.AreEqual(expectedLocation, rover.Location); Assert.AreEqual(Direction.West, rover.Orientation); } }
 [TestFixture] public class WhenMovingForward() { [Test] public void AndFacingNorthThenYIncreasesByOne() { // Arrange var rover = new Rover {Orientation = Direction.North}; var initialLocation = rover.Location; // Act rover.MoveForward(); // Assert var expectedLocation = new Coordinate { X = initialLocation.X, Y = initialLocation.Y+1}; Assert.AreEqual(expectedLocation, rover.Location); Assert.AreEqual(Direction.North, rover.Orientation); } [Test] public void AndFacingSouthThenYDecreasesByOne() { // Arrange var rover = new Rover {Orientation = Direction.South}; var initialLocation = rover.Location; // Act rover.MoveForward(); // Assert var expectedLocation = new Coordinate { X = initialLocation.X, Y = initialLocation.Y-1}; Assert.AreEqual(expectedLocation, rover.Location); Assert.AreEqual(Direction.South, rover.Orientation); } [Test] public void AndFacingEastThenXIncreasesByOne() { // Arrange var rover = new Rover {Orientation = Direction.East}; var initialLocation = rover.Location; // Act rover.MoveForward(); // Assert var expectedLocation = new Coordinate { X = initialLocation.X+1, Y = initialLocation.Y}; Assert.AreEqual(expectedLocation, rover.Location); Assert.AreEqual(Direction.East, rover.Orientation); } [Test] public void AndFacingWestThenXDecreasesByOne() { // Arrange var rover = new Rover {Orientation = Direction.West}; var initialLocation = rover.Location; // Act rover.MoveForward(); // Assert var expectedLocation = new Coordinate { X = initialLocation.X-1, Y = initialLocation.Y}; Assert.AreEqual(expectedLocation, rover.Location); Assert.AreEqual(Direction.West, rover.Orientation); } }