Advertisement
kokusz19

MestInt.KetszemelyesV6.afterStartingAgain

May 27th, 2018
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.94 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ketszemelyesV2
  8. {
  9.    
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             List<Csucs> lista = new List<Csucs>();
  15.             treeSetup(lista);
  16.  
  17.             kiirFullLista(lista);
  18.  
  19.             jatekfa.Add(new Jatekfa(0, 1, lista));
  20.  
  21.             megoldasok(jatekfa);
  22.  
  23.             jatekfakkiiratasa(jatekfa);
  24.  
  25.             Console.ReadKey();
  26.         }
  27.  
  28.  
  29.  
  30.  
  31.         static void treeSetup(List<Csucs> lista)
  32.         {
  33.             /*
  34.              *                  A
  35.              *         B                C
  36.              *     D   E   F        G       H
  37.              *   I   J     K                L
  38.              *
  39.              */
  40.  
  41.             lista.Add(new Csucs(0, 1, "A"));
  42.             lista.Add(new Csucs(1, 2, "B"));
  43.             lista.Add(new Csucs(1, 3, "C"));
  44.             lista.Add(new Csucs(2, 4, "D"));
  45.             lista.Add(new Csucs(2, 5, "E"));
  46.             lista.Add(new Csucs(2, 6, "F"));
  47.             lista.Add(new Csucs(3, 7, "G"));
  48.             lista.Add(new Csucs(3, 8, "H"));
  49.             lista.Add(new Csucs(4, 9, "I"));
  50.             lista.Add(new Csucs(4, 10, "J"));
  51.             lista.Add(new Csucs(6, 11, "K"));
  52.             lista.Add(new Csucs(8, 12, "L"));
  53.             /**/
  54.             foreach (Csucs item in lista)
  55.             {
  56.                 foreach (Csucs item2 in lista)
  57.                 {
  58.                     if (item.sorszam != item2.sorszam && item.sorszam == item2.szulosorszam)
  59.                     {
  60.                         item.gyerekekInt.Add(item2.sorszam);
  61.                         item.gyerekekString.Add(lista.Where(x => x.sorszam == item2.sorszam).First().nev);
  62.  
  63.                     }
  64.                 }
  65.                 item.letezik = 1;
  66.             }
  67.         }
  68.  
  69.         static void kiirFullLista(List<Csucs> lista)
  70.         {
  71.             Console.WriteLine("###############\t   Lista\t##################");
  72.             foreach (Csucs item in lista)
  73.             {
  74.                 if (item.letezik == 1)
  75.                 {
  76.                     Console.Write(item.sorszam + ".\t" + item.nev + "\t(szulo: " + item.szulosorszam + " gyerekei:");
  77.                     if (item.gyerekekInt.Count != 0)
  78.                     {
  79.                         foreach (int item2 in item.gyerekekInt)
  80.                         {
  81.                             Console.Write("  " + item2 + "(" + lista.Find(x => x.sorszam == item2).nev + ")");
  82.                         }
  83.                     }
  84.                     else
  85.                         Console.Write("  level elem");
  86.                     Console.WriteLine(")");
  87.                 }
  88.             }
  89.             Console.WriteLine();
  90.         }
  91.  
  92.  
  93.  
  94.  
  95.         public static List<Jatekfa> jatekfa = new List<Jatekfa>();
  96.  
  97.         static void megoldasok(List<Jatekfa> jatekfak)
  98.         {
  99.             if(jatekfa.Where(x=>x.kiterjesztve == 0).Count() != 0)
  100.             {
  101.                 List<Jatekfa> localjatekfa = new List<Jatekfa>();
  102.                 foreach (Jatekfa item in jatekfak)
  103.                     localjatekfa.Add(new Jatekfa(item.szulo, item.sorszam, item.adottFaAllapot, item.kiterjesztve));
  104.  
  105.                 // A még ki nem terjesztett fa állapotokat veszi végig
  106.                 foreach (var jatekfaa in localjatekfa.Where(x=>x.kiterjesztve == 0))
  107.                 {
  108.                     // Ha a fából lehet törölni
  109.                     int szulo = jatekfaa.sorszam;
  110.                     if (jatekfaa.adottFaAllapot.Where(x => x.letezik != 0).Count() > 1)
  111.                     {
  112.                         for (int i = 1; i <= jatekfaa.adottFaAllapot.Count(); i++)
  113.                         {
  114.                             // lokalis lista letrehozasa a fa adott allapotarol
  115.                             List<Csucs> listacopy = new List<Csucs>();
  116.                             foreach (var csucs in jatekfaa.adottFaAllapot)
  117.                                 listacopy.Add(new Csucs(csucs.szulosorszam, csucs.sorszam, csucs.nev, csucs.letezik, csucs.gyerekekInt, csucs.gyerekekString));
  118.  
  119.                             int sorszam = i;
  120.                             if (listacopy.Where(x => x.sorszam == sorszam && x.letezik == 1 && x.gyerekekInt.Count() == 0).Count() == 0)
  121.                                 continue;
  122.                             // else if (listacopy.Find(x => x.sorszam == sorszam).letezik != 1 || listacopy.Find(x => x.sorszam == sorszam).gyerekekInt.Count == 0)
  123.                             //     continue;
  124.  
  125.                             // ha törölni lehet
  126.                             else
  127.                             {
  128.                                 Csucs csucsszuloje = listacopy.Find(x => x.sorszam == sorszam);
  129.  
  130.                                 // unoka.szulosorszam updateje  
  131.                                 // unokak kigyujtese a gyerekek listaba
  132.                                 List<int> gyerekek = new List<int>();
  133.                                 foreach (Csucs gyerek in listacopy.Where(x => x.szulosorszam == sorszam))
  134.                                     foreach (int unoka in gyerek.gyerekekInt)
  135.                                         gyerekek.Add(unoka);
  136.                                 // unoka szulojenek atirasa a nagyszulojere
  137.                                 foreach (Csucs item in listacopy)
  138.                                     if (gyerekek.Contains(item.sorszam) && item.letezik == 1)
  139.                                         item.szulosorszam = sorszam;
  140.  
  141.                                 // szülő gyerekeinek logikai törlése
  142.                                 foreach (int gyereke in csucsszuloje.gyerekekInt)
  143.                                 {
  144.                                     foreach (var csucs in listacopy)
  145.                                     {
  146.                                         if (csucs.sorszam == gyereke)
  147.                                         {
  148.                                             listacopy.Find(x => x == csucs).letezik = 0;
  149.                                             listacopy.Find(x => x == csucs).gyerekekInt.Clear();
  150.                                             listacopy.Find(x => x == csucs).gyerekekString.Clear();
  151.                                         }
  152.                                     }
  153.                                 }
  154.  
  155.                                 // szülő gyerekeinek updateje
  156.                                 listacopy.Find(x => x.sorszam == sorszam).gyerekekInt.Clear();
  157.                                 listacopy.Find(x => x.sorszam == sorszam).gyerekekString.Clear();
  158.                                 foreach (Csucs item in listacopy)
  159.                                 {
  160.                                     if (item.szulosorszam == sorszam && item.letezik == 1)
  161.                                     {
  162.                                         listacopy.Find(x => x.sorszam == sorszam).gyerekekInt.Add(item.sorszam);
  163.                                         listacopy.Find(x => x.sorszam == sorszam).gyerekekString.Add(item.nev);
  164.                                     }
  165.                                 }
  166.                                 jatekfa.Add(new Jatekfa(szulo, jatekfak.Count() + 1, listacopy));
  167.                             }
  168.                         }
  169.                        // jatekfa.Find(x => x.sorszam == szulo).kiterjesztve = 1;
  170.                     }
  171.  
  172.                     // Ha a fából nem lehet törölni, akkor a csucs ki van terjesztve, nem lehet vele mit csinalni
  173.                     //else
  174.                         jatekfa.Find(x => x.sorszam == szulo).kiterjesztve = 1;
  175.                 }
  176.             }
  177.         }
  178.  
  179.  
  180.  
  181.         static void jatekfakkiiratasa(List<Jatekfa> jatekfak)
  182.         {
  183.             Console.WriteLine("\nJatekfak:");
  184.             foreach (var jatekfa in jatekfak)
  185.             {
  186.                 Console.Write(jatekfa.sorszam + ".\t(szulo: " + jatekfa.szulo + ")\t");
  187.                 foreach (var csucs in jatekfa.adottFaAllapot.Where(x=>x.letezik != 0))
  188.                 {
  189.                     Console.Write(" " + csucs.nev + "(" + csucs.sorszam + "." + (csucs.letezik == 1 ? ")" : " nem letezik)"));
  190.                 }
  191.                 Console.WriteLine("\n");
  192.             }
  193.         }
  194.     }
  195.  
  196.     class Csucs
  197.     {
  198.         public int szulosorszam;
  199.         public int sorszam;
  200.         public String nev;
  201.         public int letezik;
  202.  
  203.         public List<int> gyerekekInt = new List<int>();
  204.         public List<String> gyerekekString = new List<string>();
  205.  
  206.         public Csucs(int szulosorszam, int sorszam, String nev)
  207.         {
  208.             this.szulosorszam = szulosorszam;
  209.             this.sorszam = sorszam;
  210.             this.nev = nev;
  211.             this.letezik = 1;
  212.         }
  213.         public Csucs(int szulosorszam, int sorszam, String nev, int letezik, List<int> gyerekekInt, List<String> gyerekekString)
  214.         {
  215.             this.szulosorszam = szulosorszam;
  216.             this.sorszam = sorszam;
  217.             this.nev = nev;
  218.             this.letezik = letezik;
  219.             this.gyerekekInt = gyerekekInt.ToList();
  220.             this.gyerekekString = gyerekekString.ToList();
  221.         }
  222.     }
  223.  
  224.     class Jatekfa
  225.     {
  226.         public int szulo;
  227.         //public int win;           Később kiszámolható
  228.         public int sorszam;
  229.         public List<Csucs> adottFaAllapot;
  230.         public int kiterjesztve;
  231.  
  232.         public Jatekfa(int szulo, /* int win, */int sorszam, List<Csucs> adottFaAllapot)
  233.         {
  234.             this.szulo = szulo;
  235.             //this.win = win;
  236.             this.sorszam = sorszam;
  237.             this.adottFaAllapot = adottFaAllapot;
  238.             this.kiterjesztve = 0;
  239.         }
  240.         public Jatekfa(int szulo, /* int win, */int sorszam, List<Csucs> adottFaAllapot, int kiterjesztve)
  241.         {
  242.             this.szulo = szulo;
  243.             //this.win = win;
  244.             this.sorszam = sorszam;
  245.             this.adottFaAllapot = adottFaAllapot;
  246.             this.kiterjesztve = kiterjesztve;
  247.         }
  248.     }
  249. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement