|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Security.Policy; |
| 5 | +using System.Text; |
| 6 | + |
| 7 | +namespace reto33 |
| 8 | +{ |
| 9 | + public class deathwing696 |
| 10 | + { |
| 11 | + const string kMENSAJE = "Movimiento no permitido"; |
| 12 | + public class Posicion |
| 13 | + { |
| 14 | + private int x, y; |
| 15 | + |
| 16 | + public int X { get { return x; } } |
| 17 | + public int Y { get { return y; } } |
| 18 | + |
| 19 | + public Posicion(int x, int y) |
| 20 | + { |
| 21 | + this.x = x; |
| 22 | + this.y = y; |
| 23 | + } |
| 24 | + |
| 25 | + public void MoverDerecha() |
| 26 | + { |
| 27 | + this.y += 1; |
| 28 | + } |
| 29 | + |
| 30 | + public void MoverIzquierda() |
| 31 | + { |
| 32 | + this.y -= 1; |
| 33 | + } |
| 34 | + |
| 35 | + public void MoverArriba() |
| 36 | + { |
| 37 | + this.x -= 1; |
| 38 | + } |
| 39 | + |
| 40 | + public void MoverAbajo() |
| 41 | + { |
| 42 | + this.x += 1; |
| 43 | + } |
| 44 | + |
| 45 | + } |
| 46 | + public class Tablero |
| 47 | + { |
| 48 | + private string[,] tablero; |
| 49 | + private Posicion posicion; |
| 50 | + private bool llegada = false; |
| 51 | + |
| 52 | + public bool Llegada { get { return llegada; } } |
| 53 | + |
| 54 | + public Tablero() |
| 55 | + { |
| 56 | + tablero = new string[6, 6]; |
| 57 | + |
| 58 | + for (int i = 0; i < tablero.GetLength(0); i++) |
| 59 | + { |
| 60 | + for (int j = 0; j < tablero.GetLength(1); j++) |
| 61 | + { |
| 62 | + tablero[i,j] = "_"; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + GeneraObstaculos(); |
| 67 | + GeneraSalida(); |
| 68 | + GeneraInicioMickey(); |
| 69 | + } |
| 70 | + |
| 71 | + private void GeneraInicioMickey() |
| 72 | + { |
| 73 | + Random random = new Random(); |
| 74 | + int fila, columna; |
| 75 | + |
| 76 | + do |
| 77 | + { |
| 78 | + fila = random.Next(0, 5); |
| 79 | + columna = random.Next(0, 5); |
| 80 | + } while (tablero[fila, columna] != "_"); |
| 81 | + |
| 82 | + tablero[fila, columna] = "M"; |
| 83 | + posicion = new Posicion(fila, columna); |
| 84 | + } |
| 85 | + |
| 86 | + private void GeneraSalida() |
| 87 | + { |
| 88 | + Random random = new Random(); |
| 89 | + int fila, columna; |
| 90 | + |
| 91 | + do |
| 92 | + { |
| 93 | + fila = random.Next(0, 5); |
| 94 | + columna = random.Next(0, 5); |
| 95 | + } while (tablero[fila, columna] != "_"); |
| 96 | + |
| 97 | + tablero[fila, columna] = "S"; |
| 98 | + } |
| 99 | + |
| 100 | + private void GeneraObstaculos() |
| 101 | + { |
| 102 | + Random random = new Random(); |
| 103 | + var numObstaculos = random.Next(6, 12); |
| 104 | + |
| 105 | + for (int i = 0; i < numObstaculos; i++) |
| 106 | + { |
| 107 | + int fila, columna; |
| 108 | + |
| 109 | + do |
| 110 | + { |
| 111 | + fila = random.Next(0, 5); |
| 112 | + columna = random.Next(0, 5); |
| 113 | + } while (tablero[fila, columna] != "_"); |
| 114 | + |
| 115 | + tablero[fila, columna] = "X"; |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + public void MuestraTablero() |
| 120 | + { |
| 121 | + Console.WriteLine("Leyenda:"); |
| 122 | + Console.WriteLine("_ -> Hueco"); |
| 123 | + Console.WriteLine("X -> Obstaculo"); |
| 124 | + Console.WriteLine("M -> Mickey"); |
| 125 | + Console.WriteLine("S -> Salida"); |
| 126 | + Console.WriteLine("Tablero:"); |
| 127 | + |
| 128 | + for (int i = 0; i < tablero.GetLength(0); i++) |
| 129 | + { |
| 130 | + for (int j = 0; j < tablero.GetLength(1); j++) |
| 131 | + { |
| 132 | + Console.Write(tablero[i,j] + " "); |
| 133 | + } |
| 134 | + |
| 135 | + Console.WriteLine(); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + public bool MueveDerecha() |
| 140 | + { |
| 141 | + if (posicion.Y + 1 < tablero.GetLength(0)) |
| 142 | + { |
| 143 | + if (tablero[posicion.X, posicion.Y + 1] != "X") |
| 144 | + { |
| 145 | + if (tablero[posicion.X, posicion.Y + 1] == "S") |
| 146 | + { |
| 147 | + llegada = true; |
| 148 | + } |
| 149 | + |
| 150 | + tablero[posicion.X, posicion.Y] = "_"; |
| 151 | + tablero[posicion.X, posicion.Y + 1] = "M"; |
| 152 | + posicion.MoverDerecha(); |
| 153 | + |
| 154 | + return true; |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + return false; |
| 159 | + } |
| 160 | + |
| 161 | + public bool MueveIzquierda() |
| 162 | + { |
| 163 | + if (posicion.Y - 1 >= 0) |
| 164 | + { |
| 165 | + if (tablero[posicion.X, posicion.Y - 1] != "X") |
| 166 | + { |
| 167 | + if (tablero[posicion.X, posicion.Y - 1] == "S") |
| 168 | + { |
| 169 | + llegada = true; |
| 170 | + } |
| 171 | + |
| 172 | + tablero[posicion.X, posicion.Y] = "_"; |
| 173 | + tablero[posicion.X, posicion.Y - 1] = "M"; |
| 174 | + posicion.MoverIzquierda(); |
| 175 | + |
| 176 | + return true; |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + return false; |
| 181 | + } |
| 182 | + |
| 183 | + public bool MueveArriba() |
| 184 | + { |
| 185 | + if (posicion.X - 1 >= 0) |
| 186 | + { |
| 187 | + if (tablero[posicion.X - 1, posicion.Y] != "X") |
| 188 | + { |
| 189 | + if (tablero[posicion.X - 1, posicion.Y] == "S") |
| 190 | + { |
| 191 | + llegada = true; |
| 192 | + } |
| 193 | + |
| 194 | + tablero[posicion.X, posicion.Y] = "_"; |
| 195 | + tablero[posicion.X - 1, posicion.Y] = "M"; |
| 196 | + posicion.MoverArriba(); |
| 197 | + |
| 198 | + return true; |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + return false; |
| 203 | + } |
| 204 | + |
| 205 | + public bool MueveAbajo() |
| 206 | + { |
| 207 | + if (posicion.X + 1 < tablero.GetLength(1)) |
| 208 | + { |
| 209 | + if (tablero[posicion.X + 1, posicion.Y] != "X") |
| 210 | + { |
| 211 | + if (tablero[posicion.X + 1, posicion.Y] == "S") |
| 212 | + { |
| 213 | + llegada = true; |
| 214 | + } |
| 215 | + |
| 216 | + tablero[posicion.X, posicion.Y] = "_"; |
| 217 | + tablero[posicion.X + 1, posicion.Y] = "M"; |
| 218 | + posicion.MoverAbajo(); |
| 219 | + |
| 220 | + return true; |
| 221 | + } |
| 222 | + } |
| 223 | + |
| 224 | + return false; |
| 225 | + } |
| 226 | + } |
| 227 | + static void Main(string[] args) |
| 228 | + { |
| 229 | + Tablero tablero = new Tablero(); |
| 230 | + |
| 231 | + while (!tablero.Llegada) |
| 232 | + { |
| 233 | + tablero.MuestraTablero(); |
| 234 | + Console.WriteLine("¿Hacia dónde quieres moverte?"); |
| 235 | + Console.WriteLine("1. Derecha"); |
| 236 | + Console.WriteLine("2. Izquierda"); |
| 237 | + Console.WriteLine("3. Arriba"); |
| 238 | + Console.WriteLine("4. Abajo"); |
| 239 | + Console.Write("Opcion: "); |
| 240 | + var opcion = Int16.Parse(Console.ReadLine()); |
| 241 | + |
| 242 | + switch (opcion) |
| 243 | + { |
| 244 | + case 1: |
| 245 | + if (!tablero.MueveDerecha()) |
| 246 | + Console.WriteLine(kMENSAJE); |
| 247 | + break; |
| 248 | + case 2: |
| 249 | + if (!tablero.MueveIzquierda()) |
| 250 | + Console.WriteLine(kMENSAJE); |
| 251 | + break; |
| 252 | + case 3: |
| 253 | + if (!tablero.MueveArriba()) |
| 254 | + Console.WriteLine(kMENSAJE); |
| 255 | + break; |
| 256 | + case 4: |
| 257 | + if (!tablero.MueveAbajo()) |
| 258 | + Console.WriteLine(kMENSAJE); |
| 259 | + break; |
| 260 | + default: |
| 261 | + Console.WriteLine("Opción incorrecta"); |
| 262 | + break; |
| 263 | + } |
| 264 | + } |
| 265 | + Console.WriteLine("¡Enhorabuena! Has ayudado a Mickey a escapar del laberinto"); |
| 266 | + |
| 267 | + Console.ReadKey(); |
| 268 | + } |
| 269 | + } |
| 270 | +} |
0 commit comments