// Use this for initialization void Start() { dungeon = generate(); dungeonObjects = new GameObject[dungeon.getMaze_length()][]; for (int i = 0; i < dungeon.getMaze_length(); i++) { dungeonObjects[i] = new GameObject[dungeon.getMaze_length()]; } instantiate_maze(dungeon); }
// Use this for initialization void Start() { timer = 1 / step_per_second; maze_generator mg = this.GetComponentInParent <maze_generator>(); dungeon = mg.Get_Dungeon; int mazeLength = dungeon.getMaze_length(); tableLength = mazeLength * mazeLength; rewardTable = new int[tableLength, tableLength]; qTable = new float[tableLength, tableLength]; usedTable = new int[tableLength, tableLength]; //creates reward table for (int i = 0; i < tableLength; i++) { for (int j = 0; j < tableLength; j++) { //first put every state to state as -1 so it is not reachable rewardTable[i, j] = -1; int curVerIndex_j = i % mazeLength; int curVerIndex_i = (i - curVerIndex_j) / mazeLength; int targetVerIndex_j = j % mazeLength; int targetVerIndex_i = (j - targetVerIndex_j) / mazeLength; //foreach state to state cell current = dungeon.getCells()[curVerIndex_i][curVerIndex_j]; cell target = dungeon.getCells()[targetVerIndex_i][targetVerIndex_j]; //look for current states neighbours foreach (cell neighbour in current.getNeighbours()) { //if its reachable if (neighbour == target) { //then mark it as 0 so it is reachable rewardTable[i, j] = 0; //if it is the end point then mark it with reward if (dungeon.end_i == targetVerIndex_i && dungeon.end_j == targetVerIndex_j) { rewardTable[i, j] = reward; } break; } } } } character = Instantiate(character, new Vector3(dungeon.start_x, dungeon.start_y, 0), Quaternion.identity); curVertice = dungeon.start_i * dungeon.getMaze_length() + dungeon.start_j; }
void Awake() { height = wall.transform.localPosition.y; space = wall.transform.localScale.y; myMaze = new maze(mazeRowSize, mazeColSize, height, space); for (int i = 0; i < myMaze.index; i++) { GameObject g = Instantiate(wall); g.transform.SetParent(walls); g.transform.localPosition = myMaze.points[i]; } }
public void Equals_Obj_Test_Matching_Same_Solotion() { IHeuristic AirDistance = new MazeAirDistance(); ASearchingAlgorithm astarAir = new Astar(AirDistance); MazeGenerator DfsMazeGenerator = new DfsMazeGenerator(); maze Dfs = DfsMazeGenerator.generatMaze(20, 20); ISearchable SearchableMaze = new SearchableMaze(Dfs, false); Solution target = astarAir.Solve(SearchableMaze); Solution inObject = astarAir.Solve(SearchableMaze); bool expected = true; bool actual = target.GetSolutionPath().Capacity.Equals(inObject.GetSolutionPath().Capacity); Assert.AreEqual(expected, actual); }
public void Equals_Sol_Test_null() { IHeuristic AirDistance = new MazeAirDistance(); ASearchingAlgorithm astarAir = new Astar(AirDistance); MazeGenerator DfsMazeGenerator = new DfsMazeGenerator(); maze Dfs = DfsMazeGenerator.generatMaze(20, 20); ISearchable SearchableMazeA = new SearchableMaze(Dfs, false); ISearchable SearchableMazeB = new SearchableMaze(Dfs, true); Solution target = astarAir.Solve(SearchableMazeA); Solution inObject = astarAir.Solve(SearchableMazeB); bool expected = false; bool actual = target.Equals(inObject); Assert.AreEqual(expected, actual); }
///constractor <summary> /// /// </summary> /// <param name="mazeHight"></param> /// <param name="mazeLength"></param> /// <param name="mazeCellSize"></param> /// <param name="maze"></param> public MazeBoard(int mazeHight, int mazeLength, int mazeCellSize, maze maze) { InitializeComponent(); m_mazeCellSize = mazeCellSize; m_itemLocationX = 1; m_itemLocationY = 1; item = new ItemPoint(m_mazeCellSize); Goal G = new Goal(mazeCellSize); mazeBoard.Children.Add(item); Canvas.SetLeft(item, m_mazeCellSize); Canvas.SetTop(item, m_mazeCellSize); mazeBoard.Children.Add(G); Canvas.SetLeft(G, (mazeHight - 2) * m_mazeCellSize); Canvas.SetTop(G, (mazeLength - 2) * m_mazeCellSize); CreateMaze(mazeHight, mazeLength, m_mazeCellSize, maze); }
private void Start() { playerColl = this.GetComponent <CapsuleCollider>(); boardTurnSystem = GameObject.Find("Maze").GetComponent <RandoMazeBoard>(); maze = GameObject.Find("Maze").GetComponent <maze>(); move = GetComponent <keyMove2>(); NVM = GetComponent <navMashMove>(); //for each gameObject of Turnclass in the List of "Maze" foreach (TurnClass element in boardTurnSystem.playersGroup) { //if the gameObject Name matches the gameObject name in "Maze" /check if gameObject is registered in "Maze" //add reference of element instance into turnclass if (element.playerGameObject.name == gameObject.name) { turnClass = element; } } }
public void set_neighbours(maze dungeon) { if (c_x != 0) { neighbours[directions.north] = dungeon.getCells()[c_x - 1][c_y]; } if (c_y != dungeon.getMaze_length() - 1) { neighbours[directions.east] = dungeon.getCells()[c_x][c_y + 1]; } if (c_x != dungeon.getMaze_length() - 1) { neighbours[directions.south] = dungeon.getCells()[c_x + 1][c_y]; } if (c_y != 0) { neighbours[directions.west] = dungeon.getCells()[c_x][c_y - 1]; } }
public void Run100() { IHeuristic AirDistance = new MazeAirDistance(); ASearchingAlgorithm astarAir = new Astar(AirDistance); MazeGenerator DfsMazeGenerator = new DfsMazeGenerator(); maze Dfs = DfsMazeGenerator.generatMaze(20, 20); ISearchable SearchableMaze = new SearchableMaze(Dfs, true); Solution inObject = astarAir.Solve(SearchableMaze); Solution target; bool expected = true; for (int i = 0; i < 100; i++) { target = astarAir.Solve(SearchableMaze); bool actual = target.GetSolutionPath().Capacity.Equals(inObject.GetSolutionPath().Capacity); Assert.AreEqual(expected, actual); } }
/// ThreadPoolGeneratMaze<summary> /// thrading to genrate maze by give algorithem /// </summary> /// <param name="param">'hight' 'width' 'name'</param> public void ThreadPoolGeneratMaze(object param) { string[] p = (string[])param; IStoppable NewMazeGenerator; if (Curr_Genrator == "Dfs")//// Change from Configure { NewMazeGenerator = new DfsMazeGenerator(); } else { NewMazeGenerator = new RandomMazeGenerator(); } Workers.Add(NewMazeGenerator); maze m_Maze = new maze(); m_Status = string.Format("Maze {0} started Genarate...", p[2]); PrintEvent(); m_Maze = (maze)(((MazeGenerator)NewMazeGenerator).generatMaze(Convert.ToInt32(p[0]), Convert.ToInt32(p[1]))); if (m_Maze != null) { extendedMaze ex_Maze = new extendedMaze(m_Maze); ex_Maze.setName(p[2]); if (p[3] != "0") { ex_Maze.SaveGuiParams(p[3]); } addMaze(ex_Maze, p[2]); m_Status = string.Format("Maze {0} is ready...", p[2]); PrintEvent(); } }
public extendedMaze(maze maze) : base(maze) { }
/// <summary> /// Creates a new Node from the given position, then explores it's children /// </summary> /// <param name="position">Position of the node</param> /// <param name="fromParent">Distance from the parent node to this node</param> /// <param name="maze">Maze the node is in</param> /// <param name="keys">Currently available keys</param> private Node(Vector2 <int> position, int fromParent, Maze maze, int keys) : this(position, fromParent) => Explore(maze, keys);
//instatiation of maze and indices public void instantiate_maze(maze dungeon) { Transform mazeObject = new GameObject("mazeObject").transform; GameObject generator = GameObject.FindGameObjectWithTag("Mg"); Vector3 startPoint = generator.transform.position; if (mazeLength % 2 == 0) { startPoint.x -= ((mazeLength / 2) - 0.5f) * 16; startPoint.y += ((mazeLength / 2) - 0.5f) * 16; } else { startPoint.x -= Mathf.Floor(mazeLength / 2) * 16; startPoint.y += Mathf.Floor(mazeLength / 2) * 16; } int startIndex_x, startIndex_y, endIndex_x, endIndex_y; //Select random start and end point or make them fixed to the most left-up and the most right-down if (select_random_start_and_end) { startIndex_x = Random.Range(0, mazeLength); startIndex_y = Random.Range(0, mazeLength); endIndex_x = Random.Range(0, mazeLength); endIndex_y = Random.Range(0, mazeLength); while (startIndex_x == endIndex_x && startIndex_y == endIndex_y) { endIndex_x = Random.Range(0, mazeLength); endIndex_y = Random.Range(0, mazeLength); } } else { startIndex_x = startIndex_y = 0; endIndex_x = endIndex_y = mazeLength - 1; } dungeon.start_i = startIndex_y; dungeon.start_j = startIndex_x; dungeon.end_i = endIndex_y; dungeon.end_j = endIndex_x; dungeon.start_x = startPoint.x + dungeon.start_j * 16; dungeon.start_y = startPoint.y - dungeon.start_i * 16; for (int i = 0; i < mazeLength; i++) { for (int j = 0; j < mazeLength; j++) { Vector3 point = startPoint; point.x += j * 16; point.y -= i * 16; cell room = dungeon.getCells()[i][j]; int index = 0; bool[] doors = room.getDoors(); if (doors[0]) { index += 8; } if (doors[1]) { index += 4; } if (doors[2]) { index += 2; } if (doors[3]) { index += 1; } GameObject newRoom; if (i == startIndex_y && j == startIndex_x) { newRoom = Instantiate(roomArray_start[index], point, Quaternion.identity) as GameObject; } else if (i == endIndex_y && j == endIndex_x) { newRoom = Instantiate(roomArray_end[index], point, Quaternion.identity) as GameObject; } else { newRoom = Instantiate(roomArray[index], point, Quaternion.identity) as GameObject; } newRoom.transform.SetParent(mazeObject); GameObject marker; if (doors[0]) { marker = Instantiate(markers[directions.north], newRoom.transform); } if (doors[1]) { marker = Instantiate(markers[directions.east], newRoom.transform); } if (doors[2]) { marker = Instantiate(markers[directions.south], newRoom.transform); } if (doors[3]) { marker = Instantiate(markers[directions.west], newRoom.transform); } dungeonObjects[i][j] = newRoom; } } }
//prims maze generation algorithm public maze generate() { maze dungeon = new maze(mazeLength); //Prim's maze generator algorithm List <cell> toBeVisited = new List <cell>(); List <short> choices = new List <short>(); toBeVisited.Add(dungeon.getCells()[0][0]); while (toBeVisited.Count != 0) { //select randomly cell selected = toBeVisited[Random.Range(0, toBeVisited.Count)]; selected.visited = true; //find choices if (selected.getC_x() != 0 && selected.getNeighbours()[directions.north].visited) { choices.Add(directions.north); } if (selected.getC_y() != dungeon.getMaze_length() - 1 && selected.getNeighbours()[directions.east].visited) { choices.Add(directions.east); } if (selected.getC_x() != dungeon.getMaze_length() - 1 && selected.getNeighbours()[directions.south].visited) { choices.Add(directions.south); } if (selected.getC_y() != 0 && selected.getNeighbours()[directions.west].visited) { choices.Add(directions.west); } //choose where selected will be connected if (choices.Count > 0) { cell toWhere = selected.getNeighbours()[choices[Random.Range(0, choices.Count)]]; dungeon.createPassage(selected.getC_x(), selected.getC_y(), toWhere.getC_x(), toWhere.getC_y()); } //add unvisited nodes if (selected.getC_x() != 0 && !selected.getNeighbours()[directions.north].visited) { toBeVisited.Add(selected.getNeighbours()[directions.north]); } if (selected.getC_y() != dungeon.getMaze_length() - 1 && !selected.getNeighbours()[directions.east].visited) { toBeVisited.Add(selected.getNeighbours()[directions.east]); } if (selected.getC_x() != dungeon.getMaze_length() - 1 && !selected.getNeighbours()[directions.south].visited) { toBeVisited.Add(selected.getNeighbours()[directions.south]); } ; if (selected.getC_y() != 0 && !selected.getNeighbours()[directions.west].visited) { toBeVisited.Add(selected.getNeighbours()[directions.west]); } choices.Clear(); toBeVisited.Remove(selected); } //Remove unreachable neighbours for (int i = 0; i < mazeLength; i++) { for (int j = 0; j < mazeLength; j++) { dungeon.getCells()[i][j].fix_neighbours(); } } return(dungeon); }
private void BeginGame() { mazeInstance = Instantiate(mazePrefab) as maze; StartCoroutine(mazeInstance.Generate()); }