Untitled
unknown
csharp
4 years ago
2.7 kB
11
Indexable
using System;
namespace MarsRover
{
public class Rover
{
public int X { get; set; }
public int Y { get; set; }
public char Direction { get; set; }
public Rover(int x, int y, char direction)
{
X = x;
Y = y;
Direction = direction;
}
public void Move(char movement)
{
switch (movement)
{
case 'L':
TurnLeft();
break;
case 'R':
TurnRight();
break;
case 'M':
MoveForward();
break;
default:
throw new ArgumentOutOfRangeException();
}
}
private void MoveForward()
{
switch (Direction)
{
case 'N':
if (Y + 1 <= Grid.Height)
Y += 1;
break;
case 'E':
if (X + 1 <= Grid.Width)
X += 1;
break;
case 'S':
if (Y - 1 >= 0)
Y -= 1;
break;
case 'W':
if (X - 1 >= 0)
X -= 1;
break;
default:
throw new ArgumentOutOfRangeException();
}
}
private void TurnLeft()
{
switch (Direction)
{
case 'N':
Direction = 'W';
break;
case 'W':
Direction = 'S';
break;
case 'S':
Direction = 'E';
break;
case 'E':
Direction = 'N';
break;
default:
throw new ArgumentOutOfRangeException();
}
}
private void TurnRight()
{
switch (Direction)
{
case 'N':
Direction = 'E';
break;
case 'E':
Direction = 'S';
break;
case 'S':
Direction = 'W';
break;
case 'W':
Direction = 'N';
break;
default:
throw new ArgumentOutOfRangeException();
}
}
}
}Editor is loading...