/ Juego.cs
Juego.cs
1 using System; 2 using System.Collections.Generic; 3 using System.IO; 4 using System.Linq; 5 using System.Numerics; 6 using System.Text; 7 using System.Threading.Tasks; 8 9 namespace Laberinto 10 { 11 public class Juego 12 { 13 public Ficha Jugador; 14 public Casilla[,] Tablero; 15 public Juego(int ancho, int alto) 16 { 17 this.Jugador = new Ficha(ancho / 2, alto / 2); 18 this.Tablero = GenerarLaberinto(alto, ancho); 19 int[] salida = GenerarSalida(Tablero); 20 LaberintoSolver solver = new LaberintoSolver(Tablero); 21 22 int startX = ancho / 2, startY = alto / 2; 23 int endX = salida[0], endY = salida[1]; 24 25 solver.DFS(startX, startY, endX, endY); 26 var camino = solver.ObtenerCamino(); 27 foreach (var paso in camino) 28 { 29 Tablero[paso.Item1, paso.Item2].solucion = true; 30 31 } 32 33 34 } 35 36 public static Casilla[,] GenerarLaberinto(int alto, int ancho) 37 { 38 Casilla[,] lab = new Casilla[alto, ancho]; 39 40 // Inicializar todo como pared 41 for (int y = 0; y < alto; y++) 42 for (int x = 0; x < ancho; x++) 43 lab[y, x] = new Casilla(true); 44 45 // Generar laberinto desde el centro 46 GenerarDesde(ancho / 2, alto / 2, lab); 47 48 return lab; 49 } 50 51 private static void GenerarDesde(int y, int x, Casilla[,] laberinto) 52 { 53 laberinto[y, x].colisionable = false; //la posicion actual es un hueco 54 Random rand = new Random(); 55 int[][] direcciones = new int[][] //defino las direcciones, muevo 2 porque hay una celda que es el camino y otra que es la pared. 56 { 57 new int[] { 0, 2 }, // Derecha 58 new int[] { 0, -2 }, // Izquierda 59 new int[] { 2, 0 }, // Abajo 60 new int[] { -2, 0 } // Arriba 61 }; 62 63 // Mezclar direcciones aleatoriamente como con el anagrama 64 for (int i = 0; i < direcciones.Length; i++) 65 { 66 int j = rand.Next(i, direcciones.Length); 67 var temp = direcciones[i]; 68 direcciones[i] = direcciones[j]; 69 direcciones[j] = temp; 70 } 71 72 foreach (var dir in direcciones) //recorre cada una de las direcciones posibles 73 { 74 int nuevoY = y + dir[0]; 75 int nuevoX = x + dir[1]; 76 77 //valido limites y que la celda no haya sido visitada ya 78 if (nuevoX > 0 && nuevoX < laberinto.GetLength(1) - 1 && nuevoY > 0 && nuevoY < laberinto.GetLength(0) - 1 && laberinto[nuevoY, nuevoX].colisionable) 79 { 80 laberinto[y + dir[0] / 2, x + dir[1] / 2].colisionable = false; // Abrir camino intermedio 81 laberinto[y + dir[0], x + dir[1]].colisionable = false; // Abrir camino final 82 GenerarDesde(nuevoY, nuevoX, laberinto);//se repite el proceso en la nueva celda 83 } 84 } 85 } 86 87 private static int[] GenerarSalida(Casilla[,] laberinto) 88 { 89 Random rnd = new Random(); 90 int ladoRandom = rnd.Next(0, 4); 91 int alturaRandom = 0; 92 int anchoRandom = 0; 93 switch (ladoRandom) 94 { 95 case 0: 96 alturaRandom = rnd.Next(2, laberinto.GetLength(1) - 2); 97 laberinto[alturaRandom, 0].colisionable = false; 98 laberinto[alturaRandom, 1].colisionable = false; 99 laberinto[alturaRandom, 2].colisionable = false; 100 return [alturaRandom, 0]; 101 break; 102 103 case 1: 104 alturaRandom = rnd.Next(2, laberinto.GetLength(1) - 2); 105 laberinto[alturaRandom, laberinto.GetLength(0) - 1].colisionable = false; 106 laberinto[alturaRandom, laberinto.GetLength(0) - 2].colisionable = false; 107 laberinto[alturaRandom, laberinto.GetLength(0) - 3].colisionable = false; 108 return [alturaRandom, laberinto.GetLength(0) - 1]; 109 break; 110 111 case 2: 112 anchoRandom = rnd.Next(2, laberinto.GetLength(0) - 2); 113 laberinto[laberinto.GetLength(1) - 1, anchoRandom].colisionable = false; 114 laberinto[laberinto.GetLength(1) - 2, anchoRandom].colisionable = false; 115 laberinto[laberinto.GetLength(1) - 3, anchoRandom].colisionable = false; 116 return [laberinto.GetLength(1) - 1, anchoRandom]; 117 break; 118 119 case 3: 120 anchoRandom = rnd.Next(2, laberinto.GetLength(0) - 2); 121 laberinto[0, anchoRandom].colisionable = false; 122 laberinto[1, anchoRandom].colisionable = false; 123 laberinto[2, anchoRandom].colisionable = false; 124 return [0, anchoRandom]; 125 break; 126 127 default: 128 return null; 129 break; 130 } 131 } 132 133 134 } 135 }