Example #1
0
        public void ParameterlessRowsConstructorTest()
        {
            var expectedRowPosition = 0;

            Position position = new Position();
            Assert.AreEqual(expectedRowPosition, position.Row);
        }
        public int this[Position position]
        {
            get
            {
                if (position.X < 0 || position.X >= this.Width)
                {
                    throw new IndexOutOfRangeException("Defined row is out of range.");
                }

                if (position.Y < 0 || position.Y >= this.Width)
                {
                    throw new IndexOutOfRangeException("Defined column is out of range.");
                }

                return this.matrix[position.X, position.Y];
            }

            private set
            {
                if (position.X < 0 || position.X >= this.Width)
                {
                    throw new IndexOutOfRangeException("Defined row is out of range.");
                }

                if (position.Y < 0 || position.Y >= this.Width)
                {
                    throw new IndexOutOfRangeException("Defined column is out of range.");
                }

                this.matrix[position.X, position.Y] = value;
            }
        }
Example #3
0
        public void PositionToStringTest()
        {
            var expectedToStringResult = "43 -31";

            Position position = new Position(43, -31);
            Assert.AreEqual(expectedToStringResult, position.ToString());
        }
Example #4
0
        public void FiveRowsConstructorTest()
        {
            var expectedRowPosition = 15;

            Position position = new Position(15, 3);
            Assert.AreEqual(expectedRowPosition, position.Row);
        }
Example #5
0
        public void ParameterlessColsConstructorTest()
        {
            var expectedColPosition = 0;

            Position position = new Position();
            Assert.AreEqual(expectedColPosition, position.Column);
        }
Example #6
0
        public void FiveColsConstructorTest()
        {
            var expectedColPosition = 12;

            Position position = new Position(3, 12);
            Assert.AreEqual(expectedColPosition, position.Column);
        }
Example #7
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;
        }
        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 Position GetNeibhourPosition(Position position, Direction direction)
 {
     return new Position(
         position.X + this.directionX[(int)direction],
         position.Y + this.directionY[(int)direction]);
 }
        private Position FindSmalestEmptyCell()
        {
            ///An empty cell at the smallest possible row and as close as possible to the start of this row

            for (int x = 0; x < this.Width; x++)
            {
                for (int y = 0; y < this.Width; y++)
                {
                    Position currentPosition = new Position(x, y);
                    if (this[currentPosition] == 0)
                    {
                        return currentPosition;
                    }
                }
            }

            return null;
        }
        private void FillRotatingWalkInMatrix()
        {
            int nextNumber = 1;
            Direction direction = Direction.DownRight;
            Position currentPosition = new Position(0, 0);

            while (true)
            {
                this[currentPosition] = nextNumber;
                nextNumber++;

                Position nextFreeNeighbourPosition = this.GetNextFreeNeighbourPosition(currentPosition, ref direction);
                if (nextFreeNeighbourPosition == null)
                {
                    direction = Direction.DownRight;
                    Position smalestEmptyCellPosition = this.FindSmalestEmptyCell();
                    if (smalestEmptyCellPosition == null)
                    {
                        ///no empty cell is left in the matrix
                        break;
                    }

                    this[smalestEmptyCellPosition] = nextNumber;
                    currentPosition = smalestEmptyCellPosition;
                }
                else
                {
                    currentPosition = nextFreeNeighbourPosition;
                }
            }

            return;
        }
        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            for (int x = 0; x < this.Width; x++)
            {
                for (int y = 0; y < this.Width; y++)
                {
                    Position currentPosition = new Position(x, y);
                    sb.AppendFormat("{0,3}  ", this[currentPosition]);
                }

                sb.AppendLine();
            }

            return sb.ToString();
        }
Example #13
0
        private bool TryFindEmptyCell(out Position emptyCell)
        {
            emptyCell = new Position(0, 0);
            for (int row = 0; row < this.Dimension; row++)
            {
                for (int col = 0; col < this.Dimension; col++)
                {
                    if (this.matrix[row, col] == Matrix.EmptyCell)
                    {
                        emptyCell.Row = row;
                        emptyCell.Column = col;
                        return true;
                    }
                }
            }

            return false;
        }
Example #14
0
 private void VisitCell(Position position)
 {
     this.matrix[position.Row, position.Column] = this.nextCellValue;
     this.nextCellValue++;
 }
Example #15
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;
        }
Example #16
0
 private bool IsInsideBoundaries(Position position)
 {
     return position.Row < this.Dimension
         && position.Column < this.Dimension
         && position.Row >= 0
         && position.Column >= 0;
 }
Example #17
0
        private bool HasEmptyNeighbour(Position position)
        {
            foreach (var direction in this.coordinatesOfDirection)
            {
                Position neighbor = this.GetNeighborCell(position, direction.Key);
                if (this.IsInsideBoundaries(neighbor) && this.IsEmptyCell(neighbor))
                {
                    return true;
                }
            }

            return false;
        }
Example #18
0
 private bool IsEmptyCell(Position position)
 {
     return this.matrix[position.Row, position.Column] == Matrix.EmptyCell;
 }