Skip to content

Commit 1364d77

Browse files
committed
Changed to use unit vectors for movement.
1 parent 231fc39 commit 1364d77

13 files changed

+198
-192
lines changed

Core/Coordinate.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ namespace MarsRoverKata.Core
44
{
55
public class Coordinate
66
{
7-
public Int32 X { get; set; }
8-
public Int32 Y { get; set; }
7+
public Double X { get; set; }
8+
public Double Y { get; set; }
99

1010
public Coordinate(Int32 x, Int32 y)
1111
{

Core/Direction.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
namespace MarsRoverKata.Core
22
{
3-
public enum Direction
3+
public class Direction
44
{
5-
North,
6-
South,
7-
East,
8-
West
5+
public static readonly UnitVector North = new UnitVector(0, 1);
6+
public static readonly UnitVector South = new UnitVector(0, -1);
7+
public static readonly UnitVector East = new UnitVector(1, 0);
8+
public static readonly UnitVector West = new UnitVector(-1, 0);
99
}
1010
}

Core/IGrid.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ namespace MarsRoverKata.Core
44
{
55
public interface IGrid
66
{
7-
Coordinate GetAdjacentPosition(Coordinate position, Direction direction, Movement movement);
7+
Coordinate GetAdjacentPosition(Coordinate position, UnitVector direction);
88
Boolean IsObstacleInPosition(Coordinate coordinate);
99
}
1010
}

Core/IRover.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace MarsRoverKata.Core
55
{
66
public interface IRover
77
{
8-
Direction Direction { get; }
8+
UnitVector Direction { get; }
99
IEnumerable<Coordinate> GetObstacleCoordinates();
1010
void MoveBackward();
1111
void MoveForward();

Core/MarsRoverKata.Core.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
<Compile Include="Rover.cs" />
5050
<Compile Include="Properties\AssemblyInfo.cs" />
5151
<Compile Include="RoverController.cs" />
52+
<Compile Include="UnitVector.cs" />
5253
</ItemGroup>
5354
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
5455
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.

Core/OneByOneGrid.cs

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,38 +16,15 @@ public OneByOneGrid(Int32 numberOfRows, Int32 numberOfColumns)
1616
this.obstacleCoordinates = new HashSet<Coordinate>();
1717
}
1818

19-
public Coordinate GetAdjacentPosition(Coordinate position, Direction direction, Movement movement)
19+
public Coordinate GetAdjacentPosition(Coordinate position, UnitVector direction)
2020
{
21-
if (movement == Movement.Forward)
22-
{
23-
if (direction == Direction.North)
24-
position.Y -= 1;
25-
else if (direction == Direction.South)
26-
position.Y += 1;
27-
else if (direction == Direction.East)
28-
position.X += 1;
29-
else if (direction == Direction.West)
30-
position.X -= 1;
31-
}
32-
else if (movement == Movement.Backward)
33-
{
34-
if (direction == Direction.North)
35-
position.Y += 1;
36-
else if (direction == Direction.South)
37-
position.Y -= 1;
38-
else if (direction == Direction.East)
39-
position.X -= 1;
40-
else if (direction == Direction.West)
41-
position.X += 1;
42-
}
43-
44-
position.X = BindValue(position.X, 0, numberOfColumns - 1);
45-
position.Y = BindValue(position.Y, 0, numberOfRows - 1);
21+
position.X = BindValue(position.X + direction.X, 0, numberOfColumns - 1);
22+
position.Y = BindValue(position.Y + direction.Y, 0, numberOfRows - 1);
4623

4724
return position;
4825
}
4926

50-
private Int32 BindValue(Int32 value, Int32 min, Int32 max)
27+
private Double BindValue(Double value, Double min, Double max)
5128
{
5229
if (value < min)
5330
return max;

Core/Rover.cs

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55

66
namespace MarsRoverKata.Core
77
{
8-
public class Rover : MarsRoverKata.Core.IRover
8+
public class Rover : IRover
99
{
1010
public Coordinate Position { get; private set; }
11-
public Direction Direction { get; private set; }
11+
public UnitVector Direction { get; private set; }
1212
private IGrid grid;
1313
private HashSet<Coordinate> obstacleCoordinates;
1414

15-
public Rover(Coordinate startingPosition, Direction startingDirection, IGrid grid)
15+
public Rover(Coordinate startingPosition, UnitVector startingDirection, IGrid grid)
1616
{
1717
this.Position = startingPosition;
1818
this.Direction = startingDirection;
@@ -22,17 +22,18 @@ public Rover(Coordinate startingPosition, Direction startingDirection, IGrid gri
2222

2323
public void MoveForward()
2424
{
25-
HandleMovement(Movement.Forward);
25+
HandleMovement(Direction);
2626
}
2727

2828
public void MoveBackward()
2929
{
30-
HandleMovement(Movement.Backward);
30+
var backwardDirection = Direction.Invert();
31+
HandleMovement(backwardDirection);
3132
}
3233

33-
private void HandleMovement(Movement movement)
34+
private void HandleMovement(UnitVector direction)
3435
{
35-
var adjacentPosition = grid.GetAdjacentPosition(Position, Direction, movement);
36+
var adjacentPosition = grid.GetAdjacentPosition(Position, Direction);
3637

3738
if (grid.IsObstacleInPosition(adjacentPosition))
3839
obstacleCoordinates.Add(adjacentPosition);
@@ -42,26 +43,12 @@ private void HandleMovement(Movement movement)
4243

4344
public void TurnLeft()
4445
{
45-
if (Direction == Direction.North)
46-
Direction = Direction.West;
47-
else if (Direction == Direction.West)
48-
Direction = Direction.South;
49-
else if (Direction == Direction.South)
50-
Direction = Direction.East;
51-
else if (Direction == Direction.East)
52-
Direction = Direction.North;
46+
Direction = Direction.Rotate(90);
5347
}
5448

5549
public void TurnRight()
5650
{
57-
if (Direction == Direction.North)
58-
Direction = Direction.East;
59-
else if (Direction == Direction.West)
60-
Direction = Direction.North;
61-
else if (Direction == Direction.South)
62-
Direction = Direction.West;
63-
else if (Direction == Direction.East)
64-
Direction = Direction.South;
51+
Direction = Direction.Rotate(-90);
6552
}
6653

6754
public IEnumerable<Coordinate> GetObstacleCoordinates()

Core/UnitVector.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System;
2+
3+
namespace MarsRoverKata.Core
4+
{
5+
public class UnitVector
6+
{
7+
public Double X { get; set; }
8+
public Double Y { get; set; }
9+
10+
public UnitVector(Double x, Double y)
11+
{
12+
X = x;
13+
Y = y;
14+
}
15+
16+
public UnitVector Rotate(Int32 degrees)
17+
{
18+
var currentAngle = Math.Atan2(Y, X);
19+
var newAngle = currentAngle + (degrees * Math.PI) / 180;
20+
var x = Math.Round(Math.Cos(newAngle), 8);
21+
var y = Math.Round(Math.Sin(newAngle), 8);
22+
23+
return new UnitVector(x, y);
24+
}
25+
26+
public UnitVector Invert()
27+
{
28+
return new UnitVector(X * -1, Y * -1);
29+
}
30+
31+
public override Boolean Equals(Object obj)
32+
{
33+
var unitVector = (UnitVector)obj;
34+
return unitVector.X == X && unitVector.Y == Y;
35+
}
36+
}
37+
}

UnitTests/GridTests.cs

Lines changed: 0 additions & 124 deletions
This file was deleted.

UnitTests/MarsRoverKata.UnitTests.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,10 @@
4949
</ItemGroup>
5050
<ItemGroup>
5151
<Compile Include="Properties\AssemblyInfo.cs" />
52-
<Compile Include="GridTests.cs" />
52+
<Compile Include="OneByOneGridTests.cs" />
5353
<Compile Include="RoverControllerTests.cs" />
5454
<Compile Include="RoverTests.cs" />
55+
<Compile Include="UnitVectorTests.cs" />
5556
</ItemGroup>
5657
<ItemGroup>
5758
<ProjectReference Include="..\Core\MarsRoverKata.Core.csproj">

0 commit comments

Comments
 (0)