using System;
using System.Xml.Linq;
/// <summary>
/// Modela una torre defensiva en un juego tipo TD ("tower defense").
/// https://e...content-available-to-author-only...a.org/wiki/Tower_defense
/// PosiciĆ³n: fila, columna
/// Vida la vida restante
/// Ataque la fuerza de cada ataque
/// Impacto(): recibe un impacto
/// </summary>
public class Torre {
public Torre()
{
this.Vida = 100;
}
public int Vida { get; private set; } // 0 - 100
public /*required*/ int Fila { get; init; }
public /*required*/ int Columna { get; init; }
public /*required*/ int Ataque { get; set; }
public Tuple<int, int> Posicion
{
get => new Tuple<int, int>(Fila, Columna);
/* set
{
this.Fila = value.Item1;
this.Columna = value.Item2;
}*/
}
public void Impacto(int danno)
{
this.Vida -= danno;
}
public override string ToString()
{
return $"Torre: pos:{this.Posicion} atq:{this.Ataque}, vida:{this.Vida}";
}
}
//using System.Xml.Linq;
public class TorreXML
{
public TorreXML(Torre t)
{
this.Torre = t;
}
public XElement ToXML()
{
/*
<torres>
<torre ataque="100" fila="50" columna="32" vida="100"/>
</torres>
*/
/*
<torres>
<torre>
<ataque>100</ataque>
<fila>50</fila>
<columna>32</columna>
<vida>100</vida>
</torres>
*/
/*
<torres>
<torre columna="32" fila="50">
<ataque>100</ataque>
<vida>100</vida>
</torres>
*/
return new XElement("Torre",
new XAttribute("Vida", this.Torre.Vida),
new XAttribute("Ataque", this.Torre.Ataque),
new XAttribute("Fila", this.Torre.Fila),
new XAttribute("Columna", this.Torre.Columna));
}
public void Save(string nf)
{
this.ToXML().Save( nf );
}
public static Torre Load(string nf)
{
return FromXML( XElement.Load( nf ) );
}
public static Torre FromXML(XElement xet)
{
var fila = int.Parse(xet.Attribute("Fila")?.Value ?? "0");
var columna = int.Parse(xet.Attribute("Columna")?.Value ?? "0");
var ataque = int.Parse(xet.Attribute("Ataque")?.Value ?? "0");
return new Torre {
Fila = fila, Columna = columna, Ataque = ataque
};
}
public Torre Torre { get; }
}
public class TestTorre {
static void Main()
{
Action<object> muestra = (object o) => Console.WriteLine(o.ToString());
Func<int, int, long> suma = (x1, x2) => {
return x1 + x2;
};
Func<Torre, int, int, Torre> DoblarPotenciaTorre = (t, f, c) =>
{
int ataque = t.Ataque * 2;
return new() { Ataque = ataque, Fila = f, Columna = c};
};
var t1 = new Torre() { Fila = 11, Columna = 12, Ataque = 87 };
Console.WriteLine( "Salida lambdas" );
muestra(5);
muestra( t1 );
muestra( suma( 11, 22 ) );
muestra(DoblarPotenciaTorre(t1, 5, 6));
Console.WriteLine( "Salida prueba de torres" );
var t3 = new Torre {
Fila = 11,
Columna = 12,
Ataque = 42
};
Console.WriteLine(t3);
t3.Ataque *= 2;
Console.WriteLine(t3);
t3.Impacto( 30 );
Console.WriteLine(t3);
var l1 = new List<Torre> {
new () {Ataque = 20, Columna = 22, Fila = 11 },
new () {Ataque = 30, Columna = 32, Fila = 12 },
new () {Ataque = 40, Columna = 42, Fila = 13 },
new () {Ataque = 56, Columna = 52, Fila = 14 },
};
Console.WriteLine("Recorrido de la lista");
/*foreach (var t in l1) {
Console.WriteLine( t );
}*/
l1.ForEach(
(t) => { Console.WriteLine(t); });
var l2 = l1.Select( t1 =>
new Torre() {
Fila = t1.Columna,
Columna = t1.Fila,
Ataque = t1.Ataque
}
);
Console.WriteLine("Recorrido de la lista 2: intercambiados f,c");
new List<Torre>(l2).ForEach( t => Console.WriteLine(t) );
Console.WriteLine("Recorrido de la lista 3: ataque > 30");
var l3 = l1.FindAll( t => t.Ataque > 30 );
new List<Torre>(l3).ForEach( t => Console.WriteLine(t) );
}
}
dXNpbmcgU3lzdGVtOwp1c2luZyBTeXN0ZW0uWG1sLkxpbnE7CgoKLy8vIDxzdW1tYXJ5PgovLy8gTW9kZWxhIHVuYSB0b3JyZSBkZWZlbnNpdmEgZW4gdW4ganVlZ28gdGlwbyBURCAoInRvd2VyIGRlZmVuc2UiKS4KLy8vIGh0dHBzOi8vZS4uLmNvbnRlbnQtYXZhaWxhYmxlLXRvLWF1dGhvci1vbmx5Li4uYS5vcmcvd2lraS9Ub3dlcl9kZWZlb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nt0aGlzLlZpZGF9IjsKICAgIH0KfQoKLy91c2luZyBTeXN0ZW0uWG1sLkxpbnE7CgpwdWJsaWMgY2xhc3MgVG9ycmVYTUwKewogICAgcHVibGljIFRvcnJlWE1MKFRvcnJlIHQpCiAgICB7CiAgICAgICAgdGhpcy5Ub3JyZSA9IHQ7CiAgICB9CgogICAgcHVibGljIFhFbGVtZW50IFRvWE1MKCkKICAgIHsKICAgICAgICAvKgogICAgICAgICA8dG9ycmVzPgogICAgICAgICAgICA8dG9ycmUgYXRhcXVlPSIxMDAiIGZpbGE9IjUwIiBjb2x1bW5hPSIzMiIgdmlkYT0iMTAwIi8+CiAgICAgICAgIDwvdG9ycmVzPgogICAgICAgICAqLwogICAgICAgIC8qCiAgICAgICAgIDx0b3JyZXM+CiAgICAgICAgICAgICAgPHRvcnJlPgogICAgICAgICAgICAgICAgICAgIDxhdGFxdWU+MTAwPC9hdGFxdWU+CiAgICAgICAgICAgICAgICAgICAgPGZpbGE+NTA8L2ZpbGE+CiAgICAgICAgICAgICAgICAgICAgPGNvbHVtbmE+MzI8L2NvbHVtbmE+CiAgICAgICAgICAgICAgICAgICAgPHZpZGE+MTAwPC92aWRhPgogICAgICAgICAgIDwvdG9ycmVzPgogICAgICAgICAqLwogICAgICAgIC8qCiAgICAgICAgIDx0b3JyZXM+CiAgICAgICAgICAgICAgPHRvcnJlIGNvbHVtbmE9IjMyIiBmaWxhPSI1MCI+CiAgICAgICAgICAgICAgICAgICAgPGF0YXF1ZT4xMDA8L2F0YXF1ZT4KICAgICAgICAgICAgICAgICAgICA8dmlkYT4xMDA8L3ZpZGE+CiAgICAgICAgICAgPC90b3JyZXM+CiAgICAgICAgICovCiAgICAgICAgCiAgICAgICAgcmV0dXJuIG5ldyBYRWxlbWVudCgiVG9ycmUiLAogICAgICAgICAgICAgICAgICAgIG5ldyBYQXR0cmlidXRlKCJWaWRhIiwgdGhpcy5Ub3JyZS5WaWRhKSwKICAgICAgICAgICAgICAgICAgICBuZXcgWE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hdGFxdWUgPiAzMCIpOwoJCXZhciBsMyA9IGwxLkZpbmRBbGwoIHQgPT4gdC5BdGFxdWUgPiAzMCApOwoJCW5ldyBMaXN0PFRvcnJlPihsMykuRm9yRWFjaCggdCA9PiBDb25zb2xlLldyaXRlTGluZSh0KSApOwoJfQp9Cg==
Salida lambdas
5
Torre: pos:(11, 12) atq:87, vida:100
33
Torre: pos:(5, 6) atq:174, vida:100
Salida prueba de torres
Torre: pos:(11, 12) atq:42, vida:100
Torre: pos:(11, 12) atq:84, vida:100
Torre: pos:(11, 12) atq:84, vida:70
Recorrido de la lista
Torre: pos:(11, 22) atq:20, vida:100
Torre: pos:(12, 32) atq:30, vida:100
Torre: pos:(13, 42) atq:40, vida:100
Torre: pos:(14, 52) atq:56, vida:100
Recorrido de la lista 2: intercambiados f,c
Torre: pos:(22, 11) atq:20, vida:100
Torre: pos:(32, 12) atq:30, vida:100
Torre: pos:(42, 13) atq:40, vida:100
Torre: pos:(52, 14) atq:56, vida:100
Recorrido de la lista 3: ataque > 30
Torre: pos:(13, 42) atq:40, vida:100
Torre: pos:(14, 52) atq:56, vida:100