fork download
  1. using System;
  2. using System.Xml.Linq;
  3.  
  4.  
  5. /// <summary>
  6. /// Modela una torre defensiva en un juego tipo TD ("tower defense").
  7. /// https://e...content-available-to-author-only...a.org/wiki/Tower_defense
  8. /// PosiciĆ³n: fila, columna
  9. /// Vida la vida restante
  10. /// Ataque la fuerza de cada ataque
  11. /// Impacto(): recibe un impacto
  12. /// </summary>
  13. public class Torre {
  14. public Torre()
  15. {
  16. this.Vida = 100;
  17. }
  18.  
  19. public int Vida { get; private set; } // 0 - 100
  20. public /*required*/ int Fila { get; init; }
  21. public /*required*/ int Columna { get; init; }
  22. public /*required*/ int Ataque { get; set; }
  23.  
  24. public Tuple<int, int> Posicion
  25. {
  26. get => new Tuple<int, int>(Fila, Columna);
  27. /* set
  28.   {
  29.   this.Fila = value.Item1;
  30.   this.Columna = value.Item2;
  31.   }*/
  32. }
  33.  
  34. public void Impacto(int danno)
  35. {
  36. this.Vida -= danno;
  37. }
  38.  
  39. public override string ToString()
  40. {
  41. return $"Torre: pos:{this.Posicion} atq:{this.Ataque}, vida:{this.Vida}";
  42. }
  43. }
  44.  
  45. //using System.Xml.Linq;
  46.  
  47. public class TorreXML
  48. {
  49. public TorreXML(Torre t)
  50. {
  51. this.Torre = t;
  52. }
  53.  
  54. public XElement ToXML()
  55. {
  56. /*
  57.   <torres>
  58.   <torre ataque="100" fila="50" columna="32" vida="100"/>
  59.   </torres>
  60.   */
  61. /*
  62.   <torres>
  63.   <torre>
  64.   <ataque>100</ataque>
  65.   <fila>50</fila>
  66.   <columna>32</columna>
  67.   <vida>100</vida>
  68.   </torres>
  69.   */
  70. /*
  71.   <torres>
  72.   <torre columna="32" fila="50">
  73.   <ataque>100</ataque>
  74.   <vida>100</vida>
  75.   </torres>
  76.   */
  77.  
  78. return new XElement("Torre",
  79. new XAttribute("Vida", this.Torre.Vida),
  80. new XAttribute("Ataque", this.Torre.Ataque),
  81. new XAttribute("Fila", this.Torre.Fila),
  82. new XAttribute("Columna", this.Torre.Columna));
  83. }
  84. public void Save(string nf)
  85. {
  86. this.ToXML().Save( nf );
  87. }
  88.  
  89. public static Torre Load(string nf)
  90. {
  91. return FromXML( XElement.Load( nf ) );
  92. }
  93.  
  94. public static Torre FromXML(XElement xet)
  95. {
  96. var fila = int.Parse(xet.Attribute("Fila")?.Value ?? "0");
  97. var columna = int.Parse(xet.Attribute("Columna")?.Value ?? "0");
  98. var ataque = int.Parse(xet.Attribute("Ataque")?.Value ?? "0");
  99.  
  100. return new Torre {
  101. Fila = fila, Columna = columna, Ataque = ataque
  102. };
  103. }
  104.  
  105. public Torre Torre { get; }
  106. }
  107.  
  108. public class TestTorre {
  109. static void Main()
  110. {
  111. Action<object> muestra = (object o) => Console.WriteLine(o.ToString());
  112. Func<int, int, long> suma = (x1, x2) => {
  113. return x1 + x2;
  114. };
  115. Func<Torre, int, int, Torre> DoblarPotenciaTorre = (t, f, c) =>
  116. {
  117. int ataque = t.Ataque * 2;
  118. return new() { Ataque = ataque, Fila = f, Columna = c};
  119. };
  120.  
  121. var t1 = new Torre() { Fila = 11, Columna = 12, Ataque = 87 };
  122.  
  123. Console.WriteLine( "Salida lambdas" );
  124.  
  125. muestra(5);
  126. muestra( t1 );
  127. muestra( suma( 11, 22 ) );
  128. muestra(DoblarPotenciaTorre(t1, 5, 6));
  129.  
  130. Console.WriteLine( "Salida prueba de torres" );
  131.  
  132. var t3 = new Torre {
  133. Fila = 11,
  134. Columna = 12,
  135. Ataque = 42
  136. };
  137.  
  138. Console.WriteLine(t3);
  139.  
  140. t3.Ataque *= 2;
  141. Console.WriteLine(t3);
  142.  
  143. t3.Impacto( 30 );
  144. Console.WriteLine(t3);
  145.  
  146. var l1 = new List<Torre> {
  147. new () {Ataque = 20, Columna = 22, Fila = 11 },
  148. new () {Ataque = 30, Columna = 32, Fila = 12 },
  149. new () {Ataque = 40, Columna = 42, Fila = 13 },
  150. new () {Ataque = 56, Columna = 52, Fila = 14 },
  151. };
  152.  
  153. Console.WriteLine("Recorrido de la lista");
  154. /*foreach (var t in l1) {
  155. Console.WriteLine( t );
  156. }*/
  157. l1.ForEach(
  158. (t) => { Console.WriteLine(t); });
  159. var l2 = l1.Select( t1 =>
  160. new Torre() {
  161. Fila = t1.Columna,
  162. Columna = t1.Fila,
  163. Ataque = t1.Ataque
  164. }
  165. );
  166. Console.WriteLine("Recorrido de la lista 2: intercambiados f,c");
  167. new List<Torre>(l2).ForEach( t => Console.WriteLine(t) );
  168.  
  169. Console.WriteLine("Recorrido de la lista 3: ataque > 30");
  170. var l3 = l1.FindAll( t => t.Ataque > 30 );
  171. new List<Torre>(l3).ForEach( t => Console.WriteLine(t) );
  172. }
  173. }
  174.  
Success #stdin #stdout 0.06s 30100KB
stdin
Standard input is empty
stdout
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