Пример #1
0
        private void InitializeMaze(IAxisFactory axisFactory, IMazeViewDataFactory mazeViewDataFactory)
        {
            IMazeNodeData nodeData = _mazeNodeDataBuilder.GenerateNodeData(12345);
            IMazeViewData viewData = _mazeNodeDataBuilder.GenerateViewData(nodeData, axisFactory, mazeViewDataFactory);

            _maze.Initialize(nodeData, viewData);
        }
Пример #2
0
        private bool TryReverseDirection(IDirection direction, IUnit player, IMazeNodeData nodeData, IMazeViewData mazeView)
        {
            IDirection reverseDirection = player.CurrentMovementDirection.ReverseDirection;
            ILocation  futureLocation   = player.CurrentLocation.GetCopy().Add(mazeView.MovementCube[reverseDirection.Value]);

            return(Validator.IsFutureLocationValid(player.CurrentLocation, futureLocation, nodeData));
        }
Пример #3
0
        /// <summary>
        /// checks if the layout is valid. A layout is valid if you can make a route from a
        /// given vertex to all other vertices
        /// </summary>
        /// <returns></returns>
        public static bool IsLayoutValid(IMazeNodeData nodeData)
        {
            if (nodeData.NodesByIndex.Count == 0)
            {
                return(false);
            }

            int position     = -1;
            var foundNodeIds = new List <int>()
            {
                1
            };

            do
            {
                position++;
                IEnumerable <int> vertices = nodeData.NodesByIndex[foundNodeIds[position]].Path.Except(foundNodeIds);
                if (vertices != null)
                {
                    foundNodeIds.AddRange(vertices);
                }
            } while (position + 1 < foundNodeIds.Count);

            return(foundNodeIds.Count >= nodeData.NodesByIndex.Count);
        }
Пример #4
0
 private static INode GetNodeAtLocation(ILocation currentLocation, IMazeNodeData nodeData)
 {
     return(nodeData
            .NodesByIndex
            .Where(x => x.Value.Location.Equals(currentLocation))
            .Select(x => x.Value).FirstOrDefault());
 }
Пример #5
0
        /// <summary>
        /// Initializes the Node Location list and the Node Id list
        /// </summary>
        /// <param name="gridSize"></param>
        public void Initialize(IMazeNodeData nodeData, IMazeViewData viewData)
        {
            NodeData = nodeData;
            ViewData = viewData;

            SpawnUnitAtLocation(UnitType.Player, new Location());
        }
Пример #6
0
        private IMazeNodeData CreatePathData(int seed, IMazeNodeData nodeData)
        {
            ResetPathData(nodeData);
            IOrderedEnumerable <INode> sortedNodes = SortNodeData(nodeData);
            var random = _randomizer.GenerateRandom(seed);

            return(SetPathsForNodeData(nodeData, sortedNodes, random));
        }
Пример #7
0
        /// <summary>
        /// generates layout
        /// </summary>
        public IMazeNodeData GenerateNodeData(int seed)
        {
            IMazeNodeData nodeData = InitializeMazeNodeData();

            nodeData = InitializeNeigbours(nodeData);
            nodeData = CreatePathData(seed, nodeData);

            return(nodeData);
        }
Пример #8
0
        public void GenerateNodeDataTest()
        {
            var           settings    = new DefaultSettings();
            var           nodeBuilder = new NodeBuilder(settings, new CoinBuilder(settings));
            var           mazeNodeDataBuilderbuilder = new MazeNodeDataBuilder(new FakeMazeNodeDataBuilderSettings(5, 3), new Randomizer(), nodeBuilder);
            IMazeNodeData data = mazeNodeDataBuilderbuilder.GenerateNodeData(12345);

            Assert.NotNull(data);
        }
Пример #9
0
        /// <summary>
        /// Adds path from given node to destination node
        /// </summary>
        private void SetPath(INode node, int idOfDestinationNode, IMazeNodeData nodeData)
        {
            if (node.Path.Contains(idOfDestinationNode) || !IsPathValid(node, nodeData.NodesByIndex[idOfDestinationNode], node, 1, nodeData))
            {
                return;
            }

            node.Path.Add(idOfDestinationNode);
            nodeData.NodesByIndex[idOfDestinationNode].Path.Add(node.Id);
        }
