static void Main(string[] args) { bool noPath = false; bool back = false; do { Console.Clear(); // Get random board board = PrisonBreakTools.getNewMaze(BOARD_SIZE, rand); // Get random starting point int startX, startY; do { startX = rand.Next(0, BOARD_SIZE); startY = rand.Next(0, BOARD_SIZE); }while (board[startY, startX] != TileTypes.EMPTY || startX == 0 || startY == 0 || startX == BOARD_SIZE - 1 || startY == BOARD_SIZE - 1); Console.WriteLine("Start: {0} {1}", startY, startX); //////////////////////////// Your output goes here///////////////////////////////// /////////////////////////////////////////////////////////////////////////////////// int x = startX; int y = startY; int X; int Y; board[y, x] = TileTypes.MOVE; Stack <int> lastXY = new Stack <int>(); // for backtracking Doors.setFirstClose(board); printMaze(); while (x != 0 && y != 0 && x != BOARD_SIZE - 1 && y != BOARD_SIZE - 1) { if (Neighbor.CheckNeighbor(board, x, y)) { // remembered last x,y in stack lastXY.Push(x); lastXY.Push(y); X = x; Y = y; if (Step.mustClose) { Doors.Close(board); } Step.MakeStep(board, x, y, out X, out Y); x = X; y = Y; // out X,Y after chenges } else if (lastXY.Count != 0) { y = Convert.ToInt32(lastXY.Pop()); x = Convert.ToInt32(lastXY.Pop()); Console.WriteLine("BackTreking..."); } else { Console.WriteLine("No Path!"); noPath = true; break; } } if (noPath == false) { Console.WriteLine($"Exit: {y}, {x}"); } back = Convert.ToBoolean(Convert.ToInt16(Console.ReadLine())); } while (back); Environment.Exit(0); }
public static int[,] MakeStep(int[,] board, int x, int y, out int X, out int Y) { X = x; Y = y; int randDir = rand.Next(1, 5); // if in our direction the wall, the closed door or the old way dont step if (randDir == 1 && (board[Y, X + 1] == TileTypes.WALL || board[Y, X + 1] == TileTypes.MOVE || board[Y, X + 1] == TileTypes.CLOSEDOOR)) { return(board); } else if (randDir == 2 && (board[Y, X - 1] == TileTypes.WALL || board[Y, X - 1] == TileTypes.MOVE || board[Y, X - 1] == TileTypes.CLOSEDOOR)) { return(board); } else if (randDir == 3 && (board[Y + 1, X] == TileTypes.WALL || board[Y + 1, X] == TileTypes.MOVE || board[Y + 1, X] == TileTypes.CLOSEDOOR)) { return(board); } else if (randDir == 4 && (board[Y - 1, X] == TileTypes.WALL || board[Y - 1, X] == TileTypes.MOVE || board[Y - 1, X] == TileTypes.CLOSEDOOR)) { return(board); } // if we go through the door, open and close some doors mustClose = Doors.checkOnClose(board, X, Y, randDir); // make a step if (randDir == 1) { X++; board[Y, X] = TileTypes.MOVE; Console.WriteLine($"x: {X} y: {Y}"); Program.printMaze(); } else if (randDir == 2) { X--; board[Y, X] = TileTypes.MOVE; Console.WriteLine($"x: {X} y: {Y}"); Program.printMaze(); } else if (randDir == 3) { Y++; board[Y, X] = TileTypes.MOVE; Console.WriteLine($"x: {X} y: {Y}"); Program.printMaze(); } else if (randDir == 4) { Y--; board[Y, X] = TileTypes.MOVE; Console.WriteLine($"x: {X} y: {Y}"); Program.printMaze(); } else { Console.WriteLine("No Path"); return(board); } x = X; y = Y; return(board); }