forked from rubendelafuente-aily/Technical_Assessment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbadCode.cs
73 lines (65 loc) · 1.62 KB
/
badCode.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
using System;
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(string commands)
{
foreach (char command in commands)
{
if (command == 'L')
{
TurnLeft();
}
else if (command == 'R')
{
TurnRight();
}
else if (command == 'M')
{
MoveForward();
}
}
}
private void TurnLeft()
{
if (Direction == 'N') Direction = 'W';
else if (Direction == 'W') Direction = 'S';
else if (Direction == 'S') Direction = 'E';
else if (Direction == 'E') Direction = 'N';
}
private void TurnRight()
{
if (Direction == 'N') Direction = 'E';
else if (Direction == 'E') Direction = 'S';
else if (Direction == 'S') Direction = 'W';
else if (Direction == 'W') Direction = 'N';
}
private void MoveForward()
{
if (Direction == 'N') Y += 1;
else if (Direction == 'E') X += 1;
else if (Direction == 'S') Y -= 1;
else if (Direction == 'W') X -= 1;
}
public void PrintPosition()
{
Console.WriteLine($"Rover Position: {X}, {Y}, {Direction}");
}
}
public class Program
{
public static void Main(string[] args)
{
Rover rover = new Rover(0, 0, 'N');
rover.Move("LMLMLMLMM");
rover.PrintPosition();
}
}