/// <summary> /// Pide al usuario un movimiento (númerao natural: 0 ó 1) con qué jugar. /// 1 es cooperar. /// </summary> /// <param name="H">Lista de movimientos pasados.</param> public override int Ejecutar(Historial H) { Console.Clear(); //Console.WriteLine (); DibujaTablero(H); return(PedirJugada(H)); }
/// <summary> /// Enlista todos los posibles historiales, variando al jugador II. /// El jugador I siempre jugará 0's /// </summary> /// <returns>Un arreglo enumerando a todos los Historiales.</returns> /// <param name="Long">Longitud de las instancias de Historial a mostrar.</param> public static Historial[] ObtenerPosiblesHistorias(int Long) // TODO: Para evitar iteración, enumerar las historias con 2^Long (las que empiecen con 1 en expansión decimal, y convertirlos a Historiales vía función de Cantor. { List <Historial> ret = new List <Historial> (); if (Long == 0) { Historial H = new Historial(); ret.Add(H); return(ret.ToArray()); } else { // Paso recursivo Historial[] Iterador = ObtenerPosiblesHistorias(Long - 1); foreach (var H in Iterador) // Crear una copia para iterar { Historial Hi; Hi = (Historial)H.MemberwiseClone(); // Si no funciona, hacer a Historial IClonable. Hi.AgregaTurno(0, 0); ret.Add(Hi); Hi = (Historial)H.MemberwiseClone(); // idem Hi.AgregaTurno(0, 1); ret.Add(Hi); } return(ret.ToArray()); } }
public override void Ejecutar(MemStack Mem, Historial H = null) { foreach (var x in _Genes) { x.Ejecutar(Mem, H); } }
/// <summary> /// Ejecuta un encuentro entre dos individuos. /// </summary> public void Encuentro(EstructuraIndividuo I, EstructuraIndividuo J) { EstructuraIndividuo[] Ind = new EstructuraIndividuo[2]; Historial H = new Historial(); if (r.Next(2) == 0) { Ind [0] = I; Ind [1] = J; } else { Ind [0] = J; Ind [1] = I; } H.Ind [0] = Ind [0].Indiv; H.Ind [1] = Ind [1].Indiv; if (Ind [0].Siguiendo || Ind [1].Siguiendo) { Console.Write(""); } // Ejecutar las rondas while (H.Actual < IteracionesPorEncuentro) { // H.Actual++; int a; int b; a = Ind [0].Indiv.Ejecutar(H); b = Ind [1].Indiv.Ejecutar(H.Invertir()); // Los jugadores escogen a y b respectivamente. //Agrega en el historial las últimas desiciones. H.AgregaTurno(a, b); // Modificar la puntuación Ind [0].Punt += Torneo.Puntuación(a, b) / IteracionesPorEncuentro; Ind [1].Punt += Torneo.Puntuación(b, a) / IteracionesPorEncuentro; } if (Ind [0].Siguiendo || Ind [1].Siguiendo) { Console.WriteLine(string.Format("{0}:{1}\n{2}:{3}", Ind [0].Indiv, H.ObtenerPuntuación(0), Ind [1].Indiv, H.ObtenerPuntuación(1))); if (Console.ReadLine() != "") { // Mostrar el historial for (int i = 0; i < 2; i++) { for (int j = 0; j < H.Count; j++) { Console.Write(H [i, j]); } Console.WriteLine(" - " + H.Ind [i]); } } } }
public Historial Invertir() { Historial ret = new Historial(); for (int i = 0; i < Actual; i++) { ret.Add(new Tuple <int, int> (this [1, i], this [0, i])); } ret.Ind [0] = Ind [1]; ret.Ind [1] = Ind [0]; return(ret); }
public virtual int Ejecutar(Historial H) { MemStack Mem = new MemStack(); int ret; Genética.Ejecutar(Mem, H); if (Mem.Count > 0) { ret = Mem.Pop(); return(ret == 0 ? 0 : 1); } return(0); }
/// <summary> /// Revisa si el gen es vengativo en el turno n /// </summary> /// <returns><c>true</c>, if vengativo was esed, <c>false</c> otherwise.</returns> /// <param name="n">Turno donde se revisa si es vengativo</param> bool _EsVengativo(int n, int maxn) { Individuo I = new Individuo(); I.Genética = (GrupoGen)this; foreach (var H in Historial.ObtenerPosiblesHistorias(n)) { if (H [1, H.Actual] == 0 && I.Ejecutar(H) == 1) // Si confías después de una traición { return(false); } } return(true); }
void DibujaTablero(Historial H) { Console.Write("I |"); for (int i = 0; i < H.Actual; i++) { Console.Write(H [0, i]); } Console.WriteLine(" - " + H.ObtenerPuntuación(0) + " || Nombre: " + H.Ind [0].Genética.StringEfectivo()); Console.Write("II|"); for (int i = 0; i < H.Actual; i++) { Console.Write(H [1, i]); } Console.WriteLine(" - " + H.ObtenerPuntuación(1) + " || Nombre: " + H.Ind [1].Genética.StringEfectivo()); }
int PedirJugada(Historial H) { bool Validado = false; int ret = 0; char c; while (Console.KeyAvailable) // Limpiar el buffer. { Console.ReadKey(); } do { Console.SetCursorPosition(3 + H.Actual, 0); c = Console.ReadKey().KeyChar; if (c == '0' || c == '1') { Validado = true; } } while (!Validado); ret = int.Parse(c.ToString()); return(ret); }
public override void Ejecutar(MemStack Mem, Historial H = null) { if (H == null) { throw new Exception(""); } int StackSize = Mem.Count; switch (Instrucción) { case "!": if (StackSize >= 1) { int t = Mem.Pop(); Mem.Push(t == 0 ? 1 : 0); } break; case "+": if (StackSize >= 2) { Mem.Push(Mem.Pop() + Mem.Pop()); } break; case "*": if (StackSize >= 2) { Mem.Push(Mem.Pop() * Mem.Pop()); } break; case "-": if (StackSize >= 2) { Mem.Push(Mem.Pop() - Mem.Pop()); } break; case "%": if (StackSize >= 2) { int o1 = Mem.Pop(); int o2 = Mem.Pop(); if (o2 != 0) { Mem.Push(o1 % o2); } } break; case "?": if (StackSize >= 3) { int a0 = Mem.Pop(); int a1 = Mem.Pop(); int a2 = Mem.Pop(); Mem.Push(a0 != 0 ? a1 : a2); } break; case "<": if (StackSize >= 2) { Mem.Push(Mem.Pop() < Mem.Pop() ? 1 : 0); } break; case "=": if (StackSize >= 2) { Mem.Push(Mem.Pop() == Mem.Pop() ? 1 : 0); } break; case "h": if (StackSize >= 1 && Mem.Peek() <= H.Actual && Mem.Peek() > 0) { Mem.Push(H [1, H.Actual - Mem.Pop()]); } break; case "i": Mem.Push(H.Actual); break; default: int n; if (int.TryParse(Instrucción, out n)) { Mem.Push(n); } break; } }
//public abstract Gen Replicar (Gen Pareja) public abstract void Ejecutar(MemStack Mem, Historial H = null);