Created
April 2, 2021 13:42
-
-
Save bjornmicallef/86a8cf7428b759ddc242c65ef4f3407f 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
private Result Move() | |
{ | |
// update current position values and compare with mines list & exit in here | |
switch (_currentDirection) | |
{ | |
case "N": | |
_currentPosition.Item2--; break; | |
case "S": | |
_currentPosition.Item2++; break; | |
case "E": | |
_currentPosition.Item1++; break; | |
case "W": | |
_currentPosition.Item1--; break; | |
} | |
var result = CheckIfStillInGrid(); | |
if (result == Result.TurtleLost) result = CheckForMine(); | |
if (result == Result.TurtleLost) result = CheckForExit(); | |
return result; | |
} | |
private void RotateRight() | |
{ | |
_currentDirection = _currentDirection switch | |
{ | |
"N" => "E", | |
"S" => "W", | |
"E" => "S", | |
"W" => "N", | |
_ => throw new System.NotImplementedException(), | |
}; | |
} | |
private void RotateLeft() | |
{ | |
_currentDirection = _currentDirection switch | |
{ | |
"N" => "W", | |
"S" => "E", | |
"E" => "N", | |
"W" => "S", | |
_ => throw new System.NotImplementedException(), | |
}; | |
} | |
private Result CheckIfStillInGrid() | |
{ | |
if (_currentPosition.Item1 < 0 || | |
_currentPosition.Item2 < 0 || | |
_currentPosition.Item1 >= _gridSize.Item1 || | |
_currentPosition.Item2 >= _gridSize.Item2) | |
return Result.TurtleOutside; | |
return Result.TurtleLost; | |
} | |
private Result CheckForMine() | |
{ | |
foreach (var mine in _minePositions) | |
{ | |
if (_currentPosition.Item1 == mine.Item1 && _currentPosition.Item2 == mine.Item2) | |
return Result.TurtleMine; | |
} | |
return Result.TurtleLost; | |
} | |
private Result CheckForExit() | |
{ | |
if (_currentPosition.Item1 == _exitPosition.Item1 && _currentPosition.Item2 == _exitPosition.Item2) | |
return Result.TurtleExit; | |
return Result.TurtleLost; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment