/// <summary>Randomizes the given matrix.</summary> /// <exception cref="ArgumentNullException">Thrown when one or more required arguments are null.</exception> /// <param name="matrix" type="int[,]">The matrix.</param> /// <returns>A Point.</returns> public Point Randomize(int[,] matrix) { if (matrix == null) { throw new ArgumentNullException("The matrix cannot be null or empty"); } Point emptyPoint = new Point(matrix.GetLength(0) - 1, matrix.GetLength(1) - 1); if (matrix.GetLength(0) <= 1 && matrix.GetLength(1) <= 1) { return(emptyPoint); } int randomizeMoves = RandomGenerator.GetRandomNumber(CommonConstants.MIN_MOOVES_RANDOM_NUMBER, CommonConstants.MAX_MOOVES_RANDOM_NUMBER); for (int i = 0; i < randomizeMoves; i++) { int randomDirection = RandomGenerator.GetRandomNumber(MAX_RANDOM_DIRECTION_INDEX); Point direction = Directions.GetDirection[randomDirection]; Point newEmptyPoint = new Point(emptyPoint.Row + direction.Row, emptyPoint.Col + direction.Col); if (OutOfMatrixChecker.CheckIfOutOfMatrix(newEmptyPoint, matrix.GetLength(0))) { i--; continue; } else { EmptyCellMover.MoveEmptyCell(emptyPoint, newEmptyPoint, matrix); } } return(emptyPoint); }
private void ExecuteMooveCommand(ref int number, int[,] currentMatrix, Point emptyPoint) { Point[] directions = Directions.GetDirection; int directionsCount = directions.GetLength(0); int matrixLength = currentMatrix.GetLength(0); Point newPoint = new Point(0, 0); for (int i = 0; i <= directionsCount; i++) { if (i == matrix.GetLength(0)) { renderer.PrintLine(CommonConstants.INVALID_MOVE); break; } newPoint.Row = emptyPoint.Row + directions[i].Row; newPoint.Col = emptyPoint.Col + directions[i].Col; if (OutOfMatrixChecker.CheckIfOutOfMatrix(newPoint, matrixLength)) { continue; } if (currentMatrix[newPoint.Row, newPoint.Col] == number) { EmptyCellMover.MoveEmptyCell(emptyPoint, new Point(newPoint.Row, newPoint.Col), currentMatrix); this.IsPlayerMoved = true; break; } } }
/// <summary>Move empty cell.</summary> /// <exception cref="ArgumentNullException"> Thrown when one or more required arguments are /// null.</exception> /// <exception cref="IndexOutOfRangeException">Thrown when the index is outside the required /// range.</exception> /// <param name="emptyPoint" type="Point">The empty point.</param> /// <param name="newPoint" type="Point">The new point.</param> /// <param name="matrix" type="int[,]">The matrix.</param> public static void MoveEmptyCell(Point emptyPoint, Point newPoint, int[,] matrix) { if (matrix == null) { throw new ArgumentNullException("The matrix cannot be null"); } if (emptyPoint == null) { throw new ArgumentNullException("The empty point position cannot be null"); } if (newPoint == null) { throw new ArgumentNullException("The new empty point position cannot be null"); } int matrixSize = matrix.GetLength(0); bool isNewPointInRange = OutOfMatrixChecker.CheckIfOutOfMatrix(newPoint, matrixSize); if (isNewPointInRange) { throw new IndexOutOfRangeException("The new empty point position is outside the matrix"); } bool isEmptyPointInRange = OutOfMatrixChecker.CheckIfOutOfMatrix(emptyPoint, matrixSize); if (isEmptyPointInRange) { throw new IndexOutOfRangeException("The new empty point position is outside the matrix"); } int swapValue = matrix[newPoint.Row, newPoint.Col]; matrix[newPoint.Row, newPoint.Col] = CommonConstants.INITIAL_EMPTY_CELL; matrix[emptyPoint.Row, emptyPoint.Col] = swapValue; emptyPoint.Row = newPoint.Row; emptyPoint.Col = newPoint.Col; }