Skip to content

Instantly share code, notes, and snippets.

@msell
Created February 1, 2012 02:20
Show Gist options
  • Save msell/1714650 to your computer and use it in GitHub Desktop.
Save msell/1714650 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using NUnit.Framework;
namespace Rover.Tests
{
[TestFixture]
public class RoverTests
{
Rover rover;
Grid grid;
[SetUp]
public void setup()
{
rover = new Rover(new Point(0, 0), 'N');
grid = new Grid(-10,-10,10, 10);
}
[Test]
public void rover_can_move_forward_once()
{
var startingPoint = new Point(0, 0);
rover.Move("F");
var expectedFinishPoint = new Point(0, 1);
Assert.That(expectedFinishPoint, Is.EqualTo(rover.Position));
}
[Test]
public void rover_start_orientation()
{
rover.Move("L");
Assert.That('W', Is.EqualTo(rover.Facing));
}
[Test]
public void rover_can_accept_multiple_commands()
{
rover.Move("F,L");
Assert.That(new Point(0, 1), Is.EqualTo(rover.Position));
Assert.That('W', Is.EqualTo(rover.Facing));
}
[Test]
public void rover_moving_after_turning()
{
rover.Move("L,F");
Assert.That(new Point(-1, 0), Is.EqualTo(rover.Position));
Assert.That('W', Is.EqualTo(rover.Facing));
}
[Test]
public void make_a_lotta_differnt_movments()
{
rover.Move("B,B,B,B,R,F");
Assert.That(rover.Position,Is.EqualTo(new Point(1,-4)));
}
[Test]
public void rover_wraps_around_grid()
{
grid = new Grid(-1,-1,1,1);
rover.Move("F,F");
Assert.That(rover.Position, Is.EqualTo(new Point(0,-1)));
}
}
public class Grid
{
public Grid(int minX, int minY, int maxX, int maxY)
{
Min_x = minX;
Min_y = minY;
Max_x = maxX;
Max_y = maxY;
}
public int Min_x { get; set; }
public int Min_y { get; set; }
public int Max_x { get; set; }
public int Max_y { get; set; }
}
}
namespace Rover
{
public class Rover
{
public Rover(Point startingPoint, Char facing)
{
_postion = startingPoint;
Facing = facing;
}
public Point Position { get { return _postion; } }
public Char Facing { get; set; }
Point _postion;
public void Move(string value)
{
foreach (char command in value)
{
SetOrientation(command);
if (command == 'F')
{
MoveForward();
}
if(command == 'B')
{
MoveBackwards();
}
}
}
void MoveBackwards()
{
switch (Facing)
{
case 'N':
_postion.Y--;
break;
case 'E':
_postion.X--;
break;
case 'W':
_postion.X++;
break;
case 'S':
_postion.Y++;
break;
}
}
void MoveForward()
{
switch (Facing)
{
case 'N':
_postion.Y++;
break;
case 'E':
_postion.X++;
break;
case 'W':
_postion.X--;
break;
case 'S':
_postion.Y--;
break;
}
}
void SetOrientation(char command)
{
if (command == 'L')
{
switch (Facing)
{
case 'N':
Facing = 'W';
break;
case 'E':
Facing = 'N';
break;
case 'W':
Facing = 'S';
break;
case 'S':
Facing = 'E';
break;
}
}
else if (command == 'R')
{
switch (Facing)
{
case 'N':
Facing = 'E';
break;
case 'E':
Facing = 'S';
break;
case 'W':
Facing = 'N';
break;
case 'S':
Facing = 'W';
break;
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment