Пример #1
0
        private int Negamax(IPosition position)
        {
            if (position.NbMoves == position.Width * position.Height) // check for draw game
            {
                return(0);
            }

            for (int x = 0; x < position.Width; x++) // check if current player can win next move
            {
                if (position.CanPlay(x) && position.IsWinningMove(x))
                {
                    return((position.Width * position.Height + 1 - position.NbMoves) / 2);
                }
            }

            int bestScore = -position.Width * position.Height; // init the best possible score with a lower bound of score.

            for (int x = 0; x < position.Width; x++)           // compute the score of all possible next move and keep the best one
            {
                if (position.CanPlay(x))
                {
                    IPosition newPosition = position.Clone();
                    newPosition.Play(x);               // It's opponent turn in P2 position after current player plays x column.
                    int score = -Negamax(newPosition); // If current player plays col x, his score will be the opposite of opponent's score after playing col x
                    if (score > bestScore)
                    {
                        bestScore = score; // keep track of best possible score so far.
                    }
                }
            }

            return(bestScore);
        }
Пример #2
0
 private void CheckCurrentPosition()
 {
     if (CurrentPosition.X.IsOutOfRange(_plato.XLength) || CurrentPosition.Y.IsOutOfRange(_plato.YLength))
     {
         CurrentPosition = _startPosition.Clone();//Set rover the start position for another try.
         throw new ArgumentOutOfRangeException("The rover drives out of range!!");
     }
 }
Пример #3
0
        private int Negamax(IPosition position, int alpha, int beta)
        {
            if (position.NbMoves == position.Width * position.Height) // check for draw game
            {
                return(0);
            }

            for (int x = 0; x < position.Width; x++) // check if current player can win next move
            {
                if (position.CanPlay(x) && position.IsWinningMove(x))
                {
                    return((position.Width * position.Height + 1 - position.NbMoves) / 2);
                }
            }

            int max = (position.Width * position.Height - 1 - position.NbMoves) / 2;    // upper bound of our score as we cannot win immediately

            if (beta > max)
            {
                beta = max;                     // there is no need to keep beta above our max possible score.
                if (alpha >= beta)
                {
                    return(beta);  // prune the exploration if the [alpha;beta] window is empty.
                }
            }

            for (int x = 0; x < position.Width; x++) // compute the score of all possible next move and keep the best one
            {
                if (position.CanPlay(x))
                {
                    IPosition newPosition = position.Clone();
                    newPosition.Play(x);                              // It's opponent turn in P2 position after current player plays x column.
                    int score = -Negamax(newPosition, -beta, -alpha); // explore opponent's score within [-beta;-alpha] windows:
                                                                      // no need to have good precision for score better than beta (opponent's score worse than -beta)
                                                                      // no need to check for score worse than alpha (opponent's score worse better than -alpha)
                    if (score >= beta)
                    {
                        return(score);  // prune the exploration if we find a possible move better than what we were looking for.
                    }
                    if (score > alpha)
                    {
                        alpha = score; // reduce the [alpha;beta] window for next exploration, as we only
                    }
                    // need to search for a position that is better than the best so far.
                }
            }

            return(alpha);
        }
Пример #4
0
        public void Position_Clone_Should_CreateNewInstance_And_SetPropertiesCorrect()
        {
            IFactory  factory  = new Factory();
            IPosition position = factory.CreateInputObject <IPosition>();

            position.X         = 1;
            position.Y         = 2;
            position.Direction = Direction.North;

            IPosition newPosition = position.Clone();

            Assert.AreNotSame(position, newPosition);//Should Create New Object.
            Assert.AreEqual(position.X, newPosition.X);
            Assert.AreEqual(position.Y, newPosition.Y);
            Assert.AreEqual(position.Direction, newPosition.Direction);
        }
Пример #5
0
        protected override IPosition BoundaryReachPositionRecalculator(IPosition newPosition, IDirection direction, int xMaxValue, int yMaxValue)
        {
            var recalculatedPosition = newPosition.Clone();

            if (newPosition.Coordinate.X >= xMaxValue && direction.XSpeed == 1)
            {
                recalculatedPosition.Coordinate.X = 0;
            }
            if (newPosition.Coordinate.X <= 0 && direction.XSpeed == -1)
            {
                recalculatedPosition.Coordinate.X = xMaxValue;
            }
            if (newPosition.Coordinate.Y <= 0 && direction.YSpeed == -1)
            {
                recalculatedPosition.Coordinate.Y = yMaxValue;
            }
            if (newPosition.Coordinate.Y >= yMaxValue && direction.YSpeed == 1)
            {
                recalculatedPosition.Coordinate.Y = 0;
            }
            return(recalculatedPosition);
        }
Пример #6
0
        public void Populate(IPosition startPosition, IDirectionSequence directionsInstructions, ICellValueSequence valueProvider)
        {
            if (startPosition == null)
            {
                throw new ArgumentNullException(nameof(startPosition));
            }

            if (directionsInstructions == null)
            {
                throw new ArgumentNullException(nameof(directionsInstructions));
            }

            if (valueProvider == null)
            {
                throw new ArgumentNullException(nameof(valueProvider));
            }

            var position         = startPosition.Clone();
            var direction        = directionsInstructions.GetNextDirection();
            var initialDirection = direction.Clone();

            var matrixSize     = this.matrix.Count;
            var matrixIsFilled = false;

            do
            {
                var nextCellValue = valueProvider.GetNextCellValue();
                this.matrix[position.Row][position.Col].Value         = nextCellValue;
                this.visitedCellPositions[position.Row][position.Col] = true;

                var nextPosition = position.Clone();
                nextPosition = nextPosition.MoveInDirection(direction);

                var nextPositionIsFree = this.CheckIfPositionIsValidToMove(nextPosition, matrixSize);
                if (!nextPositionIsFree)
                {
                    for (int directionChangeCount = 1; directionChangeCount <= directionsInstructions.DirectionSequenceLength; directionChangeCount++)
                    {
                        var nextDirection        = directionsInstructions.GetNextDirection();
                        var adjustedNextPosition = position.Clone();
                        adjustedNextPosition = adjustedNextPosition.MoveInDirection(nextDirection);

                        var adjustedNextPositionIsFree = this.CheckIfPositionIsValidToMove(adjustedNextPosition, matrixSize);
                        if (adjustedNextPositionIsFree)
                        {
                            nextPosition = adjustedNextPosition;
                            direction    = nextDirection;
                            break;
                        }
                        else if (directionChangeCount == directionsInstructions.DirectionSequenceLength)
                        {
                            var positionToJumpTo = this.FindPositionToJumpTo(adjustedNextPosition, matrixSize);
                            if (positionToJumpTo == null)
                            {
                                matrixIsFilled = true;
                                break;
                            }
                            else
                            {
                                nextPosition = positionToJumpTo;
                                direction    = initialDirection.Clone();
                            }
                        }
                    }
                }

                position = nextPosition;
            }while (!matrixIsFilled);
        }
Пример #7
0
 public Rectangle(IPosition position, ISize size)
 {
     Position = position.Clone();
     Size     = size.Clone();
 }
Пример #8
0
 public Driver(IPlato plato, IPosition startPosition)
 {
     _plato          = plato;
     _startPosition  = startPosition;
     CurrentPosition = startPosition.Clone();
 }