//später noch auf Diagonale Zellen erweitern, bis jetzt noch keine Diagonalen /// <summary> /// function to update the Walls of two neighbour Cells /// </summary> /// <param name="akt">reference of current cell</param> /// <param name="next">reference of next cell</param> /// <param name="index">index which cell to update</param> private void updateWall(ref Cell akt, ref Cell next, int index) { if (next == null) { if(akt != null) { akt.setWall(index, false); } } else if (akt == null) { if (index == (int)neighbour.up) { next.setWall((int)neighbour.down, false); } else if (index == (int)neighbour.down) { next.setWall((int)neighbour.up, false); } else if (index == (int)neighbour.left) { next.setWall((int)neighbour.right, false); } else if (index == (int)neighbour.right) { next.setWall((int)neighbour.left, false); } } else { akt.setWall(index, false); if (index == (int)neighbour.up) { next.setWall((int)neighbour.down, false); } else if (index == (int)neighbour.down) { next.setWall((int)neighbour.up, false); } else if (index == (int)neighbour.left) { next.setWall((int)neighbour.right, false); } else if (index == (int)neighbour.right) { next.setWall((int)neighbour.left, false); } } }
/// <summary> /// function to update the map with the Walls of the Cell /// </summary> /// <param name="cell">cell with which map is updated</param> private void updateMap(Cell cell) { int x = cell.getMapIndexX(); int y = cell.getMapIndexY(); mapType[x, y] = (int)tiles.floor; mapType[x - 1, y] = cell.getWall((int)neighbour.up) == true ? 1 : 0; mapType[x + 1, y] = cell.getWall((int)neighbour.down) == true ? 1 : 0; mapType[x, y - 1] = cell.getWall((int)neighbour.left) == true ? 1 : 0; mapType[x, y + 1] = cell.getWall((int)neighbour.right) == true ? 1 : 0; mapType[x - 1, y - 1] = cell.getWall((int)neighbour.upLeft) == true ? 1 : 0; mapType[x - 1, y + 1] = cell.getWall((int)neighbour.upRight) == true ? 1 : 0; mapType[x + 1, y + 1] = cell.getWall((int)neighbour.downRight) == true ? 1 : 0; mapType[x + 1, y - 1] = cell.getWall((int)neighbour.downLeft) == true ? 1 : 0; }
/// <summary> /// function which returns number of not visited neighbour cells /// </summary> /// <param name="maze">Double Array with all Cells in it </param> /// <param name="cell">current Cell</param> /// <returns>integer</returns> private int numOfNotVisited(Cell[,] maze, Cell cell) { int result = 0; for(int i = 0; i < 4; i++) { if(getNext(maze, cell, i) != null) { if(getNext(maze, cell, i).getVisited() == false) { result++; } } } return result; }
/// <summary> /// function which returns an array with the indices of all neighbour cells which have not been visited yet /// </summary> /// <param name="maze">double array with all cells </param> /// <param name="cell">current cell</param> /// <param name="length">number of neighbour cells which have not been visited yet </param> /// <returns>array of integer</returns> private int[] notVisited(Cell[,] maze, Cell cell, int length) { int[] result= new int[length]; int index = 0; for (int i = 0; i < 4; i++ ) { if(getNext(maze, cell, i) != null) { if(getNext(maze, cell,i).getVisited() == false) { result[index] = i; index++; } } } return result; }
/// <summary> /// function which returns the next cell in a given direction /// </summary> /// <param name="maze">double array with all cells</param> /// <param name="cell">current cell</param> /// <param name="index">index in which directio next Cell lies </param> /// <returns>neighbour Cell in wanted direction</returns> private Cell getNext(Cell[,] maze, Cell cell, int index) { int newX = 0; int newY = 0; if (index == (int)neighbour.up && cell.getCellIndexX() - 1 >= 0) { newX = cell.getCellIndexX() - 1; newY = cell.getCellindexY(); return maze[newX, newY]; } else if (index == (int)neighbour.down && cell.getCellIndexX() + 1 < maze.GetLength(0)) { newX = cell.getCellIndexX() + 1; newY = cell.getCellindexY(); return maze[newX, newY]; } else if (index == (int)neighbour.left && cell.getCellindexY() - 1 >= 0) { newX = cell.getCellIndexX(); newY = cell.getCellindexY() - 1; return maze[newX, newY]; } else if (index == (int)neighbour.right && cell.getCellindexY() + 1 < maze.GetLength(1)) { newX = cell.getCellIndexX(); newY = cell.getCellindexY() + 1; return maze[newX, newY]; } else if (index == (int)neighbour.upLeft && cell.getCellIndexX() - 1 >= 0 && cell.getCellindexY() - 1 >= 0) { newX = cell.getCellIndexX() - 1; newY = cell.getCellindexY() - 1; return maze[newX, newY]; } else if (index == (int)neighbour.upRight && cell.getCellIndexX() - 1 >= 0 && cell.getCellindexY() + 1 < maze.GetLength(1)) { newX = cell.getCellIndexX() - 1; newY = cell.getCellindexY() + 1; return maze[newX, newY]; } else if (index == (int)neighbour.downRight && cell.getCellIndexX() + 1 < maze.GetLength(0) && cell.getCellindexY() + 1 < maze.GetLength(1)) { newX = cell.getCellIndexX() + 1; newY = cell.getCellindexY() + 1; return maze[newX, newY]; } else if (index == (int)neighbour.downLeft && cell.getCellIndexX() + 1 < maze.GetLength(0) && cell.getCellindexY() - 1 >= 0) { newX = cell.getCellIndexX() + 1; newY = cell.getCellindexY() - 1; return maze[newX, newY]; } else { newX = cell.getCellIndexX() + 1; newY = cell.getCellindexY(); return null; } }
/// <summary> /// function to create random maze, only works with the inner part, /// </summary> private void createMaze() { int numCellsX = 0; int numCellsY = 0; //determine number of cells depending on even or odd size of maze if (Settings.getMapSizeX() % 2 == 1) numCellsX = (Settings.getMapSizeX() - 1) / 2; else numCellsX = (Settings.getMapSizeX() - 2) / 2; if (Settings.getMapSizeZ() % 2 == 1) numCellsY = (Settings.getMapSizeZ() - 1) / 2; else numCellsY = (Settings.getMapSizeZ() - 2) / 2; // Array to store the Cells in Cell[,] maze = new Cell[numCellsX, numCellsY]; int MapIndexX = 1; int MapIndexY = 1; //initialize all Cells for (int i = 0; i < numCellsX; i++) { for (int j = 0; j < numCellsY; j++) { maze[i, j] = new Cell(MapIndexX, MapIndexY, i, j, true, true, true, true, true, true, true, true); MapIndexY += 2; } MapIndexY = 1; MapIndexX += 2; } //find random begin in the middleof the map int thirdX = numCellsX / 3; int thirdY = numCellsY / 3; int StartX = rnd.Next(thirdX) + thirdX; int StartY = rnd.Next(thirdY) + thirdY; Cell akt = maze[StartX, StartY]; akt.setVisited(true); Stack<Cell> stack = new Stack<Cell>(); stack.Push(akt); int lastTurn = 0; while (stack.Count != 0)//stack.Count != 0) { akt = stack.Peek(); int n = numOfNotVisited(maze, akt); if (n == 0) { updateMap(akt); stack.Pop(); } else if(n == 1) { int[] index = notVisited(maze, akt, n); int finaleIndex = index[0]; lastTurn = finaleIndex; Cell next = getNext(maze, akt, finaleIndex); next.setVisited(true); updateWall(ref akt, ref next, finaleIndex); updateMap(akt); stack.Push(next); } else { // find next neighbour int[] index = notVisited(maze, akt, n); int random = rnd.Next(n - 1); int finaleIndex = index[random]; while(finaleIndex == lastTurn) { finaleIndex = (index[random + 1 % n]); } Cell next = getNext(maze, akt, finaleIndex); next.setVisited(true); // Wand von eigener & next updaten updateWall(ref akt, ref next, finaleIndex); updateMap(akt); stack.Push(next); } } }