예제 #1
0
        private bool CanMoveInDirection(Position position, MoveDirection direction)
        {
            if (direction == MoveDirection.Up &&
                position.Row <= 0)
            {
                return false;
            }

            if (direction == MoveDirection.Down &&
                position.Row >= BOARD_SIZE - 1)
            {
                return false;
            }

            if (direction == MoveDirection.Left &&
                position.Column <= 0)
            {
                return false;
            }

            if (direction == MoveDirection.Right &&
                position.Column >= BOARD_SIZE - 1)
            {
                return false;
            }

            Position newPosition = CalculatePositionWithDirection(position, direction);
            if (this.matrix[newPosition.Row, newPosition.Column] != " ")
            {
                return false;
            }

            return true;
        }
예제 #2
0
 public void PositionConstructorTest()
 {
     int row = 0; // TODO: Initialize to an appropriate value
     int column = 0; // TODO: Initialize to an appropriate value
     Position target = new Position(row, column);
     Assert.Inconclusive("TODO: Implement code to verify target");
 }
예제 #3
0
 public void RowTest()
 {
     int row = 0; // TODO: Initialize to an appropriate value
     int column = 0; // TODO: Initialize to an appropriate value
     Position target = new Position(row, column); // TODO: Initialize to an appropriate value
     int expected = 0; // TODO: Initialize to an appropriate value
     int actual;
     target.Row = expected;
     actual = target.Row;
     Assert.AreEqual(expected, actual);
     Assert.Inconclusive("Verify the correctness of this test method.");
 }
        private void FillAvailableCells()
        {
            int cellValue = 1;

            Position direction = new Position(1, 1);

            while (true)
            {
                int currentRow = this.currentPosition.Row;
                int currentCol = this.currentPosition.Col;

                this.Content[currentRow, currentCol] = cellValue;

                if (!this.IsCellAvailable(this.currentPosition))
                {
                    this.GetNextAvailableCell();
                    currentRow = this.currentPosition.Row;
                    currentCol = this.currentPosition.Col;

                    if (this.IsCellAvailable(this.currentPosition))
                    {
                        cellValue++;
                        this.Content[currentRow, currentCol] = cellValue;
                        direction.SetRowAndCol(1, 1);
                    }
                    else
                    {
                        break;
                    }
                }

                int nextRow = currentRow + direction.Row;
                int nextCol = currentCol + direction.Col;

                while (!this.IsInRange(nextRow) || !this.IsInRange(nextCol))
                {
                    direction = this.GetDirection(direction);

                    nextRow = currentRow + direction.Row;
                    nextCol = currentCol + direction.Col;
                }

                this.currentPosition.SetRowAndCol(nextRow, nextCol);

                cellValue++;
            }
        }
        static bool HasPositionWithZeroValue(int[,] matrix, out Position position)
        {
            for (int row = 0; row < matrix.GetLength(0); row++)
            {
                for (int col = 0; col < matrix.GetLength(1); col++)
                {
                    if (matrix[row, col] == 0)
                    {
                        position = new Position(row, col);
                        return true;
                    }
                }
            }

            position = new Position(0, 0);
            return false;
        }
예제 #6
0
        private Position CalculatePositionWithDirection(Position position, 
            MoveDirection direction)
        {
            Position newPosition = (Position)position.Clone();

            switch (direction)
            {
                case MoveDirection.Down:
                    newPosition.Row++;
                    break;
                case MoveDirection.Left:
                    newPosition.Column--;
                    break;
                case MoveDirection.Right:
                    newPosition.Column++;
                    break;
                case MoveDirection.Up:
                    newPosition.Row--;
                    break;
            }
            return newPosition;
        }
예제 #7
0
 private void TryMovingInDirection(ref bool isMoveValid, Position currentPosition, 
     MoveDirection direction)
 {
     if(CanMoveInDirection(currentPosition, direction))
     {
         MoveInDirection(currentPosition, direction);
         isMoveValid = true;
         moveCount++;
     }
 }
예제 #8
0
        private void MoveInDirection(Position oldPosition, MoveDirection direction)
        {
            Position newPosition = CalculatePositionWithDirection(oldPosition,
                direction);

            if (this.matrix[newPosition.Row, newPosition.Column] == " ")
            {
                string itemToMove = this.matrix[oldPosition.Row, oldPosition.Column];
                this.matrix[newPosition.Row, newPosition.Column] = itemToMove;
                this.matrix[oldPosition.Row, oldPosition.Column] = " ";
                DrawMatrix();
            }
        }
예제 #9
0
        // TODO: Place a comment about this method with reference!
        // http://www.cs.bham.ac.uk/~mdr/teaching/modules04/java2/TilesSolvability.html
        private static bool IsSolvable(string[,] gameField)
        {
            int[] numbersInOneRow = new int[gameField.Length];
            int index = 0;
            Position blankPosition = null;

            for (int i = 0; i < gameField.GetLength(0); i++)
            {
                for (int j = 0; j < gameField.GetLength(1); j++)
                {
                    if (gameField[i, j] == " ")
                    {
                        numbersInOneRow[index] = 0;
                        blankPosition = new Position(i, j);
                    }
                    else
                    {
                        numbersInOneRow[index] = int.Parse(gameField[i, j]);
                    }

                    index++;
                }
            }

            int numberOfInversions = 0;

            for (int i = 0; i < numbersInOneRow.Length; i++)
            {
                int number = numbersInOneRow[i];

                if (number > 1)
                {
                    for (int n = i + 1; n < numbersInOneRow.Length; n++)
                    {
                        if (numbersInOneRow[n] < number)
                        {
                            numberOfInversions++;
                        }
                    }
                }
            }

            bool isFieldWidthEven = gameField.GetLength(1) % 2 == 0;
            bool isNumberOfInversionsEven = numberOfInversions % 2 == 0;
            bool isBlankOnOddRowFromBottom =
                (gameField.GetLength(0) - 1 - blankPosition.Row) % 2 == 0;

            bool isSolvable = ((!isFieldWidthEven) && isNumberOfInversionsEven) ||
                (isFieldWidthEven && (isBlankOnOddRowFromBottom == isNumberOfInversionsEven));

            return isSolvable;
        }
예제 #10
0
        private static Position findPrazno(string[,] matrica)
        {
            Position result = null;
            // TODO: Fix the "magic number"!
            for (int i = 0; i < 4; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    if (matrica[i, j] == " ")
                    {
                        result = new Position(i, j);
                    }
                }
            }

            return result;
        }
        static void Main(string[] args)
        {
            //Console.WriteLine( "Enter a positive number " );
            //string input = Console.ReadLine(  );
            //int n = 0;
            //while ( !int.TryParse( input, out n ) || n < 0 || n > 100 )
            //{
            //    Console.WriteLine( "You haven't entered a correct positive number" );
            //    input = Console.ReadLine(  );
            //}
            int n = 6;
            int[,] matrix = new int[n, n];
            int step = n;
            int numberInCell = 1;
            Position start = new Position(0, 0);
            Position d = new Position(1, 1);

            WhileLoop(matrix, ref numberInCell, start, d);

            Position positionWithValueZero = new Position(0, 0);

            bool hasZeroInMatrix = HasPositionWithZeroValue(matrix, out positionWithValueZero);

            if (hasZeroInMatrix)
            {
                numberInCell += 1;
                WhileLoop(matrix, ref numberInCell, positionWithValueZero, d);
            }

            Print(matrix);
        }
        private static void WhileLoop(int[,] matrica, ref int numberInCell, Position position, Position d)
        {
            int n = matrica.GetLength(0);
            int row = position.Row;
            int col = position.Column;
            int dx = d.Row;
            int dy = d.Column;

            while (true)
            { //malko e kofti tova uslovie, no break-a raboti 100% : )
                matrica[row, col] = numberInCell;

                if (!HasValidPosition(matrica, row, col))
                {
                    break; // prekusvame ako sme se zadunili
                }

                if (row + dx >= n || row + dx < 0 || col + dy >= n || col + dy < 0 || matrica[row + dx, col + dy] != 0)
                {

                    while ((row + dx >= n || row + dx < 0 || col + dy >= n || col + dy < 0 || matrica[row + dx, col + dy] != 0))
                    {
                        ChangeDirection(ref dx, ref dy);
                    }
                }

                row += dx;
                col += dy;
                numberInCell++;
            }
        }
        private bool IsCellAvailable(Position position)
        {
            for (int dirIndex = 0; dirIndex < PossibleDirections; dirIndex++)
            {
                int nextRow = position.Row + this.directionRow[dirIndex];

                if (!this.IsInRange(nextRow))
                {
                    this.directionRow[dirIndex] = 0;
                }

                int nextCol = position.Col + this.directionCol[dirIndex];

                if (!this.IsInRange(nextCol))
                {
                    this.directionCol[dirIndex] = 0;
                }
            }

            for (int dirIndex = 0; dirIndex < PossibleDirections; dirIndex++)
            {
                int nextRow = position.Row + this.directionRow[dirIndex];
                int nextCol = position.Col + this.directionCol[dirIndex];

                if (this.Content[nextRow, nextCol] == 0)
                {
                    return true;
                }
            }

            return false;
        }
        private Position GetDirection(Position prevDirection)
        {
            int currentDirection = 0;

            for (int dirIndex = 0; dirIndex < PossibleDirections; dirIndex++)
            {
                if (this.directionRow[dirIndex] == prevDirection.Row && this.directionCol[dirIndex] == prevDirection.Col)
                {
                    currentDirection = dirIndex;
                    break;
                }
            }

            if (currentDirection == PossibleDirections - 1)
            {
                return new Position(this.directionRow[0], this.directionCol[0]);
            }

            return new Position(
                this.directionRow[currentDirection + 1],
                this.directionCol[currentDirection + 1]);
        }