public override void Juega(ref Mundo mundo) { if (cargaNiño) { if (mundo.Casillas[X, Y] == Elemento.RobotYCorral) { DejaNiño(ref mundo); } else { Tuple <int, int> obj = MejorObjetivo(mundo, mundo.PosCorral); Tuple <int, int> pos = ProximaJugada(mundo, obj); EfectuaJugada(ref mundo, pos); } } else { Tuple <int, int> pos = new Tuple <int, int>(X, Y); if (mundo.Casillas[X, Y] == Elemento.SuciedadYRobot) { Limpia(ref mundo); } else { EfectuaJugada(ref mundo, Proxima(mundo)); } } }
public Simulacion(int N, int M, int sucias, int obstaculos, int niños, int t, params string[] robots) { int T = N * M - 2 * niños - robots.Length; int sucia = (sucias * T) / 100; int obst = (obstaculos * T) / 100; mundo = new Mundo(N, M, niños, sucia, obst, robots); this.t = t; foreach (var item in mundo.Acciones) { Acciones.Enqueue(item); } mundo.Acciones.Clear(); }
protected void DejaNiño(ref Mundo mundo) { if (mundo.Casillas[X, Y] == Elemento.RobotYCorral) { mundo.Casillas[X, Y] = Elemento.CorralOcupado; mundo.PosCorral.Remove(new Tuple <int, int>(X, Y)); Accion = "El Robot deja al niño en el corral de la posicion (" + X + "," + Y + ")"; } else { mundo.Casillas[X, Y] = Elemento.NiñoYRobot; mundo.PosNiños.Add(new Tuple <int, int>(X, Y)); Accion = "El Robot deja al niño en la posicion (" + X + "," + Y + ")"; } cargaNiño = false; }
protected Tuple <int, int> MejorObjetivo(Mundo mundo, List <Tuple <int, int> > lista) { Tuple <int, int> resp = new Tuple <int, int>(X, Y); double d; double mejor = int.MaxValue; foreach (var item in lista) { d = Distancia(X, Y, item.Item1, item.Item2); if (mejor > d) { mejor = d; resp = item; } } return(resp); }
public Tuple <int, int> Proxima(Mundo mundo) { Tuple <int, int> obj1 = null; Tuple <int, int> obj2 = null; double d1 = double.MaxValue; double d2 = double.MaxValue; if (mundo.PosSuciedad.Count != 0) { obj1 = MejorObjetivo(mundo, mundo.PosSuciedad); } if (mundo.PosNiños.Count != 0) { obj2 = MejorObjetivo(mundo, mundo.PosNiños); } if (obj1 != null && (obj1.Item1 != X || obj1.Item2 != Y)) { d1 = Distancia(X, Y, obj1.Item1, obj1.Item2); } if (obj2 != null && (obj2.Item1 != X || obj2.Item2 != Y)) { d2 = Distancia(X, Y, obj2.Item1, obj2.Item2); } if (d1 == double.MaxValue && d2 == double.MaxValue) { return(new Tuple <int, int>(X, Y)); } else if (d1 < d2) { return(ProximaJugada(mundo, obj1)); } else { return(ProximaJugada(mundo, obj2)); } }
protected List <Tuple <int, int> > PosiblesMovimientos(Mundo mundo, Tuple <int, int> P) { List <Tuple <int, int> > posibles = new List <Tuple <int, int> >(); int a = P.Item1; int b = P.Item2; for (int i = -1; i < 2; i++) { for (int j = -1; j < 2; j++) { if (mundo.Valida(a + i, b + j)) { if (mundo.Casillas[a + i, b + j] == Elemento.Vacia) { posibles.Add(new Tuple <int, int>(a + i, b + j)); } else if (mundo.Casillas[a + i, b + j] == Elemento.Suciedad) { posibles.Add(new Tuple <int, int>(a + i, b + j)); } else if (mundo.Casillas[a + i, b + j] == Elemento.Corral) { posibles.Add(new Tuple <int, int>(a + i, b + j)); } else if (!cargaNiño) { if (mundo.Casillas[a + i, b + j] == Elemento.Niño) { posibles.Add(new Tuple <int, int>(a + i, b + j)); } } } } } return(posibles); }
protected Tuple <int, int> ProximaJugada(Mundo mundo, Tuple <int, int> obj) { List <Tuple <int, int> > posibles = PosiblesMovimientos(mundo); if (cargaNiño) { int t = posibles.Count; List <Tuple <int, int> > aux; for (int i = 0; i < t; i++) { aux = PosiblesMovimientos(mundo, posibles[i]); foreach (var item in aux) { if (!posibles.Contains(item)) { posibles.Add(item); } } } } Tuple <int, int> proximo = new Tuple <int, int>(X, Y); double mejor = Double.MaxValue; foreach (var item in posibles) { double d = Distancia(item.Item1, item.Item2, obj.Item1, obj.Item2); if (d < mejor) { mejor = d; proximo = item; } } return(proximo); }
protected void EfectuaJugada(ref Mundo mundo, Tuple <int, int> pos) { int a = pos.Item1; int b = pos.Item2; if (a != X || b != Y) { Elemento hay = mundo.Casillas[a, b]; //A donde llego switch (hay) { case Elemento.Vacia: { mundo.Casillas[a, b] = Elemento.Robot; mundo.Ocupadas[a, b] = true; Accion = "El Robot se mueve a la posicion (" + X + "," + Y + ")"; } break; case Elemento.Niño: { mundo.Casillas[a, b] = Elemento.Robot; cargaNiño = true; //lo carga automaticamente mundo.PosNiños.Remove(new Tuple <int, int>(a, b)); Accion = "El Robot se mueve y carga al niño de la posicion (" + a + "," + b + ")"; } break; case Elemento.Suciedad: { mundo.Casillas[a, b] = Elemento.SuciedadYRobot; Accion = "El Robot se mueve a la posicion (" + a + "," + b + ")" + " donde hay suciedad"; } break; case Elemento.Corral: { mundo.Casillas[a, b] = Elemento.RobotYCorral; Accion = "El Robot llega al corral de la posicion (" + a + "," + b + ")"; } break; case Elemento.CorralOcupado: { mundo.Casillas[a, b] = Elemento.RobotYCorral; cargaNiño = true; mundo.PosCorral.Add(new Tuple <int, int>(a, b)); Accion = "El Robot llega al corral de la posicion (" + a + "," + b + ")" + " y carga al niño"; } break; default: break; } //donde estaba switch (mundo.Casillas[X, Y]) { case Elemento.Robot: { mundo.Casillas[X, Y] = Elemento.Vacia; mundo.Ocupadas[X, Y] = false; } break; case Elemento.SuciedadYRobot: mundo.Casillas[X, Y] = Elemento.Suciedad; break; case Elemento.NiñoYRobot: mundo.Casillas[X, Y] = Elemento.Niño; break; case Elemento.RobotYCorral: mundo.Casillas[X, Y] = Elemento.Corral; break; default: break; } X = a; Y = b; } else { Accion = "El Robot no se mueve"; } }
protected void Limpia(ref Mundo mundo) { mundo.PosSuciedad.Remove(new Tuple <int, int>(X, Y)); mundo.Casillas[X, Y] = Elemento.Robot; Accion = "El Robot limpia la suciedad de la posicion (" + X + "," + Y + ")"; }
public virtual void Juega(ref Mundo mundo) { }
protected List <Tuple <int, int> > PosiblesMovimientos(Mundo mundo) { return(PosiblesMovimientos(mundo, new Tuple <int, int>(X, Y))); }
public override void Juega(ref Mundo mundo) { if (mundo.cantNiños == 0) //el corral esta lleno o tengo un niño { if (cargaNiño) { if (mundo.Casillas[X, Y] == Elemento.RobotYCorral) { DejaNiño(ref mundo); } else { obj = MejorObjetivo(mundo, mundo.PosCorral); //busca el corral mueve = ProximaJugada(mundo, obj); EfectuaJugada(ref mundo, mueve); } } else { if (mundo.Casillas[X, Y] == Elemento.SuciedadYRobot) { Limpia(ref mundo); } else { obj = MejorObjetivo(mundo, mundo.PosSuciedad); //busca la suciedad mueve = ProximaJugada(mundo, obj); EfectuaJugada(ref mundo, mueve); } } } else //al menos hay dos niño suelto { if (mundo.suciedad < 45) //el objetivo principal es reducir los niños sueltos { if (mundo.Casillas[X, Y] == Elemento.SuciedadYRobot && !cargaNiño) { //estoy en una casilla sucia Limpia(ref mundo); } else { if (cargaNiño) //si ya lo cargo o busco el corral o lo dejo en el corral { if (mundo.Casillas[X, Y] == Elemento.RobotYCorral) { DejaNiño(ref mundo); //estoy en el corral } else { //busco el corral obj = MejorObjetivo(mundo, mundo.PosCorral); mueve = ProximaJugada(mundo, obj); EfectuaJugada(ref mundo, mueve); } } else { //busco el niño mas cerca obj = MejorObjetivo(mundo, mundo.PosNiños); mueve = ProximaJugada(mundo, obj); EfectuaJugada(ref mundo, mueve); } } } else //el objetivo principal es reducir la suciedad { Tuple <int, int> objS = MejorObjetivo(mundo, mundo.PosSuciedad); if (cargaNiño) { if (mundo.Casillas[X, Y] == Elemento.RobotYCorral) { DejaNiño(ref mundo); } else { Tuple <int, int> objC = MejorObjetivo(mundo, mundo.PosCorral); double d1 = Distancia(X, Y, objC.Item1, objC.Item2); //distancia a el corral double d2 = Distancia(X, Y, objS.Item1, objS.Item2); //distancia a la suciedad if (d1 / 2 > d2 && mundo.Casillas[X, Y] == Elemento.Robot) { DejaNiño(ref mundo); } else { mueve = ProximaJugada(mundo, objC); EfectuaJugada(ref mundo, mueve); } } } else { if (mundo.Casillas[X, Y] == Elemento.SuciedadYRobot) { Limpia(ref mundo); } else { mueve = ProximaJugada(mundo, objS); EfectuaJugada(ref mundo, mueve); } } } } }