Пример #10
0
        /// <summary>
        /// Fills in the neighbour list of each vertex
        /// </summary>
        private IMazeNodeData InitializeNeigbours(IMazeNodeData nodeData)
        {
            //initialize neigbours
            foreach (INode node in nodeData.NodesByIndex.Values)
            {
                node.Neighbours = FindNeighbouringNodes(node, nodeData);
            }

            return(nodeData);
        }
Пример #11
0
        private void SetMinimumRequiredPathsForNode(IMazeNodeData nodeData, Random randomizer, List <int> copyOfNeigours, INode node)
        {
            while (!HasMinimumRequiredPaths(node) && copyOfNeigours.Count > 0)
            {
                int neigbourId   = randomizer.Next(copyOfNeigours.Count);
                int pathToNodeId = copyOfNeigours[neigbourId];
                copyOfNeigours.RemoveAt(neigbourId);

                SetPath(node, pathToNodeId, nodeData);
            }
        }
Пример #12
0
        public MazeViewData(int start, int end, int size, IMazeNodeData nodeData, IAxisFactory axisFactory)
        {
            ViewSize  = size;
            ViewEnd   = end;
            ViewStart = start;

            UpDownRotationAxis    = axisFactory.CreateXAxis();
            LeftRightRotationAxis = axisFactory.CreateYAxis();
            MazeNodes             = CreateInitialView(nodeData, 0);

            InitializeMovementCube();
        }
Пример #13
0
        private IMazeNodeData SetPathsForNodeData(IMazeNodeData nodeData, IOrderedEnumerable <INode> sortedNodes, Random random)
        {
            var copyOfNeigours = new List <int>();

            foreach (INode node in sortedNodes)
            {
                copyOfNeigours.Clear();
                copyOfNeigours.AddRange(node.Neighbours.Select(x => x.Id));

                SetMinimumRequiredPathsForNode(nodeData, random, copyOfNeigours, node);
            }
            return(nodeData);
        }
Пример #14
0
        /// <summary>
        /// returns a list containing neigbours of a given node
        /// </summary>
        private List <NeighbourInfo> FindNeighbouringNodes(INode node, IMazeNodeData nodeData)
        {
            var neighbours = new List <NeighbourInfo>();
            IEnumerable <INode> allPossibleNeigbours = GetNeigboursOfNode(node, nodeData);

            foreach (INode neighbourNode in allPossibleNeigbours)
            {
                var tempNeighbour = new NeighbourInfo {
                    Id = neighbourNode.Id, Location = neighbourNode.Location
                };
                neighbours.Add(tempNeighbour);
            }

            return(neighbours);
        }
Пример #15
0
        private IList <INode> CreateInitialView(IMazeNodeData nodeData, int zLevel)
        {
            IList <INode>          nodes           = new List <INode>();
            IEnumerable <Location> allCombinations = CreateAllNodesForZLevel(zLevel);

            foreach (var item in allCombinations)
            {
                if (nodeData.NodesByLocation.TryGetValue(item, out INode node))
                {
                    nodes.Add(node);
                }
            }

            return(nodes);
        }
Пример #16
0
        public void InitializeTest()
        {
            IAxisFactory         axisFactory         = new AxisFactory();
            IMazeViewDataFactory mazeViewDataFactory = new MazeViewDataFactory();
            var           settings        = new DefaultSettings();
            var           nodeBuilder     = new NodeBuilder(settings, new CoinBuilder(settings));
            var           randomizer      = new Randomizer();
            var           nodeDataBuilder = new MazeNodeDataBuilder(new FakeMazeNodeDataBuilderSettings(3, 3), randomizer, nodeBuilder);
            IMazeNodeData nodeData        = nodeDataBuilder.GenerateNodeData(12345);
            IMazeViewData viewData        = nodeDataBuilder.GenerateViewData(nodeData, axisFactory, mazeViewDataFactory);
            var           maze            = new Maze(new UnitList(), new UnitFactory(randomizer));

            maze.Initialize(nodeData, viewData);

            int numberOfNodesTotal = maze.NodeData.Count;
            int numberOfNodesView  = maze.ViewData.MazeNodes.Count;
            int numberOfUnits      = maze.UnitList.Count;

            Assert.Equal(1, numberOfUnits);
            Assert.Equal(9, numberOfNodesView);
            Assert.Equal(27, numberOfNodesTotal);
        }
