Last active
April 2, 2021 13:41
-
-
Save bjornmicallef/3fc913b2bb82c9be2c7b8f1f16de9593 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Minefield | |
{ | |
private readonly (int, int) _gridSize; | |
private readonly List<(int, int)> _minePositions; | |
private readonly (int, int) _exitPosition; | |
private readonly (int, int) _startPosition; | |
private readonly string _startDirection; | |
private readonly string[] _movesList; | |
private (int, int) _currentPosition; | |
private string _currentDirection; | |
public Minefield(string[] setupLines) | |
{ | |
_gridSize = Util.ParseSetupLineInPosition(setupLines[0]); | |
_minePositions = Util.GetMines(setupLines[1]); | |
_exitPosition = Util.ParseSetupLineInPosition(setupLines[2]); | |
_startPosition = Util.ParseSetupLineInPosition(setupLines[3]); | |
_startDirection = setupLines[3].Trim().Split(' ')[2]; | |
_movesList = setupLines.Skip(4).Take(setupLines.Length - 2).ToArray(); | |
} | |
public Result ExecuteMoves() | |
{ | |
var result = Result.TurtleLost; | |
_currentPosition = _startPosition; | |
_currentDirection = _startDirection; | |
foreach (var line in _movesList) | |
{ | |
foreach (var move in line) | |
{ | |
switch (move) | |
{ | |
case 'M': | |
if (result == Result.TurtleLost) | |
result = Move(); | |
else | |
return result; | |
break; | |
case 'R': | |
RotateRight(); break; | |
case 'L': | |
RotateLeft(); break; | |
} | |
} | |
} | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment