public void TestPossibleDirectionGetNewDirection()
 {
     Direction current = new Direction(1, 1);
     Direction next = PossibleDirections.GetNextPossibleDirection(current);
     Assert.AreEqual(1, next.X);
     Assert.AreEqual(0, next.Y);
 }
        /// <summary>
        /// Assign cells in a matrix
        /// </summary>
        /// <param name="matrix">Matrix, which cells are going to be assigned</param>
        /// <param name="currentValue">Initial value to assign the first unassigned cell</param>
        /// <param name="cell">Initial cell from which start the assigning</param>
        /// <param name="direction">Initial direction to traverse the matrix</param>
        /// <exception cref="ArgumentNullException">
        /// If there is not initialized input parameter</exception>
        /// <exception cref="ArgumentException">
        /// If we try to assign a cell with non-positive value</exception>
        /// <returns>Value of last assigned cell</returns>
        public static int FillMatrix(int[,] matrix, int currentValue, Cell cell, Direction direction)
        {
            if (matrix == null || cell == null || direction == null)
            {
                throw new ArgumentNullException(
                    "Invalid input! You cannot fill matrix using empty (null) parameter.");
            }

            if (currentValue < 1)
            {
                throw new ArgumentException(
                    "Invalid input! You cannot fill matrix with non-positive number.");
            }

            matrix[cell.Row, cell.Column] = currentValue;
            while (IsPath(matrix, cell))
            {
                direction = FindDirection(matrix, cell, direction);
                cell.Row += direction.X;
                cell.Column += direction.Y;
                currentValue++;
                matrix[cell.Row, cell.Column] = currentValue;
            }

            return currentValue;
        }
Exemple #3
0
        private Direction GetClockwiseRotatedDirection(Direction direction)
        {
            switch (direction)
            {
                case Direction.East:
                    {
                        return Direction.SouthEast;
                    }

                case Direction.SouthEast:
                    {
                        return Direction.South;
                    }

                case Direction.South:
                    {
                        return Direction.SouthWest;
                    }

                case Direction.SouthWest:
                    {
                        return Direction.West;
                    }

                case Direction.West:
                    {
                        return Direction.NorthWest;
                    }

                case Direction.NorthWest:
                    {
                        return Direction.North;
                    }

                case Direction.North:
                    {
                        return Direction.NorthEast;
                    }

                case Direction.NorthEast:
                    {
                        return Direction.East;
                    }

                default:
                    {
                        throw new ArgumentOutOfRangeException("Invalid direction.");
                    }
            }
        }
        /// <summary>
        /// Generate next possible direction to traverse
        /// </summary>
        /// <param name="direction">Current direction to traverse</param>
        /// <returns>Next possible direction to traverse</returns>
        public static Direction GetNextPossibleDirection(Direction direction)
        {
            Direction[] possibleDirections = Generate();
            int index = 0;
            for (int count = 0; count < possibleDirections.Length; count++)
            {
                bool foundDirection =
                    possibleDirections[count].X == direction.X &&
                    possibleDirections[count].Y == direction.Y;
                if (foundDirection && count < possibleDirections.Length - 1)
                {
                    index = count + 1;
                    break;
                }
                else if (foundDirection && count == possibleDirections.Length - 1)
                {
                    index = 0;
                }
            }

            return possibleDirections[index];
        }
        private Position GetNextFreeNeighbourPosition(Position startPosition, ref Direction direction)
        {
            Direction currentDirection = direction;

            for (int i = 0; i < DirectionsCount; i++)
            {
                Position nextPosition = this.GetNeibhourPosition(startPosition, currentDirection);

                if ((nextPosition.X >= this.Width || nextPosition.X < 0) ||
                    (nextPosition.Y >= this.Width || nextPosition.Y < 0))
                {
                    currentDirection = this.GetNextDirection(currentDirection);
                    continue;
                }

                if (this[nextPosition] == 0)
                {
                    direction = currentDirection;
                    return nextPosition;
                }

                currentDirection = this.GetNextDirection(currentDirection);
            }

            return null;
        }
        private Direction GetNextDirection(Direction direction)
        {
            if (direction == Direction.Right)
            {
                return Direction.DownRight;
            }

            Direction nextDirection = direction + 1;
            return nextDirection;
        }
 private Position GetNeibhourPosition(Position position, Direction direction)
 {
     return new Position(
         position.X + this.directionX[(int)direction],
         position.Y + this.directionY[(int)direction]);
 }
Exemple #8
0
        private Direction GetFirstClockwiseEmptyCell(Position position, Direction direction)
        {
            Position neighbor = this.GetNeighborCell(position, direction);
            Direction neighborDirection = direction;
            while (!this.IsInsideBoundaries(neighbor) || !this.IsEmptyCell(neighbor))
            {
                neighborDirection = this.GetClockwiseRotatedDirection(neighborDirection);
                neighbor = this.GetNeighborCell(position, neighborDirection);
            }

            return neighborDirection;
        }
Exemple #9
0
        private Position GetNeighborCell(Position position, Direction direction)
        {
            Position neighbor = new Position();
            Position directionCoordinates = this.coordinatesOfDirection[direction];
            neighbor.Row = position.Row + directionCoordinates.Row;
            neighbor.Column = position.Column + directionCoordinates.Column;

            return neighbor;
        }
Exemple #10
0
 public Cursor(int startRow, int startCol, Direction dir)
 {
     this.Row = startRow;
     this.Column = startCol;
     this.Direction = dir;
 }
 public void TestDirectionZero()
 {
     Direction direction = new Direction(0, 0);
     Assert.AreEqual(0, direction.X);
     Assert.AreEqual(0, direction.Y);
 }
 public void TestDirection()
 {
     Direction direction = new Direction(-3, 3);
     Assert.AreEqual(-3, direction.X);
     Assert.AreEqual(3, direction.Y);
 }
        /// <summary>
        /// Find next possible direction to continue the traversal of a matrix
        /// </summary>
        /// <param name="matrix">Matrix to be traversed</param>
        /// <param name="cell">Cell from which is searching direction</param>
        /// <param name="direction">Current direction which could be changed</param>
        /// <returns>Next direction to traverse the matrix</returns>
        private static Direction FindDirection(int[,] matrix, Cell cell, Direction direction)
        {
            int size = matrix.GetLength(0);
            bool isOuterRow = cell.Row + direction.X >= size || cell.Row + direction.X < 0;
            bool isOuterColumn = cell.Column + direction.Y >= size || cell.Column + direction.Y < 0;
            while (isOuterRow || isOuterColumn ||
                matrix[cell.Row + direction.X, cell.Column + direction.Y] != 0)
            {
                direction = PossibleDirections.GetNextPossibleDirection(direction);
                isOuterRow = cell.Row + direction.X >= size || cell.Row + direction.X < 0;
                isOuterColumn = cell.Column + direction.Y >= size || cell.Column + direction.Y < 0;
            }

            return direction;
        }
        /// <summary>
        /// Change all directions which are leading outside the matrix
        /// </summary>
        /// <param name="possibleDirections">Current possible directions</param>
        /// <param name="matrix">Matrix to be traversed</param>
        /// <param name="cell">Cell from which is searching direction</param>
        private static void UpdatePossibleDirections(Direction[] possibleDirections, int[,] matrix, Cell cell)
        {
            for (int count = 0; count < possibleDirections.Length; count++)
            {
                int changedRow = cell.Row + possibleDirections[count].X;
                if (changedRow >= matrix.GetLength(0) || changedRow < 0)
                {
                    possibleDirections[count].X = 0;
                }

                int changedColumn = cell.Column + possibleDirections[count].Y;
                if (changedColumn >= matrix.GetLength(0) || changedColumn < 0)
                {
                    possibleDirections[count].Y = 0;
                }
            }
        }