Пример #17
0
 public RotateCommand(IRotation rotation, IUnit player, IMazeViewData mazeView, IMazeNodeData nodeData, IAxisFactory factory)
 {
     fRotation        = rotation;
     fPlayer          = player;
     fMazeView        = mazeView;
     fNodesByLocation = nodeData.NodesByLocation;
     fAxisFactory     = factory;
 }
Пример #18
0
 public IMazeViewData CreateMazeViewData(int start, int end, int size, IMazeNodeData nodeData, IAxisFactory axisFactory)
 {
     return(new MazeViewData(start, end, size, nodeData, axisFactory));
 }
Пример #19
0
 /// <summary>
 /// Returns a sequence of nodes that are all the neigbours of the given node.
 /// </summary>
 private IEnumerable <INode> GetNeigboursOfNode(INode node, IMazeNodeData nodeData)
 {
     return(node.GetAllPossibleNeighbours().Select(x => nodeData.GetNode(x)).Where(x => x != null).ToList());
 }
Пример #20
0
        /// <summary>
        /// Checks if path is valid. path is not valid if it makes a loop using only 4 nodes.
        /// A loop is made by trying to reach original node by traveling trough the neigbourNodes list
        /// Recursive method
        /// </summary>
        private bool IsPathValid(INode fromNode, INode toNode, INode nodeToFind, int level, IMazeNodeData nodeData)
        {
            if (toNode.Path.Except(new[] { fromNode.Id }).Contains(nodeToFind.Id))
            {
                return(false);
            }

            if (level < 3)
            {
                level++;
                foreach (int path in toNode.Path)
                {
                    if (!IsPathValid(toNode, nodeData.NodesByIndex[path], nodeToFind, level, nodeData))
                    {
                        return(false);
                    }
                }
            }

            return(true);
        }
Пример #21
0
 /// <summary>
 /// Clears all Path data
 /// </summary>
 private void ResetPathData(IMazeNodeData nodeData)
 {
     nodeData.ClearAllPaths();
 }
Пример #22
0
        public static bool IsFutureLocationValid(ILocation currentLocation, ILocation futureLocation, IMazeNodeData nodeData)
        {
            INode currentNode = GetNodeAtLocation(currentLocation, nodeData);
            INode futureNode  = GetNodeAtLocation(futureLocation, nodeData);

            int id = (futureNode?.Id).GetValueOrDefault(-1);

            return(currentNode.Path.Contains(id));
        }
Пример #23
0
        public void MovePlayer(IDirection direction, IUnit player, IMazeNodeData nodeData, IMazeViewData mazeView)
        {
            Action <IDirection, IUnit, IMazeViewData> movementAction = fMovementLogic.GetMovementToPreform(direction, player, nodeData, mazeView);

            movementAction?.Invoke(direction, player, mazeView);
        }
Пример #24
0
 private IOrderedEnumerable <INode> SortNodeData(IMazeNodeData nodeData)
 {
     return(nodeData.NodesByIndex.Values.OrderBy(x => x.Neighbours.Count));
 }
Пример #25
0
        private bool TryNewDirection(IDirection direction, IUnit player, IMazeNodeData nodeData, IMazeViewData mazeView)
        {
            ILocation location = player.CurrentLocation.GetCopy().Add(mazeView.MovementCube[direction.Value]);

            return(Validator.IsFutureLocationValid(player.CurrentLocation, location, nodeData));
        }
Пример #26
0
 public Action <IDirection, IUnit, IMazeViewData> GetMovementToPreform(IDirection direction, IUnit player,
                                                                       IMazeNodeData nodeData, IMazeViewData mazeView)
 {
     return(fMovementPriorityList.FirstOrDefault(kp => kp.Key(direction, player, nodeData, mazeView)).Value);
 }
Пример #27
0
 public IMazeViewData GenerateViewData(IMazeNodeData nodeData, IAxisFactory axisFactory, IMazeViewDataFactory mazeViewDataFactory)
 {
     return(mazeViewDataFactory.CreateMazeViewData(fGridStart, fGridEnd, fGridSize, nodeData, axisFactory));
 }