public void DrawLine(Transform mazeField, MazePoint p, Direction d, int z, LineColour colour)
        {
            var currentLevel = p.Z == z;

            switch (d)
            {
            case Direction.Left:
                if (currentLevel)
                {
                    DrawLeftLine(p, mazeField, colour);
                }
                break;

            case Direction.Right:
                if (currentLevel)
                {
                    DrawRightLine(p, mazeField, colour);
                }
                break;

            case Direction.Down:
                if (currentLevel)
                {
                    DrawDownLine(p, mazeField, colour);
                }
                if (p.Z - 1 == z)
                {
                    DrawUpLine(p, mazeField, colour);
                }
                break;

            case Direction.Up:
                if (currentLevel)
                {
                    DrawUpLine(p, mazeField, colour);
                }
                if (p.Z + 1 == z)
                {
                    DrawDownLine(p, mazeField, colour);
                }
                break;

            case Direction.Back:
                if (currentLevel)
                {
                    DrawBackLine(p, mazeField, colour);
                }
                break;

            case Direction.Forward:
                if (currentLevel)
                {
                    DrawForwardLine(p, mazeField, colour);
                }
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
        public IEnumerable <Direction> AdjacentPoints(MazePoint p, MazeSize size)
        {
            if (p.X < size.X - 1)
            {
                yield return(Direction.Right);
            }
            if (p.X > 0)
            {
                yield return(Direction.Left);
            }

            if (p.Y < size.Y - 1)
            {
                yield return(Direction.Forward);
            }
            if (p.Y > 0)
            {
                yield return(Direction.Back);
            }

            if (p.Z < size.Z - 1)
            {
                yield return(Direction.Up);
            }
            if (p.Z > 0)
            {
                yield return(Direction.Down);
            }
        }
        public Direction AdjacentPointsFlag(MazePoint p, MazeSize size)
        {
            //return AdjacentPoints(p, size).Aggregate(Direction.None, (seed, item) => _flagParser.AddDirectionsToFlag(seed, item));
            var flag = Direction.None;

            if (p.X < size.X - 1)
            {
                flag = _flagParser.AddDirectionsToFlag(flag, Direction.Right);
            }
            if (p.X > 0)
            {
                flag = _flagParser.AddDirectionsToFlag(flag, Direction.Left);
            }

            if (p.Y < size.Y - 1)
            {
                flag = _flagParser.AddDirectionsToFlag(flag, Direction.Forward);
            }
            if (p.Y > 0)
            {
                flag = _flagParser.AddDirectionsToFlag(flag, Direction.Back);
            }

            if (p.Z < size.Z - 1)
            {
                flag = _flagParser.AddDirectionsToFlag(flag, Direction.Up);
            }
            if (p.Z > 0)
            {
                flag = _flagParser.AddDirectionsToFlag(flag, Direction.Down);
            }
            return(flag);
        }
示例#4
0
 public bool IntersectsWith(MazePoint point)
 {
     if (point.X >= X && point.Y >= Y && point.X < X + Width && point.Y < Y + Height)
     {
         return(true);
     }
     return(false);
 }
 public MazePoint Move(MazePoint start, MazePoint final, MazeSize size)
 {
     if (CanMove(start, final, size))
     {
         return(final);
     }
     throw new ArgumentException("Invalid Point to move to");
 }
        private void DrawRightLine(MazePoint p, Transform mazeField, LineColour colour)
        {
            var sprite = _lineLoader.GetLine(LineOption.FullLine, colour);

            sprite.transform.SetParent(mazeField, false);
            sprite.transform.localPosition = new Vector3(_cellInformation.CellSize * p.X + _cellInformation.CellSize / 2, _cellInformation.CellSize * p.Y, -5);
            sprite.transform.localScale    = new Vector3(_cellInformation.LineWidth, _cellInformation.LineHeight, 0);
        }
        private void DrawDownLine(MazePoint p, Transform mazeField, LineColour colour)
        {
            var sprite = _lineLoader.GetLine(LineOption.HalfLine, colour);

            sprite.transform.SetParent(mazeField, false);
            sprite.transform.localPosition = new Vector3(_cellInformation.CellSize * p.X - _cellInformation.QuarterCellSize / 2, _cellInformation.CellSize * p.Y - _cellInformation.QuarterCellSize / 2, -5);
            sprite.transform.localScale    = new Vector3(_cellInformation.QuarterLineWidth * 3, _cellInformation.LineHeight, 0);
            sprite.transform.Rotate(0, 0, 45);
        }
        public MazePoint Move(MazePoint start, Direction d, MazeSize size)
        {
            MazePoint final;

            if (CanMove(start, d, size, out final))
            {
                return(final);
            }
            throw new ArgumentException("Cannot move in direction");
        }
示例#9
0
        public static void FloodFill(InnerMap map)
        {
            var stackje = new Stack <MazePoint>();

            stackje.Push(new MazePoint(0, 0));

            MazePoint[] targets = new MazePoint[4];

            int x = 0;
            int y = 0;

            int width  = map.Width;
            int height = map.Height;

            while (stackje.Count != 0)
            {
                MazePoint cur = stackje.Pop();
                x = cur.X;
                y = cur.Y;

                map[x, y] = true;

                int targetCount = 0;
                if (x - 1 > 0 && !map[x - 1, y])
                {
                    targets[targetCount].X = x - 1;
                    targets[targetCount].Y = y;
                    targetCount++;
                }
                if (x + 1 < width - 1 && !map[x + 1, y])
                {
                    targets[targetCount].X = x + 1;
                    targets[targetCount].Y = y;
                    targetCount++;
                }
                if (y - 1 > 0 && !map[x, y - 1])
                {
                    targets[targetCount].X = x;
                    targets[targetCount].Y = y - 1;
                    targetCount++;
                }
                if (y + 1 < height - 1 && !map[x, y + 1])
                {
                    targets[targetCount].X = x;
                    targets[targetCount].Y = y + 1;
                    targetCount++;
                }

                for (int i = 0; i < targetCount; i++)
                {
                    var target = targets[i];
                    stackje.Push(target);
                }
            }
        }
示例#10
0
 private Boolean HasLosSmart(MazePoint start, MazePoint end)
 {
     if (start.X < end.X)
     {
         return(HasLos(start, end));
     }
     else
     {
         return(HasLos(end, start));
     }
 }
示例#11
0
        private CellValidationResult GetResult(IMazeJumper maze, MazePoint point)
        {
            maze.JumpToPoint(point);
            var flag = maze.GetFlagFromPoint();

            return(new CellValidationResult
            {
                CellValid = flag != Direction.None,
                Flag = flag,
                Point = maze.CurrentPoint
            });
        }
示例#12
0
        public void OneCell_GetAdjacentPoints_NoDirectionsReturned()
        {
            //Arrange
            var point = new MazePoint(0, 0, 0);
            var size  = new MazeSize {
                Z = 1, Y = 1, X = 1
            };
            //Act
            var directions = _movementHelper.AdjacentPoints(point, size).ToList();

            //Assert
            Assert.That(directions.Count, Is.EqualTo(0));
        }
示例#13
0
        public void OneCell_GetAdjacentPoints_NoneDirectionReturned()
        {
            //Arrange
            var point = new MazePoint(0, 0, 0);
            var size  = new MazeSize {
                Z = 1, Y = 1, X = 1
            };
            //Act
            var direction = _movementHelper.AdjacentPointsFlag(point, size);

            //Assert
            Assert.That(direction, Is.EqualTo(Direction.None));
        }
示例#14
0
        public void OneCell_PassInAInvalidCellTooHigh_CellIsInvalid()
        {
            //Arrange
            var point = new MazePoint(1, 0, 0);
            var size  = new MazeSize {
                Z = 1, Y = 1, X = 1
            };
            //Act
            var valid = _pointValidity.ValidPoint(point, size);

            //Assert
            Assert.That(valid, Is.False);
        }
示例#15
0
        public bool HasPath(int[][] maze, int[] start, int[] destination)
        {
            width  = maze[0].Length;
            height = maze.Length;

            _maze = new int[width, height];
            for (int i = 0; i < width; i++)
            {
                //convert maze data store
                for (int j = 0; j < height; j++)
                {
                    _maze[i, j] = maze[j][i];
                }
            }

            visited = new HashSet <int>();
            stack   = new Stack <MazePoint>();

            MazePoint start2 = new MazePoint()
            {
                Row = start[0], Column = start[1]
            };

            stack.Push(start2);

            /////////////////////////////////
            ////essentially Broadth First Search
            /////////////////////////////////
            while (stack.Count > 0)
            {
                MazePoint current = stack.Pop();
                if (visited.Contains(coordToIndex(current)))
                {
                    continue;
                }
                if ((current.Row == destination[0]) && (current.Column == destination[1]))
                {
                    return(true);
                }

                visited.Add(coordToIndex(current));
                foreach (var next in Neighbors(current))
                {
                    stack.Push(next);
                }
            }

            return(false);
        }
示例#16
0
        public void TwoCells_GetAdjacentPointsToRightCell_OneDirectionsReturned()
        {
            //Arrange
            var point = new MazePoint(1, 0, 0);
            var size  = new MazeSize {
                Z = 1, Y = 1, X = 2
            };
            //Act
            var directions = _movementHelper.AdjacentPoints(point, size).ToList();

            //Assert
            Assert.That(directions.Count, Is.EqualTo(1));

            Assert.That(directions, Has.Exactly(1).EqualTo(Direction.Left));
        }
示例#17
0
        public void AddLive(Guid gameID, Guid userID, MazePoint point = null)
        {
            if (point == null)
            {
                point = new MazePoint(5, 5);
            }

            GameRoom room = actionGameList.Where(x => x.RoomID == gameID).First();

            LiveGameObject human = new Human(point); // TODO: Тут в зависимсти от разных типов игры должны создаваться разные игроки

            room.AddLiveObject(userID, human);       // Связываем игрока и его объект

            room.SetStatusGameToStarted();           // Запускаем игру
        }
示例#18
0
        private int CheckPoint(MazePoint point, IMazeCarver carver, int numberOfWalls, Direction preferredDirection = Direction.None)
        {
            carver.JumpToPoint(point);
            var directions = carver.CarvableDirections().ToList();

            _arrayHelper.Shuffle(directions);
            if (directions.Any())
            {
                var selectedDirection = directions.Contains(preferredDirection)
                    ? preferredDirection
                    : directions.First();
                carver.CarveInDirection(selectedDirection);
                numberOfWalls--;
            }
            return(numberOfWalls);
        }
示例#19
0
        public void EightCells_GetAdjacentPointsToRightUpForwardCell_ThreeDirectionsReturned()
        {
            //Arrange
            var point = new MazePoint(1, 1, 1);
            var size  = new MazeSize {
                Z = 2, Y = 2, X = 2
            };
            //Act
            var directions = _movementHelper.AdjacentPoints(point, size).ToList();

            //Assert
            Assert.That(directions.Count, Is.EqualTo(3));

            Assert.That(directions, Has.Exactly(1).EqualTo(Direction.Left));
            Assert.That(directions, Has.Exactly(1).EqualTo(Direction.Down));
            Assert.That(directions, Has.Exactly(1).EqualTo(Direction.Back));
        }
        public bool ValidPoint(MazePoint p, MazeSize size)
        {
            if (p.X < 0 || p.X > size.X - 1)
            {
                return(false);
            }

            if (p.Y < 0 || p.Y > size.Y - 1)
            {
                return(false);
            }

            if (p.Z < 0 || p.Z > size.Z - 1)
            {
                return(false);
            }
            return(true);
        }
示例#21
0
        public List <MazePoint> GetAdjacentPoints(MazePoint curMazePoint, int curPathStep)
        {
            //int xstart = Math.Max(curMazePoint.X - 2, 0);
            //int ystart = Math.Max(curMazePoint.Y - 2, 0);
            //int xend = Math.Min(curMazePoint.X + 2, innerMap.Width);
            //int yend = Math.Min(curMazePoint.Y + 2, innerMap.Height);


            //int xstart = 0;
            //int ystart = 0;
            //int xend = innerMap.Width;
            //int yend = innerMap.Height;

            return(path.AsParallel().Skip(curPathStep - 300).Take(600).Where(t => HasLosSmart(t, curMazePoint)).ToList());

            //List<MazePoint> probableMazePoints = new List<MazePoint>();

            //for (int x = xstart; x < xend; x++)
            //{
            //    for (int y = ystart; y < yend; y++)
            //    {
            //        var probablePoint = new MazePoint(x, y);

            //        //Not valid because its the same point
            //        //if (AreEqual(curMazePoint, probablePoint))
            //        //    break;

            //        //Not valid because its on the path
            //        //if (path.Any(t => (t.X == probablePoint.X && t.Y == probablePoint.Y)))
            //        //    continue;

            //        if (HasLosSmart(probablePoint, curMazePoint))
            //        {
            //            probableMazePoints.Add(probablePoint);
            //        }

            //        //probableMazePoints.Add(probablePoint);
            //    }
            //}

            //return probableMazePoints;
        }
示例#22
0
        public void TwentySevenCells_GetAdjacentPointsToCentreCell_AllDirectionReturned()
        {
            //Arrange
            _flagParser.Setup(x => x.AddDirectionsToFlag(It.IsAny <Direction>(), It.IsAny <Direction>())).Returns(
                (Direction seed, Direction d) =>
            {
                var flag = seed | d;
                return(flag);
            });

            var point = new MazePoint(1, 1, 1);
            var size  = new MazeSize {
                Z = 3, Y = 3, X = 3
            };
            //Act
            var direction = _movementHelper.AdjacentPointsFlag(point, size);

            //Assert
            Assert.That(direction, Is.EqualTo(Direction.All));
        }
示例#23
0
        public void TwentySevenCells_GetAdjacentPointsToCentreCell_SixDirectionsReturned()
        {
            //Arrange
            var point = new MazePoint(1, 1, 1);
            var size  = new MazeSize {
                Z = 3, Y = 3, X = 3
            };
            //Act
            var directions = _movementHelper.AdjacentPoints(point, size).ToList();

            //Assert
            Assert.That(directions.Count, Is.EqualTo(6));

            Assert.That(directions, Has.Exactly(1).EqualTo(Direction.Left));
            Assert.That(directions, Has.Exactly(1).EqualTo(Direction.Right));
            Assert.That(directions, Has.Exactly(1).EqualTo(Direction.Forward));
            Assert.That(directions, Has.Exactly(1).EqualTo(Direction.Back));
            Assert.That(directions, Has.Exactly(1).EqualTo(Direction.Up));
            Assert.That(directions, Has.Exactly(1).EqualTo(Direction.Down));
        }
示例#24
0
        private IEnumerable <MazePoint> Neighbors(MazePoint point)
        {
            int[][] directions = { new int[] { -1, 0 }, new int[] { 1, 0 }, new int[] { 0, -1 }, new int[] { 0, 1 } };
            foreach (int[] offsets in directions)
            {
                MazePoint neighbor = new MazePoint();
                neighbor.Row    = point.Row;    ////y
                neighbor.Column = point.Column; ////x

                while (((neighbor.Column + offsets[0]) >= 0) &&
                       ((neighbor.Column + offsets[0]) < width) &&
                       ((neighbor.Row + offsets[1]) >= 0) &&
                       ((neighbor.Row + offsets[1]) < height) &&
                       (_maze[neighbor.Column + offsets[0], neighbor.Row + offsets[1]] == 0))
                {
                    neighbor.Column = neighbor.Column + offsets[0];
                    neighbor.Row    = neighbor.Row + offsets[1];
                }
                yield return(neighbor);
            }
        }
        public bool CanMove(MazePoint start, Direction d, MazeSize size, out MazePoint final)
        {
            switch (d)
            {
            case Direction.None:
                final = start;
                break;

            case Direction.Right:
                final = _pointFactory.MakePoint(start.X + 1, start.Y, start.Z);
                break;

            case Direction.Left:
                final = _pointFactory.MakePoint(start.X - 1, start.Y, start.Z);
                break;

            case Direction.Forward:
                final = _pointFactory.MakePoint(start.X, start.Y + 1, start.Z);
                break;

            case Direction.Back:
                final = _pointFactory.MakePoint(start.X, start.Y - 1, start.Z);
                break;

            case Direction.Up:
                final = _pointFactory.MakePoint(start.X, start.Y, start.Z + 1);
                break;

            case Direction.Down:
                final = _pointFactory.MakePoint(start.X, start.Y, start.Z - 1);
                break;

            default:
                throw new ArgumentException("Unsupported movement direction");
            }
            return(_pointValidity.ValidPoint(final, size));
        }
示例#26
0
        private Boolean[,] GoGenerate(FastRandom r, int width, int height)
        {
            int x = 1;
            int y = 1;


            var map = new Boolean[width, height];


            List <int> stackjex = new List <int>(10000);
            List <int> stackjey = new List <int>(10000);

            int pointertje = 0;

            stackjex.Add(x);
            stackjey.Add(y);
            pointertje++;

            map[x, y] = true;
            //pixelChangedCallback.Invoke(x, y, currentStep, totSteps);

            MazePoint[] targets = new MazePoint[4];

            //form.drawPixel(x, y, brushThisUses);
            while (pointertje > 0)
            {
                x = stackjex[pointertje - 1];
                y = stackjey[pointertje - 1];

                int targetCount = 0;
                if (x - 2 > 0 && !map[x - 2, y])
                {
                    targets[targetCount].X = x - 2;
                    targets[targetCount].Y = y;
                    targetCount++;
                }
                if (x + 2 < width - 1 && !map[x + 2, y])
                {
                    targets[targetCount].X = x + 2;
                    targets[targetCount].Y = y;
                    targetCount++;
                }
                if (y - 2 > 0 && !map[x, y - 2])
                {
                    targets[targetCount].X = x;
                    targets[targetCount].Y = y - 2;
                    targetCount++;
                }
                if (y + 2 < height - 1 && !map[x, y + 2])
                {
                    targets[targetCount].X = x;
                    targets[targetCount].Y = y + 2;
                    targetCount++;
                }

                //Thread.Sleep(1000);

                if (targetCount > 0)
                {
                    var target = targets[r.Next(targetCount)];
                    if (pointertje == stackjex.Count)
                    {
                        stackjex.Add(target.X);
                        stackjey.Add(target.Y);
                    }
                    else
                    {
                        stackjex[pointertje] = target.X;
                        stackjey[pointertje] = target.Y;
                    }
                    pointertje++;

                    map[target.X, target.Y] = true;

                    if (target.X < x)
                    {
                        map[x - 1, y] = true;
                        //pixelChangedCallback.Invoke(x - 1, y, currentStep, totSteps);
                        //form.drawPixel(x - 1, y, brushThisUses);
                    }
                    else if (target.X > x)
                    {
                        map[x + 1, y] = true;
                        //pixelChangedCallback.Invoke(x + 1, y, currentStep, totSteps);
                        //form.drawPixel(x + 1, y, brushThisUses);
                    }
                    else if (target.Y < y)
                    {
                        map[x, y - 1] = true;
                        //pixelChangedCallback.Invoke(x, y - 1, currentStep, totSteps);
                        //form.drawPixel(x, y - 1, brushThisUses);
                    }
                    else if (target.Y > y)
                    {
                        map[x, y + 1] = true;
                        //pixelChangedCallback.Invoke(x, y + 1, currentStep, totSteps);
                        //form.drawPixel(x, y + 1, brushThisUses);
                    }
                    //pixelChangedCallback.Invoke(target.X, target.Y, currentStep, totSteps);
                    //form.drawPixel(target.X, target.Y, brushThisUses);
                }
                else
                {
                    pointertje--;
                }
            }

            return(map);
        }
示例#27
0
        /// <summary>
        /// Finds the path between the start and the endpoint in a maze
        /// </summary>
        /// <param name="start">The start point</param>
        /// <param name="end">The end point</param>
        /// <param name="map">The maze.InnerMap</param>
        /// <param name="callBack">The callback that can be used to see what the pathfinder is doing (or null), the boolean true = a new path find thingy or false when it determined that path is not correct</param>
        /// <returns>The shortest path in a list of points</returns>
        public static List <MazePoint> GoFind(MazePoint start, MazePoint end, InnerMap map, Action <int, int, bool> callBack)
        {
            if (callBack == null)
            {
                callBack = (x, y, z) => { };
            }


            //Callback won't work nice with this since it will find its path from back to front
            //Swap them so we don't have to reverse at the end ;)
            //MazePoint temp = start;
            //start = end;
            //end = temp;



            int width  = map.Width;
            int height = map.Height;


            List <MazePoint> stackje = new List <MazePoint>();

            stackje.Add(start);

            MazePoint cur  = new MazePoint();
            MazePoint prev = new MazePoint(-1, -1);


            var lastBackTrackDir = -1;

            while (stackje.Count != 0)
            {
                cur = stackje[stackje.Count - 1];
                var x = cur.X;
                var y = cur.Y;


                MazePoint target = new MazePoint(-1, -1);
                //Make sure the point was not the previous point, also make sure that if we backtracked we don't go to a direction we already went to, also make sure that the point is white
                if ((prev.X != x + 1 || prev.Y != y) && lastBackTrackDir < 0 && x + 1 < width - 1 && map[x + 1, y])
                {
                    target = new MazePoint(x + 1, y);
                }
                else if ((prev.X != x || prev.Y != y + 1) && lastBackTrackDir < 1 && y + 1 < height - 1 && map[x, y + 1])
                {
                    target = new MazePoint(x, y + 1);
                }
                else if ((prev.X != x - 1 || prev.Y != y) && lastBackTrackDir < 2 && x - 1 > 0 && map[x - 1, y])
                {
                    target = new MazePoint(x - 1, y);
                }
                else if ((prev.X != x || prev.Y != y - 1) && lastBackTrackDir < 3 && y - 1 > 0 && map[x, y - 1])
                {
                    target = new MazePoint(x, y - 1);
                }
                else
                {
                    var prepoppy = stackje[stackje.Count - 1];
                    stackje.RemoveAt(stackje.Count - 1);

                    if (stackje.Count == 0)
                    {
                        //No path found
                        break;
                    }

                    var newcur = stackje[stackje.Count - 1];

                    //Set the new previous point
                    if (stackje.Count == 1)
                    {
                        prev = new MazePoint(-1, -1);
                    }
                    else
                    {
                        prev = stackje.ElementAt(stackje.Count - 2);
                    }

                    //Console.WriteLine("Backtracking to X: " + newcur.X + " Y: " + newcur.Y);
                    //Console.WriteLine("Setting new prev: " + prev.X + " Y: " + prev.Y);

                    callBack.Invoke(prepoppy.X, prepoppy.Y, false);

                    //Set the direction we backtracked from
                    if (prepoppy.X > newcur.X)
                    {
                        lastBackTrackDir = 0;
                    }
                    else if (prepoppy.Y > newcur.Y)
                    {
                        lastBackTrackDir = 1;
                    }
                    else if (prepoppy.X < newcur.X)
                    {
                        lastBackTrackDir = 2;
                    }
                    else if (prepoppy.Y < newcur.Y)
                    {
                        lastBackTrackDir = 3;
                    }

                    //Console.WriteLine("Lastbacktrackdir: " + lastBackTrackDir);
                    continue;
                }

                lastBackTrackDir = -1;

                //Console.WriteLine("Going to X: " + target.X + " Y: " + target.Y);

                callBack.Invoke(x, y, true);
                stackje.Add(target);

                if (target.X == end.X && target.Y == end.Y)
                {
                    //Path found
                    break;
                }

                prev = cur;
            }

            return(stackje);
        }
示例#28
0
        private Boolean[,] GoGenerate(FastRandom r, int width, int height)
        {
            int x = 1;
            int y = 1;


            var map = new Boolean[width, height];

            //The length of this array is just enough to fit the longest path possible
            int[] stackjex = new int[width * height / 4];
            int[] stackjey = new int[width * height / 4];

            int pointertje = 0;

            stackjex[pointertje] = x;
            stackjey[pointertje] = y;
            pointertje++;

            map[x, y] = true;
            //pixelChangedCallback.Invoke(x, y, currentStep, totSteps);

            MazePoint[] targets = new MazePoint[4];

            //form.drawPixel(x, y, brushThisUses);
            while (pointertje > 0)
            {
                x = stackjex[pointertje - 1];
                y = stackjey[pointertje - 1];

                int targetCount = 0;
                if (isValid(x - 2, y, map, width, height))
                {
                    targets[targetCount].X = x - 2;
                    targets[targetCount].Y = y;
                    targetCount++;
                }
                if (isValid(x + 2, y, map, width, height))
                {
                    targets[targetCount].X = x + 2;
                    targets[targetCount].Y = y;
                    targetCount++;
                }
                if (isValid(x, y - 2, map, width, height))
                {
                    targets[targetCount].X = x;
                    targets[targetCount].Y = y - 2;
                    targetCount++;
                }
                if (isValid(x, y + 2, map, width, height))
                {
                    targets[targetCount].X = x;
                    targets[targetCount].Y = y + 2;
                    targetCount++;
                }

                //Thread.Sleep(1000);

                if (targetCount > 0)
                {
                    var target = targets[r.Next(targetCount)];
                    stackjex[pointertje] = target.X;
                    stackjey[pointertje] = target.Y;
                    pointertje++;

                    map[target.X, target.Y] = true;

                    if (target.X < x)
                    {
                        map[x - 1, y] = true;
                        //pixelChangedCallback.Invoke(x - 1, y, currentStep, totSteps);
                        //form.drawPixel(x - 1, y, brushThisUses);
                    }
                    else if (target.X > x)
                    {
                        map[x + 1, y] = true;
                        //pixelChangedCallback.Invoke(x + 1, y, currentStep, totSteps);
                        //form.drawPixel(x + 1, y, brushThisUses);
                    }
                    else if (target.Y < y)
                    {
                        map[x, y - 1] = true;
                        //pixelChangedCallback.Invoke(x, y - 1, currentStep, totSteps);
                        //form.drawPixel(x, y - 1, brushThisUses);
                    }
                    else if (target.Y > y)
                    {
                        map[x, y + 1] = true;
                        //pixelChangedCallback.Invoke(x, y + 1, currentStep, totSteps);
                        //form.drawPixel(x, y + 1, brushThisUses);
                    }
                    //pixelChangedCallback.Invoke(target.X, target.Y, currentStep, totSteps);
                    //form.drawPixel(target.X, target.Y, brushThisUses);
                }
                else
                {
                    pointertje--;
                }
            }

            return(map);
        }
示例#29
0
 public SendStartPositionRequest(Guid userID, Guid gameID, MazePoint point) : base()
 {
     UserID = userID;
     GameID = gameID;
     Point  = point;
 }
        /// <summary>
        /// Finds the path between the start and the endpoint in a maze
        /// </summary>
        /// <param name="start">The start point</param>
        /// <param name="end">The end point</param>
        /// <param name="map">The maze.InnerMap</param>
        /// <param name="callBack">The callback that can be used to see what the pathfinder is doing (or null), the boolean true = a new path find thingy or false when it determined that path is not correct</param>
        /// <returns>The shortest path in a list of points</returns>
        public static List <MazePoint> GoFind(MazePoint start, MazePoint end, InnerMap map, Action <int, int, Boolean> callBack)
        {
            if (callBack == null)
            {
                callBack = (x, y, z) => { };
            }


            //Callback won't work nice with this since it will find its path from back to front
            //Swap them so we don't have to reverse at the end ;)
            //MazePoint temp = start;
            //start = end;
            //end = temp;



            int width  = map.Width;
            int height = map.Height;

            List <MazePoint> pointlist = new List <MazePoint>();


            //@todo Controleer dit
            InnerMap visited = new BitArreintjeFastInnerMap(width, height);

            for (int x = 0; x < width; x++)
            {
                //visited[x] = new BitArreintjeFast(height);
                for (int y = 0; y < height; y++)
                {
                    if (x == 0 || y == 0 || x == width || y == height)
                    {
                        visited[x, y] = true;
                    }
                    //else
                    //{
                    //    visited[x][y] = false;
                    //}
                }
            }


            //Hier begint het gedoe
            Stack <MazePoint> stackje = new Stack <MazePoint>();

            stackje.Push(start);
            visited[start.X, start.Y] = true;
            callBack.Invoke(start.X, start.Y, true);
            //form.pixelDraw(x, y, Brushes.White);
            while (stackje.Count != 0)
            {
                MazePoint cur = stackje.Peek();
                int       x   = cur.X;
                int       y   = cur.Y;

                if (end.X == x && end.Y == y)
                {
                    callBack.Invoke(x, y, true);
                    break;
                }

                MazePoint target = new MazePoint(-1, -1);
                if (x + 1 < width - 1 && !visited[x + 1, y] && map[x + 1, y])
                {
                    target = new MazePoint(x + 1, y);
                }
                else if (y + 1 < height - 1 && !visited[x, y + 1] && map[x, y + 1])
                {
                    target = new MazePoint(x, y + 1);
                }
                else if (x - 1 > 0 && !visited[x - 1, y] && map[x - 1, y])
                {
                    target = new MazePoint(x - 1, y);
                }
                else if (y - 1 > 0 && !visited[x, y - 1] && map[x, y - 1])
                {
                    target = new MazePoint(x, y - 1);
                }

                //Thread.Sleep(1000);

                if (target.X != -1)
                {
                    callBack.Invoke(x, y, true);
                    //var target = targets[r.Next(targets.Count)];
                    stackje.Push(target);
                    visited[target.X, target.Y] = true;
                    //form.pixelDraw(target.X, target.Y, Brushes.Blue);
                    //Thread.Sleep(200);

                    //if (target.X < x)
                    //{
                    //    visited[x - 1][y] = true;
                    //    //form.pixelDraw(x - 1, y, Brushes.White);
                    //}
                    //else if (target.X > x)
                    //{
                    //    visited[x + 1][y] = true;
                    //    //form.pixelDraw(x + 1, y, Brushes.White);
                    //}
                    //else if (target.Y < y)
                    //{
                    //    visited[x][y - 1] = true;
                    //    //form.pixelDraw(x, y - 1, Brushes.White);
                    //}
                    //else if (target.Y > y)
                    //{
                    //    visited[x][y + 1] = true;
                    //    //form.pixelDraw(x, y + 1, Brushes.White);
                    //}
                }
                else
                {
                    callBack.Invoke(x, y, false);
                    stackje.Pop();
                }
            }

            pointlist.AddRange(stackje);

            pointlist.Reverse();

            return(pointlist);
        }
        private Boolean[,] GoGenerate(Random r, int width, int height)
        {
            int x = 1;
            int y = 1;

            var map = new Boolean[width, height];

            Stack<MazePoint> stackje = new Stack<MazePoint>();
            stackje.Push(new MazePoint(x, y));
            map[x, y] = true;
            //pixelChangedCallback.Invoke(x, y, currentStep, totSteps);

            MazePoint[] targets = new MazePoint[4];

            //form.drawPixel(x, y, brushThisUses);
            while (stackje.Count != 0)
            {
                MazePoint cur = stackje.Peek();
                x = cur.X;
                y = cur.Y;

                int targetCount = 0;
                if (x - 2 > 0 && !map[x - 2, y])
                {
                    targets[targetCount].X = x - 2;
                    targets[targetCount].Y = y;
                    targetCount++;
                }
                if (x + 2 < width - 1 && !map[x + 2, y])
                {
                    targets[targetCount].X = x + 2;
                    targets[targetCount].Y = y;
                    targetCount++;
                }
                if (y - 2 > 0 && !map[x, y - 2])
                {
                    targets[targetCount].X = x;
                    targets[targetCount].Y = y - 2;
                    targetCount++;
                }
                if (y + 2 < height - 1 && !map[x, y + 2])
                {
                    targets[targetCount].X = x;
                    targets[targetCount].Y = y + 2;
                    targetCount++;
                }

                //Thread.Sleep(1000);

                if (targetCount > 0)
                {
                    var target = targets[r.Next(targetCount)];
                    stackje.Push(target);
                    map[target.X, target.Y] = true;

                    if (target.X < x)
                    {
                        map[x - 1, y] = true;
                        //pixelChangedCallback.Invoke(x - 1, y, currentStep, totSteps);
                        //form.drawPixel(x - 1, y, brushThisUses);
                    }
                    else if (target.X > x)
                    {
                        map[x + 1, y] = true;
                        //pixelChangedCallback.Invoke(x + 1, y, currentStep, totSteps);
                        //form.drawPixel(x + 1, y, brushThisUses);
                    }
                    else if (target.Y < y)
                    {
                        map[x, y - 1] = true;
                        //pixelChangedCallback.Invoke(x, y - 1, currentStep, totSteps);
                        //form.drawPixel(x, y - 1, brushThisUses);
                    }
                    else if (target.Y > y)
                    {
                        map[x, y + 1] = true;
                        //pixelChangedCallback.Invoke(x, y + 1, currentStep, totSteps);
                        //form.drawPixel(x, y + 1, brushThisUses);
                    }
                    //pixelChangedCallback.Invoke(target.X, target.Y, currentStep, totSteps);
                    //form.drawPixel(target.X, target.Y, brushThisUses);
                }
                else
                {
                    stackje.Pop();
                }
            }

            return map;
        }
        /// <summary>
        /// Finds the path between the start and the endpoint in a maze
        /// </summary>
        /// <param name="start">The start point</param>
        /// <param name="end">The end point</param>
        /// <param name="map">The maze.InnerMap</param>
        /// <param name="callBack">The callback that can be used to see what the pathfinder is doing (or null), the boolean true = a new path find thingy or false when it determined that path is not correct</param>
        /// <returns>The shortest path in a list of points</returns>
        public static List<MazePoint> GoFind(MazePoint start, MazePoint end, InnerMap map, Action<int, int, Boolean> callBack)
        {
            if (callBack == null)
            {
                callBack = (x, y, z) => { };
            }

            //Callback won't work nice with this since it will find its path from back to front
            //Swap them so we don't have to reverse at the end ;)
            //MazePoint temp = start;
            //start = end;
            //end = temp;

            int width = map.Width;
            int height = map.Height;

            InnerMap visitedMap = new BitArreintjeFastInnerMap(width, height);

            List<MazePoint> pointlist = new List<MazePoint>();

            //@todo Controleer dit
            InnerMap visited = new BitArreintjeFastInnerMap(width, height);
            for (int x = 0; x < width; x++)
            {
                //visited[x] = new BitArreintjeFast(height);
                for (int y = 0; y < height; y++)
                {
                    if (x == 0 || y == 0 || x == width || y == height)
                    {
                        visited[x, y] = true;
                    }
                    //else
                    //{
                    //    visited[x][y] = false;
                    //}
                }
            }

            //Hier begint het gedoe
            Stack<MazePoint> stackje = new Stack<MazePoint>();
            stackje.Push(start);
            visited[start.X, start.Y] = true;
            callBack.Invoke(start.X, start.Y, true);
            //form.pixelDraw(x, y, Brushes.White);
            while (stackje.Count != 0)
            {
                MazePoint cur = stackje.Peek();
                int x = cur.X;
                int y = cur.Y;

                if (end.X == x && end.Y == y)
                {
                    callBack.Invoke(x, y, true);
                    break;
                }

                MazePoint target = new MazePoint(-1, -1);
                if (isValid(x + 1, y, map, visited, width, height))
                {
                    target = new MazePoint(x + 1, y);
                }
                else if (isValid(x, y + 1, map, visited, width, height))
                {
                    target = new MazePoint(x, y + 1);
                }
                else if (isValid(x - 1, y, map, visited, width, height))
                {
                    target = new MazePoint(x - 1, y);
                }
                else if (isValid(x, y - 1, map, visited, width, height))
                {
                    target = new MazePoint(x, y - 1);
                }
                //Thread.Sleep(1000);

                if (target.X != -1)
                {
                    callBack.Invoke(x, y, true);
                    //var target = targets[r.Next(targets.Count)];
                    stackje.Push(target);
                    visited[target.X, target.Y] = true;
                    //form.pixelDraw(target.X, target.Y, Brushes.Blue);
                    //Thread.Sleep(200);

                    //if (target.X < x)
                    //{
                    //    visited[x - 1][y] = true;
                    //    //form.pixelDraw(x - 1, y, Brushes.White);
                    //}
                    //else if (target.X > x)
                    //{
                    //    visited[x + 1][y] = true;
                    //    //form.pixelDraw(x + 1, y, Brushes.White);
                    //}
                    //else if (target.Y < y)
                    //{
                    //    visited[x][y - 1] = true;
                    //    //form.pixelDraw(x, y - 1, Brushes.White);
                    //}
                    //else if (target.Y > y)
                    //{
                    //    visited[x][y + 1] = true;
                    //    //form.pixelDraw(x, y + 1, Brushes.White);
                    //}
                }
                else
                {
                    callBack.Invoke(x, y, false);
                    stackje.Pop();
                }
            }

            pointlist.AddRange(stackje);

            pointlist.Reverse();

            return pointlist;
        }
        private Boolean[,] GoGenerate(FastRandom r, int width, int height)
        {
            int x = 1;
            int y = 1;

            var map = new Boolean[width, height];

            //The length of this array is just enough to fit the longest path possible
            int[] stackjex = new int[width * height / 4];
            int[] stackjey = new int[width * height / 4];

            int pointertje = 0;

            stackjex[pointertje] = x;
            stackjey[pointertje] = y;
            pointertje++;

            map[x, y] = true;
            //pixelChangedCallback.Invoke(x, y, currentStep, totSteps);

            MazePoint[] targets = new MazePoint[4];

            //form.drawPixel(x, y, brushThisUses);
            while (pointertje > 0)
            {
                x = stackjex[pointertje - 1];
                y = stackjey[pointertje - 1];

                int targetCount = 0;
                if (x - 2 > 0 && !map[x - 2, y])
                {
                    targets[targetCount].X = x - 2;
                    targets[targetCount].Y = y;
                    targetCount++;
                }
                if (x + 2 < width - 1 && !map[x + 2, y])
                {
                    targets[targetCount].X = x + 2;
                    targets[targetCount].Y = y;
                    targetCount++;
                }
                if (y - 2 > 0 && !map[x, y - 2])
                {
                    targets[targetCount].X = x;
                    targets[targetCount].Y = y - 2;
                    targetCount++;
                }
                if (y + 2 < height - 1 && !map[x, y + 2])
                {
                    targets[targetCount].X = x;
                    targets[targetCount].Y = y + 2;
                    targetCount++;
                }

                //Thread.Sleep(1000);

                if (targetCount > 0)
                {
                    var target = targets[r.Next(targetCount)];
                    stackjex[pointertje] = target.X;
                    stackjey[pointertje] = target.Y;
                    pointertje++;

                    map[target.X, target.Y] = true;

                    if (target.X < x)
                    {
                        map[x - 1, y] = true;
                        //pixelChangedCallback.Invoke(x - 1, y, currentStep, totSteps);
                        //form.drawPixel(x - 1, y, brushThisUses);
                    }
                    else if (target.X > x)
                    {
                        map[x + 1, y] = true;
                        //pixelChangedCallback.Invoke(x + 1, y, currentStep, totSteps);
                        //form.drawPixel(x + 1, y, brushThisUses);
                    }
                    else if (target.Y < y)
                    {
                        map[x, y - 1] = true;
                        //pixelChangedCallback.Invoke(x, y - 1, currentStep, totSteps);
                        //form.drawPixel(x, y - 1, brushThisUses);
                    }
                    else if (target.Y > y)
                    {
                        map[x, y + 1] = true;
                        //pixelChangedCallback.Invoke(x, y + 1, currentStep, totSteps);
                        //form.drawPixel(x, y + 1, brushThisUses);
                    }
                    //pixelChangedCallback.Invoke(target.X, target.Y, currentStep, totSteps);
                    //form.drawPixel(target.X, target.Y, brushThisUses);
                }
                else
                {
                    pointertje--;
                }

            }

            return map;
        }
        private Boolean[,] GoGenerate(FastRandom r, int width, int height)
        {
            int x = 1;
            int y = 1;

            var map = new Boolean[width, height];

            List<int> stackjex = new List<int>(10000);
            List<int> stackjey = new List<int>(10000);

            int pointertje = 0;

            stackjex.Add(x);
            stackjey.Add(y);
            pointertje++;

            map[x, y] = true;
            //pixelChangedCallback.Invoke(x, y, currentStep, totSteps);

            MazePoint[] targets = new MazePoint[4];

            //form.drawPixel(x, y, brushThisUses);
            while (pointertje > 0)
            {
                x = stackjex[pointertje - 1];
                y = stackjey[pointertje - 1];

                int targetCount = 0;
                if (x - 2 > 0 && !map[x - 2, y])
                {
                    targets[targetCount].X = x - 2;
                    targets[targetCount].Y = y;
                    targetCount++;
                }
                if (x + 2 < width - 1 && !map[x + 2, y])
                {
                    targets[targetCount].X = x + 2;
                    targets[targetCount].Y = y;
                    targetCount++;
                }
                if (y - 2 > 0 && !map[x, y - 2])
                {
                    targets[targetCount].X = x;
                    targets[targetCount].Y = y - 2;
                    targetCount++;
                }
                if (y + 2 < height - 1 && !map[x, y + 2])
                {
                    targets[targetCount].X = x;
                    targets[targetCount].Y = y + 2;
                    targetCount++;
                }

                //Thread.Sleep(1000);

                if (targetCount > 0)
                {
                    var target = targets[r.Next(targetCount)];
                    if (pointertje == stackjex.Count)
                    {
                        stackjex.Add(target.X);
                        stackjey.Add(target.Y);
                    }
                    else
                    {
                        stackjex[pointertje] = target.X;
                        stackjey[pointertje] = target.Y;
                    }
                    pointertje++;

                    map[target.X, target.Y] = true;

                    if (target.X < x)
                    {
                        map[x - 1, y] = true;
                        //pixelChangedCallback.Invoke(x - 1, y, currentStep, totSteps);
                        //form.drawPixel(x - 1, y, brushThisUses);
                    }
                    else if (target.X > x)
                    {
                        map[x + 1, y] = true;
                        //pixelChangedCallback.Invoke(x + 1, y, currentStep, totSteps);
                        //form.drawPixel(x + 1, y, brushThisUses);
                    }
                    else if (target.Y < y)
                    {
                        map[x, y - 1] = true;
                        //pixelChangedCallback.Invoke(x, y - 1, currentStep, totSteps);
                        //form.drawPixel(x, y - 1, brushThisUses);
                    }
                    else if (target.Y > y)
                    {
                        map[x, y + 1] = true;
                        //pixelChangedCallback.Invoke(x, y + 1, currentStep, totSteps);
                        //form.drawPixel(x, y + 1, brushThisUses);
                    }
                    //pixelChangedCallback.Invoke(target.X, target.Y, currentStep, totSteps);
                    //form.drawPixel(target.X, target.Y, brushThisUses);
                }
                else
                {
                    pointertje--;
                }
            }

            return map;
        }
        private void GoGenerate(InnerMap map, Maze maze, Random r, Action<int, int, long, long> pixelChangedCallback)
        {
            int x = 1;
            int y = 1;

            Stack<MazePoint> stackje = new Stack<MazePoint>();
            stackje.Push(new MazePoint(x, y));
            map[x, y] = true;
            //pixelChangedCallback.Invoke(x, y, currentStep, totSteps);

            MazePoint[] targets = new MazePoint[4];

            //form.drawPixel(x, y, brushThisUses);
            while (stackje.Count != 0)
            {

                MazePoint cur = stackje.Peek();
                x = cur.X;
                y = cur.Y;

                int targetCount = 0;
                if (x - 2 > 0 && !map[x - 2, y])
                {
                    targets[targetCount].X = x - 2;
                    targets[targetCount].Y = y;
                    targetCount++;
                }
                if (x + 2 < maze.Width - 1 && !map[x + 2, y])
                {
                    targets[targetCount].X = x + 2;
                    targets[targetCount].Y = y;
                    targetCount++;
                }
                if (y - 2 > 0 && !map[x, y - 2])
                {
                    targets[targetCount].X = x;
                    targets[targetCount].Y = y - 2;
                    targetCount++;
                }
                if (y + 2 < maze.Height - 1 && !map[x, y + 2])
                {
                    targets[targetCount].X = x;
                    targets[targetCount].Y = y + 2;
                    targetCount++;
                }

                //Thread.Sleep(1000);

                if (targetCount > 0)
                {
                    var target = targets[r.Next(targetCount)];
                    stackje.Push(target);
                    map[target.X, target.Y] = true;

                    if (target.X < x)
                    {
                        map[x - 1, y] = true;
                        //pixelChangedCallback.Invoke(x - 1, y, currentStep, totSteps);
                        //form.drawPixel(x - 1, y, brushThisUses);
                    }
                    else if (target.X > x)
                    {
                        map[x + 1, y] = true;
                        //pixelChangedCallback.Invoke(x + 1, y, currentStep, totSteps);
                        //form.drawPixel(x + 1, y, brushThisUses);
                    }
                    else if (target.Y < y)
                    {
                        map[x, y - 1] = true;
                        //pixelChangedCallback.Invoke(x, y - 1, currentStep, totSteps);
                        //form.drawPixel(x, y - 1, brushThisUses);
                    }
                    else if (target.Y > y)
                    {
                        map[x, y + 1] = true;
                        //pixelChangedCallback.Invoke(x, y + 1, currentStep, totSteps);
                        //form.drawPixel(x, y + 1, brushThisUses);
                    }
                    //pixelChangedCallback.Invoke(target.X, target.Y, currentStep, totSteps);
                    //form.drawPixel(target.X, target.Y, brushThisUses);
                }
                else
                {
                    stackje.Pop();
                }

            }
        }