private void InitializeMaze(IAxisFactory axisFactory, IMazeViewDataFactory mazeViewDataFactory) { IMazeNodeData nodeData = _mazeNodeDataBuilder.GenerateNodeData(12345); IMazeViewData viewData = _mazeNodeDataBuilder.GenerateViewData(nodeData, axisFactory, mazeViewDataFactory); _maze.Initialize(nodeData, viewData); }
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)); }
/// <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); }
private static INode GetNodeAtLocation(ILocation currentLocation, IMazeNodeData nodeData) { return(nodeData .NodesByIndex .Where(x => x.Value.Location.Equals(currentLocation)) .Select(x => x.Value).FirstOrDefault()); }
/// <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()); }
private IMazeNodeData CreatePathData(int seed, IMazeNodeData nodeData) { ResetPathData(nodeData); IOrderedEnumerable <INode> sortedNodes = SortNodeData(nodeData); var random = _randomizer.GenerateRandom(seed); return(SetPathsForNodeData(nodeData, sortedNodes, random)); }
/// <summary> /// generates layout /// </summary> public IMazeNodeData GenerateNodeData(int seed) { IMazeNodeData nodeData = InitializeMazeNodeData(); nodeData = InitializeNeigbours(nodeData); nodeData = CreatePathData(seed, nodeData); return(nodeData); }
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); }
/// <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); }
/// <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); }
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); } }
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(); }
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); }
/// <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); }
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); }
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); }
public RotateCommand(IRotation rotation, IUnit player, IMazeViewData mazeView, IMazeNodeData nodeData, IAxisFactory factory) { fRotation = rotation; fPlayer = player; fMazeView = mazeView; fNodesByLocation = nodeData.NodesByLocation; fAxisFactory = factory; }
public IMazeViewData CreateMazeViewData(int start, int end, int size, IMazeNodeData nodeData, IAxisFactory axisFactory) { return(new MazeViewData(start, end, size, nodeData, axisFactory)); }
/// <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()); }
/// <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); }
/// <summary> /// Clears all Path data /// </summary> private void ResetPathData(IMazeNodeData nodeData) { nodeData.ClearAllPaths(); }
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)); }
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); }
private IOrderedEnumerable <INode> SortNodeData(IMazeNodeData nodeData) { return(nodeData.NodesByIndex.Values.OrderBy(x => x.Neighbours.Count)); }
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)); }
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); }
public IMazeViewData GenerateViewData(IMazeNodeData nodeData, IAxisFactory axisFactory, IMazeViewDataFactory mazeViewDataFactory) { return(mazeViewDataFactory.CreateMazeViewData(fGridStart, fGridEnd, fGridSize, nodeData, axisFactory)); }