private static bool IsValidPosition(int[,] fieldGrid, Position currentPosition) { int rows = fieldGrid.GetLength(0); int cols = fieldGrid.GetLength(1); bool isRowInRange = (currentPosition.Row <= rows && currentPosition.Row >= 0); bool isColInRange = (currentPosition.Col >= 0 && currentPosition.Col <= cols); bool isCellEmpty = fieldGrid[currentPosition.Row, currentPosition.Col] == 0; return isCellEmpty && (isRowInRange && isColInRange); }
public Playfield(int[,] customLabyrinth, Position customStartPosition) : this(customLabyrinth) { if (customStartPosition.Row >= labyrinthGridRows || customStartPosition.Col >= labyrinthGridCols) { throw new IndexOutOfRangeException( String.Format("The numbers of the custom position rows and cols must not be bigger than rows = {0} and cols = {1}! Your input: row = {0} and col = {1}", Playfield.labyrinthGridRows, Playfield.labyrinthGridCols, customStartPosition.Row, customStartPosition.Col)); } this.PlayerPosition = customStartPosition; }
public static bool IsValidMove(Playfield playfield, Direction nextDirection) { int playerCurrentRow = playfield.PlayerPosition.Row; int playerCurrentCol = playfield.PlayerPosition.Col; Position playerCurrentPosition = new Position(playerCurrentRow, playerCurrentCol); if (playerCurrentPosition.IsWinner()) { return false; } playerCurrentPosition.MoveAtDirection(nextDirection); int[,] fieldGrid = playfield.LabyrinthGrid; return IsValidPosition(fieldGrid, playerCurrentPosition); }
/// <summary> /// Ensure clear escaping path from player start position /// </summary> private void EnsureClearPath() { Direction nextDirection = new Direction(); Position currentPosition = new Position(); while (!currentPosition.IsWinner()) { int randomNumber = randomNumberGenerator.Next(-1, 4); nextDirection = (Direction)(randomNumber); if (!MovesChecker.IsValidMove(this, nextDirection)) { currentPosition.MoveAtDirection(nextDirection); this.LabyrinthGrid[currentPosition.Row, currentPosition.Col] = 0; } } }