Exemple #1
0
        /// <summary>
        /// Performs ability's update
        /// </summary>
        /// <param name="context">The environment context.</param>
        /// <returns>The direction to walk.</returns>
        public WalkDirection Walk(IMazeEnvironmentContext context)
        {
            WalkDirection randomDirection;

            // Look for a direction to walk.
            do
            {
                randomDirection = (WalkDirection)m_random.Next(0, 4);
            }while (!context.CanWalkTo(randomDirection));

            // Return direction.
            return(randomDirection);
        }
Exemple #2
0
        private WalkDirection?GetResult(IMazeEnvironmentContext ctx, bool onlyNotVisited)
        {
            if (ctx.CanWalkTo(WalkDirection.Up) && (!onlyNotVisited || !HasBeenVisited(WalkDirection.Up)))
            {
                return(WalkDirection.Up);
            }

            if (ctx.CanWalkTo(WalkDirection.Right) && (!onlyNotVisited || !HasBeenVisited(WalkDirection.Right)))
            {
                return(WalkDirection.Right);
            }

            if (ctx.CanWalkTo(WalkDirection.Down) && (!onlyNotVisited || !HasBeenVisited(WalkDirection.Down)))
            {
                return(WalkDirection.Down);
            }

            if (ctx.CanWalkTo(WalkDirection.Left) && (!onlyNotVisited || !HasBeenVisited(WalkDirection.Left)))
            {
                return(WalkDirection.Left);
            }

            return(null);
        }
Exemple #3
0
        /// <summary>
        /// Choose the walk direction.
        /// </summary>
        /// <param name="context">The maze environment's context.</param>
        /// <returns>
        /// The walk diretion choosen.
        /// </returns>
        public WalkDirection Walk(IMazeEnvironmentContext context)
        {
            SetVisitedCell();

            var result = GetResult(context, true);

            if (!result.HasValue)
            {
                var aroundVisitedCells = new Dictionary <MazeCell, long>();
                var s = m_myCell;


                var aroundCell = GetVisitedAroundCell(WalkDirection.Up);

                if (context.CanWalkTo(WalkDirection.Up))
                {
                    if (aroundCell.Key == null)
                    {
                        return(PrepareResult(WalkDirection.Up));
                    }

                    if (aroundCell.Key != null)
                    {
                        aroundVisitedCells.Add(aroundCell.Key, aroundCell.Value);
                    }
                }

                if (context.CanWalkTo(WalkDirection.Right))
                {
                    aroundCell = GetVisitedAroundCell(WalkDirection.Right);

                    if (aroundCell.Key == null)
                    {
                        return(PrepareResult(WalkDirection.Right));
                    }

                    if (aroundCell.Key != null)
                    {
                        aroundVisitedCells.Add(aroundCell.Key, aroundCell.Value);
                    }
                }

                if (context.CanWalkTo(WalkDirection.Down))
                {
                    aroundCell = GetVisitedAroundCell(WalkDirection.Down);

                    if (aroundCell.Key == null)
                    {
                        return(PrepareResult(WalkDirection.Down));
                    }
                    if (aroundCell.Key != null)
                    {
                        aroundVisitedCells.Add(aroundCell.Key, aroundCell.Value);
                    }
                }

                if (context.CanWalkTo(WalkDirection.Left))
                {
                    aroundCell = GetVisitedAroundCell(WalkDirection.Left);

                    if (aroundCell.Key == null)
                    {
                        return(PrepareResult(WalkDirection.Left));
                    }

                    if (aroundCell.Key != null)
                    {
                        aroundVisitedCells.Add(aroundCell.Key, aroundCell.Value);
                    }
                }

                var futureCellEntry = aroundVisitedCells.OrderBy(a => a.Value).FirstOrDefault();

                if (futureCellEntry.Key == null)
                {
                    return(PrepareResult(GetResult(context, false)));
                }

                var futureCell = futureCellEntry.Key;

                if (futureCell.Y == s.Y - 1 && futureCell.X == s.X && context.CanWalkTo(WalkDirection.Up))
                {
                    return(PrepareResult(WalkDirection.Up));
                }
                else if (futureCell.Y == s.Y && futureCell.X == s.X + 1 && context.CanWalkTo(WalkDirection.Right))
                {
                    return(PrepareResult(WalkDirection.Right));
                }
                else if (futureCell.Y == s.Y + 1 && futureCell.X == s.X && context.CanWalkTo(WalkDirection.Down))
                {
                    return(PrepareResult(WalkDirection.Down));
                }
                else if (futureCell.Y == s.Y && futureCell.X == s.X - 1 && context.CanWalkTo(WalkDirection.Left))
                {
                    return(PrepareResult(WalkDirection.Left));
                }
            }

            return(PrepareResult(result));
        }
Exemple #4
0
 /// <summary>
 /// Choose the walk direction.
 /// </summary>
 /// <param name="context">The maze environment's context.</param>
 /// <returns>The walk diretion choosen.</returns>
 public WalkDirection Walk(IMazeEnvironmentContext context)
 {
     return((WalkDirection)RandomHelper.NextInt(0, 4));
 }