Ejemplo n.º 1
0
        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>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);
        }