Advertisement
kokusz19

MestInt.KetszemelyesV3.nearlyDone.MinimaxNeeded

May 26th, 2018
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.95 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.  
  17.         public List<int> gyerekekInt = new List<int>();
  18.         public List<String> gyerekekString = new List<string>();
  19.  
  20.         public Csucs(int szulosorszam, int sorszam, String nev)
  21.         {
  22.             this.szulosorszam = szulosorszam;
  23.             this.sorszam = sorszam;
  24.             this.nev = nev;
  25.         }
  26.     }
  27.  
  28.     class Program
  29.     {
  30.         static void treeSetup(List<Csucs> lista)
  31.         {
  32.             /*
  33.              *                  A
  34.              *         B                C
  35.              *     D   E   F        G       H
  36.              *   I   J     K                L
  37.              *
  38.              */
  39.  
  40.             lista.Add(new Csucs(0, 1, "A"));
  41.             lista.Add(new Csucs(1, 2, "B"));
  42.             lista.Add(new Csucs(1, 3, "C"));
  43.             lista.Add(new Csucs(2, 4, "D"));
  44.             lista.Add(new Csucs(2, 5, "E"));
  45.             lista.Add(new Csucs(2, 6, "F"));
  46.             lista.Add(new Csucs(3, 7, "G"));
  47.             lista.Add(new Csucs(3, 8, "H"));
  48.             lista.Add(new Csucs(4, 9, "I"));
  49.             lista.Add(new Csucs(4, 10, "J"));
  50.             lista.Add(new Csucs(6, 11, "K"));
  51.             lista.Add(new Csucs(8, 12, "L"));
  52.             /**/
  53.             foreach (Csucs item in lista)
  54.             {
  55.                 foreach (Csucs item2 in lista)
  56.                 {
  57.                     if (item.sorszam != item2.sorszam && item.sorszam == item2.szulosorszam)
  58.                     {
  59.                         item.gyerekekInt.Add(item2.sorszam);
  60.                         item.gyerekekString.Add(lista.Where(x => x.sorszam == item2.sorszam).First().nev);
  61.  
  62.                     }
  63.                 }
  64.                 item.letezik = 1;
  65.             }
  66.         }
  67.  
  68.  
  69.  
  70.  
  71.         static void kiirFullLista(List<Csucs> lista)
  72.         {
  73.             Console.WriteLine();
  74.             foreach (Csucs item in lista)
  75.             {
  76.                 if (item.letezik == 1)
  77.                 {
  78.                     Console.Write(item.sorszam + ".\t" + item.nev + "\t(szulo: " + item.szulosorszam + " gyerekei:");
  79.                     if (item.gyerekekInt.Count != 0)
  80.                     {
  81.                         foreach (int item2 in item.gyerekekInt)
  82.                         {
  83.                             Console.Write("  " + item2 + "(" + lista.Find(x => x.sorszam == item2).nev + ")");
  84.                         }
  85.                     }
  86.                     else
  87.                         Console.Write("  level elem");
  88.                     Console.WriteLine(")");
  89.                 }
  90.             }
  91.         }
  92.  
  93.         static void csucsGyerekeiTorlese(List<Csucs> lista)
  94.         {
  95.             hiba: Console.WriteLine();
  96.             int sorszam = int.Parse(Console.ReadLine());
  97.  
  98.             if (lista.Find(x => x.sorszam == sorszam).gyerekekInt.Count() == 0)
  99.             {
  100.                 Console.WriteLine("Nincs gyereke a csucsnak, minimum egy gyereket torolni kell, valasszon ujra!");
  101.                 goto hiba;
  102.             }
  103.  
  104.  
  105.             else
  106.             {
  107.                 Csucs szulo = lista.Find(x => x.sorszam == sorszam);
  108.  
  109.                 // unoka.szulosorszam updateje  
  110.                 List<int> gyerekek = new List<int>();
  111.                 foreach (Csucs gyerek in lista.Where(x => x.szulosorszam == sorszam))
  112.                     foreach (int unoka in gyerek.gyerekekInt)
  113.                         gyerekek.Add(unoka);
  114.                 foreach (Csucs item in lista)
  115.                     if (gyerekek.Contains(item.sorszam) && item.letezik == 1)
  116.                         item.szulosorszam = sorszam;
  117.  
  118.                 // szülő gyerekeinek torlese
  119.                 foreach (int item in szulo.gyerekekInt)
  120.                 {
  121.                     foreach (var item2 in lista)
  122.                     {
  123.                         if (item2.sorszam == item)
  124.                             lista.Find(x => x == item2).letezik = 0;
  125.                     }
  126.                 }
  127.  
  128.                 // szülő.gyerekei update
  129.                 lista.Find(x => x.sorszam == sorszam).gyerekekInt.Clear();
  130.                 lista.Find(x => x.sorszam == sorszam).gyerekekString.Clear();
  131.                 foreach (Csucs item in lista)
  132.                 {
  133.                     if (item.szulosorszam == sorszam && item.letezik == 1)
  134.                     {
  135.                         lista.Find(x => x.sorszam == sorszam).gyerekekInt.Add(item.sorszam);
  136.                         lista.Find(x => x.sorszam == sorszam).gyerekekString.Add(item.nev);
  137.                     }
  138.                 }
  139.             }
  140.         }
  141.  
  142.  
  143.  
  144.  
  145.  
  146.         static void singlePlayer(List<Csucs> lista)
  147.         {
  148.             while (lista.Count() != 1)
  149.             {
  150.                 int elem = 0;
  151.                 foreach (var item in lista)
  152.                 {
  153.                     if (item.letezik == 1)
  154.                         elem++;
  155.                 }
  156.                 if (elem == 1) break;
  157.  
  158.                 csucsGyerekeiTorlese(lista);
  159.                 kiirFullLista(lista);
  160.             }
  161.         }
  162.  
  163.         static void multiPlayer(List<Csucs> lista, int player)
  164.         {
  165.  
  166.             if (lista.Where(x => x.letezik == 1).Count() != 1)
  167.             {
  168.                 for (int i = 1; i <= lista.Count(); i++)
  169.                 {
  170.  
  171.  
  172.                     List<Csucs> listacopy = new List<Csucs>();
  173.                     foreach (Csucs csucs in lista.Where(x => x.letezik == 1))
  174.                     {
  175.                         listacopy.Add(new Csucs(csucs.szulosorszam, csucs.sorszam, csucs.nev));
  176.                         foreach (var item in csucs.gyerekekInt)
  177.                             listacopy.Find(x => x.sorszam == csucs.sorszam).gyerekekInt.Add(item);
  178.                         foreach (var item in csucs.gyerekekString)
  179.                             listacopy.Find(x => x.sorszam == csucs.sorszam).gyerekekString = csucs.gyerekekString;
  180.                         listacopy.Find(x => x.sorszam == csucs.sorszam).letezik = csucs.letezik;
  181.                     }
  182.                     /*
  183.                     Console.WriteLine("i: " + i);
  184.                     listacopy.ForEach(delegate (Csucs s)
  185.                     {
  186.                         if (s.letezik == 1)
  187.                             Console.Write(" " + s.nev + "(" + s.sorszam +", " + (s.letezik == 1 ? "live" : "nem letezik") + ", " + s.gyerekekInt.Count() + " child)");
  188.                     });
  189.                     Console.WriteLine();
  190.                     */
  191.  
  192.  
  193.                    
  194.  
  195.                     int sorszam = i;
  196.                     if (listacopy.Where(x => x.sorszam == sorszam).Count() == 0)
  197.                             continue;
  198.                     else if(listacopy.Find(x => x.sorszam == sorszam).letezik != 1 || listacopy.Find(x => x.sorszam == sorszam).gyerekekInt.Count == 0)
  199.                             continue;
  200.  
  201.  
  202.  
  203.                     else
  204.                     {
  205.                         Csucs szulo = listacopy.Find(x => x.sorszam == sorszam);
  206.  
  207.                         // unoka.szulosorszam updateje  
  208.                         List<int> gyerekek = new List<int>();
  209.                         foreach (Csucs gyerek in listacopy.Where(x => x.szulosorszam == sorszam))
  210.                             foreach (int unoka in gyerek.gyerekekInt)
  211.                                 gyerekek.Add(unoka);
  212.                         foreach (Csucs item in listacopy)
  213.                             if (gyerekek.Contains(item.sorszam) && item.letezik == 1)
  214.                                 item.szulosorszam = sorszam;
  215.  
  216.                         // szülő gyerekeinek logikai törlése
  217.                         foreach (int item in szulo.gyerekekInt)
  218.                         {
  219.                             foreach (var item2 in listacopy)
  220.                             {
  221.                                 if (item2.sorszam == item)
  222.                                 {
  223.                                     listacopy.Find(x => x == item2).letezik = 0;
  224.                                     listacopy.Find(x => x == item2).gyerekekInt.Clear();
  225.                                     listacopy.Find(x => x == item2).gyerekekString.Clear();
  226.                                 }
  227.                             }
  228.                         }
  229.  
  230.                         // szülő gyerekeinek updateje
  231.                         listacopy.Find(x => x.sorszam == sorszam).gyerekekInt.Clear();
  232.                         listacopy.Find(x => x.sorszam == sorszam).gyerekekString.Clear();
  233.                         foreach (Csucs item in listacopy)
  234.                         {
  235.                             if (item.szulosorszam == sorszam && item.letezik == 1)
  236.                             {
  237.                                 listacopy.Find(x => x.sorszam == sorszam).gyerekekInt.Add(item.sorszam);
  238.                                 listacopy.Find(x => x.sorszam == sorszam).gyerekekString.Add(item.nev);
  239.                             }
  240.                         }
  241.                     }
  242.  
  243.  
  244.                     if (player == 1)
  245.                         multiPlayer(listacopy, 2);
  246.                     if (player == 2)
  247.                         multiPlayer(listacopy, 1);
  248.                 }
  249.             }
  250.             else if (lista.Where(x => x.letezik == 1).Count() == 1 && player == 2)
  251.                 lehetsegesEredmenyek.Add(1);
  252.             else if (lista.Where(x => x.letezik == 1).Count() == 1 && player == 1)
  253.                 lehetsegesEredmenyek.Add(-1);
  254.             else lehetsegesEredmenyek.Add(0);
  255.         }
  256.  
  257.  
  258.  
  259.  
  260.         /*class Eredmenyek
  261.         {
  262.             public int eredmeny;
  263.             public int sorszam;
  264.         }*/
  265.         public static List<int> lehetsegesEredmenyek = new List<int>();
  266.         //public static int num;
  267.  
  268.         static void check(List<Csucs> lista)
  269.         {
  270.             List<Csucs> listacopy = lista;
  271.             if (listacopy.Count() != 0)
  272.             {
  273.  
  274.                 listacopy.ForEach(delegate (Csucs a)
  275.                 {
  276.                     Console.Write(" " + a.nev);
  277.                 });
  278.                 Console.WriteLine();
  279.  
  280.                 listacopy.Remove(listacopy.Find(x => x.sorszam == listacopy.Max(y => y.sorszam)));
  281.                 check(listacopy);
  282.             }
  283.         }
  284.  
  285.         static void Main(string[] args)
  286.         {
  287.  
  288.             List<Csucs> lista = new List<Csucs>();
  289.             treeSetup(lista);
  290.  
  291.             //kiirFullLista(lista);
  292.  
  293.             //singlePlayer(lista);
  294.  
  295.  
  296.             multiPlayer(lista, 1);
  297.             if (lehetsegesEredmenyek.Contains(1))
  298.                 Console.WriteLine("win");
  299.  
  300.             else
  301.                 Console.WriteLine("lose");
  302.  
  303.             lehetsegesEredmenyek.ForEach(delegate (int a)
  304.             {
  305.                 Console.Write(" " + a);
  306.             });
  307.            
  308.             //check(lista);
  309.  
  310.             Console.ReadKey();
  311. }
  312.     }
  313. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement