kokusz19

MestInt.KetszemelyesV4.minimaxNeeded.HasMoreIncorrectSolutio

May 27th, 2018
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 14.29 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 ketszemelyesV1
  8. {
  9.  
  10.     class Csucs
  11.     {
  12.         public int szulosorszam;
  13.         public int sorszam;
  14.         public String nev;
  15.         public int letezik;
  16.         public int eredmeny;
  17.  
  18.         public List<int> gyerekekInt = new List<int>();
  19.         public List<String> gyerekekString = new List<string>();
  20.  
  21.         public Csucs(int szulosorszam, int sorszam, String nev)
  22.         {
  23.             this.szulosorszam = szulosorszam;
  24.             this.sorszam = sorszam;
  25.             this.nev = nev;
  26.         }
  27.     }
  28.  
  29.     class Program
  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.         static void csucsGyerekeiTorlese(List<Csucs> lista)
  93.         {
  94.             hiba: Console.WriteLine();
  95.             int sorszam = int.Parse(Console.ReadLine());
  96.  
  97.             if (lista.Find(x => x.sorszam == sorszam).gyerekekInt.Count() == 0)
  98.             {
  99.                 Console.WriteLine("Nincs gyereke a csucsnak, minimum egy gyereket torolni kell, valasszon ujra!");
  100.                 goto hiba;
  101.             }
  102.  
  103.  
  104.             else
  105.             {
  106.                 Csucs szulo = lista.Find(x => x.sorszam == sorszam);
  107.  
  108.                 // unoka.szulosorszam updateje  
  109.                 List<int> gyerekek = new List<int>();
  110.                 foreach (Csucs gyerek in lista.Where(x => x.szulosorszam == sorszam))
  111.                     foreach (int unoka in gyerek.gyerekekInt)
  112.                         gyerekek.Add(unoka);
  113.                 foreach (Csucs item in lista)
  114.                     if (gyerekek.Contains(item.sorszam) && item.letezik == 1)
  115.                         item.szulosorszam = sorszam;
  116.  
  117.                 // szülő gyerekeinek torlese
  118.                 foreach (int item in szulo.gyerekekInt)
  119.                 {
  120.                     foreach (var item2 in lista)
  121.                     {
  122.                         if (item2.sorszam == item)
  123.                             lista.Find(x => x == item2).letezik = 0;
  124.                     }
  125.                 }
  126.  
  127.                 // szülő.gyerekei update
  128.                 lista.Find(x => x.sorszam == sorszam).gyerekekInt.Clear();
  129.                 lista.Find(x => x.sorszam == sorszam).gyerekekString.Clear();
  130.                 foreach (Csucs item in lista)
  131.                 {
  132.                     if (item.szulosorszam == sorszam && item.letezik == 1)
  133.                     {
  134.                         lista.Find(x => x.sorszam == sorszam).gyerekekInt.Add(item.sorszam);
  135.                         lista.Find(x => x.sorszam == sorszam).gyerekekString.Add(item.nev);
  136.                     }
  137.                 }
  138.             }
  139.         }
  140.  
  141.         static void singlePlayer(List<Csucs> lista)
  142.         {
  143.             while (lista.Count() != 1)
  144.             {
  145.                 int elem = 0;
  146.                 foreach (var item in lista)
  147.                 {
  148.                     if (item.letezik == 1)
  149.                         elem++;
  150.                 }
  151.                 if (elem == 1) break;
  152.  
  153.                 csucsGyerekeiTorlese(lista);
  154.                 kiirFullLista(lista);
  155.             }
  156.         }
  157.  
  158.         static void check(List<Csucs> lista)
  159.         {
  160.             List<Csucs> listacopy = lista;
  161.             if (listacopy.Count() != 0)
  162.             {
  163.  
  164.                 listacopy.ForEach(delegate (Csucs a)
  165.                 {
  166.                     Console.Write(" " + a.nev);
  167.                 });
  168.                 Console.WriteLine();
  169.  
  170.                 listacopy.Remove(listacopy.Find(x => x.sorszam == listacopy.Max(y => y.sorszam)));
  171.                 check(listacopy);
  172.             }
  173.         }
  174.  
  175.  
  176.  
  177.  
  178.         public class Eredmenyek
  179.         {
  180.             public int kimenetel;
  181.             public int sorszam;
  182.             public int szulo;
  183.             public List<Eredmenyek> lista = new List<Eredmenyek>();
  184.  
  185.             public Eredmenyek(int kimenetel, int sorszam)
  186.             {
  187.                 this.kimenetel = kimenetel;
  188.                 this.sorszam = sorszam;
  189.             }
  190.             public Eredmenyek(int szulo, int kimenetel, int sorszam)
  191.             {
  192.                 this.szulo = szulo;
  193.                 this.kimenetel = kimenetel;
  194.                 this.sorszam = sorszam;
  195.             }
  196.         }
  197.         public static List<Eredmenyek> lehetsegesEredmenyek = new List<Eredmenyek>();
  198.  
  199.         public class Minimax
  200.         {
  201.             public int eredmeny;
  202.             public List<int> lepesek = new List<int>();
  203.  
  204.             public Minimax(int eredmeny, List<int> lepesek)
  205.             {
  206.                 this.eredmeny = eredmeny;
  207.                 this.lepesek = lepesek;
  208.             }
  209.         }
  210.  
  211.  
  212.         static void multiPlayer(List<Csucs> lista, int player)
  213.         {
  214.             /* Ha a listában szerepel törölhető elem, akkor elem törlése*/
  215.             if (lista.Where(x => x.letezik == 1).Count() != 0)
  216.             {
  217.                 for (int i = 1; i <= lista.Count(); i++)
  218.                 {
  219.  
  220.                     /* lokális lista másolat létrehozása*/
  221.                     List<Csucs> listacopy = new List<Csucs>();
  222.                     if (true)
  223.                     {
  224.                         foreach (Csucs csucs in lista.Where(x => x.letezik == 1))
  225.                         {
  226.                             listacopy.Add(new Csucs(csucs.szulosorszam, csucs.sorszam, csucs.nev));
  227.                             foreach (var item in csucs.gyerekekInt)
  228.                                 listacopy.Find(x => x.sorszam == csucs.sorszam).gyerekekInt.Add(item);
  229.                             foreach (var item in csucs.gyerekekString)
  230.                                 listacopy.Find(x => x.sorszam == csucs.sorszam).gyerekekString = csucs.gyerekekString;
  231.                             listacopy.Find(x => x.sorszam == csucs.sorszam).letezik = csucs.letezik;
  232.                         }
  233.                     }
  234.  
  235.                     /* ha az adott csúcsnak nincs gyereke, akkor nem lehet választani (min 1 gyereket törölni kell) → skip */
  236.                     int sorszam = i;
  237.                     if (listacopy.Where(x => x.sorszam == sorszam).Count() == 0)
  238.                             continue;
  239.                     else if(listacopy.Find(x => x.sorszam == sorszam).letezik != 1 || listacopy.Find(x => x.sorszam == sorszam).gyerekekInt.Count == 0)
  240.                             continue;
  241.  
  242.                     /* ha törölni kell */
  243.                     else
  244.                     {
  245.                         Csucs szulo = listacopy.Find(x => x.sorszam == sorszam);
  246.  
  247.                         // unoka.szulosorszam updateje  
  248.                         List<int> gyerekek = new List<int>();
  249.                         foreach (Csucs gyerek in listacopy.Where(x => x.szulosorszam == sorszam))
  250.                             foreach (int unoka in gyerek.gyerekekInt)
  251.                                 gyerekek.Add(unoka);
  252.                         foreach (Csucs item in listacopy)
  253.                             if (gyerekek.Contains(item.sorszam) && item.letezik == 1)
  254.                                 item.szulosorszam = sorszam;
  255.  
  256.                         // szülő gyerekeinek logikai törlése
  257.                         foreach (int item in szulo.gyerekekInt)
  258.                         {
  259.                             foreach (var item2 in listacopy)
  260.                             {
  261.                                 if (item2.sorszam == item)
  262.                                 {
  263.                                     listacopy.Find(x => x == item2).letezik = 0;
  264.                                     listacopy.Find(x => x == item2).gyerekekInt.Clear();
  265.                                     listacopy.Find(x => x == item2).gyerekekString.Clear();
  266.                                 }
  267.                             }
  268.                         }
  269.  
  270.                         // szülő gyerekeinek updateje
  271.                         listacopy.Find(x => x.sorszam == sorszam).gyerekekInt.Clear();
  272.                         listacopy.Find(x => x.sorszam == sorszam).gyerekekString.Clear();
  273.                         foreach (Csucs item in listacopy)
  274.                         {
  275.                             if (item.szulosorszam == sorszam && item.letezik == 1)
  276.                             {
  277.                                 listacopy.Find(x => x.sorszam == sorszam).gyerekekInt.Add(item.sorszam);
  278.                                 listacopy.Find(x => x.sorszam == sorszam).gyerekekString.Add(item.nev);
  279.                             }
  280.                         }
  281.                         lehetsegesEredmenyek.Add(new Eredmenyek(0, szulo.sorszam));
  282.                     }
  283.  
  284.  
  285.  
  286.  
  287.                     /* törlés után az eljárás rekurzív újrahívása */
  288.                     if (player == 1)
  289.                         multiPlayer(listacopy, 2);
  290.                     if (player == 2)
  291.                         multiPlayer(listacopy, 1);
  292.                 }
  293.                
  294.             }
  295.  
  296.             /* Ha a listában csak 1 elem szerepel, akkor PLAYER szerint eredmény visszaadása */
  297.             else if (lista.Where(x => x.letezik == 1).Count() == 1 && player == 2) {
  298.                 lehetsegesEredmenyek.Add(new Eredmenyek(1, 0));
  299.             }
  300.             else
  301.                 lehetsegesEredmenyek.Add(new Eredmenyek(-1, 0));
  302.         }
  303.  
  304.  
  305.         static void minimaxToFa(List<Eredmenyek> fa, List<Minimax> minimax, List<Csucs> lista)
  306.         {
  307.             int i, j;
  308.             foreach (Minimax kimenetelek in minimax)
  309.             {
  310.                 i = 0;
  311.                 j = 0;
  312.                 foreach (int lepesek in kimenetelek.lepesek)
  313.                 {
  314.                     if (j + 1 == kimenetelek.lepesek.Count())
  315.                         fa.Add(new Eredmenyek(j, kimenetelek.eredmeny, lepesek));
  316.                     else
  317.                         fa.Add(new Eredmenyek(j, 0, lepesek));
  318.                     j++;
  319.                 }
  320.                 j = 0;
  321.                 i++;
  322.             }
  323.             Console.WriteLine("###############\t   Fa\t##################");
  324.             foreach (var item in fa)
  325.             {
  326.                 Console.WriteLine(item.kimenetel + " " + item.sorszam + " " + item.szulo);
  327.                 if (item.kimenetel != 0) Console.WriteLine();
  328.             }
  329.             Console.WriteLine();
  330.             eredmenyBeallitasa(fa, minimax, lista);
  331.         }
  332.  
  333.         static void eredmenyBeallitasa(List<Eredmenyek> fa, List<Minimax> minimax, List<Csucs> lista)
  334.         {
  335.            
  336.         }
  337.  
  338.  
  339.         static void Main(string[] args)
  340.         {
  341.  
  342.             List<Csucs> lista = new List<Csucs>();
  343.             treeSetup(lista);
  344.  
  345.             kiirFullLista(lista);
  346.  
  347.             //singlePlayer(lista);
  348.  
  349.  
  350.             multiPlayer(lista, 1);
  351.             Console.WriteLine("###############\tCheck\t##################");
  352.             /*if (lehetsegesEredmenyek.First().kimenetel == 1)
  353.                 Console.WriteLine("win");
  354.  
  355.             else
  356.                 Console.WriteLine("lose");
  357.                 */
  358.             lehetsegesEredmenyek.ForEach(delegate (Eredmenyek a)
  359.             {
  360.                 Console.Write(" " + a.kimenetel);
  361.             });
  362.  
  363.             Console.WriteLine("\n\n##########################################");
  364.  
  365.  
  366.             List<Minimax> minimax = new List<Minimax>();
  367.             List<int> lepesekLocal = new List<int>();
  368.  
  369.             foreach (var item in lehetsegesEredmenyek)
  370.             {
  371.                 lepesekLocal.Add(item.sorszam);
  372.                 if (item.kimenetel != 0)
  373.                 {
  374.                     minimax.Add(new Minimax(item.kimenetel, lepesekLocal.ToList()));
  375.                     lepesekLocal.Clear();
  376.                 }
  377.             }
  378.  
  379.             Console.WriteLine("###############\tMinimax\t##################");
  380.             foreach (var item in minimax)
  381.             {
  382.                 Console.Write(item.eredmeny + "\t");
  383.                 item.lepesek.ForEach(delegate (int a) { Console.Write(" " + a); });
  384.                 Console.WriteLine();
  385.             }
  386.             Console.WriteLine();
  387.  
  388.             List<Eredmenyek> fa = new List<Eredmenyek>();
  389.             minimaxToFa(fa, minimax, lista);
  390.  
  391.             //check(lista);
  392.  
  393.             Console.ReadKey();
  394. }
  395.     }
  396. }
Advertisement
Add Comment
Please, Sign In to add comment